16
16
"""Demos for working with notification configs."""
17
17
18
18
19
- def create_notification_config (organization_id , notification_config_id , pubsub_topic ):
20
-
21
- # [START securitycenter_create_notification_config]
19
+ # [START securitycenter_create_notification_config]
20
+ def create_notification_config (parent_id , notification_config_id , pubsub_topic ):
21
+ """
22
+ Args:
23
+ parent_id: must be in one of the following formats:
24
+ "organizations/{organization_id}"
25
+ "projects/{project_id}"
26
+ "folders/{folder_id}"
27
+ notification_config_id: "your-config-id"
28
+ pubsub_topic: "projects/{your-project-id}/topics/{your-topic-ic}"
29
+
30
+ Ensure this ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the new topic.
31
+ """
22
32
from google .cloud import securitycenter as securitycenter
23
33
24
34
client = securitycenter .SecurityCenterClient ()
25
35
26
- # TODO: organization_id = "your-org-id"
27
- # TODO: notification_config_id = "your-config-id"
28
- # TODO: pubsub_topic = "projects/{your-project-id}/topics/{your-topic-ic}"
29
- # Ensure this ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the new topic.
30
-
31
- org_name = "organizations/{org_id}" .format (org_id = organization_id )
32
-
33
36
created_notification_config = client .create_notification_config (
34
37
request = {
35
- "parent" : org_name ,
38
+ "parent" : parent_id ,
36
39
"config_id" : notification_config_id ,
37
40
"notification_config" : {
38
41
"description" : "Notification for active findings" ,
@@ -47,88 +50,99 @@ def create_notification_config(organization_id, notification_config_id, pubsub_t
47
50
return created_notification_config
48
51
49
52
50
- def delete_notification_config (organization_id , notification_config_id ):
51
-
52
- # [START securitycenter_delete_notification_config]
53
+ # [START securitycenter_delete_notification_config]
54
+ def delete_notification_config (parent_id , notification_config_id ):
55
+ """
56
+ Args:
57
+ parent_id: must be in one of the following formats:
58
+ "organizations/{organization_id}"
59
+ "projects/{project_id}"
60
+ "folders/{folder_id}"
61
+ notification_config_id: "your-config-id"
62
+ """
53
63
from google .cloud import securitycenter as securitycenter
54
64
55
65
client = securitycenter .SecurityCenterClient ()
56
66
57
- # TODO: organization_id = "your-org-id"
58
- # TODO: notification_config_id = "your-config-id"
59
-
60
67
notification_config_name = (
61
- "organizations/{org_id}/notificationConfigs/{config_id}" .format (
62
- org_id = organization_id , config_id = notification_config_id
63
- )
68
+ f"{ parent_id } /notificationConfigs/{ notification_config_id } "
64
69
)
65
70
66
71
client .delete_notification_config (request = {"name" : notification_config_name })
67
- print ("Deleted notification config: {}" . format ( notification_config_name ) )
72
+ print (f "Deleted notification config: { notification_config_name } " )
68
73
# [END securitycenter_delete_notification_config]
69
74
return True
70
75
71
76
72
- def get_notification_config (organization_id , notification_config_id ):
73
-
74
- # [START securitycenter_get_notification_config]
77
+ # [START securitycenter_get_notification_config]
78
+ def get_notification_config (parent_id , notification_config_id ):
79
+ """
80
+ Args:
81
+ parent_id: must be in one of the following formats:
82
+ "organizations/{organization_id}"
83
+ "projects/{project_id}"
84
+ "folders/{folder_id}"
85
+ notification_config_id: "your-config-id"
86
+ """
75
87
from google .cloud import securitycenter as securitycenter
76
88
77
89
client = securitycenter .SecurityCenterClient ()
78
90
79
- # TODO: organization_id = "your-org-id"
80
- # TODO: notification_config_id = "your-config-id"
81
-
82
91
notification_config_name = (
83
- "organizations/{org_id}/notificationConfigs/{config_id}" .format (
84
- org_id = organization_id , config_id = notification_config_id
85
- )
92
+ f"{ parent_id } /notificationConfigs/{ notification_config_id } "
86
93
)
87
94
88
95
notification_config = client .get_notification_config (
89
96
request = {"name" : notification_config_name }
90
97
)
91
- print ("Got notification config: {}" . format ( notification_config ) )
98
+ print (f "Got notification config: { notification_config } " )
92
99
# [END securitycenter_get_notification_config]
93
100
return notification_config
94
101
95
102
96
- def list_notification_configs (organization_id ):
97
-
98
- # [START securitycenter_list_notification_configs]
103
+ # [START securitycenter_list_notification_configs]
104
+ def list_notification_configs (parent_id ):
105
+ """
106
+ Args:
107
+ parent_id: must be in one of the following formats:
108
+ "organizations/{organization_id}"
109
+ "projects/{project_id}"
110
+ "folders/{folder_id}"
111
+ """
99
112
from google .cloud import securitycenter as securitycenter
100
113
101
114
client = securitycenter .SecurityCenterClient ()
102
115
103
- # TODO: organization_id = "your-org-id"
104
- org_name = "organizations/{org_id}" .format (org_id = organization_id )
105
-
106
116
notification_configs_iterator = client .list_notification_configs (
107
- request = {"parent" : org_name }
117
+ request = {"parent" : parent_id }
108
118
)
109
119
for i , config in enumerate (notification_configs_iterator ):
110
- print ("{ }: notification_config: {}" . format ( i , config ) )
120
+ print (f" { i } : notification_config: { config } " )
111
121
# [END securitycenter_list_notification_configs]]
112
122
return notification_configs_iterator
113
123
114
124
115
- def update_notification_config (organization_id , notification_config_id , pubsub_topic ):
116
- # [START securitycenter_update_notification_config]
125
+ # [START securitycenter_update_notification_config]
126
+ def update_notification_config (parent_id , notification_config_id , pubsub_topic ):
127
+ """
128
+ Args:
129
+ parent_id: must be in one of the following formats:
130
+ "organizations/{organization_id}"
131
+ "projects/{project_id}"
132
+ "folders/{folder_id}"
133
+ notification_config_id: "config-id-to-update"
134
+ pubsub_topic: "projects/{new-project}/topics/{new-topic}"
135
+
136
+ If updating a pubsub_topic, ensure this ServiceAccount has the
137
+ "pubsub.topics.setIamPolicy" permission on the new topic.
138
+ """
117
139
from google .cloud import securitycenter as securitycenter
118
140
from google .protobuf import field_mask_pb2
119
141
120
142
client = securitycenter .SecurityCenterClient ()
121
143
122
- # TODO organization_id = "your-org-id"
123
- # TODO notification_config_id = "config-id-to-update"
124
- # TODO pubsub_topic = "projects/{new-project}/topics/{new-topic}"
125
- # If updating a pubsub_topic, ensure this ServiceAccount has the
126
- # "pubsub.topics.setIamPolicy" permission on the new topic.
127
-
128
144
notification_config_name = (
129
- "organizations/{org_id}/notificationConfigs/{config_id}" .format (
130
- org_id = organization_id , config_id = notification_config_id
131
- )
145
+ f"{ parent_id } /notificationConfigs/{ notification_config_id } "
132
146
)
133
147
134
148
updated_description = "New updated description"
0 commit comments