Skip to content

Commit 6b2960f

Browse files
authored
chore: upgrade servicedirectory samples to new format (GoogleCloudPlatform#1762)
1 parent b0e4eab commit 6b2960f

File tree

8 files changed

+218
-183
lines changed

8 files changed

+218
-183
lines changed

servicedirectory/src/create_endpoint.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,49 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc < 6 || $argc > 8) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID ENDPOINT_ID [IP] [PORT]\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId, $serviceId, $endpointId) = $argv;
26-
$ip = isset($argv[6]) ? $argv[6] : '';
27-
$port = isset($argv[7]) ? (int) $argv[7] : 0;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2820

2921
// [START servicedirectory_create_endpoint]
3022
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
3123
use Google\Cloud\ServiceDirectory\V1beta1\Endpoint;
3224

33-
/** Uncomment and populate these variables in your code */
34-
// $projectId = '[YOUR_PROJECT_ID]';
35-
// $locationId = '[YOUR_GCP_REGION]';
36-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
37-
// $serviceId = '[YOUR_SERVICE_NAME]';
38-
// $endpointId = '[YOUR_ENDPOINT_NAME]';
39-
// $ip = '[IP_ADDRESS]'; // (Optional) Defaults to ''
40-
// $port = [PORT]; // (Optional) Defaults to 0
41-
42-
// Instantiate a client.
43-
$client = new RegistrationServiceClient();
44-
45-
// Construct Endpoint object.
46-
$endpointObject = (new Endpoint())
47-
->setAddress($ip)
48-
->setPort($port);
49-
50-
// Run request.
51-
$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
52-
$endpoint = $client->createEndpoint($serviceName, $endpointId, $endpointObject);
53-
54-
// Print results.
55-
printf('Created Endpoint: %s' . PHP_EOL, $endpoint->getName());
56-
printf(' IP: %s' . PHP_EOL, $endpoint->getAddress());
57-
printf(' Port: %d' . PHP_EOL, $endpoint->getPort());
25+
/**
26+
* @param string $projectId Your Cloud project ID
27+
* @param string $locationId Your GCP region
28+
* @param string $namespaceId Your namespace name
29+
* @param string $serviceId Your service name
30+
* @param string $endpointId Your endpoint name
31+
* @param string $ip (Optional) Defaults to ''
32+
* @param int $port (Optional) Defaults to 0
33+
*/
34+
function create_endpoint(
35+
string $projectId,
36+
string $locationId,
37+
string $namespaceId,
38+
string $serviceId,
39+
string $endpointId,
40+
string $ip = '',
41+
int $port = 0
42+
): void {
43+
// Instantiate a client.
44+
$client = new RegistrationServiceClient();
45+
46+
// Construct Endpoint object.
47+
$endpointObject = (new Endpoint())
48+
->setAddress($ip)
49+
->setPort($port);
50+
51+
// Run request.
52+
$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
53+
$endpoint = $client->createEndpoint($serviceName, $endpointId, $endpointObject);
54+
55+
// Print results.
56+
printf('Created Endpoint: %s' . PHP_EOL, $endpoint->getName());
57+
printf(' IP: %s' . PHP_EOL, $endpoint->getAddress());
58+
printf(' Port: %d' . PHP_EOL, $endpoint->getPort());
59+
}
5860
// [END servicedirectory_create_endpoint]
61+
62+
// The following 2 lines are only needed to execute the samples on the CLI
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

servicedirectory/src/create_namespace.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,34 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc != 4) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId) = $argv;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2620

2721
// [START servicedirectory_create_namespace]
2822
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
2923
use Google\Cloud\ServiceDirectory\V1beta1\PBNamespace;
3024

