Skip to content

Commit

Permalink
feat!: generate with microgenerator (#49)
Browse files Browse the repository at this point in the history
See UPGRADING.md for details.
  • Loading branch information
busunkim96 authored and telpirion committed Mar 13, 2023
1 parent 1bdee1f commit abf2113
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 197 deletions.
185 changes: 107 additions & 78 deletions securitycenter/snippets/snippets_findings.py

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions securitycenter/snippets/snippets_findings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def source_name(organization_id):
org_name = "organizations/{org_id}".format(org_id=organization_id)

source = client.create_source(
org_name,
{
"display_name": "Unit test source",
"description": "A new custom source that does X",
},
request={
"parent": org_name,
"source": {
"display_name": "Unit test source",
"description": "A new custom source that does X",
},
}
)
return source.name

Expand Down
43 changes: 25 additions & 18 deletions securitycenter/snippets/snippets_list_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def list_all_assets(organization_id):
org_name = "organizations/{org_id}".format(org_id=organization_id)

# Call the API and print results.
asset_iterator = client.list_assets(org_name)
asset_iterator = client.list_assets(request={"parent": org_name})
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
# [END demo_list_all_assets]
Expand All @@ -53,7 +53,9 @@ def list_assets_with_filters(organization_id):
+ '"google.cloud.resourcemanager.Project"'
)
# Call the API and print results.
asset_iterator = client.list_assets(org_name, filter_=project_filter)
asset_iterator = client.list_assets(
request={"parent": org_name, "filter": project_filter}
)
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
# [END demo_list_assets_with_filter]
Expand All @@ -66,8 +68,6 @@ def list_assets_with_filters_and_read_time(organization_id):
# [START demo_list_assets_with_filter_and_time]
from datetime import datetime, timedelta

from google.protobuf.timestamp_pb2 import Timestamp

from google.cloud import securitycenter

client = securitycenter.SecurityCenterClient()
Expand All @@ -83,12 +83,14 @@ def list_assets_with_filters_and_read_time(organization_id):

# Lists assets as of yesterday.
read_time = datetime.utcnow() - timedelta(days=1)
timestamp_proto = Timestamp()
timestamp_proto.FromDatetime(read_time)

# Call the API and print results.
asset_iterator = client.list_assets(
org_name, filter_=project_filter, read_time=timestamp_proto
request={
"parent": org_name,
"filter": project_filter,
"read_time": read_time,
}
)
for i, asset_result in enumerate(asset_iterator):
print(i, asset_result)
Expand All @@ -102,7 +104,6 @@ def list_point_in_time_changes(organization_id):
# [START demo_list_assets_changes]
from datetime import timedelta

from google.protobuf.duration_pb2 import Duration
from google.cloud import securitycenter

client = securitycenter.SecurityCenterClient()
Expand All @@ -117,12 +118,14 @@ def list_point_in_time_changes(organization_id):

# List assets and their state change the last 30 days
compare_delta = timedelta(days=30)
# Convert the timedelta to a Duration
duration_proto = Duration()
duration_proto.FromTimedelta(compare_delta)

# Call the API and print results.
asset_iterator = client.list_assets(
org_name, filter_=project_filter, compare_duration=duration_proto
request={
"parent": org_name,
"filter": project_filter,
"compare_duration": compare_delta,
}
)
for i, asset in enumerate(asset_iterator):
print(i, asset)
Expand All @@ -145,7 +148,9 @@ def group_assets(organization_id):

group_by_type = "security_center_properties.resource_type"

