Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public function listInstances(array $args)
public function getInstance(array $args)
{
$projectId = $this->pluck('projectId', $args);

$mask = [];
AVaksman marked this conversation as resolved.
Show resolved Hide resolved
foreach (array_values($this->pluck('fieldMask', $args)) as $key) {
$mask[] = Serializer::toSnakeCase($key);
}

$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
25 changes: 22 additions & 3 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[] $fieldMask 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,14 +236,25 @@ 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[] $fieldMask 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 = [])
{
$fieldMask = [];
if (isset($options['fieldMask'])) {
$fieldMask = $this->pluck('fieldMask', $options);
}
$this->info = $this->connection->getInstance($options + [
'name' => $this->name,
'projectId' => $this->projectId
'projectId' => $this->projectId,
'fieldMask' => $fieldMask
AVaksman marked this conversation as resolved.
Show resolved Hide resolved
]);

return $this->info;
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
34 changes: 32 additions & 2 deletions Spanner/tests/Unit/Connection/GrpcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,41 @@ public function testListInstances()

public function testGetInstance()
{
$fieldNames = [];
AVaksman marked this conversation as resolved.
Show resolved Hide resolved

$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
'projectId' => self::PROJECT,
'fieldMask' => $fieldNames
], $this->expectResourceHeader(self::PROJECT, [
self::INSTANCE
self::INSTANCE,
['fieldMask' => $fieldMask]
]));
}

public function testGetInstanceWithFieldMask()
{
$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]
]));
}

Expand Down
51 changes: 50 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,11 @@ 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),
Argument::withEntry('fieldMask', [])
))
->shouldBeCalledTimes(1)
->willReturn($instance);

Expand All @@ -128,6 +154,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