31-
/** Uncomment and populate these variables in your code */
32-
// $projectId = '[YOUR_PROJECT_ID]';
33-
// $locationId = '[YOUR_GCP_REGION]';
34-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
35-
36-
// Instantiate a client.
37-
$client = new RegistrationServiceClient();
38-
39-
// Run request.
40-
$locationName = RegistrationServiceClient::locationName($projectId, $locationId);
41-
$namespace = $client->createNamespace($locationName, $namespaceId, new PBNamespace());
42-
43-
// Print results.
44-
printf('Created Namespace: %s' . PHP_EOL, $namespace->getName());
25+
/**
26+
* @param string $projectId Your Cloud project ID
27+
* @param string $locationId Your GCP region
28+
* @param string $namespaceId Your namespace name
29+
*/
30+
function create_namespace(
31+
string $projectId,
32+
string $locationId,
33+
string $namespaceId
34+
): void {
35+
// Instantiate a client.
36+
$client = new RegistrationServiceClient();
37+
38+
// Run request.
39+
$locationName = RegistrationServiceClient::locationName($projectId, $locationId);
40+
$namespace = $client->createNamespace($locationName, $namespaceId, new PBNamespace());
41+
42+
// Print results.
43+
printf('Created Namespace: %s' . PHP_EOL, $namespace->getName());
44+
}
4545
// [END servicedirectory_create_namespace]
46+
47+
// The following 2 lines are only needed to execute the samples on the CLI
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

servicedirectory/src/create_service.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,36 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc != 5) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId, $serviceId) = $argv;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2620

2721
// [START servicedirectory_create_service]
2822
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
2923
use Google\Cloud\ServiceDirectory\V1beta1\Service;
3024

31-
/** Uncomment and populate these variables in your code */
32-
// $projectId = '[YOUR_PROJECT_ID]';
33-
// $locationId = '[YOUR_GCP_REGION]';
34-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
35-
// $serviceId = '[YOUR_SERVICE_NAME]';
36-
37-
// Instantiate a client.
38-
$client = new RegistrationServiceClient();
39-
40-
// Run request.
41-
$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
42-
$service = $client->createService($namespaceName, $serviceId, new Service());
43-
44-
// Print results.
45-
printf('Created Service: %s' . PHP_EOL, $service->getName());
25+
/**
26+
* @param string $projectId Your Cloud project ID
27+
* @param string $locationId Your GCP region
28+
* @param string $namespaceId Your namespace name
29+
* @param string $serviceId Your service name
30+
*/
31+
function create_service(
32+
string $projectId,
33+
string $locationId,
34+
string $namespaceId,
35+
string $serviceId
36+
): void {
37+
// Instantiate a client.
38+
$client = new RegistrationServiceClient();
39+
40+
// Run request.
41+
$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
42+
$service = $client->createService($namespaceName, $serviceId, new Service());
43+
44+
// Print results.
45+
printf('Created Service: %s' . PHP_EOL, $service->getName());
46+
}
4647
// [END servicedirectory_create_service]
48+
49+
// The following 2 lines are only needed to execute the samples on the CLI
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

servicedirectory/src/delete_endpoint.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,37 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc != 6) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID ENDPOINT_ID\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId, $serviceId, $endpointId) = $argv;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2620

2721
// [START servicedirectory_delete_endpoint]
2822
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
2923

30-
/** Uncomment and populate these variables in your code */
31-
// $projectId = '[YOUR_PROJECT_ID]';
32-
// $locationId = '[YOUR_GCP_REGION]';
33-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
34-
// $serviceId = '[YOUR_SERVICE_NAME]';
35-
// $endpointId = '[YOUR_ENDPOINT_NAME]';
36-
37-
// Instantiate a client.
38-
$client = new RegistrationServiceClient();
39-
40-
// Run request.
41-
$endpointName = RegistrationServiceClient::endpointName($projectId, $locationId, $namespaceId, $serviceId, $endpointId);
42-
$endpoint = $client->deleteEndpoint($endpointName);
43-
44-
// Print results.
45-
printf('Deleted Endpoint: %s' . PHP_EOL, $endpointName);
24+
/**
25+
* @param string $projectId Your Cloud project ID
26+
* @param string $locationId Your GCP region
27+
* @param string $namespaceId Your namespace name
28+
* @param string $serviceId Your service name
29+
* @param string $endpointId Your endpoint name
30+
*/
31+
function delete_endpoint(
32+
string $projectId,
33+
string $locationId,
34+
string $namespaceId,
35+
string $serviceId,
36+
string $endpointId
37+
): void {
38+
// Instantiate a client.
39+
$client = new RegistrationServiceClient();
40+
41+
// Run request.
42+
$endpointName = RegistrationServiceClient::endpointName($projectId, $locationId, $namespaceId, $serviceId, $endpointId);
43+
$endpoint = $client->deleteEndpoint($endpointName);
44+
45+
// Print results.
46+
printf('Deleted Endpoint: %s' . PHP_EOL, $endpointName);
47+
}
4648
// [END servicedirectory_delete_endpoint]
49+
50+
// The following 2 lines are only needed to execute the samples on the CLI
51+
require_once __DIR__ . '/../../testing/sample_helpers.php';
52+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

