Skip to content

Commit ff6140f

Browse files
authored
feat(spanner): add create instance sample (GoogleCloudPlatform#1129)
1 parent 894b6aa commit ff6140f

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

spanner/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ To run the Spanner Samples:
9999
add-timestamp-column Adds a commit timestamp column to a table.
100100
batch-query-data Batch queries sample data from the database using SQL.
101101
create-database Creates a database and tables for sample data.
102+
create-instance Creates an instance.
102103
create-index Adds a simple index to the example database.
103104
create-storing-index Adds an storing index to the example database.
104105
create-table-timestamp Creates a table with a commit timestamp column.

spanner/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"src/read_data.php",
1111
"src/read_write_transaction.php",
1212
"src/create_database.php",
13+
"src/create_instance.php",
1314
"src/query_data.php",
1415
"src/read_data_with_index.php",
1516
"src/update_data.php",

spanner/spanner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@
4848
new InputArgument('backup_id', InputArgument::REQUIRED, 'The backup id'),
4949
]);
5050

51+
// Create Instance command
52+
$application->add((new Command('create-instance'))
53+
->setDefinition($instanceInputDefinition)
54+
->setDescription('Creates an instance.')
55+
->setCode(function ($input, $output) {
56+
create_instance(
57+
$input->getArgument('instance_id')
58+
);
59+
})
60+
);
61+
5162
// Create Database command
5263
$application->add((new Command('create-database'))
5364
->setDefinition($inputDefinition)

spanner/src/create_instance.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* Copyright 2020 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_create_instance]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Creates an instance.
31+
* Example:
32+
* ```
33+
* create_instance($instanceId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
*/
38+
function create_instance($instanceId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instanceConfig = $spanner->instanceConfiguration(
42+
'regional-us-central1'
43+
);
44+
$operation = $spanner->createInstance(
45+
$instanceConfig,
46+
$instanceId,
47+
[
48+
'displayName' => 'This is a display name.',
49+
'nodeCount' => 1,
50+
'labels' => [
51+
'cloud_spanner_samples' => true,
52+
]
53+
]
54+
);
55+
56+
print('Waiting for operation to complete...' . PHP_EOL);
57+
$operation->pollUntilComplete();
58+
59+
printf('Created instance %s' . PHP_EOL, $instanceId);
60+
}
61+
// [END spanner_create_instance]

spanner/test/spannerTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,29 @@ public static function setUpBeforeClass()
5757
if (!extension_loaded('grpc')) {
5858
self::markTestSkipped('Must enable grpc extension.');
5959
}
60-
self::$instanceId = self::requireEnv('GOOGLE_SPANNER_INSTANCE_ID');
6160

6261
$spanner = new SpannerClient([
6362
'projectId' => self::$projectId,
6463
]);
6564

65+
self::$instanceId = 'test-' . time() . rand();
6666
self::$databaseId = 'test-' . time() . rand();
6767
self::$backupId = 'backup-' . self::$databaseId;
6868
self::$instance = $spanner->instance(self::$instanceId);
6969
}
7070

71+
public function testCreateInstance()
72+
{
73+
$output = $this->runCommand('create-instance', [
74+
'instance_id' => self::$instanceId
75+
]);
76+
$this->assertContains('Waiting for operation to complete...', $output);
77+
$this->assertContains('Created instance test-', $output);
78+
}
79+
80+
/**
81+
* @depends testCreateInstance
82+
*/
7183
public function testCreateDatabase()
7284
{
7385
$output = $this->runCommand('create-database');
@@ -612,8 +624,11 @@ public function testCreateClientWithQueryOptions()
612624
});
613625
}
614626

615-
private function runCommand($commandName)
627+
private function runCommand($commandName, $params = [])
616628
{
629+
if (!empty($params)) {
630+
return $this->traitRunCommand($commandName, $params);
631+
}
617632
return $this->traitRunCommand($commandName, [
618633
'instance_id' => self::$instanceId,
619634
'database_id' => self::$databaseId,
@@ -626,5 +641,6 @@ public static function tearDownAfterClass()
626641
$database = self::$instance->database(self::$databaseId);
627642
$database->drop();
628643
}
644+
self::$instance->delete();
629645
}
630646
}

0 commit comments

Comments
 (0)