def Run(self, args): """Create a domain mapping.""" conn_context = connection_context.GetConnectionContext( args, product=flags.Product.RUN) domain_mapping_ref = args.CONCEPTS.domain.Parse() # Check if the provided domain has already been verified # if mapping to a non-CRoGKE service if flags.GetPlatform() == flags.PLATFORM_MANAGED: client = global_methods.GetServerlessClientInstance() all_domains = global_methods.ListVerifiedDomains( client, flags.GetRegion(args)) # If not already verified, explain and error out if all(d.id not in domain_mapping_ref.Name() for d in all_domains): if not all_domains: domains_text = 'You currently have no verified domains.' else: domains = ['* {}'.format(d.id) for d in all_domains] domains_text = ('Currently verified domains:\n{}'.format( '\n'.join(domains))) raise exceptions.DomainMappingCreationError( 'The provided domain does not appear to be verified ' 'for the current account so a domain mapping ' 'cannot be created. Visit [{help}] for more information.' '\n{domains}'.format(help=DOMAIN_MAPPINGS_HELP_DOCS_URL, domains=domains_text)) with serverless_operations.Connect(conn_context) as client: mapping = client.CreateDomainMapping(domain_mapping_ref, args.service, args.force_override) for record in mapping.records: record.name = record.name or mapping.route_name return mapping.records
def Run(self, args): """List available services.""" is_managed = flags.GetPlatform() == flags.PLATFORM_MANAGED if is_managed and not args.IsSpecified('region'): self._SetFormat(args, show_region=True) client = global_methods.GetServerlessClientInstance() self.SetPartialApiEndpoint(client.url) args.CONCEPTS.namespace.Parse() # Error if no proj. # Don't consider region property here, we'll default to all regions return commands.SortByName(global_methods.ListServices(client)) else: conn_context = connection_context.GetConnectionContext( args, product=flags.Product.RUN) self._SetFormat(args, show_region=is_managed, show_namespace=(not is_managed)) namespace_ref = args.CONCEPTS.namespace.Parse() with serverless_operations.Connect(conn_context) as client: self.SetCompleteApiEndpoint(conn_context.endpoint) if not is_managed: location_msg = ' in [{}]'.format( conn_context.cluster_location) log.status.Print('For cluster [{cluster}]{zone}:'.format( cluster=conn_context.cluster_name, zone=location_msg if conn_context.cluster_location else '')) return commands.SortByName(client.ListServices(namespace_ref))
def GetRegion(args, prompt=False): """Prompt for region if not provided. Region is decided in the following order: - region argument; - run/region gcloud config; - compute/region gcloud config; - prompt user. Args: args: Namespace, The args namespace. prompt: bool, whether to attempt to prompt. Returns: A str representing region. """ if getattr(args, 'region', None): return args.region if properties.VALUES.run.region.IsExplicitlySet(): return properties.VALUES.run.region.Get() if properties.VALUES.compute.region.IsExplicitlySet(): return properties.VALUES.compute.region.Get() if prompt and console_io.CanPrompt(): client = global_methods.GetServerlessClientInstance() all_regions = global_methods.ListRegions(client) idx = console_io.PromptChoice(all_regions, message='Please specify a region:\n', cancel_option=True) region = all_regions[idx] # set the region on args, so we're not embarassed the next time we call # GetRegion args.region = region log.status.Print('To make this the default region, run ' '`gcloud config set run/region {}`.\n'.format(region)) return region
def Run(self, args): """List available services.""" is_managed = flags.IsManaged(args) if is_managed and not getattr(args, 'region', None): self._SetFormat(args, show_region=True) client = global_methods.GetServerlessClientInstance() self.SetPartialApiEndpoint(client.url) args.CONCEPTS.namespace.Parse() # Error if no proj. locations_ref = args.CONCEPTS.region.Parse() return commands.SortByName( global_methods.ListServices(client, locations_ref.RelativeName())) else: conn_context = connection_context.GetConnectionContext(args) self._SetFormat(args, show_region=is_managed, show_namespace=(not is_managed)) namespace_ref = args.CONCEPTS.namespace.Parse() with serverless_operations.Connect(conn_context) as client: self.SetCompleteApiEndpoint(conn_context.endpoint) if not is_managed: location_msg = ' in [{}]'.format( conn_context.cluster_location) log.status.Print('For cluster [{cluster}]{zone}:'.format( cluster=conn_context.cluster_name, zone=location_msg if conn_context.cluster_location else '')) return commands.SortByName(client.ListServices(namespace_ref))
def Run(self, args): try: project = properties.VALUES.core.project.Get() client = api_client.GetApiClientForTrack(self.ReleaseTrack()) return client.ListVerifiedDomains() # Note: the domain user-verified listing API is availible through two # routes, one App Engine and one Cloud Run. The command should work if the # user has *either* API activated. The following falls back to Cloud Run # if the user does not have App Engine activated. except (apitools_exceptions.HttpNotFoundError, apitools_exceptions.HttpForbiddenError) as appengine_err: try: run_client = run_methods.GetServerlessClientInstance() return run_methods.ListVerifiedDomains(run_client) except (apitools_exceptions.HttpNotFoundError, apitools_exceptions.HttpForbiddenError): log.error( 'To list user-verified domains, you must activate either' ' the App Engine or Cloud Run API and have read permissions ' 'on one of them.') log.error('To activate App Engine, visit:') log.error( 'https://console.cloud.google.com/apis/api/' 'appengine.googleapis.com/overview?project={}'.format( project)) log.error('To activate Cloud Run, visit:') log.error( 'https://console.cloud.google.com/apis/api/' 'run.googleapis.com/overview?project={}'.format(project)) raise appengine_err
def Run(self, args): """Create a domain mapping.""" # domains.cloudrun.com api group only supports v1alpha1 on clusters. conn_context = connection_context.GetConnectionContext( args, flags.Product.RUN, self.ReleaseTrack(), version_override=('v1alpha1' if platforms.GetPlatform() != platforms.PLATFORM_MANAGED else None)) domain_mapping_ref = args.CONCEPTS.domain.Parse() changes = [ config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack()) ] # Check if the provided domain has already been verified # if mapping to a non-CRoGKE service if platforms.GetPlatform() == platforms.PLATFORM_MANAGED: client = global_methods.GetServerlessClientInstance() all_domains = global_methods.ListVerifiedDomains(client) # If not already verified, explain and error out if all(d.id not in domain_mapping_ref.Name() for d in all_domains): if not all_domains: domains_text = 'You currently have no verified domains.' else: domains = ['* {}'.format(d.id) for d in all_domains] domains_text = ('Currently verified domains:\n{}'.format( '\n'.join(domains))) raise exceptions.DomainMappingCreationError( 'The provided domain does not appear to be verified ' 'for the current account so a domain mapping ' 'cannot be created. Visit [{help}] for more information.' '\n{domains}'.format(help=DOMAIN_MAPPINGS_HELP_DOCS_URL, domains=domains_text)) with serverless_operations.Connect(conn_context) as client: try: mapping = client.CreateDomainMapping(domain_mapping_ref, args.service, changes, args.force_override) except exceptions.DomainMappingAlreadyExistsError as e: if console_io.CanPrompt() and console_io.PromptContinue( ('This domain is already being used as a mapping elsewhere. ' 'The existing mapping can be overriden by passing ' '`--force-override` or by continuing at the prompt below.' ), prompt_string='Override the existing mapping'): deletion.Delete(domain_mapping_ref, client.GetDomainMapping, client.DeleteDomainMapping, async_=False) mapping = client.CreateDomainMapping( domain_mapping_ref, args.service, changes, True) else: raise e for record in mapping.records: record.name = record.name or mapping.route_name return mapping.records
def Run(self, args): """List available services.""" if not flags.ValidateIsGKE(args) and not getattr(args, 'region', None): client = global_methods.GetServerlessClientInstance() self.SetPartialApiEndpoint(client.url) locations_ref = args.CONCEPTS.region.Parse() return global_methods.ListServices(client, locations_ref.RelativeName()) else: conn_context = connection_context.GetConnectionContext(args) namespace_ref = args.CONCEPTS.namespace.Parse() with serverless_operations.Connect(conn_context) as client: self.SetCompleteApiEndpoint(conn_context.endpoint) return client.ListServices(namespace_ref)
def Run(self, args): """List jobs.""" # Use the mixer for global request if there's no --region flag. namespace_ref = args.CONCEPTS.namespace.Parse() if not args.IsSpecified('region'): client = global_methods.GetServerlessClientInstance( api_version='v1') self.SetPartialApiEndpoint(client.url) # Don't consider region property here, we'll default to all regions return commands.SortByName( global_methods.ListJobs(client, namespace_ref)) conn_context = connection_context.GetConnectionContext( args, flags.Product.RUN, self.ReleaseTrack()) with serverless_operations.Connect(conn_context) as client: self.SetCompleteApiEndpoint(conn_context.endpoint) return commands.SortByName(client.ListJobs(namespace_ref))
def PromptForRegion(): """Prompt for region from list of available regions. This method is referenced by the declaritive iam commands as a fallthrough for getting the region. Returns: The region specified by the user, str """ if console_io.CanPrompt(): client = global_methods.GetServerlessClientInstance() all_regions = global_methods.ListRegions(client) idx = console_io.PromptChoice( all_regions, message='Please specify a region:\n', cancel_option=True) region = all_regions[idx] log.status.Print('To make this the default region, run ' '`gcloud config set run/region {}`.\n'.format(region)) return region
def Run(self, args): """List available routes.""" client = global_methods.GetServerlessClientInstance() self.SetPartialApiEndpoint(client.url) return global_methods.ListLocations(client)