Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/c7n_gcp/actions/notify.py: 69%
26 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# Copyright The Cloud Custodian Authors.
2# SPDX-License-Identifier: Apache-2.0
4from c7n.actions import BaseNotify
5from c7n import utils
6from c7n.resolver import ValuesFrom
7from c7n_gcp.provider import resources as gcp_resources
8from c7n.version import version
11class Notify(BaseNotify):
12 """
13 :example:
15 .. code-block:: yaml
17 policies:
18 - name: bad-instance-get
19 resource: gcp.instance
20 filters:
21 - Name: bad-instance
22 actions:
23 - type: notify
24 to:
25 - email@address
26 # which template for the email should we use
27 template: policy-template
28 transport:
29 type: pubsub
30 topic: projects/yourproject/topics/yourtopic
31 """
33 batch_size = 1000
35 schema = {
36 'type': 'object',
37 'addtionalProperties': False,
38 'anyOf': [
39 {'required': ['type', 'transport', 'to']},
40 {'required': ['type', 'transport', 'to_from']}],
41 'properties': {
42 'type': {'enum': ['notify']},
43 'to': {'type': 'array', 'items': {'type': 'string'}},
44 'owner_absent_contact': {'type': 'array', 'items': {'type': 'string'}},
45 'to_from': ValuesFrom.schema,
46 'cc': {'type': 'array', 'items': {'type': 'string'}},
47 'cc_from': ValuesFrom.schema,
48 'cc_manager': {'type': 'boolean'},
49 'from': {'type': 'string'},
50 'subject': {'type': 'string'},
51 'template': {'type': 'string'},
52 'transport': {
53 'oneOf': [
54 {'type': 'object',
55 'required': ['type', 'topic'],
56 'properties': {
57 'topic': {'type': 'string'},
58 'type': {'enum': ['pubsub']},
59 }}],
60 },
61 }
62 }
63 schema_alias = True
64 permissions = ('pubsub.topics.publish',)
66 def process(self, resources, event=None):
67 session = utils.local_session(self.manager.session_factory)
68 client = session.client('pubsub', 'v1', 'projects.topics')
70 project = session.get_default_project()
71 message = {
72 'event': event,
73 'account_id': project,
74 'account': project,
75 'region': 'all',
76 'policy': self.manager.data,
77 'execution_id': self.manager.ctx.execution_id,
78 'execution_start': self.manager.ctx.start_time,
79 'version': version
80 }
82 message['action'] = self.expand_variables(message)
84 for batch in utils.chunks(resources, self.batch_size):
85 message['resources'] = batch
86 self.publish_message(message, client)
88 # Methods to handle GCP Pub Sub topic publishing
89 def publish_message(self, message, client):
90 """Publish message to a GCP pub/sub topic
91 """
92 return client.execute_command('publish', {
93 'topic': self.data['transport']['topic'],
94 'body': {
95 'messages': {
96 'data': self.pack(message)
97 }
98 }
99 })
101 @classmethod
102 def register_resource(cls, registry, resource_class):
103 if resource_class.action_registry:
104 resource_class.action_registry.register('notify', Notify)
107gcp_resources.subscribe(Notify.register_resource)