def _start(self): """ Creates the route53 record set that matches current state definition :return: response of boto3 change resource record set with action CREATE """ desired_state_def = self.get_desired_state_definition() if not list(desired_state_def.get("ResourceRecords")): return None start_definition = pcf_util.keep_and_remove_keys( desired_state_def, Route53Record.REMOVE_PARAM_CONVERSIONS) change_entry = { 'Action': 'CREATE', 'ResourceRecordSet': start_definition, } #TTL, Name including zone ending, type required change_batch = { 'Changes': [change_entry], } return self.client.change_resource_record_sets( HostedZoneId=self.hosted_zone, ChangeBatch=change_batch)
def test_apply_states(self): conn = boto3.client('route53', region_name='us-east-1') resp = conn.create_hosted_zone(Name="lol.catz.com", CallerReference=str(hash('foo')), HostedZoneConfig=dict( PrivateZone=True, Comment="db", )) self.particle_definition["aws_resource"]["HostedZoneId"] = resp[ "HostedZone"]["Id"] particle = Route53Record(self.particle_definition) # Test start particle.set_desired_state(State.running) particle.apply(sync=True) assert particle.get_state() == State.running assert particle.get_current_state_definition( ) == pcf_util.keep_and_remove_keys( particle.get_desired_state_definition(), Route53Record.REMOVE_PARAM_CONVERSIONS) # Test Update # Note: error will be thrown if you try to convert record set types self.particle_definition["aws_resource"]["ResourceRecords"] = [{ "Value": "192.168.0.1" }] # Tests picking up existing record particle = Route53Record(self.particle_definition) particle.set_desired_state(State.running) particle.apply(sync=True) assert particle.get_current_state_definition( ) == pcf_util.keep_and_remove_keys( particle.get_desired_state_definition(), Route53Record.REMOVE_PARAM_CONVERSIONS) assert particle.get_state() == State.running # Test Terminate particle.set_desired_state(State.terminated) particle.apply(sync=True) assert particle.get_state() == State.terminated
def is_state_definition_equivalent(self): """ Determines if the current state definition matches the current state definition. Uses keep and remove function from pcf util to remove extra params in desired state that are not in the current state. :return: bool """ self.get_state() diff = pcf_util.diff_dict( self.current_state_definition, pcf_util.keep_and_remove_keys( self.get_desired_state_definition(), Route53Record.REMOVE_PARAM_CONVERSIONS)) if not diff or len(diff) == 0: return True elif self.state == State.terminated: return True else: logger.debug( "State is not equivalent for {0} with diff: {1}".format( self.get_pcf_id(), json.dumps(diff))) return False
def _update(self): """ Updates the route53 record set that matches current state definition :return: response of boto3 change resource record set with action UPSERT """ desired_state_def = self.get_desired_state_definition() if not list(desired_state_def.get("ResourceRecords")): return None update_definition = pcf_util.keep_and_remove_keys( desired_state_def, Route53Record.REMOVE_PARAM_CONVERSIONS) change_entry = { 'Action': 'UPSERT', 'ResourceRecordSet': update_definition, } change_batch = { 'Changes': [change_entry], } return self.client.change_resource_record_sets( HostedZoneId=self.hosted_zone, ChangeBatch=change_batch)