def main(): """Main function""" module = AnsibleModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), record=dict(required=True, aliases=['name'], type='str'), zone=dict(type='str'), zone_id=dict(type='str'), type=dict(required=True, choices=SUPPORTED_RECORD_TYPES, type='str'), record_data=dict(aliases=['value'], type='list'), ttl=dict(default=300, type='int'), overwrite=dict(default=False, type='bool'), service_account_email=dict(type='str'), pem_file=dict(type='path'), credentials_file=dict(type='path'), project_id=dict(type='str') ), required_if=[ ('state', 'present', ['record_data']), ('overwrite', False, ['record_data']) ], required_one_of=[['zone', 'zone_id']], supports_check_mode=True ) _sanity_check(module) record_name = module.params['record'] record_type = module.params['type'] state = module.params['state'] ttl = module.params['ttl'] zone_name = module.params['zone'] zone_id = module.params['zone_id'] json_output = dict( state=state, record=record_name, zone=zone_name, zone_id=zone_id, type=record_type, record_data=module.params['record_data'], ttl=ttl, overwrite=module.boolean(module.params['overwrite']) ) # Google Cloud DNS wants the trailing dot on all DNS names. if zone_name is not None and zone_name[-1] != '.': zone_name = zone_name + '.' if record_name[-1] != '.': record_name = record_name + '.' # Build a connection object that we can use to connect with Google Cloud # DNS. gcdns = gcdns_connect(module, provider=PROVIDER) # We need to check that the zone we're creating a record for actually # exists. zone = _get_zone(gcdns, zone_name, zone_id) if zone is None and zone_name is not None: module.fail_json( msg='zone name was not found: %s' % zone_name, changed=False ) elif zone is None and zone_id is not None: module.fail_json( msg='zone id was not found: %s' % zone_id, changed=False ) # Populate the returns with the actual zone information. json_output['zone'] = zone.domain json_output['zone_id'] = zone.id # We also need to check if the record we want to create or remove actually # exists. try: record = _get_record(gcdns, zone, record_type, record_name) except InvalidRequestError: # We gave Google Cloud DNS an invalid DNS record name. module.fail_json( msg='record name is invalid: %s' % record_name, changed=False ) _additional_sanity_checks(module, zone) diff = dict() # Build the 'before' diff if record is None: diff['before'] = '' diff['before_header'] = '<absent>' else: diff['before'] = dict( record=record.data['name'], type=record.data['type'], record_data=record.data['rrdatas'], ttl=record.data['ttl'] ) diff['before_header'] = "%s:%s" % (record_type, record_name) # Create, remove, or modify the record. if state == 'present': diff['after'] = dict( record=record_name, type=record_type, record_data=module.params['record_data'], ttl=ttl ) diff['after_header'] = "%s:%s" % (record_type, record_name) changed = create_record(module, gcdns, zone, record) elif state == 'absent': diff['after'] = '' diff['after_header'] = '<absent>' changed = remove_record(module, gcdns, record) module.exit_json(changed=changed, diff=diff, **json_output)
def main(): """Main function""" module = AnsibleModule( argument_spec=dict( state=dict(default='present', choices=['present', 'absent'], type='str'), zone=dict(required=True, aliases=['name'], type='str'), description=dict(default='', type='str'), service_account_email=dict(type='str'), pem_file=dict(type='path'), credentials_file=dict(type='path'), project_id=dict(type='str') ), supports_check_mode=True ) _sanity_check(module) zone_name = module.params['zone'] state = module.params['state'] # Google Cloud DNS wants the trailing dot on the domain name. if zone_name[-1] != '.': zone_name = zone_name + '.' json_output = dict( state=state, zone=zone_name, description=module.params['description'] ) # Build a connection object that was can use to connect with Google # Cloud DNS. gcdns = gcdns_connect(module, provider=PROVIDER) # We need to check if the zone we're attempting to create already exists. zone = _get_zone(gcdns, zone_name) diff = dict() # Build the 'before' diff if zone is None: diff['before'] = '' diff['before_header'] = '<absent>' else: diff['before'] = dict( zone=zone.domain, description=zone.extra['description'] ) diff['before_header'] = zone_name # Create or remove the zone. if state == 'present': diff['after'] = dict( zone=zone_name, description=module.params['description'] ) diff['after_header'] = zone_name changed = create_zone(module, gcdns, zone) elif state == 'absent': diff['after'] = '' diff['after_header'] = '<absent>' changed = remove_zone(module, gcdns, zone) module.exit_json(changed=changed, diff=diff, **json_output)