def _CreateProject(project_id, project_ids): """Create a project and check that it isn't in the known project IDs.""" if project_ids and project_id in project_ids: raise ValueError('Attempting to create a project that already exists.') project_ref = resources.REGISTRY.Create('cloudresourcemanager.projects', projectId=project_id) try: create_op = projects_api.Create(project_ref) except Exception as err: # pylint: disable=broad-except log.warning( 'Project creation failed: {err}\n' 'Please make sure to create the project [{project}] using\n' ' $ gcloud projects create {project}\n' 'or change to another project using\n' ' $ gcloud config set project <PROJECT ID>'.format( err=six.text_type(err), project=project_id)) return None # wait for async Create operation to complete and check the status. try: create_op = operations.WaitForOperation(create_op) except operations.OperationFailedException as err: log.warning( 'Project creation for project [{project}] failed:\n {err}'.format( err=six.text_type(err), project=project_id)) return None return project_id
def SetUp(self): self.name_generator = e2e_utils.GetResourceNameGenerator('sdk-e2e') project_name = next(self.name_generator) self.project_id = command_lib_util.IdFromName(project_name) self.project_ref = command_lib_util.ParseProject(self.project_id) create_op = projects_api.Create( self.project_ref, parent=projects_api.ParentNameToResourceId( CLOUD_SDK_TESTING_FOLDER_ID)) log.CreatedResource(self.project_ref, is_async=True) operations.WaitForOperation(create_op) log.debug('Enabling cloudapis.googleapis.com') services_enable_api.EnableService(self.project_ref.Name(), 'cloudapis.googleapis.com') self.Run('services enable cloudbilling') self.Run(('alpha billing accounts projects link {project} ' '--account-id={billing}').format(project=self.project_id, billing=self.BillingId())) self.Run('projects add-iam-policy-binding {project} ' '--member="group:[email protected]" ' '--role="roles/owner"'.format(project=self.project_id)) properties.VALUES.core.disable_prompts.Set(True) # This is set to false by sdk_test_base.SdkBase. properties.VALUES.core.should_prompt_to_enable_api.Set(True) # The api enablement check will prompt, and this will inject a yes into that self.StartPatch( 'googlecloudsdk.core.console.console_io.PromptContinue', return_value=True)
def Run(self, args): """Default Run method implementation.""" flags.CheckParentFlags(args, parent_required=False) project_id = args.id if not project_id and args.name: candidate = command_lib_util.IdFromName(args.name) if candidate and console_io.PromptContinue( 'No project id provided.', 'Use [{}] as project id'.format(candidate), throw_if_unattended=True): project_id = candidate if not project_id: raise exceptions.RequiredArgumentException( 'PROJECT_ID', 'an id must be provided for the new project') project_ref = command_lib_util.ParseProject(project_id) labels = labels_util.ParseCreateArgs( args, projects_util.GetMessages().Project.LabelsValue) try: create_op = projects_api.Create( project_ref, display_name=args.name, parent=projects_api.ParentNameToResourceId( flags.GetParentFromFlags(args)), labels=labels) except apitools_exceptions.HttpConflictError: msg = ( 'Project creation failed. The project ID you specified is ' 'already in use by another project. Please try an alternative ' 'ID.') core_exceptions.reraise(exceptions.HttpException(msg)) log.CreatedResource(project_ref, is_async=True) create_op = operations.WaitForOperation(create_op) # Enable cloudapis.googleapis.com if args.enable_cloud_apis: log.debug('Enabling cloudapis.googleapis.com') services_client = apis.GetClientInstance('servicemanagement', 'v1') enable_operation = services_enable_api.EnableServiceApiCall( project_ref.Name(), 'cloudapis.googleapis.com') enable_operation_ref = resources.REGISTRY.Parse( enable_operation.name, collection='servicemanagement.operations') services_util.WaitForOperation(enable_operation_ref, services_client) if args.set_as_default: project_property = properties.FromString('core/project') properties.PersistProperty(project_property, project_id) log.status.Print( 'Updated property [core/project] to [{0}].'.format(project_id)) return operations.ExtractOperationResponse( create_op, apis.GetMessagesModule('cloudresourcemanager', 'v1').Project)
def Run(self, args): flags.CheckParentFlags(args) messages = folders.FoldersMessages() operation = folders.FoldersService().Create( messages.CloudresourcemanagerFoldersCreateRequest( parent=flags.GetParentFromFlags(args), folder=messages.Folder(displayName=args.display_name))) if args. async: return operation else: finished_operation = operations.WaitForOperation(operation) result = operations.ExtractOperationResponse( finished_operation, messages.Folder) log.CreatedResource(result)
def Run(self, args): flags.CheckParentFlags(args) messages = folders.FoldersMessages() move_request = messages.CloudresourcemanagerFoldersMoveRequest( foldersId=args.id, moveFolderRequest=messages.MoveFolderRequest( destinationParent=flags.GetParentFromFlags(args))) operation = folders.FoldersService().Move(move_request) if args. async: return operation else: finished_op = operations.WaitForOperation(operation) result = operations.ExtractOperationResponse( finished_op, messages.Folder) log.UpdatedResource(result)
def Run(self, args): flags.CheckParentFlags(args, parent_required=False) project_ref = command_lib_util.ParseProject(args.id) try: create_op = projects_api.Create( project_ref, display_name=args.name, parent=projects_api.ParentNameToResourceId( flags.GetParentFromFlags(args)), update_labels=labels_util.GetUpdateLabelsDictFromArgs(args)) except apitools_exceptions.HttpError as error: if error.status_code == httplib.CONFLICT: msg = ( 'Project creation failed. The project ID you specified is ' 'already in use by another project. Please try an alternative ' 'ID.') unused_type, unused_value, traceback = sys.exc_info() raise exceptions.HttpException, msg, traceback raise log.CreatedResource(project_ref, async=True) create_op = operations.WaitForOperation(create_op) # Enable cloudapis.googleapis.com if args.enable_cloud_apis: log.debug('Enabling cloudapis.googleapis.com') services_client = apis.GetClientInstance('servicemanagement', 'v1') enable_operation = services_enable_api.EnableServiceApiCall( project_ref.Name(), 'cloudapis.googleapis.com') services_util.WaitForOperation(enable_operation.name, services_client) # TODO(user): Retry in case it failed? return operations.ExtractOperationResponse( create_op, apis.GetMessagesModule('cloudresourcemanager', 'v1').Project)