|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
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; |
24 | 19 |
|
25 | 20 | // [START securitycenter_update_notification_config] |
26 | 21 | use Google\Cloud\SecurityCenter\V1\SecurityCenterClient; |
27 | 22 | use Google\Cloud\SecurityCenter\V1\NotificationConfig; |
28 | 23 | use Google\Cloud\SecurityCenter\V1\NotificationConfig\StreamingConfig; |
29 | 24 | use Google\Protobuf\FieldMask; |
30 | 25 |
|
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 | +} |
55 | 56 | // [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