Skip to content

Commit 80801c9

Browse files
authored
chore: update pubsub samples to new test runner (GoogleCloudPlatform#1356)
1 parent c594414 commit 80801c9

26 files changed

+209
-422
lines changed

pubsub/api/README.md

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,80 @@ This simple command-line application demonstrates how to invoke
77

88
[pubsub]: https://cloud.google.com/pubsub/docs/quickstart-client-libraries
99

10-
## Build and Run
11-
1. **Enable APIs** - [Enable the Pub\Sub API](https://console.cloud.google.com/flows/enableapi?apiid=pubsub)
12-
and create a new project or select an existing project.
13-
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
14-
and select "Service Account Key". Create a new service account, use the JSON key type, and
15-
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
16-
to the path of the JSON key that was downloaded.
17-
3. **Clone the repo** and cd into this directory
18-
19-
```sh
20-
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
21-
$ cd php-docs-samples/pubsub/api
22-
```
23-
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
10+
## Setup
11+
12+
### Authentication
13+
14+
Authentication is typically done through [Application Default Credentials][adc]
15+
which means you do not have to change the code to authenticate as long as
16+
your environment has credentials. You have a few options for setting up
17+
authentication:
18+
19+
1. When running locally, use the [Google Cloud SDK][google-cloud-sdk]
20+
21+
gcloud auth application-default login
22+
23+
1. When running on App Engine or Compute Engine, credentials are already
24+
set-up. However, you may need to configure your Compute Engine instance
25+
with [additional scopes][additional_scopes].
26+
27+
1. You can create a [Service Account key file][service_account_key_file]. This file can be used to
28+
authenticate to Google Cloud Platform services from any environment. To use
29+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
30+
the path to the key file, for example:
31+
32+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
33+
34+
[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
35+
[additional_scopes]: https://cloud.google.com/compute/docs/authentication#using
36+
[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
37+
38+
## Install Dependencies
39+
40+
1. Ensure the [gRPC PHP Extension][php_grpc] is installed and enabled on your machine.
41+
1. [Enable the Cloud Pub/Sub API](https://console.cloud.google.com/flows/enableapi?apiid=pubsub.googleapis.com).
42+
43+
1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
2444
Run `php composer.phar install` (if composer is installed locally) or `composer install`
2545
(if composer is installed globally).
26-
5. Run `php pubsub.php`. The following commands are available:
2746

28-
```sh
29-
iam Manage IAM for Pub\Sub
30-
subscription Manage subscriptions for Pub\Sub
31-
topic Manage topics for Pub\Sub
47+
1. Create a service account at the
48+
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)
49+
50+
1. Download the json key file of the service account.
51+
52+
1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to that file.
53+
54+
## Samples
55+
56+
To run the Pub/Sub Samples, run any of the files in `src/` on the CLI:
57+
58+
```
59+
$ php src/create-topic.php
60+
61+
Usage: create_topic.php $projectId $topicName
62+
63+
@param string $projectId The Google project ID.
64+
@param string $topicName The Pub/Sub topic name.
3265
```
33-
6. Run `php pubsub.php COMMAND --help` to print information about the usage of each command.
3466

35-
## Contributing changes
67+
## Troubleshooting
68+
69+
If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID:
70+
71+
```
72+
[Google\Cloud\Core\Exception\GoogleException]
73+
No project ID was provided, and we were unable to detect a default project ID.
74+
```
3675

37-
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
76+
## The client library
3877

39-
## Licensing
78+
This sample uses the [Google Cloud Client Library for PHP][google-cloud-php].
79+
You can read the documentation for more details on API usage and use GitHub
80+
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
4081

41-
* See [LICENSE](../../LICENSE)
82+
[php_grpc]: http://cloud.google.com/php/grpc
83+
[google-cloud-php]: https://googlecloudplatform.github.io/google-cloud-php
84+
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
85+
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
86+
[google-cloud-sdk]: https://cloud.google.com/sdk/

pubsub/api/composer.json

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,5 @@
33
"php": ">=5.4",
44
"google/cloud-pubsub": "^1.29",
55
"symfony/console": " ^3.0"
6-
},
7-
"autoload": {
8-
"files": [
9-
"src/create_subscription.php",
10-
"src/create_topic.php",
11-
"src/dead_letter_create_subscription.php",
12-
"src/dead_letter_delivery_attempt.php",
13-
"src/dead_letter_remove.php",
14-
"src/dead_letter_update_subscription.php",
15-
"src/create_push_subscription.php",
16-
"src/delete_subscription.php",
17-
"src/delete_topic.php",
18-
"src/detach_subscription.php",
19-
"src/get_subscription_policy.php",
20-
"src/get_topic_policy.php",
21-
"src/list_subscriptions.php",
22-
"src/list_topics.php",
23-
"src/publish_message_batch.php",
24-
"src/publish_message.php",
25-
"src/pull_messages.php",
26-
"src/set_subscription_policy.php",
27-
"src/set_topic_policy.php",
28-
"src/test_subscription_permissions.php",
29-
"src/test_topic_permissions.php"
30-
]
316
}
327
}

pubsub/api/pubsub.php

Lines changed: 0 additions & 247 deletions
This file was deleted.

pubsub/api/src/create_push_subscription.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ function create_push_subscription($projectId, $topicName, $subscriptionName, $en
4848
printf('Subscription created: %s' . PHP_EOL, $subscription->name());
4949
}
5050
# [END pubsub_create_push_subscription]
51+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
52+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/src/create_subscription.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ function create_subscription($projectId, $topicName, $subscriptionName)
4545
printf('Subscription created: %s' . PHP_EOL, $subscription->name());
4646
}
4747
# [END pubsub_create_pull_subscription]
48+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)