Skip to content

Commit 80116b7

Browse files
authored
chore: upgrade securitycenter samples to new format (GoogleCloudPlatform#1761)
1 parent 6b2960f commit 80116b7

File tree

7 files changed

+150
-134
lines changed

7 files changed

+150
-134
lines changed

securitycenter/src/create_notification.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,44 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 4) {
21-
return printf('Usage: php %s ORGANIZATION_ID NOTIFICATION_ID PROJECT_ID TOPIC_NAME\n', basename(__FILE__));
22-
}
23-
list($_, $organizationId, $notificationConfigId, $projectId, $topicName) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_create_notification_config]
2621
use Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
2722
use Google\Cloud\SecurityCenter\V1\NotificationConfig;
2823
use Google\Cloud\SecurityCenter\V1\NotificationConfig\StreamingConfig;
2924

30-
/** Uncomment and populate these variables in your code */
31-
// $organizationId = "{your-org-id}";
32-
// $notificationConfigId = {"your-unique-id"};
33-
// $projectId = "{your-project}"";
34-
// $topicName = "{your-topic}";
35-
36-
$securityCenterClient = new SecurityCenterClient();
37-
$organizationName = $securityCenterClient::organizationName($organizationId);
38-
$pubsubTopic = $securityCenterClient::topicName($projectId, $topicName);
39-
40-
$streamingConfig = (new StreamingConfig())->setFilter('state = "ACTIVE"');
41-
$notificationConfig = (new NotificationConfig())
42-
->setDescription('A sample notification config')
43-
->setPubsubTopic($pubsubTopic)
44-
->setStreamingConfig($streamingConfig);
25+
/**
26+
* @param string $organizationId Your org ID
27+
* @param string $notificationConfigId A unique identifier
28+
* @param string $projectId Your Cloud Project ID
29+
* @param string $topicName Your topic name
30+
*/
31+
function create_notification(
32+
string $organizationId,
33+
string $notificationConfigId,
34+
string $projectId,
35+
string $topicName
36+
): void {
37+
$securityCenterClient = new SecurityCenterClient();
38+
$organizationName = $securityCenterClient::organizationName($organizationId);
39+
$pubsubTopic = $securityCenterClient::topicName($projectId, $topicName);
4540

46-
$response = $securityCenterClient->createNotificationConfig(
47-
$organizationName,
48-
$notificationConfigId,
49-
$notificationConfig
50-
);
51-
printf('Notification config was created: %s' . PHP_EOL, $response->getName());
41+
$streamingConfig = (new StreamingConfig())->setFilter('state = "ACTIVE"');
42+
$notificationConfig = (new NotificationConfig())
43+
->setDescription('A sample notification config')
44+
->setPubsubTopic($pubsubTopic)
45+
->setStreamingConfig($streamingConfig);
5246

47+
$response = $securityCenterClient->createNotificationConfig(
48+
$organizationName,
49+
$notificationConfigId,
50+
$notificationConfig
51+
);
52+
printf('Notification config was created: %s' . PHP_EOL, $response->getName());
53+
}
5354
// [END securitycenter_create_notification_config]
55+
56+
// The following 2 lines are only needed to execute the samples on the CLI
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

securitycenter/src/delete_notification.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 2) {
21-
return printf('Usage: php %s ORGANIZATION_ID NOTIFICATION_ID\n', basename(__FILE__));
22-
}
23-
list($_, $organizationId, $notificationConfigId) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_delete_notification_config]
2621
use Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
2722

28-
/** Uncomment and populate these variables in your code */
29-
// $organizationId = '{your-org-id}';
30-
// $notificationConfigId = {'your-unique-id'};
31-
32-
$securityCenterClient = new SecurityCenterClient();
33-
$notificationConfigName = $securityCenterClient::notificationConfigName(
34-
$organizationId,
35-
$notificationConfigId
36-
);
37-
38-
$response = $securityCenterClient->deleteNotificationConfig($notificationConfigName);
39-
print('Notification config was deleted' . PHP_EOL);
23+
/**
24+
* @param string $organizationId Your org ID
25+
* @param string $notificationConfigId A unique identifier
26+
*/
27+
function delete_notification(string $organizationId, string $notificationConfigId): void
28+
{
29+
$securityCenterClient = new SecurityCenterClient();
30+
$notificationConfigName = $securityCenterClient::notificationConfigName(
31+
$organizationId,
32+
$notificationConfigId
33+
);
4034

35+
$response = $securityCenterClient->deleteNotificationConfig($notificationConfigName);
36+
print('Notification config was deleted' . PHP_EOL);
37+
}
4138
// [END securitycenter_delete_notification_config]
39+
40+
// The following 2 lines are only needed to execute the samples on the CLI
41+
require_once __DIR__ . '/../../testing/sample_helpers.php';
42+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

