예제 #1
0
  def Run(self, args):
    dns = self.context['dns_client']
    messages = self.context['dns_messages']
    resources = self.context['dns_resources']
    project_id = properties.VALUES.core.project.Get(required=True)

    # Get the managed-zone.
    zone_ref = resources.Parse(args.zone, collection='dns.managedZones')
    try:
      zone = dns.managedZones.Get(zone_ref.Request())
    except apitools_base.HttpError as error:
      raise exceptions.HttpException(util.GetErrorMessage(error))

    # Get all the record-sets.
    record_sets = []
    for record_set in apitools_base.YieldFromList(
        dns.resourceRecordSets,
        messages.DnsResourceRecordSetsListRequest(project=project_id,
                                                  managedZone=zone_ref.Name()),
        field='rrsets'):
      record_sets.append(record_set)

    # Export the record-sets.
    try:
      with files.Context(open(args.records_file, 'w')) as export_file:
        if args.zone_file_format:
          export_util.WriteToZoneFile(export_file, record_sets, zone.dnsName)
        else:
          export_util.WriteToYamlFile(export_file, record_sets)
    except Exception as exp:
      msg = 'unable to export record-sets to file [{0}]: {1}'.format(
          args.records_file, exp)
      raise exceptions.ToolException(msg)

    log.status.Print('Exported record-sets to [{0}].'.format(args.records_file))
예제 #2
0
    def Run(self, args):
        if os.path.isfile(args.transaction_file):
            raise exceptions.ToolException(
                'transaction already exists at [{0}]'.format(
                    args.transaction_file))

        dns = self.context['dns_client']
        messages = self.context['dns_messages']
        resources = self.context['dns_resources']
        project_id = properties.VALUES.core.project.Get(required=True)

        # Get the managed-zone.
        zone_ref = resources.Parse(args.zone, collection='dns.managedZones')
        try:
            zone = dns.managedZones.Get(zone_ref.Request())
        except apitools.HttpError as error:
            raise exceptions.HttpException(util.GetErrorMessage(error))

        # Initialize an empty change
        change = messages.Change()

        # Get the SOA record, there will be one and only one.
        # Add addition and deletion for SOA incrementing to change.
        records = [
            record for record in apitools.YieldFromList(
                dns.resourceRecordSets,
                messages.DnsResourceRecordSetsListRequest(
                    project=project_id,
                    managedZone=zone_ref.Name(),
                    name=zone.dnsName,
                    type='SOA'),
                field='rrsets')
        ]
        change.deletions.append(records[0])
        change.additions.append(import_util.NextSOARecordSet(records[0]))

        # Write change to transaction file
        try:
            with files.Context(open(args.transaction_file,
                                    'w')) as transaction_file:
                transaction_util.WriteToYamlFile(transaction_file, change)
        except Exception as exp:
            msg = 'unable to write transaction [{0}] because [{1}]'
            msg = msg.format(args.transaction_file, exp)
            raise exceptions.ToolException(msg)

        log.status.Print('Transaction started [{0}].'.format(
            args.transaction_file))
예제 #3
0
    def Run(self, args):
        if not os.path.isfile(args.records_file):
            raise exceptions.ToolException('no such file [{0}]'.format(
                args.records_file))

        dns = self.context['dns_client']
        messages = self.context['dns_messages']
        resources = self.context['dns_resources']
        project_id = properties.VALUES.core.project.Get(required=True)

        # Get the managed-zone.
        zone_ref = resources.Parse(args.zone, collection='dns.managedZones')
        try:
            zone = dns.managedZones.Get(zone_ref.Request())
        except apitools.HttpError as error:
            raise exceptions.HttpException(util.GetErrorMessage(error))

        # Get the current record-sets.
        current = {}
        for record in apitools.YieldFromList(
                dns.resourceRecordSets,
                messages.DnsResourceRecordSetsListRequest(
                    project=project_id, managedZone=zone_ref.Name()),
                field='rrsets'):
            current[(record.name, record.type)] = record

        # Get the imported record-sets.
        try:
            with files.Context(open(args.records_file)) as import_file:
                if args.zone_file_format:
                    imported = import_util.RecordSetsFromZoneFile(
                        import_file, zone.dnsName)
                else:
                    imported = import_util.RecordSetsFromYamlFile(import_file)
        except Exception as exp:
            msg = (
                'unable to read record-sets from specified records-file [{0}] '
                'because [{1}]')
            msg = msg.format(args.records_file, exp.message)
            raise exceptions.ToolException(msg)

        # Get the change resulting from the imported record-sets.
        change = import_util.ComputeChange(current, imported,
                                           args.delete_all_existing)
        if not change:
            msg = 'Nothing to do, all the records in [{0}] already exist.'.format(
                args.records_file)
            log.status.Print(msg)
            return None

        # Send the change to the service.
        result = dns.changes.Create(
            messages.DnsChangesCreateRequest(change=change,
                                             managedZone=zone.name,
                                             project=project_id))
        change_ref = resources.Create(collection='dns.changes',
                                      project=project_id,
                                      managedZone=zone.name,
                                      changeId=result.id)
        msg = 'Imported record-sets from [{0}] into managed-zone [{1}].'.format(
            args.records_file, zone_ref.Name())
        log.status.Print(msg)
        log.CreatedResource(change_ref)
        return result