Skip to content

Commit b34ce44

Browse files
author
Takashi Matsuo
authored
Added project-id flag for documentation (GoogleCloudPlatform#206)
1 parent 2c805cc commit b34ce44

File tree

8 files changed

+96
-30
lines changed

8 files changed

+96
-30
lines changed

datastore/tutorial/README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ https://cloud.google.com/datastore/docs/datastore-api-tutorial
66
The code is using
77
[Google Cloud Client Library for PHP](https://googlecloudplatform.github.io/google-cloud-php/#/).
88

9-
To run the sample, do the following:
9+
To run the sample, do the following first:
1010

1111
1. [Enable billing](https://support.google.com/cloud/answer/6293499#enable-billing).
1212
1. [Enable the Cloud Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com).
13-
1. Create a service account at the
14-
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)
15-
1. Download the json key file of the service account.
16-
1. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.
1713
1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
1814
Run `php composer.phar install` (if composer is installed locally) or `composer install`
1915
(if composer is installed globally).
20-
1. Run the command: `php tasks`
16+
17+
Then use one of the following methods:
18+
19+
1. Run `gcloud auth application-default login`
20+
21+
or
22+
23+
1. Create a service account at the
24+
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)
25+
1. Download the json key file of the service account.
26+
1. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.
27+
28+
Then you can run the command: `php tasks`

datastore/tutorial/src/CreateTaskCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Command\Command;
2121
use Symfony\Component\Console\Input\InputArgument;
2222
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
2324
use Symfony\Component\Console\Output\OutputInterface;
2425

2526
/**
@@ -38,12 +39,23 @@ protected function configure()
3839
'description',
3940
InputArgument::REQUIRED,
4041
'The description of the new task'
42+
)
43+
->addOption(
44+
'project-id',
45+
null,
46+
InputOption::VALUE_OPTIONAL,
47+
'Your cloud project id'
4148
);
4249
}
4350

4451
protected function execute(InputInterface $input, OutputInterface $output)
4552
{
46-
$datastore = build_datastore_service();
53+
$projectId = $input->getOption('project-id');
54+
if (!empty($projectId)) {
55+
$datastore = build_datastore_service($projectId);
56+
} else {
57+
$datastore = build_datastore_service_with_namespace();
58+
}
4759
$description = $input->getArgument('description');
4860
$task = add_task($datastore, $description);
4961
$output->writeln(

datastore/tutorial/src/DeleteTaskCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Command\Command;
2121
use Symfony\Component\Console\Input\InputArgument;
2222
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
2324
use Symfony\Component\Console\Output\OutputInterface;
2425

2526
/**
@@ -38,12 +39,24 @@ protected function configure()
3839
'taskId',
3940
InputArgument::REQUIRED,
4041
'The id of the task to delete'
42+
)
43+
->addOption(
44+
'project-id',
45+
null,
46+
InputOption::VALUE_OPTIONAL,
47+
'Your cloud project id'
4148
);
49+
4250
}
4351

4452
protected function execute(InputInterface $input, OutputInterface $output)
4553
{
46-
$datastore = build_datastore_service();
54+
$projectId = $input->getOption('project-id');
55+
if (!empty($projectId)) {
56+
$datastore = build_datastore_service($projectId);
57+
} else {
58+
$datastore = build_datastore_service_with_namespace();
59+
}
4760
$taskId = intval($input->getArgument('taskId'));
4861
delete_task($datastore, $taskId);
4962
$output->writeln(sprintf('Task %d deleted successfully.', $taskId));

datastore/tutorial/src/ListTasksCommand.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Console\Command\Command;
2222
use Symfony\Component\Console\Helper\Table;
2323
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
2425
use Symfony\Component\Console\Output\OutputInterface;
2526

2627
/**
@@ -35,12 +36,23 @@ protected function configure()
3536
{
3637
$this->setName('list-tasks')
3738
->setDescription(
38-
'List all the tasks in ascending order of creation time');
39+
'List all the tasks in ascending order of creation time')
40+
->addOption(
41+
'project-id',
42+
null,
43+
InputOption::VALUE_OPTIONAL,
44+
'Your cloud project id'
45+
);
3946
}
4047

4148
protected function execute(InputInterface $input, OutputInterface $output)
4249
{
43-
$datastore = build_datastore_service();
50+
$projectId = $input->getOption('project-id');
51+
if (!empty($projectId)) {
52+
$datastore = build_datastore_service($projectId);
53+
} else {
54+
$datastore = build_datastore_service_with_namespace();
55+
}
4456
$result = list_tasks($datastore);
4557
$table = new Table($output);
4658
$table->setHeaders(array('ID', 'Description', 'Status', 'Created'));

datastore/tutorial/src/MarkTaskDoneCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Command\Command;
2121
use Symfony\Component\Console\Input\InputArgument;
2222
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
2324
use Symfony\Component\Console\Output\OutputInterface;
2425

2526
/**
@@ -38,12 +39,23 @@ protected function configure()
3839
'taskId',
3940
InputArgument::REQUIRED,
4041
'The id of the task to mark as done'
42+
)
43+
->addOption(
44+
'project-id',
45+
null,
46+
InputOption::VALUE_OPTIONAL,
47+
'Your cloud project id'
4148
);
4249
}
4350

4451
protected function execute(InputInterface $input, OutputInterface $output)
4552
{
46-
$datastore = build_datastore_service();
53+
$projectId = $input->getOption('project-id');
54+
if (!empty($projectId)) {
55+
$datastore = build_datastore_service($projectId);
56+
} else {
57+
$datastore = build_datastore_service_with_namespace();
58+
}
4759
$taskId = intval($input->getArgument('taskId'));
4860
mark_done($datastore, $taskId);
4961
$output->writeln(sprintf('Task %d updated successfully.', $taskId));

datastore/tutorial/src/functions.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,54 @@
1717

1818
namespace Google\Cloud\Samples\Datastore\Tasks;
1919

20+
use DateTime;
2021
use Generator;
22+
use Google;
23+
// [START build_service]
2124
use Google\Cloud\Datastore\DatastoreClient;
22-
use Google\Cloud\Datastore\Entity;
2325

2426
/**
25-
* Create a Datastore service.
27+
* Create a Cloud Datastore client.
2628
*
29+
* @param string $projectId
2730
* @return DatastoreClient
2831
*/
29-
function build_datastore_service()
32+
function build_datastore_service($projectId)
3033
{
31-
// [START build_service]
32-
$datastore = new DatastoreClient();
33-
// [END build_service]
34-
$namespace = getenv('CLOUD_DATASTORE_NAMESPACE');
35-
if ($namespace !== false) {
36-
$datastore = new DatastoreClient(['namespaceId' => $namespace]);
37-
}
34+
$datastore = new DatastoreClient(['projectId' => $projectId]);
3835
return $datastore;
3936
}
37+
// [END build_service]
38+
39+
/**
40+
* Create a Cloud Datastore client with a namespace.
41+
*
42+
* @return DatastoreClient
43+
*/
44+
function build_datastore_service_with_namespace()
45+
{
46+
$namespaceId = getenv('CLOUD_DATASTORE_NAMESPACE');
47+
if ($namespaceId === false) {
48+
return new DatastoreClient();
49+
}
50+
return new DatastoreClient(['namespaceId' => $namespaceId]);
51+
}
4052

4153
// [START add_entity]
4254
/**
4355
* Create a new task with a given description.
4456
*
4557
* @param DatastoreClient $datastore
4658
* @param $description
47-
* @return \Google\Cloud\Datastore\Entity
59+
* @return Google\Cloud\Datastore\Entity
4860
*/
4961
function add_task(DatastoreClient $datastore, $description)
5062
{
5163
$taskKey = $datastore->key('Task');
5264
$task = $datastore->entity(
5365
$taskKey,
5466
[
55-
'created' => new \DateTime(),
67+
'created' => new DateTime(),
5668
'description' => $description,
5769
'done' => false
5870
],
@@ -100,7 +112,7 @@ function delete_task(DatastoreClient $datastore, $taskId)
100112
* Return a generator for all the tasks in ascending order of creation time.
101113
*
102114
* @param DatastoreClient $datastore
103-
* @return Generator<Entity>
115+
* @return Generator<Google\Cloud\Datastore\Entity>
104116
*/
105117
function list_tasks(DatastoreClient $datastore)
106118
{

datastore/tutorial/test/CommandSystemTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function setUp()
3838
'No service account credentials were found.'
3939
);
4040
}
41-
$this->datastore = build_datastore_service();
41+
$this->datastore = build_datastore_service_with_namespace();
4242
// Also delete stale entities here.
4343
/* @var array<Key> $keys */
4444
$keys = [];
@@ -47,7 +47,6 @@ public function setUp()
4747
$keys[] = $entity->key();
4848
}
4949
$this->datastore->deleteBatch($keys);
50-
5150
$this->keys = array();
5251
}
5352

datastore/tutorial/test/FunctionsTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ public static function setUpBeforeClass()
3838
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
3939
self::$hasCredentials = $path && file_exists($path) &&
4040
filesize($path) > 0;
41-
self::$datastore = new DatastoreClient(
42-
array('namespaceId' => uniqid())
43-
);
41+
self::$datastore = build_datastore_service_with_namespace();
4442
self::$keys[] = self::$datastore->key('Task', 'sampleTask');
4543
}
4644

4745
public function testBuildDatastoreService()
4846
{
49-
$client = build_datastore_service();
47+
$client = build_datastore_service('my-project-id');
5048
$this->assertInstanceOf(DatastoreClient::class, $client);
5149
}
5250

0 commit comments

Comments
 (0)