def _isDeploymentInPreview(self, args): deployment = dm_api_util.FetchDeployment(self.client, self.messages, dm_base.GetProject(), args.deployment) if deployment.update: return True return False
def Run(self, args): """Run 'deployments update'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: If --async=true, returns Operation to poll. Else, returns a struct containing the list of resources and list of outputs in the deployment. Raises: HttpException: An http error response was received while executing api request. """ deployment_ref = self.resources.Parse( args.deployment_name, params={'project': properties.VALUES.core.project.GetOrFail}, collection='deploymentmanager.deployments') if not args.IsSpecified('format') and args. async: args.format = flags.OPERATION_FORMAT patch_request = False deployment = self.messages.Deployment(name=deployment_ref.deployment, ) if not (args.config is None and args.template is None and args.composite_type is None): deployment.target = importer.BuildTargetConfig( self.messages, config=args.config, template=args.template, composite_type=args.composite_type, properties=args.properties) elif (self.ReleaseTrack() in [base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA] and args.manifest_id): deployment.target = importer.BuildTargetConfigFromManifest( self.client, self.messages, dm_base.GetProject(), deployment_ref.deployment, args.manifest_id, args.properties) # Get the fingerprint from the deployment to update. try: current_deployment = self.client.deployments.Get( self.messages.DeploymentmanagerDeploymentsGetRequest( project=dm_base.GetProject(), deployment=deployment_ref.deployment)) if args.fingerprint: deployment.fingerprint = dm_util.DecodeFingerprint( args.fingerprint) else: # If no fingerprint is present, default to an empty fingerprint. # TODO(b/34966984): Remove the empty default after cleaning up all # deployments that has no fingerprint deployment.fingerprint = current_deployment.fingerprint or b'' # Get the credential from the deployment to update. if self.ReleaseTrack() in [base.ReleaseTrack.ALPHA ] and args.credential: deployment.credential = dm_util.CredentialFrom( self.messages, args.credential) # Update the labels of the deployment deployment.labels = self._GetUpdatedDeploymentLabels( args, current_deployment) # If no config or manifest_id are specified, but try to update labels, # only add patch_request header when directly updating a non-previewed # deployment no_manifest = (self.ReleaseTrack() is base.ReleaseTrack.GA) or not args.manifest_id patch_request = not args.config and no_manifest and (bool( args.update_labels) or bool(args.remove_labels)) if args.description is None: deployment.description = current_deployment.description elif not args.description or args.description.isspace(): deployment.description = None else: deployment.description = args.description except apitools_exceptions.HttpError as error: raise exceptions.HttpException(error, dm_api_util.HTTP_ERROR_FORMAT) if patch_request: args.format = flags.DEPLOYMENT_FORMAT try: # Necessary to handle API Version abstraction below parsed_delete_flag = Update._delete_policy_flag_map.GetEnumForChoice( args.delete_policy).name if self.ReleaseTrack() in [ base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA ]: parsed_create_flag = ( Update._create_policy_v2beta_flag_map.GetEnumForChoice( args.create_policy).name) else: parsed_create_flag = ( Update._create_policy_flag_map.GetEnumForChoice( args.create_policy).name) request = self.messages.DeploymentmanagerDeploymentsUpdateRequest( deploymentResource=deployment, project=dm_base.GetProject(), deployment=deployment_ref.deployment, preview=args.preview, createPolicy=( self.messages.DeploymentmanagerDeploymentsUpdateRequest. CreatePolicyValueValuesEnum(parsed_create_flag)), deletePolicy=( self.messages.DeploymentmanagerDeploymentsUpdateRequest. DeletePolicyValueValuesEnum(parsed_delete_flag))) client = self.client client.additional_http_headers['X-Cloud-DM-Patch'] = patch_request operation = client.deployments.Update(request) # Fetch and print the latest fingerprint of the deployment. updated_deployment = dm_api_util.FetchDeployment( self.client, self.messages, dm_base.GetProject(), deployment_ref.deployment) if patch_request: if args. async: log.warning( 'Updating Deployment metadata is synchronous, --async flag ' 'is ignored.') log.status.Print( 'Update deployment metadata completed successfully.') return updated_deployment dm_util.PrintFingerprint(updated_deployment.fingerprint) except apitools_exceptions.HttpError as error: raise exceptions.HttpException(error, dm_api_util.HTTP_ERROR_FORMAT) if args. async: return operation else: op_name = operation.name try: operation = dm_write.WaitForOperation( self.client, self.messages, op_name, 'update', dm_base.GetProject(), timeout=OPERATION_TIMEOUT) dm_util.LogOperationStatus(operation, 'Update') except apitools_exceptions.HttpError as error: raise exceptions.HttpException(error, dm_api_util.HTTP_ERROR_FORMAT) return dm_api_util.FetchResourcesAndOutputs( self.client, self.messages, dm_base.GetProject(), deployment_ref.deployment, self.ReleaseTrack() is base.ReleaseTrack.ALPHA)