diff --git a/servicedirectory/src/create_endpoint.php b/servicedirectory/src/create_endpoint.php index 367fb7f394..25ff6ae2f5 100644 --- a/servicedirectory/src/create_endpoint.php +++ b/servicedirectory/src/create_endpoint.php @@ -16,43 +16,49 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc < 6 || $argc > 8) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID ENDPOINT_ID [IP] [PORT]\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId, $serviceId, $endpointId) = $argv; -$ip = isset($argv[6]) ? $argv[6] : ''; -$port = isset($argv[7]) ? (int) $argv[7] : 0; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_create_endpoint] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; use Google\Cloud\ServiceDirectory\V1beta1\Endpoint; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; -// $serviceId = '[YOUR_SERVICE_NAME]'; -// $endpointId = '[YOUR_ENDPOINT_NAME]'; -// $ip = '[IP_ADDRESS]'; // (Optional) Defaults to '' -// $port = [PORT]; // (Optional) Defaults to 0 - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Construct Endpoint object. -$endpointObject = (new Endpoint()) - ->setAddress($ip) - ->setPort($port); - -// Run request. -$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); -$endpoint = $client->createEndpoint($serviceName, $endpointId, $endpointObject); - -// Print results. -printf('Created Endpoint: %s' . PHP_EOL, $endpoint->getName()); -printf(' IP: %s' . PHP_EOL, $endpoint->getAddress()); -printf(' Port: %d' . PHP_EOL, $endpoint->getPort()); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + * @param string $serviceId Your service name + * @param string $endpointId Your endpoint name + * @param string $ip (Optional) Defaults to '' + * @param int $port (Optional) Defaults to 0 + */ +function create_endpoint( + string $projectId, + string $locationId, + string $namespaceId, + string $serviceId, + string $endpointId, + string $ip = '', + int $port = 0 +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Construct Endpoint object. + $endpointObject = (new Endpoint()) + ->setAddress($ip) + ->setPort($port); + + // Run request. + $serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); + $endpoint = $client->createEndpoint($serviceName, $endpointId, $endpointObject); + + // Print results. + printf('Created Endpoint: %s' . PHP_EOL, $endpoint->getName()); + printf(' IP: %s' . PHP_EOL, $endpoint->getAddress()); + printf(' Port: %d' . PHP_EOL, $endpoint->getPort()); +} // [END servicedirectory_create_endpoint] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/create_namespace.php b/servicedirectory/src/create_namespace.php index 60fdb81f9c..4de95cbe50 100644 --- a/servicedirectory/src/create_namespace.php +++ b/servicedirectory/src/create_namespace.php @@ -16,30 +16,34 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 4) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_create_namespace] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; use Google\Cloud\ServiceDirectory\V1beta1\PBNamespace; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Run request. -$locationName = RegistrationServiceClient::locationName($projectId, $locationId); -$namespace = $client->createNamespace($locationName, $namespaceId, new PBNamespace()); - -// Print results. -printf('Created Namespace: %s' . PHP_EOL, $namespace->getName()); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + */ +function create_namespace( + string $projectId, + string $locationId, + string $namespaceId +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Run request. + $locationName = RegistrationServiceClient::locationName($projectId, $locationId); + $namespace = $client->createNamespace($locationName, $namespaceId, new PBNamespace()); + + // Print results. + printf('Created Namespace: %s' . PHP_EOL, $namespace->getName()); +} // [END servicedirectory_create_namespace] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/create_service.php b/servicedirectory/src/create_service.php index 8aa976fcfe..2b3aa2aa78 100644 --- a/servicedirectory/src/create_service.php +++ b/servicedirectory/src/create_service.php @@ -16,31 +16,36 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 5) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId, $serviceId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_create_service] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; use Google\Cloud\ServiceDirectory\V1beta1\Service; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; -// $serviceId = '[YOUR_SERVICE_NAME]'; - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Run request. -$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId); -$service = $client->createService($namespaceName, $serviceId, new Service()); - -// Print results. -printf('Created Service: %s' . PHP_EOL, $service->getName()); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + * @param string $serviceId Your service name + */ +function create_service( + string $projectId, + string $locationId, + string $namespaceId, + string $serviceId +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Run request. + $namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId); + $service = $client->createService($namespaceName, $serviceId, new Service()); + + // Print results. + printf('Created Service: %s' . PHP_EOL, $service->getName()); +} // [END servicedirectory_create_service] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/delete_endpoint.php b/servicedirectory/src/delete_endpoint.php index 22367b73e9..e6f14e486f 100644 --- a/servicedirectory/src/delete_endpoint.php +++ b/servicedirectory/src/delete_endpoint.php @@ -16,31 +16,37 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 6) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID ENDPOINT_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId, $serviceId, $endpointId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_delete_endpoint] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; -// $serviceId = '[YOUR_SERVICE_NAME]'; -// $endpointId = '[YOUR_ENDPOINT_NAME]'; - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Run request. -$endpointName = RegistrationServiceClient::endpointName($projectId, $locationId, $namespaceId, $serviceId, $endpointId); -$endpoint = $client->deleteEndpoint($endpointName); - -// Print results. -printf('Deleted Endpoint: %s' . PHP_EOL, $endpointName); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + * @param string $serviceId Your service name + * @param string $endpointId Your endpoint name + */ +function delete_endpoint( + string $projectId, + string $locationId, + string $namespaceId, + string $serviceId, + string $endpointId +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Run request. + $endpointName = RegistrationServiceClient::endpointName($projectId, $locationId, $namespaceId, $serviceId, $endpointId); + $endpoint = $client->deleteEndpoint($endpointName); + + // Print results. + printf('Deleted Endpoint: %s' . PHP_EOL, $endpointName); +} // [END servicedirectory_delete_endpoint] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/delete_namespace.php b/servicedirectory/src/delete_namespace.php index 89eee30152..0be477aeb2 100644 --- a/servicedirectory/src/delete_namespace.php +++ b/servicedirectory/src/delete_namespace.php @@ -16,29 +16,33 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 4) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_delete_namespace] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Run request. -$namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId); -$client->deleteNamespace($namespaceName); - -// Print results. -printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + */ +function delete_namespace( + string $projectId, + string $locationId, + string $namespaceId +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Run request. + $namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId); + $client->deleteNamespace($namespaceName); + + // Print results. + printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName); +} // [END servicedirectory_delete_namespace] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/delete_service.php b/servicedirectory/src/delete_service.php index c63218ea20..574705debe 100644 --- a/servicedirectory/src/delete_service.php +++ b/servicedirectory/src/delete_service.php @@ -16,30 +16,35 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 5) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId, $serviceId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_delete_service] use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; -// $serviceId = '[YOUR_SERVICE_NAME]'; - -// Instantiate a client. -$client = new RegistrationServiceClient(); - -// Run request. -$serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); -$client->deleteService($serviceName); - -// Print results. -printf('Deleted Service: %s' . PHP_EOL, $serviceName); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + * @param string $serviceId Your service name + */ +function delete_service( + string $projectId, + string $locationId, + string $namespaceId, + string $serviceId +): void { + // Instantiate a client. + $client = new RegistrationServiceClient(); + + // Run request. + $serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); + $client->deleteService($serviceName); + + // Print results. + printf('Deleted Service: %s' . PHP_EOL, $serviceName); +} // [END servicedirectory_delete_service] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/src/resolve_service.php b/servicedirectory/src/resolve_service.php index f1826e8c4f..4b74de6824 100644 --- a/servicedirectory/src/resolve_service.php +++ b/servicedirectory/src/resolve_service.php @@ -16,37 +16,42 @@ * limitations under the License. */ -// Include Google Cloud dependendencies using Composer -require_once __DIR__ . '/../vendor/autoload.php'; - -if ($argc != 5) { - return printf("Usage: php %s PROJECT_ID LOCATION_ID NAMESPACE_ID SERVICE_ID\n", basename(__FILE__)); -} -list($_, $projectId, $locationId, $namespaceId, $serviceId) = $argv; +namespace Google\Cloud\Samples\ServiceDirectory; // [START servicedirectory_resolve_service] use Google\Cloud\ServiceDirectory\V1beta1\LookupServiceClient; use Google\Cloud\ServiceDirectory\V1beta1\Service; -/** Uncomment and populate these variables in your code */ -// $projectId = '[YOUR_PROJECT_ID]'; -// $locationId = '[YOUR_GCP_REGION]'; -// $namespaceId = '[YOUR_NAMESPACE_NAME]'; -// $serviceId = '[YOUR_SERVICE_NAME]'; - -// Instantiate a client. -$client = new LookupServiceClient(); - -// Run request. -$serviceName = LookupServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); -$service = $client->resolveService($serviceName)->getService(); - -// Print results. -printf('Resolved Service: %s' . PHP_EOL, $service->getName()); -print('Endpoints:' . PHP_EOL); -foreach ($service->getEndpoints() as $endpoint) { - printf(' Name: %s' . PHP_EOL, $endpoint->getName()); - printf(' IP: %s' . PHP_EOL, $endpoint->getAddress()); - printf(' Port: %d' . PHP_EOL, $endpoint->getPort()); +/** + * @param string $projectId Your Cloud project ID + * @param string $locationId Your GCP region + * @param string $namespaceId Your namespace name + * @param string $serviceId Your service name + */ +function resolve_service( + string $projectId, + string $locationId, + string $namespaceId, + string $serviceId +): void { + // Instantiate a client. + $client = new LookupServiceClient(); + + // Run request. + $serviceName = LookupServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId); + $service = $client->resolveService($serviceName)->getService(); + + // Print results. + printf('Resolved Service: %s' . PHP_EOL, $service->getName()); + print('Endpoints:' . PHP_EOL); + foreach ($service->getEndpoints() as $endpoint) { + printf(' Name: %s' . PHP_EOL, $endpoint->getName()); + printf(' IP: %s' . PHP_EOL, $endpoint->getAddress()); + printf(' Port: %d' . PHP_EOL, $endpoint->getPort()); + } } // [END servicedirectory_resolve_service] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/servicedirectory/test/servicedirectoryTest.php b/servicedirectory/test/servicedirectoryTest.php index 913b430a60..aaaf557bb1 100644 --- a/servicedirectory/test/servicedirectoryTest.php +++ b/servicedirectory/test/servicedirectoryTest.php @@ -47,14 +47,14 @@ public function testNamespaces() $namespaceId = uniqid('sd-php-namespace-'); $namespaceName = sprintf('projects/%s/locations/%s/namespaces/%s', self::$projectId, self::$locationId, $namespaceId); - $output = $this->runSnippet('create_namespace', [ + $output = $this->runFunctionSnippet('create_namespace', [ self::$projectId, self::$locationId, $namespaceId ]); $this->assertStringContainsString('Created Namespace: ' . $namespaceName, $output); - $output = $this->runSnippet('delete_namespace', [ + $output = $this->runFunctionSnippet('delete_namespace', [ self::$projectId, self::$locationId, $namespaceId @@ -70,13 +70,13 @@ public function testServices() $serviceName = sprintf('%s/services/%s', $namespaceName, $serviceId); // Setup: create a namespace for the service to live in. - $output = $this->runSnippet('create_namespace', [ + $output = $this->runFunctionSnippet('create_namespace', [ self::$projectId, self::$locationId, $namespaceId ]); $this->assertStringContainsString('Created Namespace: ' . $namespaceName, $output); - $output = $this->runSnippet('create_service', [ + $output = $this->runFunctionSnippet('create_service', [ self::$projectId, self::$locationId, $namespaceId, @@ -84,7 +84,7 @@ public function testServices() ]); $this->assertStringContainsString('Created Service: ' . $serviceName, $output); - $output = $this->runSnippet('delete_service', [ + $output = $this->runFunctionSnippet('delete_service', [ self::$projectId, self::$locationId, $namespaceId, @@ -105,13 +105,13 @@ public function testEndpoints() $port = 8080; // Setup: create a namespace and service for the service to live in. - $output = $this->runSnippet('create_namespace', [ + $output = $this->runFunctionSnippet('create_namespace', [ self::$projectId, self::$locationId, $namespaceId ]); $this->assertStringContainsString('Created Namespace: ' . $namespaceName, $output); - $output = $this->runSnippet('create_service', [ + $output = $this->runFunctionSnippet('create_service', [ self::$projectId, self::$locationId, $namespaceId, @@ -119,7 +119,7 @@ public function testEndpoints() ]); $this->assertStringContainsString('Created Service: ' . $serviceName, $output); - $output = $this->runSnippet('create_endpoint', [ + $output = $this->runFunctionSnippet('create_endpoint', [ self::$projectId, self::$locationId, $namespaceId, @@ -132,7 +132,7 @@ public function testEndpoints() $this->assertStringContainsString('IP: ' . $ip, $output); $this->assertStringContainsString('Port: ' . $port, $output); - $output = $this->runSnippet('delete_endpoint', [ + $output = $this->runFunctionSnippet('delete_endpoint', [ self::$projectId, self::$locationId, $namespaceId, @@ -154,20 +154,20 @@ public function testResolveService() $port = 8080; // Setup: create a namespace, service, and endpoint. - $output = $this->runSnippet('create_namespace', [ + $output = $this->runFunctionSnippet('create_namespace', [ self::$projectId, self::$locationId, $namespaceId ]); $this->assertStringContainsString('Created Namespace: ' . $namespaceName, $output); - $output = $this->runSnippet('create_service', [ + $output = $this->runFunctionSnippet('create_service', [ self::$projectId, self::$locationId, $namespaceId, $serviceId ]); $this->assertStringContainsString('Created Service: ' . $serviceName, $output); - $output = $this->runSnippet('create_endpoint', [ + $output = $this->runFunctionSnippet('create_endpoint', [ self::$projectId, self::$locationId, $namespaceId, @@ -178,7 +178,7 @@ public function testResolveService() ]); $this->assertStringContainsString('Created Endpoint: ' . $endpointName, $output); - $output = $this->runSnippet('resolve_service', [ + $output = $this->runFunctionSnippet('resolve_service', [ self::$projectId, self::$locationId, $namespaceId,