Skip to content

Commit efd78b3

Browse files
author
Takashi Matsuo
authored
Added UpdateSinkCommand (GoogleCloudPlatform#161)
* Added UpdateSinkCommand
1 parent e95c0d5 commit efd78b3

File tree

7 files changed

+243
-19
lines changed

7 files changed

+243
-19
lines changed

logging/composer.lock

Lines changed: 20 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

logging/logging.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Google\Cloud\Samples\Logging\DeleteSinkCommand;
2424
use Google\Cloud\Samples\Logging\ListEntriesCommand;
2525
use Google\Cloud\Samples\Logging\ListSinksCommand;
26+
use Google\Cloud\Samples\Logging\UpdateSinkCommand;
2627
use Google\Cloud\Samples\Logging\WriteCommand;
2728
use Symfony\Component\Console\Application;
2829

@@ -32,5 +33,6 @@
3233
$application->add(new DeleteSinkCommand());
3334
$application->add(new ListEntriesCommand());
3435
$application->add(new ListSinksCommand());
36+
$application->add(new UpdateSinkCommand());
3537
$application->add(new WriteCommand());
3638
$application->run();

logging/src/UpdateSinkCommand.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
namespace Google\Cloud\Samples\Logging;
19+
20+
// [START update_sink_use]
21+
use Google\Cloud\Logging\LoggingClient;
22+
// [END update_sink_use]
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
/**
28+
* Class UpdateSinkCommand
29+
* @package Google\Cloud\Samples\Logging
30+
*
31+
* This command simply updates a sink.
32+
*/
33+
class UpdateSinkCommand extends BaseCommand
34+
{
35+
protected function configure()
36+
{
37+
$this
38+
->setName('update-sink')
39+
->setDescription('Updates a Logging sink')
40+
->addOption(
41+
'sink',
42+
null,
43+
InputOption::VALUE_OPTIONAL,
44+
'The name of the Logging sink',
45+
'my_sink'
46+
)->addOption(
47+
'filter',
48+
null,
49+
InputOption::VALUE_OPTIONAL,
50+
'The filter expression for the sink',
51+
''
52+
);
53+
$this->addCommonOptions();
54+
}
55+
56+
protected function execute(InputInterface $input, OutputInterface $output)
57+
{
58+
$sinkName = $input->getOption('sink');
59+
$projectId = $input->getOption('project');
60+
$loggerName = $input->getOption('logger');
61+
$filter = $input->getOption('filter');
62+
// [START update_sink]
63+
$loggerFullName = sprintf(
64+
'projects/%s/logs/%s',
65+
$projectId,
66+
$loggerName
67+
);
68+
$filterString = sprintf('logName = "%s"', $loggerFullName);
69+
if (!empty($filter)) {
70+
$filterString .= ' AND ' . $filter;
71+
}
72+
$logging = new LoggingClient(['projectId' => $projectId]);
73+
$sink = $logging->sink($sinkName);
74+
$sink->update(['filter' => $filterString]);
75+
// [END update_sink]
76+
printf("Updated a sink '%s'." . PHP_EOL, $sinkName);
77+
}
78+
}

logging/test/CreateSinkCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function tearDown()
7171
public function testCreateSink()
7272
{
7373
if (!$bucket = getenv('GOOGLE_BUCKET_NAME')) {
74-
$this->markTestSkipped('No SINK_BUCKET envvar');
74+
$this->markTestSkipped('No GOOGLE_BUCKET_NAME envvar');
7575
}
7676
$application = new Application();
7777
$application->add(new CreateSinkCommand());

logging/test/DeleteSinkCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function setUp()
5252
$this->markTestSkipped('No project ID');
5353
}
5454
if (!$bucket = getenv('GOOGLE_BUCKET_NAME')) {
55-
$this->markTestSkipped('No SINK_BUCKET envvar');
55+
$this->markTestSkipped('No GOOGLE_BUCKET_NAME envvar');
5656
}
5757
$application = new Application();
5858
$application->add(new CreateSinkCommand());

logging/test/ListSinksCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function setUp()
5353
$this->markTestSkipped('No project ID');
5454
}
5555
if (!$bucket = getenv('GOOGLE_BUCKET_NAME')) {
56-
$this->markTestSkipped('No SINK_BUCKET envvar');
56+
$this->markTestSkipped('No GOOGLE_BUCKET_NAME envvar');
5757
}
5858
$application = new Application();
5959
$application->add(new CreateSinkCommand());
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
namespace Google\Cloud\Samples\Logging\Tests;
19+
20+
use Google\Cloud\Logging\LoggingClient;
21+
use Google\Cloud\Samples\Logging\CreateSinkCommand;
22+
use Google\Cloud\Samples\Logging\DeleteSinkCommand;
23+
use Google\Cloud\Samples\Logging\UpdateSinkCommand;
24+
use Symfony\Component\Console\Application;
25+
use Symfony\Component\Console\Tester\CommandTester;
26+
27+
/**
28+
* Functional tests for UpdateSinkCommand.
29+
*/
30+
class UpdateSinkCommandTest extends \PHPUnit_Framework_TestCase
31+
{
32+
/* @var $hasCredentials boolean */
33+
protected static $hasCredentials;
34+
/* @var $sinkName string */
35+
protected static $sinkName;
36+
/* @var $projectId mixed|string */
37+
private $projectId;
38+
39+
public static function setUpBeforeClass()
40+
{
41+
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
42+
self::$hasCredentials = $path && file_exists($path) &&
43+
filesize($path) > 0;
44+
self::$sinkName = sprintf("sink-%s", uniqid());
45+
}
46+
47+
public function setUp()
48+
{
49+
if (!self::$hasCredentials) {
50+
$this->markTestSkipped('No application credentials were found.');
51+
}
52+
53+
if (!$this->projectId = getenv('GOOGLE_PROJECT_ID')) {
54+
$this->markTestSkipped('No project ID');
55+
}
56+
if (!$bucket = getenv('GOOGLE_BUCKET_NAME')) {
57+
$this->markTestSkipped('No GOOGLE_BUCKET_NAME envvar');
58+
}
59+
$application = new Application();
60+
$application->add(new CreateSinkCommand());
61+
$commandTester = new CommandTester($application->get('create-sink'));
62+
$loggerName = 'my_test_logger';
63+
$commandTester->execute(
64+
[
65+
'--project' => $this->projectId,
66+
'--logger' => $loggerName,
67+
'--bucket' => $bucket,
68+
'--sink' => self::$sinkName,
69+
],
70+
['interactive' => false]
71+
);
72+
}
73+
74+
public function tearDown()
75+
{
76+
// Clean up the sink after the test
77+
$application = new Application();
78+
$application->add(new DeleteSinkCommand());
79+
$commandTester = new CommandTester($application->get('delete-sink'));
80+
$commandTester->execute(
81+
[
82+
'--project' => $this->projectId,
83+
'--sink' => self::$sinkName,
84+
],
85+
['interactive' => false]
86+
);
87+
}
88+
89+
public function testUpdateSink()
90+
{
91+
$application = new Application();
92+
$application->add(new UpdateSinkCommand());
93+
$commandTester = new CommandTester($application->get('update-sink'));
94+
$commandTester->execute(
95+
[
96+
'--project' => $this->projectId,
97+
'--sink' => self::$sinkName,
98+
'--logger' => 'updated-logger',
99+
],
100+
['interactive' => false]
101+
);
102+
$this->expectOutputRegex(
103+
sprintf("/Updated a sink '%s'./", self::$sinkName)
104+
);
105+
// Check the updated filter value
106+
$logging = new LoggingClient(['projectId' => $this->projectId]);
107+
$sink = $logging->sink(self::$sinkName);
108+
$sink->reload();
109+
$this->assertRegExp(
110+
sprintf(
111+
'|projects/%s/logs/%s|',
112+
$this->projectId,
113+
'updated-logger'
114+
),
115+
$sink->info['filter']);
116+
}
117+
118+
public function testUpdateSinkWithFilter()
119+
{
120+
$application = new Application();
121+
$application->add(new UpdateSinkCommand());
122+
$commandTester = new CommandTester($application->get('update-sink'));
123+
$commandTester->execute(
124+
[
125+
'--project' => $this->projectId,
126+
'--sink' => self::$sinkName,
127+
'--filter' => 'severity >= INFO',
128+
],
129+
['interactive' => false]
130+
);
131+
$this->expectOutputRegex(
132+
sprintf("/Updated a sink '%s'./", self::$sinkName)
133+
);
134+
// Check the updated filter value
135+
$logging = new LoggingClient(['projectId' => $this->projectId]);
136+
$sink = $logging->sink(self::$sinkName);
137+
$sink->reload();
138+
$this->assertRegExp('/severity >= INFO/', $sink->info['filter']);
139+
}
140+
}

0 commit comments

Comments
 (0)