本文整理匯總了Python中c7n.cwe.CloudWatchEvents類的典型用法代碼示例。如果您正苦於以下問題:Python CloudWatchEvents類的具體用法?Python CloudWatchEvents怎麽用?Python CloudWatchEvents使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了CloudWatchEvents類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: disabled_xtest_get_ids
def disabled_xtest_get_ids(self):
self.assertEqual(
CloudWatchEvents.get_ids(
event_data('event-instance-state.json'),
{'type': 'ec2-instance-state'}),
['i-a2d74f12'])
self.assertEqual(
CloudWatchEvents.get_ids(
{'detail': event_data('event-cloud-trail-run-instances.json')},
{'type': 'cloudtrail', 'events': ['RunInstances']}),
['i-d49cf94c'])
示例2: test_get_ids_multiple_events
def test_get_ids_multiple_events(self):
d = event_data('event-cloud-trail-run-instances.json')
d['eventName'] = 'StartInstances'
self.assertEqual(
CloudWatchEvents.get_ids(
{'detail': d},
{'type': 'cloudtrail', 'events': [
# wrong event name
{'source': 'ec2.amazonaws.com',
'event': 'CreateTags',
'ids': 'requestParameters.resourcesSet.items[].resourceId'},
# wrong event source
{'source': 'ecs.amazonaws.com',
'event': 'StartInstances',
'ids': 'responseElements.instancesSet.items'},
# matches no resource ids
{'source': 'ec2.amazonaws.com',
'event': 'StartInstances',
'ids': 'responseElements.instancesSet2.items[].instanceId'},
# correct
{'source': 'ec2.amazonaws.com',
'event': 'StartInstances',
'ids': 'responseElements.instancesSet.items[].instanceId'},
# we don't fall off the end
{'source': 'ec2.amazonaws.com',
'event': 'StartInstances',
'ids': 'responseElements.instancesSet.items[]'},
]}),
['i-784cdacd', u'i-7b4cdace'])
示例3: test_asg_state
def test_asg_state(self):
self.assertEqual(
CloudWatchEvents.get_ids(
event_data('event-asg-instance-failed.json'),
{'type': 'asg-instance-state',
'events': ['EC2 Instance Launch Unsuccessful']}),
['CustodianTest'])
示例4: test_ec2_state
def test_ec2_state(self):
self.assertEqual(
CloudWatchEvents.get_ids(
event_data("event-instance-state.json"), {"type": "ec2-instance-state"}
),
["i-a2d74f12"],
)
示例5: test_get_ids
def test_get_ids(self):
self.assertEqual(
CloudWatchEvents.get_ids(
{"detail": event_data("event-cloud-trail-run-instances.json")},
{"type": "cloudtrail", "events": ["RunInstances"]},
),
["i-784cdacd", u"i-7b4cdace"],
)
示例6: test_cloud_trail_resource
def test_cloud_trail_resource(self):
self.assertEqual(
CloudWatchEvents.match(event_data("event-cloud-trail-s3.json")),
{
"source": "s3.amazonaws.com",
"ids": jmespath.compile("detail.requestParameters.bucketName"),
},
)
示例7: push
def push(self, event, lambda_ctx):
"""Run policy in push mode against given event.
Lambda automatically generates cloud watch logs, and metrics
for us, albeit with some deficienies, metrics no longer count
against valid resources matches, but against execution.
Fortunately we already have replacements.
TODO: better customization around execution context outputs
TODO: support centralized lambda exec across accounts.
"""
mode = self.data.get('mode', {})
mode_type = mode.get('type')
if mode_type == 'periodic':
return self.poll()
resource_ids = CloudWatchEvents.get_ids(event, mode)
if resource_ids is None:
raise ValueError("Unknown push event mode %s" % self.data)
self.log.info('Found resource ids: %s' % resource_ids)
if not resource_ids:
self.log.warning("Could not find resource ids")
return
resources = self.resource_manager.get_resources(resource_ids)
if 'debug' in event:
self.log.info("Resources %s", resources)
resources = self.resource_manager.filter_resources(resources, event)
if 'debug' in event:
self.log.info("Filtered resources %d" % len(resources))
if not resources:
self.log.info("policy: %s resources: %s no resources matched" % (
self.name, self.resource_type))
return
self.ctx.metrics.put_metric(
'ResourceCount', len(resources), 'Count', Scope="Policy",
buffer=False)
if 'debug' in event:
self.log.info("Invoking actions %s", self.resource_manager.actions)
for action in self.resource_manager.actions:
self.log.info(
"policy: %s invoking action: %s resources: %d",
self.name, action.name, len(resources))
if isinstance(action, EventAction):
action.process(resources, event)
else:
action.process(resources)
return resources
示例8: test_custom_event
def test_custom_event(self):
d = {'detail': event_data('event-cloud-trail-run-instances.json')}
d['detail']['eventName'] = 'StartInstances'
self.assertEqual(
CloudWatchEvents.get_ids(
d,
{'type': 'cloudtrail', 'events': [{
'event': 'StartInstances',
'ids': 'responseElements.instancesSet.items[].instanceId',
'source': 'ec2.amazonaws.com'}]}),
['i-784cdacd', u'i-7b4cdace'])
示例9: test_asg_state
def test_asg_state(self):
self.assertEqual(
CloudWatchEvents.get_ids(
event_data("event-asg-instance-failed.json"),
{
"type": "asg-instance-state",
"events": ["EC2 Instance Launch Unsuccessful"],
},
),
["CustodianTest"],
)
示例10: resolve_cloudtrail_payload
def resolve_cloudtrail_payload(self, payload):
ids = []
sources = self.data.get('sources', [])
for e in self.data.get('events'):
event_info = CloudWatchEvents.get(e)
if event_info is None:
continue
sources.append(event_info['source'])
payload['detail'] = {
'eventSource': list(set(sources)),
'eventName': self.data.get('events', [])}
示例11: resolve_resources
def resolve_resources(self, event):
mode = self.policy.data.get('mode', {})
resource_ids = CloudWatchEvents.get_ids(event, mode)
if resource_ids is None:
raise ValueError("Unknown push event mode %s" % self.data)
self.policy.log.info('Found resource ids: %s' % resource_ids)
if not resource_ids:
self.policy.log.warning("Could not find resource ids")
return []
resources = self.policy.resource_manager.get_resources(resource_ids)
if 'debug' in event:
self.policy.log.info("Resources %s", resources)
return resources
示例12: resolve_cloudtrail_payload
def resolve_cloudtrail_payload(self, payload):
sources = self.data.get('sources', [])
events = []
for e in self.data.get('events'):
if not isinstance(e, dict):
events.append(e)
event_info = CloudWatchEvents.get(e)
if event_info is None:
continue
else:
event_info = e
events.append(e['event'])
sources.append(event_info['source'])
payload['detail'] = {
'eventSource': list(set(sources)),
'eventName': events}
示例13: resolve_resources
def resolve_resources(self, event):
self.assume_member(event)
mode = self.policy.data.get('mode', {})
resource_ids = CloudWatchEvents.get_ids(event, mode)
if resource_ids is None:
raise ValueError("Unknown push event mode %s", self.data)
self.policy.log.info('Found resource ids: %s', resource_ids)
# Handle multi-resource type events, like ec2 CreateTags
resource_ids = self.policy.resource_manager.match_ids(resource_ids)
if not resource_ids:
self.policy.log.warning("Could not find resource ids")
return []
resources = self.policy.resource_manager.get_resources(resource_ids)
if 'debug' in event:
self.policy.log.info("Resources %s", resources)
return resources
示例14: test_get_ids_multiple_events
def test_get_ids_multiple_events(self):
d = event_data("event-cloud-trail-run-instances.json")
d["eventName"] = "StartInstances"
self.assertEqual(
CloudWatchEvents.get_ids(
{"detail": d},
{
"type": "cloudtrail",
"events": [
# wrong event name
{
"source": "ec2.amazonaws.com",
"event": "CreateTags",
"ids": "requestParameters.resourcesSet.items[].resourceId",
},
# wrong event source
{
"source": "ecs.amazonaws.com",
"event": "StartInstances",
"ids": "responseElements.instancesSet.items",
},
# matches no resource ids
{
"source": "ec2.amazonaws.com",
"event": "StartInstances",
"ids": "responseElements.instancesSet2.items[].instanceId",
},
# correct
{
"source": "ec2.amazonaws.com",
"event": "StartInstances",
"ids": "responseElements.instancesSet.items[].instanceId",
},
# we don't fall off the end
{
"source": "ec2.amazonaws.com",
"event": "StartInstances",
"ids": "responseElements.instancesSet.items[]",
},
],
},
),
["i-784cdacd", u"i-7b4cdace"],
)
示例15: test_custom_event
def test_custom_event(self):
d = {"detail": event_data("event-cloud-trail-run-instances.json")}
d["detail"]["eventName"] = "StartInstances"
self.assertEqual(
CloudWatchEvents.get_ids(
d,
{
"type": "cloudtrail",
"events": [
{
"event": "StartInstances",
"ids": "responseElements.instancesSet.items[].instanceId",
"source": "ec2.amazonaws.com",
}
],
},
),
["i-784cdacd", u"i-7b4cdace"],
)