Skip to content

Commit 2e3407f

Browse files
feat: support customer managed instance configurations (#1696)
* feat: support customer managed instance configurations Co-authored-by: Saransh Dhingra <[email protected]>
1 parent 7d64905 commit 2e3407f

6 files changed

+328
-1
lines changed

spanner/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-spanner": "^1.49.0"
3+
"google/cloud-spanner": "^1.54.0"
44
}
55
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_config]
27+
use Google\Cloud\Spanner\Admin\Instance\V1\ReplicaInfo;
28+
use Google\Cloud\Spanner\SpannerClient;
29+
30+
/**
31+
* Creates a customer managed instance configuration.
32+
* Example:
33+
* ```
34+
* create_instance_config($instanceConfigId);
35+
* ```
36+
*
37+
* @param string $userConfigId The customer managed instance configuration id. The id must start with 'custom-'.
38+
* @param string $baseConfigId Base configuration ID to be used for creation, e.g. nam11.
39+
*/
40+
function create_instance_config($userConfigId, $baseConfigId)
41+
{
42+
$spanner = new SpannerClient();
43+
44+
// Get a Google Managed instance configuration to use as the base for our custom instance configuration.
45+
$baseInstanceConfig = $spanner->instanceConfiguration(
46+
$baseConfigId
47+
);
48+
49+
$instanceConfiguration = $spanner->instanceConfiguration($userConfigId);
50+
$operation = $instanceConfiguration->create(
51+
$baseInstanceConfig,
52+
array_merge(
53+
$baseInstanceConfig->info()['replicas'],
54+
// The replicas for the custom instance configuration must include all the replicas of the base
55+
// configuration, in addition to at least one from the list of optional replicas of the base
56+
// configuration.
57+
[new ReplicaInfo(
58+
[
59+
'location' => 'us-east1',
60+
'type' => ReplicaInfo\ReplicaType::READ_ONLY,
61+
'default_leader_location' => false
62+
]
63+
)]
64+
),
65+
[
66+
'displayName' => 'This is a display name',
67+
'labels' => [
68+
'php_cloud_spanner_samples' => true,
69+
]
70+
]
71+
);
72+
73+
print('Waiting for operation to complete...' . PHP_EOL);
74+
$operation->pollUntilComplete();
75+
76+
printf('Created instance configuration %s' . PHP_EOL, $userConfigId);
77+
}
78+
// [END spanner_create_instance_config]
79+
80+
// The following 2 lines are only needed to run the samples
81+
require_once __DIR__ . '/../../testing/sample_helpers.php';
82+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_delete_instance_config]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Deletes a customer managed instance configuration.
31+
* Example:
32+
* ```
33+
* delete_instance_config($instanceConfigId);
34+
* ```
35+
*
36+
* @param string $instanceConfigId The customer managed instance configuration id. The id must start with 'custom-'.
37+
*/
38+
function delete_instance_config($instanceConfigId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instanceConfiguration = $spanner->instanceConfiguration($instanceConfigId);
42+
43+
$instanceConfiguration->delete();
44+
45+
printf('Deleted instance configuration %s' . PHP_EOL, $instanceConfigId);
46+
}
47+
// [END spanner_delete_instance_config]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google LLC.
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_list_instance_config_operations]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Lists the instance configuration operations for a project.
31+
* Example:
32+
* ```
33+
* list_instance_config_operations();
34+
* ```
35+
*/
36+
function list_instance_config_operations()
37+
{
38+
$spanner = new SpannerClient();
39+
40+
$operations = $spanner->instanceConfigOperations();
41+
foreach ($operations as $operation) {
42+
$meta = $operation->info()['metadata'];
43+
$instanceConfig = $meta['instanceConfig'];
44+
$configName = basename($instanceConfig['name']);
45+
$type = $meta['typeUrl'];
46+
printf(
47+
'Instance config operation for %s of type %s has status %s.' . PHP_EOL,
48+
$configName,
49+
$type,
50+
$operation->done() ? 'done' : 'running'
51+
);
52+
}
53+
}
54+
// [END spanner_list_instance_config_operations]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_update_instance_config]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Updates a customer managed instance configuration.
31+
* Example:
32+
* ```
33+
* update_instance_config($instanceConfigId);
34+
* ```
35+
*
36+
* @param string $instanceConfigId The customer managed instance configuration id. The id must start with 'custom-'.
37+
*/
38+
function update_instance_config($instanceConfigId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instanceConfiguration = $spanner->instanceConfiguration($instanceConfigId);
42+
43+
$operation = $instanceConfiguration->update(
44+
[
45+
'displayName' => 'New display name',
46+
'labels' => [
47+
'cloud_spanner_samples' => true,
48+
'updated' => true,
49+
]
50+
]
51+
);
52+
53+
print('Waiting for operation to complete...' . PHP_EOL);
54+
$operation->pollUntilComplete();
55+
56+
printf('Updated instance configuration %s' . PHP_EOL, $instanceConfigId);
57+
}
58+
// [END spanner_update_instance_config]
59+
60+
// The following 2 lines are only needed to run the samples
61+
require_once __DIR__ . '/../../testing/sample_helpers.php';
62+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Google\Cloud\Samples\Spanner;
1919

