def Run(self, args): with trans_util.TransactionFile(args.transaction_file) as trans_file: change = trans_util.ChangeFromYamlFile(trans_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) record_to_remove = trans_util.CreateRecordSetFromArgs(args) # Ensure the record to be removed exists zone_ref = resources.Parse(args.zone, collection='dns.managedZones') existing_records = [record for record in list_pager.YieldFromList( dns.resourceRecordSets, messages.DnsResourceRecordSetsListRequest( project=project_id, managedZone=zone_ref.Name(), name=util.AppendTrailingDot(args.name), type=args.type), field='rrsets')] if not existing_records or existing_records[0] != record_to_remove: raise exceptions.ToolException('Record to be removed does not exist') change.deletions.append(record_to_remove) with trans_util.TransactionFile(args.transaction_file, 'w') as trans_file: trans_util.WriteToYamlFile(trans_file, change) log.status.Print( 'Record removal appended to transaction at [{0}].'.format( args.transaction_file))
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 testTransactionExecute(self): with open(transaction_util.DEFAULT_PATH, 'w') as transaction_file: transaction_util.WriteToYamlFile(transaction_file, util.GetImportChange()) test_zone = util.GetManagedZones()[0] self.mocked_dns_v1.changes.Create.Expect( self.messages.DnsChangesCreateRequest( change=util.GetImportChange(), managedZone=test_zone.name, project=self.Project()), util.GetImportChangeAfterCreation()) self.Run('dns record-sets transaction execute -z {0}'.format( test_zone.name)) self.AssertErrContains( 'Executed transaction [{0}] for managed-zone [{1}].'.format( transaction_util.DEFAULT_PATH, test_zone.name)) self.AssertOutputContains("""\ ID START_TIME STATUS 1 today now pending """) self.AssertErrContains("""\ Created [{0}projects/{1}/managedZones/mz/changes/1]. """.format(self.client.BASE_URL, self.Project())) self.assertFalse(os.path.isfile(transaction_util.DEFAULT_PATH))
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' elif self.ReleaseTrack() == base.ReleaseTrack.ALPHA: api_version = 'v1alpha2' with trans_util.TransactionFile(args.transaction_file) as trans_file: change = trans_util.ChangeFromYamlFile(trans_file, api_version=api_version) change.additions.append( rrsets_util.CreateRecordSetFromArgs( args, api_version=api_version, allow_extended_records=( self.ReleaseTrack() == base.ReleaseTrack.ALPHA))) with trans_util.TransactionFile(args.transaction_file, 'w') as trans_file: trans_util.WriteToYamlFile(trans_file, change) log.status.Print( 'Record addition appended to transaction at [{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))
def Run(self, args): with trans_util.TransactionFile(args.transaction_file) as trans_file: change = trans_util.ChangeFromYamlFile(trans_file) change.additions.append(trans_util.CreateRecordSetFromArgs(args)) with trans_util.TransactionFile(args.transaction_file, 'w') as trans_file: trans_util.WriteToYamlFile(trans_file, change) log.status.Print( 'Record addition appended to transaction at [{0}].'.format( args.transaction_file))
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' elif self.ReleaseTrack() == base.ReleaseTrack.ALPHA: api_version = 'v1alpha2' with trans_util.TransactionFile(args.transaction_file) as trans_file: change = trans_util.ChangeFromYamlFile(trans_file, api_version=api_version) dns = util.GetApiClient(api_version) record_to_remove = rrsets_util.CreateRecordSetFromArgs( args, api_version=api_version, allow_extended_records=( self.ReleaseTrack() == base.ReleaseTrack.ALPHA)) # Ensure the record to be removed exists zone_ref = util.GetRegistry(api_version).Parse( args.zone, params={ 'project': properties.VALUES.core.project.GetOrFail, }, collection='dns.managedZones') existing_records = [ record for record in list_pager.YieldFromList( dns.resourceRecordSets, dns.MESSAGES_MODULE.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name(), name=util.AppendTrailingDot(args.name), type=args.type), field='rrsets') ] if not existing_records or existing_records[0] != record_to_remove: raise trans_util.RecordDoesNotExist( 'Record to be removed does not exist') change.deletions.append(record_to_remove) with trans_util.TransactionFile(args.transaction_file, 'w') as trans_file: trans_util.WriteToYamlFile(trans_file, change) log.status.Print( 'Record removal appended to transaction at [{0}].'.format( args.transaction_file))
def Run(self, args): with trans_util.TransactionFile(args.transaction_file) as trans_file: change = trans_util.ChangeFromYamlFile(trans_file) dns = apis.GetClientInstance('dns', 'v1') messages = apis.GetMessagesModule('dns', 'v1') record_to_remove = trans_util.CreateRecordSetFromArgs(args) # Ensure the record to be removed exists zone_ref = resources.REGISTRY.Parse( args.zone, params={ 'project': properties.VALUES.core.project.GetOrFail, }, collection='dns.managedZones') existing_records = [ record for record in list_pager.YieldFromList( dns.resourceRecordSets, messages.DnsResourceRecordSetsListRequest( project=zone_ref.project, managedZone=zone_ref.Name(), name=util.AppendTrailingDot(args.name), type=args.type), field='rrsets') ] if not existing_records or existing_records[0] != record_to_remove: raise exceptions.ToolException( 'Record to be removed does not exist') change.deletions.append(record_to_remove) with trans_util.TransactionFile(args.transaction_file, 'w') as trans_file: trans_util.WriteToYamlFile(trans_file, change) log.status.Print( 'Record removal appended to transaction at [{0}].'.format( args.transaction_file))