def testUpdateLabelsArg(self): args = GetAndValidateArgsTest.Args() args.update_labels = {'a': 'b'} labels_util.GetAndValidateOpsFromArgs(args) diff = labels_util.GetAndValidateOpsFromArgs(args) self.assertEqual(diff._additions['a'], 'b') self.assertFalse(diff._subtractions)
def testRemoveLabelsArg(self): args = GetAndValidateArgsTest.Args() args.remove_labels = ['xyz'] labels_util.GetAndValidateOpsFromArgs(args) diff = labels_util.GetAndValidateOpsFromArgs(args) self.assertFalse(diff._additions) self.assertEqual(diff._subtractions[0], 'xyz')
def Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages snapshot_ref = Update.SnapshotArg.ResolveAsResource( args, holder.resources) labels_diff = labels_util.GetAndValidateOpsFromArgs(args) snapshot = client.snapshots.Get( messages.ComputeSnapshotsGetRequest(**snapshot_ref.AsDict())) labels_update = labels_diff.Apply( messages.GlobalSetLabelsRequest.LabelsValue, snapshot.labels) if not labels_update.needs_update: return snapshot request = messages.ComputeSnapshotsSetLabelsRequest( project=snapshot_ref.project, resource=snapshot_ref.snapshot, globalSetLabelsRequest=messages.GlobalSetLabelsRequest( labelFingerprint=snapshot.labelFingerprint, labels=labels_update.labels)) operation = client.snapshots.SetLabels(request) operation_ref = holder.resources.Parse( operation.selfLink, collection='compute.globalOperations') operation_poller = poller.Poller(client.snapshots) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of snapshot [{0}]'.format(snapshot_ref.Name()))
def Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages disk_ref = self.DISK_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(holder.client)) labels_diff = labels_util.GetAndValidateOpsFromArgs(args) disk_info = api_util.GetDiskInfo(disk_ref, client, messages) disk = disk_info.GetDiskResource() set_label_req = disk_info.GetSetLabelsRequestMessage() labels_update = labels_diff.Apply(set_label_req.LabelsValue, disk.labels) request = disk_info.GetSetDiskLabelsRequestMessage( disk, labels_update.GetOrNone()) if not labels_update.needs_update: return disk service = disk_info.GetService() operation = service.SetLabels(request) operation_ref = holder.resources.Parse( operation.selfLink, collection=disk_info.GetOperationCollection()) operation_poller = poller.Poller(service) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of disk [{0}]'.format(disk_ref.Name()))
def testMissingArguments(self): args = GetAndValidateArgsTest.Args() with self.assertRaisesRegex( calliope_exceptions.RequiredArgumentException, 'At least one of --update-labels, --remove-labels, or ' '--clear-labels must be specified.'): labels_util.GetAndValidateOpsFromArgs(args)
def testUpdateAndRemoveLabelsArg(self): args = GetAndValidateArgsTest.Args() args.update_labels = {'a': 'b'} args.remove_labels = ['xyz'] diff = labels_util.GetAndValidateOpsFromArgs(args) self.assertEqual(diff._additions['a'], 'b') self.assertEqual(diff._subtractions[0], 'xyz')
def Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages disk_ref = self.DISK_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(holder.client)) labels_diff = labels_util.GetAndValidateOpsFromArgs(args) service = self.GetDisksService(disk_ref, client) disk = service.Get(self.GetDiskGetRequest(disk_ref, messages)) set_labels_request = self.GetLabelsReplacementRequest( disk_ref, disk, messages, labels_diff) if not set_labels_request: return disk operation = service.SetLabels(set_labels_request) operation_ref = holder.resources.Parse( operation.selfLink, collection=self.GetOperationCollection(disk_ref)) operation_poller = poller.Poller(service) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of disk [{0}]'.format(disk_ref.Name()))
def _Run(self, args, support_user_licenses=False): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages disk_ref = self.DISK_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(holder.client)) disk_info = api_util.GetDiskInfo(disk_ref, client, messages) service = disk_info.GetService() if support_user_licenses and args.IsSpecified( 'zone') and _UserLicensesFlagsIncluded(args): disk_res = messages.Disk(name=disk_ref.Name()) if args.IsSpecified('update_user_licenses'): disk_res.userLicenses = args.update_user_licenses disk_update_request = messages.ComputeDisksUpdateRequest( project=disk_ref.project, disk=disk_ref.Name(), diskResource=disk_res, zone=disk_ref.zone, paths=['userLicenses']) update_operation = service.Update(disk_update_request) update_operation_ref = holder.resources.Parse( update_operation.selfLink, collection=disk_info.GetOperationCollection()) update_operation_poller = poller.Poller(service) result = waiter.WaitFor( update_operation_poller, update_operation_ref, 'Updating user licenses of disk [{0}]'.format(disk_ref.Name())) if not _LabelsFlagsIncluded(args): return result labels_diff = labels_util.GetAndValidateOpsFromArgs(args) disk = disk_info.GetDiskResource() set_label_req = disk_info.GetSetLabelsRequestMessage() labels_update = labels_diff.Apply(set_label_req.LabelsValue, disk.labels) request = disk_info.GetSetDiskLabelsRequestMessage( disk, labels_update.GetOrNone()) if not labels_update.needs_update: return disk operation = service.SetLabels(request) operation_ref = holder.resources.Parse( operation.selfLink, collection=disk_info.GetOperationCollection()) operation_poller = poller.Poller(service) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of disk [{0}]'.format(disk_ref.Name()))
def _RunUpdate(self, client, messages, original_cert, args): # Collect the list of update masks labels_diff = labels_util.GetAndValidateOpsFromArgs(args) labels_update = labels_diff.Apply(messages.Certificate.LabelsValue, original_cert.labels) if not labels_update.needs_update: raise exceptions.InvalidArgumentException( 'labels', self.NO_CHANGES_MESSAGE.format(certificate=original_cert.name)) original_cert.labels = labels_update.labels return client.projects_locations_caPools_certificates.Patch( messages.PrivatecaProjectsLocationsCaPoolsCertificatesPatchRequest( name=original_cert.name, certificate=original_cert, updateMask='labels', requestId=request_utils.GenerateRequestId()))
def Run(self, args): """Run the update command.""" labels_util.GetAndValidateOpsFromArgs(args) identifiers = args.CONCEPTS.archive_deployment.Parse().AsDict() # First get the existing lables by calling describe on the current archive. existing_archive = apigee.ArchivesClient.Describe(identifiers) # Modify the label set based on provided flag values. if "labels" in existing_archive and not args.clear_labels: new_labels = existing_archive["labels"] else: new_labels = {} if args.update_labels: new_labels.update(args.update_labels) if args.remove_labels: for label in args.remove_labels: if label in new_labels: del new_labels[label] labels_proto = {"labels": new_labels} return apigee.ArchivesClient.Update(identifiers, labels_proto)
def Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages disk_ref = self.DISK_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(holder.client)) labels_diff = labels_util.GetAndValidateOpsFromArgs(args) service = client.disks request_type = messages.ComputeDisksGetRequest disk = service.Get(request_type(**disk_ref.AsDict())) labels_update = labels_diff.Apply( messages.ZoneSetLabelsRequest.LabelsValue, disk.labels) if not labels_update.needs_update: return disk request = messages.ComputeDisksSetLabelsRequest( project=disk_ref.project, resource=disk_ref.disk, zone=disk_ref.zone, zoneSetLabelsRequest=messages.ZoneSetLabelsRequest( labelFingerprint=disk.labelFingerprint, labels=labels_update.labels)) operation = service.SetLabels(request) operation_ref = holder.resources.Parse( operation.selfLink, collection='compute.zoneOperations') operation_poller = poller.Poller(service) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of disk [{0}]'.format(disk_ref.Name()))
def _RunUpdate(self, client, messages, original_cert, args): # Collect the list of update masks labels_diff = labels_util.GetAndValidateOpsFromArgs(args) labels_update = labels_diff.Apply(messages.Certificate.LabelsValue, original_cert.labels) if not labels_update.needs_update: raise exceptions.InvalidArgumentException( 'labels', self.NO_CHANGES_MESSAGE.format(certificate=original_cert.name)) original_cert.labels = labels_update.labels operation = client.projects_locations_certificateAuthorities_certificates.Patch( messages. PrivatecaProjectsLocationsCertificateAuthoritiesCertificatesPatchRequest( name=original_cert.name, certificate=original_cert, updateMask='labels', requestId=six.text_type(uuid.uuid4()))) return operations.Await(operation, 'Updating Certificate.')
def Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages image_ref = self.DISK_IMAGE_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(holder.client)) labels_diff = labels_util.GetAndValidateOpsFromArgs(args) image = client.images.Get( messages.ComputeImagesGetRequest(**image_ref.AsDict())) labels_update = labels_diff.Apply( messages.GlobalSetLabelsRequest.LabelsValue, image.labels) if not labels_update.needs_update: return image request = messages.ComputeImagesSetLabelsRequest( project=image_ref.project, resource=image_ref.image, globalSetLabelsRequest= messages.GlobalSetLabelsRequest( labelFingerprint=image.labelFingerprint, labels=labels_update.labels)) operation = client.images.SetLabels(request) operation_ref = holder.resources.Parse( operation.selfLink, collection='compute.globalOperations') operation_poller = poller.Poller(client.images) return waiter.WaitFor( operation_poller, operation_ref, 'Updating labels of image [{0}]'.format( image_ref.Name()))
def _Run(self, args, patch_enable=False): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client messages = holder.client.messages image_ref = self.DISK_IMAGE_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetDefaultScopeLister(client)) requests = [] result = None # check if need to update labels if patch_enable: # Throws a different error message. labels_diff = labels_util.Diff.FromUpdateArgs(args) else: labels_diff = labels_util.GetAndValidateOpsFromArgs(args) if labels_diff.MayHaveUpdates(): image = holder.client.apitools_client.images.Get( messages.ComputeImagesGetRequest(**image_ref.AsDict())) labels_update = labels_diff.Apply( messages.GlobalSetLabelsRequest.LabelsValue, image.labels) if labels_update.needs_update: request = messages.ComputeImagesSetLabelsRequest( project=image_ref.project, resource=image_ref.image, globalSetLabelsRequest= messages.GlobalSetLabelsRequest( labelFingerprint=image.labelFingerprint, labels=labels_update.labels)) requests.append((client.apitools_client.images, 'SetLabels', request)) if patch_enable: should_patch = False image_resource = messages.Image() if args.IsSpecified('family'): image_resource.family = args.family should_patch = True if args.IsSpecified('description'): image_resource.description = args.description should_patch = True if should_patch: request = messages.ComputeImagesPatchRequest( project=image_ref.project, imageResource=image_resource, image=image_ref.Name()) requests.append((client.apitools_client.images, 'Patch', request)) errors_to_collect = [] result = client.BatchRequests(requests, errors_to_collect) if errors_to_collect: raise exceptions.MultiError(errors_to_collect) if result: log.status.Print('Updated [{0}].'.format(image_ref)) return result