def CreateRecordSetFromArgs(args):
  """Creates and returns a record-set from the given args.

  Args:
    args: The arguments to use to create the record-set.

  Raises:
    ToolException: If given record-set type is not supported

  Returns:
    ResourceRecordSet, the record-set created from the given args.
  """
  rd_type = rdatatype.from_text(args.type)
  if rd_type not in import_util.RDATA_TRANSLATIONS:
    raise exceptions.ToolException(
        'unsupported record-set type [{0}]'.format(args.type))

  record_set = messages.ResourceRecordSet()
  # Need to assign kind to default value for useful equals comparisons.
  record_set.kind = record_set.kind
  record_set.name = util.AppendTrailingDot(args.name)
  record_set.ttl = args.ttl
  record_set.type = args.type
  record_set.rrdatas = args.data
  if rd_type is rdatatype.TXT or rd_type is rdatatype.SPF:
    record_set.rrdatas = [import_util.QuotedText(datum) for datum in args.data]
  return record_set
Example #2
0
def _RecordSetFromZoneRecord(name, rdset, origin):
    """Returns the Cloud DNS ResourceRecordSet for the given zone file record.

  Args:
    name: Name, Domain name of the zone record.
    rdset: Rdataset, The zone record object.
    origin: Name, The origin domain of the zone file.

  Returns:
    The ResourceRecordSet equivalent for the given zone record, or None for
    unsupported record types.
  """
    if rdset.rdtype not in RDATA_TRANSLATIONS:
        return None

    record_set = messages.ResourceRecordSet()
    # Need to assign kind to default value for useful equals comparisons.
    record_set.kind = record_set.kind
    record_set.name = name.derelativize(origin).to_text()
    record_set.ttl = rdset.ttl
    record_set.type = rdatatype.to_text(rdset.rdtype)
    rdatas = []
    for rdata in rdset:
        rdatas.append(RDATA_TRANSLATIONS[rdset.rdtype](rdata, origin))
    record_set.rrdatas = rdatas
    return record_set
Example #3
0
def _RecordSetCopy(record_set):
    """Returns a copy of the given record-set.

  Args:
    record_set: ResourceRecordSet, Record-set to be copied.

  Returns:
    Returns a copy of the given record-set.
  """
    copy = messages.ResourceRecordSet()
    copy.kind = record_set.kind
    copy.name = record_set.name
    copy.type = record_set.type
    copy.ttl = record_set.ttl
    copy.rrdatas = list(record_set.rrdatas)
    return copy
def _RecordSetsFromDictionaries(record_set_dictionaries):
  """Converts list of record-set dictionaries into list of ResourceRecordSets.

  Args:
    record_set_dictionaries: [{str:str}], list of record-sets as dictionaries.

  Returns:
    list of ResourceRecordSets equivalent to given list of yaml record-sets
  """
  record_sets = []
  for record_set_dict in record_set_dictionaries:
    record_set = messages.ResourceRecordSet()
    # Need to assign kind to default value for useful equals comparisons.
    record_set.kind = record_set.kind
    record_set.name = record_set_dict['name']
    record_set.ttl = record_set_dict['ttl']
    record_set.type = record_set_dict['type']
    record_set.rrdatas = record_set_dict['rrdatas']
    record_sets.append(record_set)
  return record_sets
Example #5
0
def RecordSetsFromYamlFile(yaml_file):
    """Returns record-sets read from the given yaml file.

  Args:
    yaml_file: file, A yaml file with records.

  Returns:
    A (name, type) keyed dict of ResourceRecordSets that were obtained from the
    yaml file. Note that only A, AAAA, CNAME, MX, PTR, SOA, SPF, SRV, and TXT
    record-sets are retrieved. Other record-set types are not supported by Cloud
    DNS. Also, the master NS field for SOA records is discarded since that is
    provided by Cloud DNS.
  """
    record_sets = {}

    yaml_record_sets = yaml.safe_load_all(yaml_file)
    for yaml_record_set in yaml_record_sets:
        rdata_type = rdatatype.from_text(yaml_record_set['type'])
        if rdata_type not in RDATA_TRANSLATIONS:
            continue

        record_set = messages.ResourceRecordSet()
        # Need to assign kind to default value for useful equals comparisons.
        record_set.kind = record_set.kind
        record_set.name = yaml_record_set['name']
        record_set.ttl = yaml_record_set['ttl']
        record_set.type = yaml_record_set['type']
        record_set.rrdatas = yaml_record_set['rrdatas']

        if rdata_type is rdatatype.SOA:
            # Make master NS name substitutable.
            record_set.rrdatas[0] = re.sub(r'\S+',
                                           '{0}',
                                           record_set.rrdatas[0],
                                           count=1)

        record_sets[(record_set.name, record_set.type)] = record_set

    return record_sets