Skip to content

Commit

Permalink
pubsub/logging grpc fixes (#239)
Browse files Browse the repository at this point in the history
* pubsub/logging grpc fixes

* fix tests
  • Loading branch information
dwsupplee authored and jdpedrie committed Nov 10, 2016
1 parent 0d8dd65 commit 721846c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/ArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ trait ArrayTrait
*
* @param string $key
* @param array $arr
* @return string
* @param bool $isRequired
* @return string|null
* @throws \InvalidArgumentException
*/
private function pluck($key, array &$arr)
private function pluck($key, array &$arr, $isRequired = true)
{
if (!isset($arr[$key])) {
throw new \InvalidArgumentException(
"Key $key does not exist in the provided array."
);
if (!array_key_exists($key, $arr)) {
if ($isRequired) {
throw new \InvalidArgumentException(
"Key $key does not exist in the provided array."
);
}

return null;
}

$value = $arr[$key];
Expand All @@ -55,7 +60,9 @@ private function pluckArray(array $keys, &$arr)
$values = [];

foreach ($keys as $key) {
$values[$key] = $this->pluck($key, $arr);
if (array_key_exists($key, $arr)) {
$values[$key] = $this->pluck($key, $arr, false);
}
}

return $values;
Expand Down
8 changes: 6 additions & 2 deletions src/Logging/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function getMetric(array $args = [])
*/
public function listMetrics(array $args = [])
{
return $this->send([$this->metricsApi, 'getLogMetric'], [
return $this->send([$this->metricsApi, 'listLogMetrics'], [
$this->pluck('parent', $args),
$args
]);
Expand Down Expand Up @@ -305,7 +305,7 @@ public function deleteLog(array $args = [])

/**
* @param array $entry
* @return array
* @return LogEntry
*/
private function buildEntry(array $entry)
{
Expand All @@ -321,6 +321,10 @@ private function buildEntry(array $entry)
$entry['resource']['labels'] = $this->formatLabelsForApi($entry['resource']['labels']);
}

if (isset($entry['severity'])) {
$entry['severity'] = array_flip(Logger::getLogLevelMap())[$entry['severity']];
}

return (new LogEntry)->deserialize($entry, $this->codec);
}
}
18 changes: 14 additions & 4 deletions src/PubSub/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Google\Cloud\GrpcRequestWrapper;
use Google\Cloud\GrpcTrait;
use Grpc\ChannelCredentials;
use google\iam\v1\Policy;
use google\pubsub\v1\PubsubMessage;
use google\pubsub\v1\PushConfig;

Expand Down Expand Up @@ -270,7 +271,7 @@ public function setTopicIamPolicy(array $args)
{
return $this->send([$this->publisherApi, 'setIamPolicy'], [
$this->pluck('resource', $args),
$this->pluck('policy', $args),
$this->buildPolicy($this->pluck('policy', $args)),
$args
]);
}
Expand Down Expand Up @@ -305,7 +306,7 @@ public function setSubscriptionIamPolicy(array $args)
{
return $this->send([$this->subscriberApi, 'setIamPolicy'], [
$this->pluck('resource', $args),
$this->pluck('policy', $args),
$this->buildPolicy($this->pluck('policy', $args)),
$args
]);
}
Expand All @@ -324,7 +325,7 @@ public function testSubscriptionIamPermissions(array $args)

/**
* @param array $message
* @return array
* @return PubsubMessage
*/
private function buildMessage(array $message)
{
Expand All @@ -335,9 +336,18 @@ private function buildMessage(array $message)
return (new PubsubMessage())->deserialize($message, $this->codec);
}

/**
* @param array $policy
* @return Policy
*/
private function buildPolicy(array $policy)
{
return (new Policy())->deserialize($policy, $this->codec);
}

/**
* @param array $pushConfig
* @return array
* @return PushConfig
*/
private function buildPushConfig(array $pushConfig)
{
Expand Down
38 changes: 33 additions & 5 deletions tests/unit/PubSub/Connection/GrpcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Google\Cloud\PubSub\Connection\Grpc;
use Google\Cloud\GrpcRequestWrapper;
use Prophecy\Argument;
use google\iam\v1\Binding;
use google\iam\v1\Policy;
use google\pubsub\v1\PubsubMessage;
use google\pubsub\v1\PubsubMessage\AttributesEntry as MessageAttributesEntry;
use google\pubsub\v1\PushConfig\AttributesEntry as PushConfigAttributesEntry;
Expand Down Expand Up @@ -73,7 +75,13 @@ public function methodProvider()
$pbMessageAttribute->setKey($attributeKey);
$pbMessageAttribute->setValue($attributeValue);
$pbMessage->addAttributes($pbMessageAttribute);
$policy = ['fake' => 'policy'];
$bindingRole = 'test_role';
$bindingMember = 'test_member';
$pbPolicy = new Policy();
$pbBinding = new Binding();
$pbBinding->setRole($bindingRole);
$pbBinding->addMembers($bindingMember);
$pbPolicy->addBindings($pbBinding);
$permissions = ['fake' => 'permissions'];
$pbPushConfig = new PushConfig();
$pushEndpoint = 'http://www.example.com';
Expand Down Expand Up @@ -132,8 +140,18 @@ public function methodProvider()
],
[
'setTopicIamPolicy',
['resource' => $value, 'policy' => $policy],
[$value, $policy, []]
[
'resource' => $value,
'policy' => [
'bindings' => [
[
'role' => $bindingRole,
'members' => [$bindingMember]
]
]
]
],
[$value, $pbPolicy, []]
],
[
'testTopicIamPermissions',
Expand Down Expand Up @@ -200,8 +218,18 @@ public function methodProvider()
],
[
'setSubscriptionIamPolicy',
['resource' => $value, 'policy' => $policy],
[$value, $policy, []]
[
'resource' => $value,
'policy' => [
'bindings' => [
[
'role' => $bindingRole,
'members' => [$bindingMember]
]
]
]
],
[$value, $pbPolicy, []]
],
[
'testSubscriptionIamPermissions',
Expand Down

0 comments on commit 721846c

Please sign in to comment.