Skip to content

Commit 6ad9c85

Browse files
authored
feat(servicedirectory): add samples for Service Directory (GoogleCloudPlatform#1127)
1 parent 93fb450 commit 6ad9c85

13 files changed

+713
-0
lines changed

servicedirectory/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Google Service Directory PHP Sample Application
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=servicedirectory
7+
8+
## Description
9+
10+
This simple command-line application demonstrates how to invoke the
11+
[Google Service Directory API][servicedirectory-api] from PHP.
12+
13+
[servicedirectory-api]: https://cloud.google.com/service-directory/
14+
15+
## Build and Run
16+
1. **Enable APIs** - [Enable the Service Directory API](
17+
https://console.cloud.google.com/flows/enableapi?apiid=servicedirectory.googleapis.com)
18+
and create a new project or select an existing project.
19+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click
20+
"New Credentials"
21+
and select "Service Account Key". Create a new service account, use the JSON key type, and
22+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
23+
to the path of the JSON key that was downloaded.
24+
3. **Clone the repo** and cd into this directory
25+
```
26+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
27+
$ cd php-docs-samples/servicedirectory
28+
```
29+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
30+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
31+
(if composer is installed globally).
32+
5. Execute the snippets in the [src/](src/) directory by running
33+
`php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
34+
are provided:
35+
```sh
36+
$ php src/quickstart.php
37+
Usage: php src/quickstart.php PROJECT_ID STRING
38+
39+
$ php src/quickstart.php your-project-id us-east1
40+
Namespaces: projects/you-project-id/locations/us-east1/namespaces/your-namespace
41+
```
42+
43+
See the [Service Directory Documentation](https://cloud.google.com/service-directory/docs/) for more information.
44+
45+
## Troubleshooting
46+
47+
### bcmath extension missing
48+
49+
If you see an error like this:
50+
51+
```
52+
PHP Fatal error: Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp() in /usr/local/google/home/crwilson/github/GoogleCloudPlatform/php-docs-samples/dlp/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:986
53+
```
54+
55+
You may need to install the bcmath PHP extension.
56+
e.g. (may depend on your php version)
57+
```
58+
$ sudo apt-get install php7.3-bcmath
59+
```
60+
61+
62+
## Contributing changes
63+
64+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
65+
66+
## Licensing
67+
68+
* See [LICENSE](../LICENSE)

servicedirectory/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-service-directory": "^0.1.3"
4+
}
5+
}

servicedirectory/phpunit.xml.dist

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../testing/bootstrap.php"
18+
convertNoticesToExceptions="false">
19+
<testsuites>
20+
<testsuite name="PHP Service Directory test">
21+
<directory>testdirectory>
22+
testsuite>
23+
testsuites>
24+
<logging>
25+
<log type="coverage-clover" target="build/logs/clover.xml"/>
26+
logging>
27+
<filter>
28+
<whitelist>
29+
<directory suffix=".php">./srcdirectory>
30+
<exclude>
31+
<directory>./vendordirectory>
32+
exclude>
33+
whitelist>
34+
filter>
35+
phpunit>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
/**
4+
* Copyright 2020 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
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;
28+
29+
// [START servicedirectory_create_endpoint]
30+
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
31+
use Google\Cloud\ServiceDirectory\V1beta1\Endpoint;
32+
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());
58+
// [END servicedirectory_create_endpoint]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
/**
4+
* Copyright 2020 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
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;
26+
27+
// [START servicedirectory_create_namespace]
28+
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
29+
use Google\Cloud\ServiceDirectory\V1beta1\PBNamespace;
30+
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());
45+
// [END servicedirectory_create_namespace]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
3+
/**
4+
* Copyright 2020 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
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;
26+
27+
// [START servicedirectory_create_service]
28+
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
29+
use Google\Cloud\ServiceDirectory\V1beta1\Service;
30+
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());
46+
// [END servicedirectory_create_service]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
/**
4+
* Copyright 2020 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
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;
26+
27+
// [START servicedirectory_delete_endpoint]
28+
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
29+
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+
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+
// [END servicedirectory_delete_endpoint]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
/**
4+
* Copyright 2020 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
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;
26+
27+
// [START servicedirectory_delete_namespace]
28+
use Google\Cloud\ServiceDirectory\V1beta1\RegistrationServiceClient;
29+
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);
44+
// [END servicedirectory_delete_namespace]

0 commit comments

Comments
 (0)