Пример #1
0
    def testComputeChangeReplaceAll(self):
        current_record_sets = dict(
            ((record_set.name, record_set.type), record_set)
            for record_set in util.GetRecordSets())
        change = import_util.ComputeChange(current_record_sets,
                                           util.GetImportedRecordSets(), True,
                                           'zone.com.', True)
        self.assertEqual(util.GetImportReplaceChange(), change)
        change = import_util.ComputeChange(current_record_sets,
                                           util.GetImportedRecordSets(), True,
                                           'zone.com.', False)

        self.assertEqual(util.GetImportReplaceChangeNoReplaceOrigin(), change)
Пример #2
0
 def testComputeChangeSOASerialIncrementing(self):
     change = import_util.ComputeChange(util.GetSOASequence()[0],
                                        util.GetImportableRecord())
     self.assertEqual(list(util.GetSOASequence()[0].values()),
                      change.deletions)
     self.assertEqual(
         list(util.GetImportableRecord().values()) +
         list(util.GetSOASequence()[1].values()), change.additions)
     change = import_util.ComputeChange(util.GetSOASequence()[1],
                                        util.GetImportableRecord())
     self.assertEqual(list(util.GetSOASequence()[1].values()),
                      change.deletions)
     self.assertEqual(
         list(util.GetImportableRecord().values()) +
         list(util.GetSOASequence()[2].values()), change.additions)
Пример #3
0
 def testComputeChangeNoConflicts(self):
     current_record_sets = dict(
         ((record_set.name, record_set.type), record_set)
         for record_set in util.GetRecordSets())
     change = import_util.ComputeChange(
         current_record_sets, util.GetImportedRecordSetsWithoutConflicts(),
         False, 'zone.com.', True)
     self.assertEqual(util.GetImportChange(), change)