20+
use Google\Cloud\Spanner\InstanceConfiguration;
2021
use Google\Cloud\Spanner\SpannerClient;
2122
use Google\Cloud\Spanner\Instance;
2223
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
@@ -85,6 +86,15 @@ class spannerTest extends TestCase
8586
/** @var $lastUpdateData int */
8687
protected static $lastUpdateDataTimestamp;
8788

89+
/** @var string $baseConfigId */
90+
protected static $baseConfigId;
91+
92+
/** @var string $customInstanceConfigId */
93+
protected static $customInstanceConfigId;
94+
95+
/** @var InstanceConfiguration $customInstanceConfig */
96+
protected static $customInstanceConfig;
97+
8898
public static function setUpBeforeClass(): void
8999
{
90100
self::checkProjectEnvVars();
@@ -113,6 +123,9 @@ public static function setUpBeforeClass(): void
113123
self::$defaultLeader = 'us-central1';
114124
self::$updatedDefaultLeader = 'us-east4';
115125
self::$multiInstance = $spanner->instance(self::$multiInstanceId);
126+
self::$baseConfigId = 'nam7';
127+
self::$customInstanceConfigId = 'custom-' . time() . rand();
128+
self::$customInstanceConfig = $spanner->instanceConfiguration(self::$customInstanceConfigId);
116129
}
117130

118131
public function testCreateInstance()
@@ -133,6 +146,64 @@ public function testCreateInstanceWithProcessingUnits()
133146
$this->assertStringContainsString('Created instance test-', $output);
134147
}
135148

149+
public function testCreateInstanceConfig()
150+
{
151+
$output = $this->runFunctionSnippet('create_instance_config', [
152+
self::$customInstanceConfigId, self::$baseConfigId
153+
]);
154+
155+
$this->assertStringContainsString(sprintf('Created instance configuration %s', self::$customInstanceConfigId), $output);
156+
}
157+
158+
/**
159+
* @depends testCreateInstanceConfig
160+
*/
161+
public function testUpdateInstanceConfig()
162+
{
163+
$output = $this->runFunctionSnippet('update_instance_config', [
164+
self::$customInstanceConfigId
165+
]);
166+
167+
$this->assertStringContainsString(sprintf('Updated instance configuration %s', self::$customInstanceConfigId), $output);
168+
}
169+
170+
/**
171+
* @depends testUpdateInstanceConfig
172+
*/
173+
public function testDeleteInstanceConfig()
174+
{
175+
$output = $this->runFunctionSnippet('delete_instance_config', [
176+
self::$customInstanceConfigId
177+
]);
178+
$this->assertStringContainsString(sprintf('Deleted instance configuration %s', self::$customInstanceConfigId), $output);
179+
}
180+
181+
/**
182+
* @depends testUpdateInstanceConfig
183+
*/
184+
public function testListInstanceConfigOperations()
185+
{
186+
$output = $this->runFunctionSnippet('list_instance_config_operations', [
187+
self::$customInstanceConfigId
188+
]);
189+
190+
$this->assertStringContainsString(
191+
sprintf(
192+
'Instance config operation for %s of type %s has status done.',
193+
self::$customInstanceConfigId,
194+
'type.googleapis.com/google.spanner.admin.instance.v1.CreateInstanceConfigMetadata'
195+
),
196+
$output);
197+
198+
$this->assertStringContainsString(
199+
sprintf(
200+
'Instance config operation for %s of type %s has status done.',
201+
self::$customInstanceConfigId,
202+
'type.googleapis.com/google.spanner.admin.instance.v1.UpdateInstanceConfigMetadata'
203+
),
204+
$output);
205+
}
206+
136207
/**
137208
* @depends testCreateInstance
138209
*/
@@ -902,5 +973,8 @@ public static function tearDownAfterClass(): void
902973
$database->drop();
903974
self::$instance->delete();
904975
self::$lowCostInstance->delete();
976+
if (self::$customInstanceConfig->exists()) {
977+
self::$customInstanceConfig->delete();
978+
}
905979
}
906980
}

0 commit comments

Comments
 (0)