def testWriteToZoneFile(self): with open(self.result_file_path, 'w') as zone_file: export_util.WriteToZoneFile(zone_file, util.GetRecordSetsForExport(), 'zone.com.') with open(self.result_file_path) as results: with open(self.zone_file_path) as expected: self.assertEqual(expected.read().splitlines(), results.read().splitlines())
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 self.ReleaseTrack() == base.ReleaseTrack.ALPHA: api_version = 'v1alpha2' dns = util.GetApiClient(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 calliope_exceptions.HttpException(error) # Get all the record-sets. record_sets = [] for record_set in list_pager.YieldFromList( dns.resourceRecordSets, dns.MESSAGES_MODULE.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name()), field='rrsets'): record_sets.append(record_set) # Export the record-sets. try: with files.FileWriter(args.records_file) 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 export_util.UnableToExportRecordsToFile(msg) log.status.Print('Exported record-sets to [{0}].'.format( args.records_file))
def Run(self, args): dns = self.context['dns_client'] messages = self.context['dns_messages'] resources = self.context['dns_resources'] # Get the managed-zone. zone_ref = resources.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 all the record-sets. record_sets = [] for record_set in list_pager.YieldFromList( dns.resourceRecordSets, messages.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name()), field='rrsets'): record_sets.append(record_set) # Export the record-sets. try: with 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))
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))
def Run(self, args): api_version = util.GetApiFromTrackAndArgs(self.ReleaseTrack(), args) 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 all the record-sets. record_sets = [] list_request = dns.MESSAGES_MODULE.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name()) if api_version == 'v2' and self._IsBetaOrAlpha(): list_request.location = args.location for record_set in list_pager.YieldFromList(dns.resourceRecordSets, list_request, field='rrsets'): # Alpha is handled differently as it supports ALIAS records. if self._IsAlpha(): # For BIND file format, ALIAS record sets must be ignored, as they are # not DNS standards. A zone will have at most one ALIAS record. if args.zone_file_format: if record_set.type == 'ALIAS': log.warning( 'Skippig ALIAS record found in zone, as ALIAS record are custom' ' to Cloud DNS and do not have a standard BIND format. To ' 'export ALIAS records, use YAML format instead.') continue else: # beta or GA # Quietly ignore ALIAS records in beta/GA - they aren't supported yet. if record_set.type == 'ALIAS': continue record_sets.append(record_set) # Export the record-sets. try: with files.FileWriter(args.records_file) 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 export_util.UnableToExportRecordsToFile(msg) log.status.Print('Exported record-sets to [{0}].'.format( args.records_file))