Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ To run the Spanner Samples:
add-timestamp-column Adds a commit timestamp column to a table.
batch-query-data Batch queries sample data from the database using SQL.
create-database Creates a database and tables for sample data.
create-instance Creates an instance.
create-index Adds a simple index to the example database.
create-storing-index Adds an storing index to the example database.
create-table-timestamp Creates a table with a commit timestamp column.
Expand Down
1 change: 1 addition & 0 deletions spanner/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"src/read_data.php",
"src/read_write_transaction.php",
"src/create_database.php",
"src/create_instance.php",
"src/query_data.php",
"src/read_data_with_index.php",
"src/update_data.php",
Expand Down
11 changes: 11 additions & 0 deletions spanner/spanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
new InputArgument('backup_id', InputArgument::REQUIRED, 'The backup id'),
]);

// Create Instance command
$application->add((new Command('create-instance'))
->setDefinition($instanceInputDefinition)
->setDescription('Creates an instance.')
->setCode(function ($input, $output) {
create_instance(
$input->getArgument('instance_id')
);
})
);

// Create Database command
$application->add((new Command('create-database'))
->setDefinition($inputDefinition)
Expand Down
61 changes: 61 additions & 0 deletions spanner/src/create_instance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
*/

namespace Google\Cloud\Samples\Spanner;

// [START spanner_create_instance]
use Google\Cloud\Spanner\SpannerClient;

/**
* Creates an instance.
* Example:
* ```
* create_instance($instanceId);
* ```
*
* @param string $instanceId The Spanner instance ID.
*/
function create_instance($instanceId)
{
$spanner = new SpannerClient();
$instanceConfig = $spanner->instanceConfiguration(
'regional-us-central1'
);
$operation = $spanner->createInstance(
$instanceConfig,
$instanceId,
[
'displayName' => 'This is a display name.',
'nodeCount' => 1,
'labels' => [
'cloud_spanner_samples' => true,
]
]
);

print('Waiting for operation to complete...' . PHP_EOL);
$operation->pollUntilComplete();

printf('Created instance %s' . PHP_EOL, $instanceId);
}
// [END spanner_create_instance]
20 changes: 18 additions & 2 deletions spanner/test/spannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,29 @@ public static function setUpBeforeClass()
if (!extension_loaded('grpc')) {
self::markTestSkipped('Must enable grpc extension.');
}
self::$instanceId = self::requireEnv('GOOGLE_SPANNER_INSTANCE_ID');

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

self::$instanceId = 'test-' . time() . rand();
self::$databaseId = 'test-' . time() . rand();
self::$backupId = 'backup-' . self::$databaseId;
self::$instance = $spanner->instance(self::$instanceId);
}

public function testCreateInstance()
{
$output = $this->runCommand('create-instance', [
'instance_id' => self::$instanceId
]);
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Created instance test-', $output);
}

/**
* @depends testCreateInstance
*/
public function testCreateDatabase()
{
$output = $this->runCommand('create-database');
Expand Down Expand Up @@ -612,8 +624,11 @@ public function testCreateClientWithQueryOptions()
});
}

private function runCommand($commandName)
private function runCommand($commandName, $params = [])
{
if (!empty($params)) {
return $this->traitRunCommand($commandName, $params);
}
return $this->traitRunCommand($commandName, [
'instance_id' => self::$instanceId,
'database_id' => self::$databaseId,
Expand All @@ -626,5 +641,6 @@ public static function tearDownAfterClass()
$database = self::$instance->database(self::$databaseId);
$database->drop();
}
self::$instance->delete();
}
}