def build_desired_state(zones): """ Build a State object that represents the desired state :param zones: a representation of DNS zones as retrieved from app-interface :type zones: dict :return: returns a tuple that contains the State object and whether there \ were any errors :rtype: (State, bool) """ state = State('app-interface') errors = False for zone in zones: account_name = zone['account']['name'] account = state.get_account(account_name) if not account: account = Account(account_name) new_zone = Zone(zone['name'], zone) for record in zone['records']: new_record = Record(new_zone, record['name'], { 'type': record['type'], 'ttl': record['ttl'] or DEFAULT_RECORD_TTL }, record) targets = [] record_target = record.get('target') if record_target: if record['type'] == 'TXT': # TXT records values need to be enclosed in double quotes targets.append(f'"{record_target}"') else: targets.append(record_target) record_targets = record.get('targets') if record_targets: targets.extend(record_targets) record_target_cluster = record.get('target_cluster') if record_target_cluster: cluster = record_target_cluster cluster_name = cluster['name'] elb_fqdn = cluster.get('elbFQDN') if not elb_fqdn: logging.error(f'[{account}] elbFQDN not set for cluster ' f'{cluster_name}') errors = True continue targets.append(elb_fqdn) if not targets: logging.error(f'[{account}] no targets found for ' f'{new_record} in {new_zone}') errors = True continue new_record.add_targets(targets) new_zone.add_record(new_record) try: account.add_zone(new_zone) except DuplicateException as e: logging.error(e) errors = True if not state.get_account(account_name): state.add_account(account) return state, errors
def test_record_returns_values(self): """Record can return it's values""" zone = Zone('test.example.com') record = Record(zone, 'test-record', {'type': 'A', 'ttl': 300}) record.add_targets(['1.1.1.1', '2.2.2.2', '3.3.3.3']) self.assertListEqual(record.values, ['1.1.1.1', '2.2.2.2', '3.3.3.3'])