def create_or_delete_domain(args=None): ''' We need to perform some initial sanity checking and also look up required info before handing it off to create or delete. ''' retvals, payload = dict(), dict() has_changed, has_failed = False, False msg, stderr, memset_api = None, None, None # get the zones and check if the relevant zone exists. api_method = 'dns.zone_list' has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) if has_failed: # this is the first time the API is called; incorrect credentials will # manifest themselves at this point so we need to ensure the user is # informed of the reason. retvals['failed'] = has_failed retvals['msg'] = msg retvals['stderr'] = "API returned an error: {0}".format( response.status_code) return (retvals) zone_exists, msg, counter, zone_id = get_zone_id( zone_name=args['zone'], current_zones=response.json()) if not zone_exists: # the zone needs to be unique - this isn't a requirement of Memset's API but it # makes sense in the context of this module. has_failed = True if counter == 0: stderr = "DNS zone '{0}' does not exist, cannot create domain.".format( args['zone']) elif counter > 1: stderr = "{0} matches multiple zones, cannot create domain.".format( args['zone']) retvals['failed'] = has_failed retvals['msg'] = stderr return (retvals) if args['state'] == 'present': has_failed, has_changed, msg = create_zone_domain( args=args, zone_exists=zone_exists, zone_id=zone_id, payload=payload) if args['state'] == 'absent': has_failed, has_changed, memset_api, msg = delete_zone_domain( args=args, payload=payload) retvals['changed'] = has_changed retvals['failed'] = has_failed for val in ['msg', 'stderr', 'memset_api']: if val is not None: retvals[val] = eval(val) return (retvals)
def create_or_delete(args=None): ''' We need to perform some initial sanity checking and also look up required info before handing it off to create or delete functions. Check mode is integrated into the create or delete functions. ''' has_failed, has_changed = False, False msg, memset_api, stderr = None, None, None retvals, payload = dict(), dict() # get the zones and check if the relevant zone exists. api_method = 'dns.zone_list' _has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) if _has_failed: # this is the first time the API is called; incorrect credentials will # manifest themselves at this point so we need to ensure the user is # informed of the reason. retvals['failed'] = _has_failed retvals['msg'] = msg retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) return(retvals) zone_exists, _msg, counter, zone_id = get_zone_id(zone_name=args['zone'], current_zones=response.json()) if not zone_exists: has_failed = True if counter == 0: stderr = "DNS zone {0} does not exist." . format(args['zone']) elif counter > 1: stderr = "{0} matches multiple zones." . format(args['zone']) retvals['failed'] = has_failed retvals['msg'] = stderr retvals['stderr'] = stderr return(retvals) # get a list of all records ( as we can't limit records by zone) api_method = 'dns.zone_record_list' _has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) # find any matching records records = [record for record in response.json() if record['zone_id'] == zone_id and record['record'] == args['record'] and record['type'] == args['type']] if args['state'] == 'present': has_changed, has_failed, memset_api, msg = create_zone_record(args=args, zone_id=zone_id, records=records, payload=payload) if args['state'] == 'absent': has_changed, has_failed, memset_api, msg = delete_zone_record(args=args, records=records, payload=payload) retvals['changed'] = has_changed retvals['failed'] = has_failed for val in ['msg', 'stderr', 'memset_api']: if val is not None: retvals[val] = eval(val) return(retvals)
def create_zone(args=None, zone_exists=None, payload=None): ''' At this point we already know whether the zone exists, so we just need to make the API reflect the desired state. ''' has_changed, has_failed = False, False msg, memset_api = None, None if not zone_exists: payload['ttl'] = args['ttl'] payload['nickname'] = args['name'] api_method = 'dns.zone_create' has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload) if not has_failed: has_changed = True else: api_method = 'dns.zone_list' _has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) for zone in response.json(): if zone['nickname'] == args['name']: break if zone['ttl'] != args['ttl']: # update the zone if the desired TTL is different. payload['id'] = zone['id'] payload['ttl'] = args['ttl'] api_method = 'dns.zone_update' has_failed, msg, response = memset_api_call( api_key=args['api_key'], api_method=api_method, payload=payload) if not has_failed: has_changed = True # populate return var with zone info. api_method = 'dns.zone_list' _has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) zone_exists, msg, counter, zone_id = get_zone_id( zone_name=args['name'], current_zones=response.json()) if zone_exists: payload = dict() payload['id'] = zone_id api_method = 'dns.zone_info' _has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload) memset_api = response.json() return (has_failed, has_changed, memset_api, msg)
def create_or_delete(args=None): ''' We need to perform some initial sanity checking and also look up required info before handing it off to create or delete. ''' retvals, payload = dict(), dict() has_failed, has_changed = False, False msg, memset_api, stderr = None, None, None # get the zones and check if the relevant zone exists. api_method = 'dns.zone_list' _has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method) if _has_failed: # this is the first time the API is called; incorrect credentials will # manifest themselves at this point so we need to ensure the user is # informed of the reason. retvals['failed'] = _has_failed retvals['msg'] = _msg return (retvals) zone_exists, _msg, counter, _zone_id = get_zone_id( zone_name=args['name'], current_zones=response.json()) if args['state'] == 'present': has_failed, has_changed, memset_api, msg = create_zone( args=args, zone_exists=zone_exists, payload=payload) elif args['state'] == 'absent': has_failed, has_changed, memset_api, msg = delete_zone( args=args, zone_exists=zone_exists, payload=payload) retvals['failed'] = has_failed retvals['changed'] = has_changed for val in ['msg', 'stderr', 'memset_api']: if val is not None: retvals[val] = eval(val) return (retvals)