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 = 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
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 testRecordSetsFromYamlFile(self): yaml_file = io.open(self.yaml_file_path, mode='rt') rsets = import_util.RecordSetsFromYamlFile(yaml_file) self.assertEqual(util.GetImportedRecordSets(), rsets) yaml_file.close()