Пример #4
0
 def testComputeChangeConflicts(self):
     current_record_sets = dict(
         ((record_set.name, record_set.type), record_set)
         for record_set in util.GetRecordSets())
     with self.assertRaises(import_util.ConflictingRecordsFound) as context:
         import_util.ComputeChange(current_record_sets,
                                   util.GetImportedRecordSets(), False,
                                   'zone.com.', True)
         self.assertEqual(
             context.exception.message,
             'The following records (name type) already exist: '
             '[\'mail.zone.com. A\', \'zone.com. A\', \'zone.com. SOA\']')
  def Run(self, args):
    if not os.path.exists(args.records_file):
      raise exceptions.ToolException(
          'no such file [{0}]'.format(args.records_file))
    if os.path.isdir(args.records_file):
      raise exceptions.ToolException(
          '[{0}] is a directory'.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_exceptions.HttpError as error:
      raise exceptions.HttpException(error)

    # Get the current record-sets.
    current = {}
    for record in list_pager.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 = (u'unable to read record-sets from specified records-file [{0}] '
             u'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,
                                       zone.dnsName, args.replace_origin_ns)
    if not change:
      msg = u'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 = u'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
  def Run(self, args):
    api_version = 'v1'
    # If in the future there are differences between API version, do NOT use
    # this patter of checking ReleaseTrack. Break this into multiple classes.
    if self.ReleaseTrack() == base.ReleaseTrack.BETA:
      api_version = 'v1beta2'

    if not os.path.exists(args.records_file):
      raise exceptions.ToolException(
          'no such file [{0}]'.format(args.records_file))
    if os.path.isdir(args.records_file):
      raise exceptions.ToolException(
          '[{0}] is a directory'.format(args.records_file))

    dns = apis.GetClientInstance('dns', api_version)

    # Get the managed-zone.
    zone_ref = util.GetRegistry(api_version).Parse(
        args.zone,
        params={
            'project': properties.VALUES.core.project.GetOrFail,
        },
        collection='dns.managedZones')

    try:
      zone = dns.managedZones.Get(
          dns.MESSAGES_MODULE.DnsManagedZonesGetRequest(
              project=zone_ref.project,
              managedZone=zone_ref.managedZone))
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(error)

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

    # Get the imported record-sets.
    try:
      with open(args.records_file) as import_file:
        if args.zone_file_format:
          imported = import_util.RecordSetsFromZoneFile(
              import_file, zone.dnsName, api_version=api_version)
        else:
          imported = import_util.RecordSetsFromYamlFile(
              import_file, api_version=api_version)
    except Exception as exp:
      msg = (u'unable to read record-sets from specified records-file [{0}] '
             u'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,
                                       zone.dnsName, args.replace_origin_ns,
                                       api_version=api_version)
    if not change:
      msg = u'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(
        dns.MESSAGES_MODULE.DnsChangesCreateRequest(
            change=change,
            managedZone=zone.name,
            project=zone_ref.project))
    change_ref = util.GetRegistry(api_version).Create(
        collection='dns.changes',
        project=zone_ref.project,
        managedZone=zone.name,
        changeId=result.id)
    msg = u'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
    def Run(self, args):
        api_version = util.GetApiFromTrackAndArgs(self.ReleaseTrack(), args)

        if not os.path.exists(args.records_file):
            raise import_util.RecordsFileNotFound(
                'Specified record file [{0}] not found.'.format(
                    args.records_file))
        if os.path.isdir(args.records_file):
            raise import_util.RecordsFileIsADirectory(
                'Specified record file [{0}] is a directory'.format(
                    args.records_file))

        dns = util.GetApiClient(api_version)

        # Get the managed-zone.
        zone_ref = util.GetRegistry(api_version).Parse(
            args.zone,
            params=util.GetParamsForRegistry(api_version, args),
            collection='dns.managedZones')

        try:
            get_request = dns.MESSAGES_MODULE.DnsManagedZonesGetRequest(
                project=zone_ref.project, managedZone=zone_ref.managedZone)

            if api_version == 'v2' and self._IsBetaOrAlpha():
                get_request.location = args.location

            zone = dns.managedZones.Get(get_request)
        except apitools_exceptions.HttpError as error:
            raise calliope_exceptions.HttpException(error)

        # Get the current record-sets.
        current = {}
        list_request = dns.MESSAGES_MODULE.DnsResourceRecordSetsListRequest(
            project=zone_ref.project, managedZone=zone_ref.Name())

        if api_version == 'v2':
            list_request.location = args.location

        for record in list_pager.YieldFromList(dns.resourceRecordSets,
                                               list_request,
                                               field='rrsets'):
            current[(record.name, record.type)] = record

        # Get the imported record-sets.
        try:
            with files.FileReader(args.records_file) as import_file:
                if args.zone_file_format:
                    imported = import_util.RecordSetsFromZoneFile(
                        import_file, zone.dnsName, api_version=api_version)
                else:
                    imported = import_util.RecordSetsFromYamlFile(
                        import_file,
                        include_extended_records=self._IsAlpha(),
                        api_version=api_version)
        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 import_util.UnableToReadRecordsFile(msg)

        # Get the change resulting from the imported record-sets.
        change = import_util.ComputeChange(current,
                                           imported,
                                           args.delete_all_existing,
                                           zone.dnsName,
                                           args.replace_origin_ns,
                                           api_version=api_version)
        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.
        create_request = dns.MESSAGES_MODULE.DnsChangesCreateRequest(
            change=change, managedZone=zone.name, project=zone_ref.project)

        if api_version == 'v2' and self._IsBetaOrAlpha():
            create_request.location = args.location

        result = dns.changes.Create(create_request)
        param = util.GetParamsForRegistry(api_version,
                                          args,
                                          parent='managedZones')
        param['changeId'] = result.id
        change_ref = util.GetRegistry(api_version).Parse(
            line=None, collection='dns.changes', params=param)
        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
Пример #8
0
 def testComputeChangeIdenticalRecordsReplaceAll(self):
     change = import_util.ComputeChange(util_beta.GetImportedRecordSets(),
                                        util_beta.GetImportedRecordSets(),
                                        replace_all=True,
                                        api_version=self.api_version)
     self.assertEqual(None, change)