servicedirectory/src/delete_namespace.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc != 4) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId) = $argv;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2620

2721
// [START servicedirectory_delete_namespace]
2822
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
2923

30-
/** Uncomment and populate these variables in your code */
31-
// $projectId = '[YOUR_PROJECT_ID]';
32-
// $locationId = '[YOUR_GCP_REGION]';
33-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
34-
35-
// Instantiate a client.
36-
$client = new RegistrationServiceClient();
37-
38-
// Run request.
39-
$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
40-
$client->deleteNamespace($namespaceName);
41-
42-
// Print results.
43-
printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName);
24+
/**
25+
* @param string $projectId Your Cloud project ID
26+
* @param string $locationId Your GCP region
27+
* @param string $namespaceId Your namespace name
28+
*/
29+
function delete_namespace(
30+
string $projectId,
31+
string $locationId,
32+
string $namespaceId
33+
): void {
34+
// Instantiate a client.
35+
$client = new RegistrationServiceClient();
36+
37+
// Run request.
38+
$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
39+
$client->deleteNamespace($namespaceName);
40+
41+
// Print results.
42+
printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName);
43+
}
4444
// [END servicedirectory_delete_namespace]
45+
46+
// The following 2 lines are only needed to execute the samples on the CLI
47+
require_once __DIR__ . '/../../testing/sample_helpers.php';
48+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

servicedirectory/src/delete_service.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,35 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Include Google Cloud dependendencies using Composer
20-
require_once __DIR__ . '/../vendor/autoload.php';
21-
22-
if ($argc != 5) {
23-
return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID\n", basename(__FILE__));
24-
}
25-
list($_, $projectId, $locationId, $namespaceId, $serviceId) = $argv;
19+
namespace Google\Cloud\Samples\ServiceDirectory;
2620

2721
// [START servicedirectory_delete_service]
2822
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
2923

30-
/** Uncomment and populate these variables in your code */
31-
// $projectId = '[YOUR_PROJECT_ID]';
32-
// $locationId = '[YOUR_GCP_REGION]';
33-
// $namespaceId = '[YOUR_NAMESPACE_NAME]';
34-
// $serviceId = '[YOUR_SERVICE_NAME]';
35-
36-
// Instantiate a client.
37-
$client = new RegistrationServiceClient();
38-
39-
// Run request.
40-
$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
41-
$client->deleteService($serviceName);
42-
43-
// Print results.
44-
printf('Deleted Service: %s' . PHP_EOL, $serviceName);
24+
/**
25+
* @param string $projectId Your Cloud project ID
26+
* @param string $locationId Your GCP region
27+
* @param string $namespaceId Your namespace name
28+
* @param string $serviceId Your service name
29+
*/
30+
function delete_service(
31+
string $projectId,
32+
string $locationId,
33+
string $namespaceId,
34+
string $serviceId
35+
): void {
36+
// Instantiate a client.
37+
$client = new RegistrationServiceClient();
38+
39+
// Run request.
40+
$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
41+
$client->deleteService($serviceName);
42+
43+
// Print results.
44+
printf('Deleted Service: %s' . PHP_EOL, $serviceName);
45+
}
4546
// [END servicedirectory_delete_service]
47+
48+
// The following 2 lines are only needed to execute the samples on the CLI
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)