async def validate_rrsets_by_zone(self, zone, rrsets): """Given a zone, validate current versus desired rrsets. If there are any missing records, a corrective message for each record will be published. Args: zone (str): zone to query Google Cloud DNS API. rrsets (list): desired record sets to which to compare the Cloud DNS API's response. """ desired_rrsets = [ gdns.GCPResourceRecordSet(**record) for record in rrsets ] actual_rrsets = await self.dns_client.get_records_for_zone(zone) # NOTE: only working on "additions" (which includes changes to # current records) right now. # TODO: (FEATURE) add support for cleaning up records that # should have been deleted. missing_rrsets = [ rs for rs in desired_rrsets if rs not in actual_rrsets ] msg = (f'[{zone}] Processed {len(actual_rrsets)} rrset messages ' f'and found {len(missing_rrsets)} missing rrsets.') logging.info(msg) # TODO (lynn): emit or incr metric of missing_rrsets by zone once # aioshumway is released # TODO: (FEATURE): have separate metrics for additions and deletions await self.publish_change_messages(missing_rrsets, action='additions')
def test_create_gcp_rrset(): """Create valid GCPResourceRecordSet instances.""" data = {'name': 'test', 'type': 'A', 'rrdatas': ['10.1.2.3'], 'ttl': 500} rrset = gdns.GCPResourceRecordSet(**data) assert data == attr.asdict(rrset) # default TTL when not provided data.pop('ttl') rrset = gdns.GCPResourceRecordSet(**data) data['ttl'] = 300 assert data == attr.asdict(rrset) # Raise when required params are missing missing_params = {'name': 'test'} with pytest.raises(TypeError): gdns.GCPResourceRecordSet(**missing_params)
async def test_publish_change_messages(recon_client, fake_response_data, caplog): """Publish message to changes queue.""" rrsets = fake_response_data['rrsets'] desired_rrsets = [gdns.GCPResourceRecordSet(**kw) for kw in rrsets] await recon_client.publish_change_messages(desired_rrsets) assert 3 == recon_client.changes_channel.qsize() assert 4 == len(caplog.records)
async def mock_get_records_for_zone(*args, **kwargs): nonlocal mock_get_records_for_zone_called mock_get_records_for_zone_called += 1 rrsets = fake_response_data['rrsets'] rrsets[0]['rrdatas'] = ['10.4.5.6'] return [gdns.GCPResourceRecordSet(**kw) for kw in rrsets]