Skip to content

Commit

Permalink
feat: enable 'FieldMask' param in Instance::reload() method options (#…
Browse files Browse the repository at this point in the history
…2530)

* feat: enable 'FieldMask' param in Instance::reload() method options

* lint

* refactor: simplify and clean up

* feat: accept FieldMask as single string field name or an array of field names
  • Loading branch information
AVaksman authored and jdpedrie committed Jan 7, 2020
1 parent bb0e3d9 commit 006487a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ public function listInstances(array $args)
public function getInstance(array $args)
{
$projectId = $this->pluck('projectId', $args);

if (isset($args['fieldMask'])) {
$mask = [];
if (is_array($args['fieldMask'])) {
foreach (array_values($args['fieldMask']) as $field) {
$mask[] = Serializer::toSnakeCase($field);
}
} else {
$mask[] = Serializer::toSnakeCase($args['fieldMask']);
}
$fieldMask = $this->serializer->decodeMessage(new FieldMask(), ['paths' => $mask]);
$args['fieldMask'] = $fieldMask;
}

return $this->send([$this->getInstanceAdminClient(), 'getInstance'], [
$this->pluck('name', $args),
$this->addResourcePrefixHeader($args, $projectId)
Expand Down
18 changes: 16 additions & 2 deletions Spanner/src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ public function name()
* echo $info['nodeCount'];
* ```
*
* @param array $options [optional] Configuration options.
* @param array $options [optional] {
* Configuration options
*
* @type string|string[] $fieldMask One or a list of `Instance` fields that should be returned.
* Eligible values are: `name`, `displayName`, `endpointUris`, `labels`, `config`, `nodeCount`, `state`.
* If absent, all fields are returned.
* Note: This parameter will only apply when service call is required (`info` values are not present).
* }
*
* @return array
*/
public function info(array $options = [])
Expand Down Expand Up @@ -228,7 +236,13 @@ public function exists(array $options = [])
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.GetInstanceRequest GetInstanceRequest
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] Configuration options.
* @param array $options [optional] {
* Configuration options
*
* @type string|string[] $fieldMask One or a list of `Instance` fields that should be returned.
* Eligible values are: `name`, `displayName`, `endpointUris`, `labels`, `config`, `nodeCount`, `state`.
* If absent, all fields are returned.
* }
* @return array
*/
public function reload(array $options = [])
Expand Down
13 changes: 13 additions & 0 deletions Spanner/tests/System/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public function testInstance()

$instance = $client->instance(self::INSTANCE_NAME);
$this->assertEquals($displayName, $instance->info()['displayName']);

$requestedFieldNames = ['name', 'state'];
$expectedInfo = [
'endpointUris' => [],
'labels' => [],
'name' => $instance->name(),
'displayName' => '',
'nodeCount' => 0,
'state' => Instance::STATE_READY,
'config' => ''
];
$info = $instance->reload(['fieldMask' => $requestedFieldNames]);
$this->assertEquals($expectedInfo, $info);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions Spanner/tests/Unit/Connection/GrpcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ public function testGetInstance()
]));
}

public function testGetInstanceWithFieldMaskArray()
{
$fieldNames = ['name', 'displayName', 'nodeCount'];

$mask = [];
foreach (array_values($fieldNames) as $key) {
$mask[] = Serializer::toSnakeCase($key);
}

$fieldMask = $this->serializer->decodeMessage(new FieldMask, ['paths' => $mask]);
$this->assertCallCorrect('getInstance', [
'name' => self::INSTANCE,
'projectId' => self::PROJECT,
'fieldMask' => $fieldNames
], $this->expectResourceHeader(self::PROJECT, [
self::INSTANCE,
['fieldMask' => $fieldMask]
]));
}

public function testGetInstanceWithFieldMaskString()
{
$fieldNames = 'nodeCount';
$mask[] = Serializer::toSnakeCase($fieldNames);

$fieldMask = $this->serializer->decodeMessage(new FieldMask, ['paths' => $mask]);
$this->assertCallCorrect('getInstance', [
'name' => self::INSTANCE,
'projectId' => self::PROJECT,
'fieldMask' => $fieldNames
], $this->expectResourceHeader(self::PROJECT, [
self::INSTANCE,
['fieldMask' => $fieldMask]
]));
}

public function testCreateInstance()
{
list ($args, $instance) = $this->instance();
Expand Down
50 changes: 49 additions & 1 deletion Spanner/tests/Unit/InstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ public function testInfoWithReload()
$this->assertEquals($info, $this->instance->info());
}

public function testInfoWithReloadAndFieldMask()
{
$instance = [
'name' => $this->instance->name(),
'node_count' => 1
];

$requestedFieldNames = ["name", 'node_count'];
$this->connection->getInstance(Argument::allOf(
Argument::withEntry('name', $this->instance->name()),
Argument::withEntry('fieldMask', $requestedFieldNames)
))
->shouldBeCalledTimes(1)
->willReturn($instance);

$this->instance->___setProperty('connection', $this->connection->reveal());

$info = $this->instance->info(['fieldMask' => $requestedFieldNames]);

$this->assertEquals($info, $this->instance->info());
}

public function testExists()
{
$this->connection->getInstance(Argument::any())->shouldBeCalled()->willReturn([]);
Expand All @@ -117,7 +139,10 @@ public function testReload()
{
$instance = $this->getDefaultInstance();

$this->connection->getInstance(Argument::any())
$this->connection->getInstance(Argument::allOf(
Argument::withEntry('name', $this->instance->name()),
Argument::withEntry('projectId', self::PROJECT_ID)
))
->shouldBeCalledTimes(1)
->willReturn($instance);

Expand All @@ -128,6 +153,29 @@ public function testReload()
$this->assertEquals('Instance Name', $info['displayName']);
}

public function testReloadWithFieldMask()
{
$instance = [
'name' => $this->instance->name(),
'node_count' => 1
];

$requestedFieldNames = ["name", 'node_count'];
$this->connection->getInstance(Argument::allOf(
Argument::withEntry('name', $this->instance->name()),
Argument::withEntry('projectId', self::PROJECT_ID),
Argument::withEntry('fieldMask', $requestedFieldNames)
))
->shouldBeCalledTimes(1)
->willReturn($instance);

$this->instance->___setProperty('connection', $this->connection->reveal());

$info = $this->instance->reload(['fieldMask' => $requestedFieldNames]);

$this->assertEquals($info, $this->instance->info());
}

public function testState()
{
$instance = $this->getDefaultInstance();
Expand Down

0 comments on commit 006487a

Please sign in to comment.