|
16 | 16 | * limitations under the License. |
17 | 17 | */ |
18 | 18 |
|
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; |
28 | 20 |
|
29 | 21 | // [START servicedirectory_create_endpoint] |
30 | 22 | use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient; |
31 | 23 | use Google\Cloud\ServiceDirectory\V1beta1\Endpoint; |
32 | 24 |
|
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 | +} |
58 | 60 | // [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); |
0 commit comments