result_iterator = client.group_assets(org_name, group_by=group_by_type)
result_iterator = client.group_assets(
request={"parent": org_name, "group_by": group_by_type}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
# [END group_all_assets]
Expand All @@ -170,7 +175,7 @@ def group_filtered_assets(organization_id):
+ '"google.cloud.resourcemanager.Project"'
)
result_iterator = client.group_assets(
org_name, group_by=group_by_type, filter_=only_projects
request={"parent": org_name, "group_by": group_by_type, "filter": only_projects}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
Expand All @@ -186,18 +191,20 @@ def group_assets_by_changes(organization_id):
from datetime import timedelta

from google.cloud import securitycenter
from google.protobuf.duration_pb2 import Duration

client = securitycenter.SecurityCenterClient()

duration_proto = Duration()
duration_proto.FromTimedelta(timedelta(days=5))
duration = timedelta(days=5)

# organization_id is the numeric ID of the organization.
# organization_id = "1234567777"
org_name = "organizations/{org_id}".format(org_id=organization_id)
result_iterator = client.group_assets(
org_name, group_by="state_change", compare_duration=duration_proto
request={
"parent": org_name,
"group_by": "state_change",
"compare_duration": duration,
}
)
for i, result in enumerate(result_iterator):
print((i + 1), result)
Expand Down
42 changes: 25 additions & 17 deletions securitycenter/snippets/snippets_notification_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def create_notification_config(organization_id, notification_config_id, pubsub_t
org_name = "organizations/{org_id}".format(org_id=organization_id)

created_notification_config = client.create_notification_config(
org_name,
notification_config_id,
{
"description": "Notification for active findings",
"pubsub_topic": pubsub_topic,
"streaming_config": {"filter": 'state = "ACTIVE"'},
},
request={
"parent": org_name,
"config_id": notification_config_id,
"notification_config": {
"description": "Notification for active findings",
"pubsub_topic": pubsub_topic,
"streaming_config": {"filter": 'state = "ACTIVE"'},
},
}
)

print(created_notification_config)
Expand All @@ -59,7 +61,7 @@ def delete_notification_config(organization_id, notification_config_id):
org_id=organization_id, config_id=notification_config_id
)

client.delete_notification_config(notification_config_name)
client.delete_notification_config(request={"name": notification_config_name})
print("Deleted notification config: {}".format(notification_config_name))
# [END scc_delete_notification_config]
return True
Expand All @@ -79,7 +81,9 @@ def get_notification_config(organization_id, notification_config_id):
org_id=organization_id, config_id=notification_config_id
)

notification_config = client.get_notification_config(notification_config_name)
notification_config = client.get_notification_config(
request={"name": notification_config_name}
)
print("Got notification config: {}".format(notification_config))
# [END scc_get_notification_config]
return notification_config
Expand All @@ -95,7 +99,9 @@ def list_notification_configs(organization_id):
# TODO: organization_id = "your-org-id"
org_name = "organizations/{org_id}".format(org_id=organization_id)

notification_configs_iterator = client.list_notification_configs(org_name)
notification_configs_iterator = client.list_notification_configs(
request={"parent": org_name}
)
for i, config in enumerate(notification_configs_iterator):
print("{}: notification_config: {}".format(i, config))
# [END scc_list_notification_configs]
Expand Down Expand Up @@ -128,13 +134,15 @@ def update_notification_config(organization_id, notification_config_id, pubsub_t
)

updated_notification_config = client.update_notification_config(
{
"name": notification_config_name,
"description": updated_description,
"pubsub_topic": pubsub_topic,
"streaming_config": {"filter": updated_filter},
},
update_mask=field_mask,
request={
"notification_config": {
"name": notification_config_name,
"description": updated_description,
"pubsub_topic": pubsub_topic,
"streaming_config": {"filter": updated_filter},
},
"update_mask": field_mask,
}
)

print(updated_notification_config)
Expand Down
8 changes: 2 additions & 6 deletions securitycenter/snippets/snippets_notification_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ def receive_notifications(project_id, subscription_name):
import concurrent

from google.cloud import pubsub_v1
from google.cloud.securitycenter_v1.proto.notification_message_pb2 import (
NotificationMessage,
)
from google.protobuf import json_format
from google.cloud.securitycenter_v1 import NotificationMessage