securitycenter/src/get_notification.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 2) {
21-
return printf('Usage: php %s ORGANIZATION_ID NOTIFICATION_ID\n', basename(__FILE__));
22-
}
23-
list($_, $organizationId, $notificationConfigId) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_get_notification_config]
2621
use Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
2722

28-
/** Uncomment and populate these variables in your code */
29-
// $organizationId = '{your-org-id}';
30-
// $notificationConfigId = {'your-unique-id'};
31-
32-
$securityCenterClient = new SecurityCenterClient();
33-
$notificationConfigName = $securityCenterClient::notificationConfigName(
34-
$organizationId,
35-
$notificationConfigId
36-
);
37-
38-
$response = $securityCenterClient->getNotificationConfig($notificationConfigName);
39-
printf('Notification config was retrieved: %s' . PHP_EOL, $response->getName());
23+
/**
24+
* @param string $organizationId Your org ID
25+
* @param string $notificationConfigId A unique identifier
26+
*/
27+
function get_notification(string $organizationId, string $notificationConfigId): void
28+
{
29+
$securityCenterClient = new SecurityCenterClient();
30+
$notificationConfigName = $securityCenterClient::notificationConfigName(
31+
$organizationId,
32+
$notificationConfigId
33+
);
4034

35+
$response = $securityCenterClient->getNotificationConfig($notificationConfigName);
36+
printf('Notification config was retrieved: %s' . PHP_EOL, $response->getName());
37+
}
4138
// [END securitycenter_get_notification_config]
39+
40+
// The following 2 lines are only needed to execute the samples on the CLI
41+
require_once __DIR__ . '/../../testing/sample_helpers.php';
42+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

securitycenter/src/list_notification.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 1) {
21-
return printf('Usage: php %s ORGANIZATION_ID\n', basename(__FILE__));
22-
}
23-
list($_, $organizationId) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_list_notification_configs]
2621
use Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
2722

28-
/** Uncomment and populate these variables in your code */
29-
// $organizationId = '{your-org-id}';
23+
/**
24+
* @param string $organizationId Your org ID
25+
*/
26+
function list_notification(string $organizationId): void
27+
{
28+
$securityCenterClient = new SecurityCenterClient();
29+
$organizationName = $securityCenterClient::organizationName($organizationId);
3030

31-
$securityCenterClient = new SecurityCenterClient();
32-
$organizationName = $securityCenterClient::organizationName($organizationId);
31+
foreach ($securityCenterClient->listNotificationConfigs($organizationName) as $element) {
32+
printf('Found notification config %s' . PHP_EOL, $element->getName());
33+
}
3334

34-
foreach ($securityCenterClient->listNotificationConfigs($organizationName) as $element) {
35-
printf('Found notification config %s' . PHP_EOL, $element->getName());
35+
print('Notification configs were listed' . PHP_EOL);
3636
}
37-
38-
print('Notification configs were listed' . PHP_EOL);
39-
4037
// [END securitycenter_list_notification_configs]
38+
39+
// The following 2 lines are only needed to execute the samples on the CLI
40+
require_once __DIR__ . '/../../testing/sample_helpers.php';
41+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

securitycenter/src/receive_notification.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 2) {
21-
return printf('Usage: php %s PROJECT_ID SUSBSCRIPTION_ID\n', basename(__FILE__));
22-
}
23-
list($_, $projectId, $subscriptionId) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_receive_notifications]
2621
use Google\Cloud\PubSub\PubSubClient;
2722

