@ -107,24 +107,27 @@ rule:
description : CloudWatch Event rule data
returned : success
type : dict
sample : " { ' arn ' : ' arn:aws:events:us-east-1:123456789012:rule/MyCronTask ' , ' description ' : ' Run my scheduled task ' , ' name ' : ' MyCronTask ' , ' schedule_expression ' : ' cron(0 20 * * ? *) ' , ' state ' : ' ENABLED ' } "
sample :
arn : ' arn:aws:events:us-east-1:123456789012:rule/MyCronTask '
description : ' Run my scheduled task '
name : ' MyCronTask '
schedule_expression : ' cron(0 20 * * ? *) '
state : ' ENABLED '
targets :
description : CloudWatch Event target ( s ) assigned to the rule
returned : success
type : list
sample : " [ { ' arn ' : ' arn:aws:lambda:us-east-1:123456789012:function:MyFunction ' , ' id ' : ' MyTargetId ' }] "
''' # NOQA
'''
try :
import boto3 . exception
import botocore . exceptions
import botocore
except ImportError :
# module_utils.ec2.HAS_BOTO3 will do the right thing
pass
pass # handled by AnsibleAWSModule
from ansible . module_utils . basic import Ansible Module
from ansible . module_utils . ec2 import ( HAS_BOTO3 , boto3_conn , camel_dict_to_snake_dict ,
ec2_argument_spec , get_aws_connection_info )
from ansible . module_utils . aws. core import AnsibleAWS Module
from ansible . module_utils . ec2 import boto3_conn , camel_dict_to_snake_dict
from ansible . module_utils . ec2 import ec2_argument_spec , get_aws_connection_info
class CloudWatchEventRule ( object ) :
@ -137,6 +140,7 @@ class CloudWatchEventRule(object):
self . event_pattern = event_pattern
self . description = description
self . role_arn = role_arn
self . module = module
def describe ( self ) :
""" Returns the existing details of the rule in AWS """
@ -146,7 +150,9 @@ class CloudWatchEventRule(object):
error_code = e . response . get ( ' Error ' , { } ) . get ( ' Code ' )
if error_code == ' ResourceNotFoundException ' :
return { }
raise
self . module . fail_json_aws ( e , msg = " Could not describe rule %s " % self . name )
except botocore . exceptions . BotoCoreError as e :
self . module . fail_json_aws ( e , msg = " Could not describe rule %s " % self . name )
return self . _snakify ( rule_info )
def put ( self , enabled = True ) :
@ -163,26 +169,39 @@ class CloudWatchEventRule(object):
request [ ' Description ' ] = self . description
if self . role_arn :
request [ ' RoleArn ' ] = self . role_arn
response = self . client . put_rule ( * * request )
try :
response = self . client . put_rule ( * * request )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not create/update rule %s " % self . name )
self . changed = True
return response
def delete ( self ) :
""" Deletes the rule in AWS """
self . remove_all_targets ( )
response = self . client . delete_rule ( Name = self . name )
try :
response = self . client . delete_rule ( Name = self . name )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not delete rule %s " % self . name )
self . changed = True
return response
def enable ( self ) :
""" Enables the rule in AWS """
response = self . client . enable_rule ( Name = self . name )
try :
response = self . client . enable_rule ( Name = self . name )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not enable rule %s " % self . name )
self . changed = True
return response
def disable ( self ) :
""" Disables the rule in AWS """
response = self . client . disable_rule ( Name = self . name )
try :
response = self . client . disable_rule ( Name = self . name )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not disable rule %s " % self . name )
self . changed = True
return response
@ -194,7 +213,9 @@ class CloudWatchEventRule(object):
error_code = e . response . get ( ' Error ' , { } ) . get ( ' Code ' )
if error_code == ' ResourceNotFoundException ' :
return [ ]
raise
self . module . fail_json_aws ( e , msg = " Could not find target for rule %s " % self . name )
except botocore . exceptions . BotoCoreError as e :
self . module . fail_json_aws ( e , msg = " Could not find target for rule %s " % self . name )
return self . _snakify ( targets ) [ ' targets ' ]
def put_targets ( self , targets ) :
@ -205,7 +226,10 @@ class CloudWatchEventRule(object):
' Rule ' : self . name ,
' Targets ' : self . _targets_request ( targets ) ,
}
response = self . client . put_targets ( * * request )
try :
response = self . client . put_targets ( * * request )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not create/update rule targets for rule %s " % self . name )
self . changed = True
return response
@ -217,7 +241,10 @@ class CloudWatchEventRule(object):
' Rule ' : self . name ,
' Ids ' : target_ids
}
response = self . client . remove_targets ( * * request )
try :
response = self . client . remove_targets ( * * request )
except ( botocore . exceptions . BotoCoreError , botocore . exceptions . ClientError ) as e :
self . module . fail_json_aws ( e , msg = " Could not remove rule targets from rule %s " % self . name )
self . changed = True
return response
@ -379,26 +406,25 @@ def get_cloudwatchevents_client(module):
resource = ' events ' ,
region = region , endpoint = ec2_url ,
* * aws_conn_kwargs )
except boto 3. exception . NoAuthHandler Found as e :
except boto core. exceptions . ProfileNot Found as e :
module . fail_json ( msg = str ( e ) )
def main ( ) :
argument_spec = ec2_argument_spec ( )
argument_spec . update ( dict (
name = dict ( required = True ) ,
schedule_expression = dict ( ) ,
event_pattern = dict ( ) ,
state = dict ( choices = [ ' present ' , ' disabled ' , ' absent ' ] ,
default = ' present ' ) ,
description = dict ( ) ,
role_arn = dict ( ) ,
targets = dict ( type = ' list ' , default = [ ] ) ,
) )
module = AnsibleModule ( argument_spec = argument_spec )
if not HAS_BOTO3 :
module . fail_json ( msg = ' boto3 required for this module ' )
argument_spec . update (
dict (
name = dict ( required = True ) ,
schedule_expression = dict ( ) ,
event_pattern = dict ( ) ,
state = dict ( choices = [ ' present ' , ' disabled ' , ' absent ' ] ,
default = ' present ' ) ,
description = dict ( ) ,
role_arn = dict ( ) ,
targets = dict ( type = ' list ' , default = [ ] ) ,
)
)
module = AnsibleAWSModule ( argument_spec = argument_spec )
rule_data = dict (
[ ( rf , module . params . get ( rf ) ) for rf in CloudWatchEventRuleManager . RULE_FIELDS ]