# TODO: project_id = "your-project-id"
# TODO: subscription_name = "your-subscription-name"

def callback(message):
print("Received message")

notification_msg = NotificationMessage()
json_format.Parse(message.data, notification_msg)
notification_msg = NotificationMessage.from_json(message.data)

print(
"Notification config name: {}".format(
Expand Down
62 changes: 32 additions & 30 deletions securitycenter/snippets/snippets_notification_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def cleanup_notification_config(notification_config_id):
notification_config_name = "organizations/{org_id}/notificationConfigs/{config_id}".format(
org_id=ORG_ID, config_id=notification_config_id
)
client.delete_notification_config(notification_config_name)
client.delete_notification_config(request={"name": notification_config_name})


@pytest.fixture
Expand All @@ -51,13 +51,15 @@ def new_notification_config_for_update():
org_name = "organizations/{org_id}".format(org_id=ORG_ID)

created_notification_config = client.create_notification_config(
org_name,
UPDATE_CONFIG_ID,
{
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
request={
"parent": org_name,
"config_id": UPDATE_CONFIG_ID,
"notification_config": {
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
}
)
yield created_notification_config
cleanup_notification_config(UPDATE_CONFIG_ID)
Expand All @@ -70,13 +72,15 @@ def new_notification_config_for_get():
org_name = "organizations/{org_id}".format(org_id=ORG_ID)

created_notification_config = client.create_notification_config(
org_name,
GET_CONFIG_ID,
{
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
request={
"parent": org_name,
"config_id": GET_CONFIG_ID,
"notification_config": {
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
}
)
yield created_notification_config
cleanup_notification_config(GET_CONFIG_ID)
Expand All @@ -89,13 +93,15 @@ def deleted_notification_config():
org_name = "organizations/{org_id}".format(org_id=ORG_ID)

created_notification_config = client.create_notification_config(
org_name,
DELETE_CONFIG_ID,
{
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
request={
"parent": org_name,
"config_id": DELETE_CONFIG_ID,
"notification_config": {
"description": "Notification for active findings",
"pubsub_topic": PUBSUB_TOPIC,
"streaming_config": {"filter": ""},
},
}
)
return created_notification_config

Expand All @@ -110,10 +116,8 @@ def test_create_notification_config():


def test_delete_notification_config(deleted_notification_config):
assert (
snippets_notification_configs.delete_notification_config(
ORG_ID, DELETE_CONFIG_ID
)
assert snippets_notification_configs.delete_notification_config(
ORG_ID, DELETE_CONFIG_ID
)


Expand All @@ -137,8 +141,6 @@ def test_update_notification_config(new_notification_config_for_update):


def test_receive_notifications():
assert (
snippets_notification_receiver.receive_notifications(
PROJECT_ID, PUBSUB_SUBSCRIPTION
)
assert snippets_notification_receiver.receive_notifications(
PROJECT_ID, PUBSUB_SUBSCRIPTION
)
11 changes: 8 additions & 3 deletions securitycenter/snippets/snippets_orgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_settings(organization_id):

org_settings_name = client.organization_settings_path(organization_id)

org_settings = client.get_organization_settings(org_settings_name)
org_settings = client.get_organization_settings(request={"name": org_settings_name})
print(org_settings)
# [END get_org_settings]

Expand All @@ -50,8 +50,13 @@ def update_asset_discovery_org_settings(organization_id):
field_mask = field_mask_pb2.FieldMask(paths=["enable_asset_discovery"])
# Call the service.
updated = client.update_organization_settings(
{"name": org_settings_name, "enable_asset_discovery": True},
update_mask=field_mask,
request={
"organization_settings": {
"name": org_settings_name,
"enable_asset_discovery": True,
},
"update_mask": field_mask,
}
)
print("Asset Discovery Enabled? {}".format(updated.enable_asset_discovery))
# [END update_org_settings]
Expand Down
Loading

0 comments on commit abf2113

Please sign in to comment.