28-
/** Uncomment and populate these variables in your code */
29-
// $projectId = "{your-project-id}";
30-
// $subscriptionId = "{your-subscription-id}";
31-
32-
$pubsub = new PubSubClient([
33-
'projectId' => $projectId,
34-
]);
35-
$subscription = $pubsub->subscription($subscriptionId);
23+
/**
24+
* @param string $projectId Your Cloud Project ID
25+
* @param string $subscriptionId Your subscription ID
26+
*/
27+
function receive_notification(string $projectId, string $subscriptionId): void
28+
{
29+
$pubsub = new PubSubClient([
30+
'projectId' => $projectId,
31+
]);
32+
$subscription = $pubsub->subscription($subscriptionId);
3633

37-
foreach ($subscription->pull() as $message) {
38-
printf('Message: %s' . PHP_EOL, $message->data());
39-
// Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times.
40-
$subscription->acknowledge($message);
34+
foreach ($subscription->pull() as $message) {
35+
printf('Message: %s' . PHP_EOL, $message->data());
36+
// Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times.
37+
$subscription->acknowledge($message);
38+
}
4139
}
42-
4340
// [END securitycenter_receive_notifications]
41+
42+
// The following 2 lines are only needed to execute the samples on the CLI
43+
require_once __DIR__ . '/../../testing/sample_helpers.php';
44+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

securitycenter/src/update_notification.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,46 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
if (count($argv) < 4) {
21-
return printf('Usage: php %s ORGANIZATION_ID NOTIFICATION_ID PROJECT_ID TOPIC_NAME\n', basename(__FILE__));
22-
}
23-
list($_, $organizationId, $notificationConfigId, $projectId, $topicName) = $argv;
18+
namespace Google\Cloud\Samples\SecurityCenter;
2419

2520
// [START securitycenter_update_notification_config]
2621
use Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
2722
use Google\Cloud\SecurityCenter\V1\NotificationConfig;
2823
use Google\Cloud\SecurityCenter\V1\NotificationConfig\StreamingConfig;
2924
use Google\Protobuf\FieldMask;
3025

31-
/** Uncomment and populate these variables in your code */
32-
// $organizationId = '{your-org-id}';
33-
// $notificationConfigId = {'your-unique-id'};
34-
// $projectId = '{your-project}';
35-
// $topicName = '{your-topic}';
36-
37-
$securityCenterClient = new SecurityCenterClient();
38-
39-
// Ensure this ServiceAccount has the 'pubsub.topics.setIamPolicy' permission on the topic.
40-
// https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/setIamPolicy
41-
$pubsubTopic = $securityCenterClient::topicName($projectId, $topicName);
42-
$notificationConfigName = $securityCenterClient::notificationConfigName($organizationId, $notificationConfigId);
43-
44-
$streamingConfig = (new StreamingConfig())->setFilter('state = "ACTIVE"');
45-
$fieldMask = (new FieldMask())->setPaths(['description', 'pubsub_topic', 'streaming_config.filter']);
46-
$notificationConfig = (new NotificationConfig())
47-
->setName($notificationConfigName)
48-
->setDescription('Updated description.')
49-
->setPubsubTopic($pubsubTopic)
50-
->setStreamingConfig($streamingConfig);
51-
52-
$response = $securityCenterClient->updateNotificationConfig($notificationConfig, [$fieldMask]);
53-
printf('Notification config was updated: %s' . PHP_EOL, $response->getName());
54-
26+
/**
27+
* @param string $organizationId Your org ID
28+
* @param string $notificationConfigId A unique identifier
29+
* @param string $projectId Your Cloud Project ID
30+
* @param string $topicName Your topic name
31+
*/
32+
function update_notification(
33+
string $organizationId,
34+
string $notificationConfigId,
35+
string $projectId,
36+
string $topicName
37+
): void {
38+
$securityCenterClient = new SecurityCenterClient();
39+
40+
// Ensure this ServiceAccount has the 'pubsub.topics.setIamPolicy' permission on the topic.
41+
// https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/setIamPolicy
42+
$pubsubTopic = $securityCenterClient::topicName($projectId, $topicName);
43+
$notificationConfigName = $securityCenterClient::notificationConfigName($organizationId, $notificationConfigId);
44+
45+
$streamingConfig = (new StreamingConfig())->setFilter('state = "ACTIVE"');
46+
$fieldMask = (new FieldMask())->setPaths(['description', 'pubsub_topic', 'streaming_config.filter']);
47+
$notificationConfig = (new NotificationConfig())
48+
->setName($notificationConfigName)
49+
->setDescription('Updated description.')
50+
->setPubsubTopic($pubsubTopic)
51+
->setStreamingConfig($streamingConfig);
52+
53+
$response = $securityCenterClient->updateNotificationConfig($notificationConfig, [$fieldMask]);
54+
printf('Notification config was updated: %s' . PHP_EOL, $response->getName());
55+
}
5556
// [END securitycenter_update_notification_config]
57+
58+
// The following 2 lines are only needed to execute the samples on the CLI
59+
require_once __DIR__ . '/../../testing/sample_helpers.php';
60+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)