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 os.path.isfile(args.transaction_file): raise transaction_util.TransactionFileAlreadyExists( 'Transaction already exists at [{0}]'.format( args.transaction_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 calliope_exceptions.HttpException(error) # Initialize an empty change change = dns.MESSAGES_MODULE.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 list_pager.YieldFromList( dns.resourceRecordSets, dns.MESSAGES_MODULE.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name(), name=zone.dnsName, type='SOA'), field='rrsets') ] change.deletions.append(records[0]) change.additions.append( import_util.NextSOARecordSet(records[0], api_version=api_version)) # Write change to transaction file try: with files.FileWriter(args.transaction_file) 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 transaction_util.UnableToAccessTransactionFile(msg) log.status.Print('Transaction started [{0}].'.format( args.transaction_file))
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'] # 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) # 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 list_pager.YieldFromList( dns.resourceRecordSets, messages.DnsResourceRecordSetsListRequest( project=zone_ref.project, 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 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))