Skip to content

Commit 2d291b1

Browse files
authored
chore: upgrade auth samples to latest format (GoogleCloudPlatform#1375)
1 parent 5da07b5 commit 2d291b1

18 files changed

+103
-398
lines changed

auth/README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,24 @@ methods will work on any Google Cloud API.
2626
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
2727
Run `php composer.phar install --no-dev` (if composer is installed locally) or `composer install --no-dev`
2828
(if composer is installed globally).
29-
5. Run `php auth.php`. The following commands are available and work on command line:
29+
5. **Run the samples** to run the auth samples, run any of the files in `src/` on the CLI:
3030
```
31-
auth-cloud-implicit Authenticate to a cloud client library using a service account implicitly.
32-
auth-cloud-explicit Authenticate to a cloud client library using a service account explicitly.
33-
auth-api-implicit Authenticate to a cloud API using a service account implicitly.
34-
auth-api-explicit Authenticate to a cloud API using a service account explicitly.
35-
auth-http-implicit Authenticate to a cloud API with HTTP using a service account implicitly.
36-
auth-http-explicit Authenticate to a cloud API with HTTP using a service account explicitly.
31+
$ php src/auth_api_explicit.php
32+
33+
Usage: auth_api_explicit.php $projectId $serviceAccountPath
34+
35+
@param string $projectId The Google project ID.
36+
@param string $serviceAccountPath Path to service account credentials JSON.
3737
```
38-
6. The following commands are available but will throw a ServiceException when
39-
run from command-line. The Compute Engine method only works on Compute Engine,
40-
App Engine Flexible, Cloud Functions, and Container Engine. The App Engine
41-
method only works on App Engine Standard.
38+
6. The following files are available but cannot be run from the CLI. The Compute
39+
methods only work on Compute Engine, App Engine, Cloud Functions,
40+
and Container Engine.
4241
```
43-
auth-cloud-explicit-compute-engine Authenticate to a cloud client library using Compute Engine credentials explicitly.
44-
auth-cloud-explicit-app-engine Authenticate to a cloud client library using App Engine Standard credentials explicitly.
45-
auth-api-explicit-compute-engine Authenticate to a cloud API using Compute Engine credentials explicitly.
46-
auth-api-explicit-app-engine Authenticate to a cloud API using App Engine Standard credentials explicitly.
42+
src/auth_cloud_explicit_compute.php
43+
src/auth_api_explicit_compute.php
4744
```
48-
7. You can test the samples that use Compute Engine / App Engine credentials by
49-
deploying to either App Engine Flexible (which allows usage of Compute Engine
50-
credentials since App Engine Flexible apps run on Compute Engine instances) or
51-
App Engine Standard. Run either `gcloud app deploy app-standard.yaml` or
52-
`gcloud app deploy app-flex.yaml`.
53-
54-
8. Run `php auth.php COMMAND --help` to print information about the usage of each command.
45+
7. You can test the samples that use Compute credentials by deploying to App
46+
Engine Standard. Run `gcloud app deploy`.
5547

5648
## Contributing changes
5749

auth/app-flex.yaml

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

auth/app-standard.yaml

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

auth/app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: php72

auth/auth.php

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

auth/composer.json

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22
"require": {
33
"google/apiclient": "^2.1",
44
"google/cloud-storage": "^1.3",
5-
"symfony/console": " ^3.0",
65
"google/auth":"^1.0"
76
},
7+
"scripts": {
8+
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
9+
},
10+
"extra": {
11+
"google/apiclient-services": [
12+
"Storage"
13+
]
14+
},
815
"autoload": {
9-
"psr-4": {
10-
"Google\\Cloud\\Samples\\Auth\\": "src/"
11-
},
1216
"files": [
13-
"src/auth_cloud_implicit.php",
14-
"src/auth_cloud_explicit.php",
15-
"src/auth_cloud_explicit_compute_engine.php",
16-
"src/auth_cloud_explicit_app_engine.php",
17-
"src/auth_api_implicit.php",
18-
"src/auth_api_explicit.php",
19-
"src/auth_api_explicit_compute_engine.php",
20-
"src/auth_api_explicit_app_engine.php",
21-
"src/auth_http_implicit.php",
22-
"src/auth_http_explicit.php"
17+
"src/auth_cloud_explicit_compute.php",
18+
"src/auth_api_explicit_compute.php"
2319
]
2420
}
2521
}

auth/index.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,20 @@
1717

1818
namespace Google\Cloud\Samples\Auth;
1919

20-
use Google\Auth\Credentials\GCECredentials;
21-
use google\appengine\api\app_identity\AppIdentityService;
22-
2320
// Install composer dependencies with "composer install --no-dev"
2421
// @see http://getcomposer.org for more information.
2522
require __DIR__ . '/vendor/autoload.php';
2623

27-
$onGce = GCECredentials::onGce();
28-
$projectId = $onGce
29-
? getenv('GCLOUD_PROJECT')
30-
: AppIdentityService::getApplicationId();
24+
$projectId = getenv('GOOGLE_CLOUD_PROJECT')
25+
3126
?>
3227

3328

Buckets retrieved using the cloud client library:

3429
35-
if ($onGce): ?>
36-
auth_cloud_explicit_compute_engine($projectId) ?>
37-
else: ?>
38-
auth_cloud_explicit_app_engine($projectId) ?>
39-
endif ?>
30+
auth_cloud_explicit_compute($projectId) ?>
4031
4132

4233

Buckets retrieved using the api client:

4334
44-
if ($onGce): ?>
45-
auth_api_explicit_compute_engine($projectId) ?>
46-
else: ?>
47-
auth_api_explicit_app_engine($projectId) ?>
48-
endif ?>
35+
auth_api_explicit_compute($projectId) ?>
4936

auth/src/auth_api_explicit.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
use Google_Client;
2727
use Google_Service_Storage;
2828

29+
/**
30+
* Authenticate to a cloud API using a service account explicitly.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $serviceAccountPath Path to service account credentials JSON.
34+
*/
2935
function auth_api_explicit($projectId, $serviceAccountPath)
3036
{
3137
$client = new Google_Client();
@@ -42,3 +48,6 @@ function auth_api_explicit($projectId, $serviceAccountPath)
4248
}
4349
}
4450
# [END auth_api_explicit]
51+
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)