def GetConnectionContext(args): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.IsKubernetes(args): kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, args.context) if flags.IsGKE(args): cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref) if flags.IsManaged(args): region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') return _RegionalConnectionContext(region)
def GetConnectionContext(args, product=flags.Product.RUN, release_track=base.ReleaseTrack.GA, version_override=None, platform=None): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. product: Which product is requesting connection context. release_track: Release track of the command being run. version_override: If specified, the given api version will be used no matter the other parameters. platform: 'gke', 'kubernetes', or 'managed'. If not specified, the value of the --platform flag will be used instead. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if platform is None: platform = flags.GetPlatform() if platform == flags.PLATFORM_KUBERNETES: kubeconfig = flags.GetKubeconfig(args) api_name = _GetApiName(product, release_track, is_cluster=True) api_version = _GetApiVersion(product, release_track, is_cluster=True, version_override=version_override) return _KubeconfigConnectionContext(kubeconfig, api_name, api_version, args.context) if platform == flags.PLATFORM_GKE: cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') api_name = _GetApiName(product, release_track, is_cluster=True) api_version = _GetApiVersion(product, release_track, is_cluster=True, version_override=version_override) return _GKEConnectionContext(cluster_ref, api_name, api_version) if platform == flags.PLATFORM_MANAGED: region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') api_name = _GetApiName(product, release_track) api_version = _GetApiVersion(product, release_track, version_override=version_override) return _RegionalConnectionContext(region, api_name, api_version)
def Run(self, args): """List available routes.""" if args.uri: raise flags.ArgumentError('--uri flag is not supported for this resource') conn_context = connection_context.GetConnectionContext(args) namespace_ref = args.CONCEPTS.namespace.Parse() with serverless_operations.Connect(conn_context) as client: return client.ListRoutes(namespace_ref)
def Run(self, args): """Obtain details about a given route.""" conn_context = connection_context.GetConnectionContext(args) route_ref = args.CONCEPTS.route.Parse() with serverless_operations.Connect(conn_context) as client: conf = client.GetRoute(route_ref) if not conf: raise flags.ArgumentError('Cannot find route [{}]'.format( route_ref.routesId)) return conf
def Run(self, args): """Obtain details about a given service.""" conn_context = connection_context.GetConnectionContext(args) service_ref = flags.GetService(args) with serverless_operations.Connect(conn_context) as client: serv = client.GetService(service_ref) if not serv: raise flags.ArgumentError('Cannot find service [{}]'.format( service_ref.servicesId)) return serv
def Run(self, args): """Obtain details about a given configuration.""" conn_context = connection_context.GetConnectionContext( args, product=connection_context.Product.RUN) configuration_ref = args.CONCEPTS.configuration.Parse() with serverless_operations.Connect(conn_context) as client: conf = client.GetConfiguration(configuration_ref) if not conf: raise flags.ArgumentError('Cannot find configuration [{}]'.format( configuration_ref.configurationsId)) return conf
def Run(self, args): """Describe a domain mapping.""" conn_context = connection_context.GetConnectionContext(args) domain_mapping_ref = args.CONCEPTS.domain.Parse() with serverless_operations.Connect(conn_context) as client: domain_mapping = client.GetDomainMapping(domain_mapping_ref) if not domain_mapping: raise flags.ArgumentError( 'Cannot find domain mapping for domain name [{}]'.format( domain_mapping_ref.domainmappingsId)) return domain_mapping
def _Call(self, parsed_args): if flags.IsGKE(parsed_args) or flags.IsKubernetes(parsed_args): return 'default' elif not (getattr(parsed_args, 'project', None) or properties.VALUES.core.project.Get()): # HACK: Compensate for how "namespace" is actually "project" in Cloud Run # by providing an error message explicitly early here. raise flags.ArgumentError( 'The [project] resource is not properly specified. ' 'Please specify the argument [--project] on the command line or ' 'set the property [core/project].') return None
def Run(self, args): """Show details about a revision.""" conn_context = connection_context.GetConnectionContext(args) revision_ref = args.CONCEPTS.revision.Parse() with serverless_operations.Connect(conn_context) as client: wrapped_revision = client.GetRevision(revision_ref) if not wrapped_revision: raise flags.ArgumentError('Cannot find revision [{}]'.format( revision_ref.revisionsId)) return wrapped_revision
def GetConnectionContext(args, product=Product.RUN): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. product: Which product is requesting connection context. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.GetPlatform() == flags.PLATFORM_KUBERNETES: kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, _CLUSTER_API_VERSION, args.context) if flags.GetPlatform() == flags.PLATFORM_GKE: cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref, _CLUSTER_API_VERSION) if flags.GetPlatform() == flags.PLATFORM_MANAGED: region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') if product == Product.RUN: version = global_methods.SERVERLESS_API_VERSION elif product == Product.EVENTS: version = _EVENTS_API_VERSION else: raise ValueError('Unrecognized product: ' + six.u(product)) return _RegionalConnectionContext(region, version)
def Run(self, args): """List available services.""" if args.uri: raise flags.ArgumentError( '--uri flag is not supported for this resource') if not flags.IsGKE(args) and not getattr(args, 'region', None): locations_ref = args.CONCEPTS.region.Parse() return global_methods.ListServices(locations_ref.RelativeName()) else: conn_context = connection_context.GetConnectionContext(args) namespace_ref = args.CONCEPTS.namespace.Parse() with serverless_operations.Connect(conn_context) as client: return client.ListServices(namespace_ref)
def GetConnectionContext(args, track=base.ReleaseTrack.BETA): """Gets the regional, kubeconfig, or GKE connection context. Args: args: Namespace, the args namespace. track: ReleaseTrack, the release track. Raises: ArgumentError if region or cluster is not specified. Returns: A GKE or regional ConnectionInfo object. """ if flags.IsKubernetes(args): kubeconfig = flags.GetKubeconfig(args) return _KubeconfigConnectionContext(kubeconfig, args.context) if flags.IsGKE(args): cluster_ref = args.CONCEPTS.cluster.Parse() if not cluster_ref: raise flags.ArgumentError( 'You must specify a cluster in a given location. ' 'Either use the `--cluster` and `--cluster-location` flags ' 'or set the run/cluster and run/cluster_location properties.') return _GKEConnectionContext(cluster_ref) if flags.IsManaged(args): region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify a region. Either use the `--region` flag ' 'or set the run/region property.') if track == base.ReleaseTrack.ALPHA: version = 'v1' else: version = global_methods.SERVERLESS_API_VERSION return _RegionalConnectionContext(region, version)
def Run(self, args): """Obtain details about a given service.""" # TODO(b/143898356) Begin code that should be in Args resource_printer.RegisterFormatter( service_printer.SERVICE_PRINTER_FORMAT, service_printer.ServicePrinter) args.GetDisplayInfo().AddFormat('service') # End code that should be in Args conn_context = connection_context.GetConnectionContext( args, flags.Product.RUN, self.ReleaseTrack()) service_ref = flags.GetService(args) with serverless_operations.Connect(conn_context) as client: serv = client.GetService(service_ref) if not serv: raise flags.ArgumentError('Cannot find service [{}]'.format( service_ref.servicesId)) return serv
def Run(self, args): """Describe 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 flags.GetPlatform() != flags.PLATFORM_MANAGED else None)) domain_mapping_ref = args.CONCEPTS.domain.Parse() with serverless_operations.Connect(conn_context) as client: domain_mapping = client.GetDomainMapping(domain_mapping_ref) if not domain_mapping: raise flags.ArgumentError( 'Cannot find domain mapping for domain name [{}]'.format( domain_mapping_ref.domainmappingsId)) return domain_mapping
def Run(self, args): """Show details about a revision.""" # TODO(b/143898356) Begin code that should be in Args resource_printer.RegisterFormatter( revision_printer.REVISION_PRINTER_FORMAT, revision_printer.RevisionPrinter) args.GetDisplayInfo().AddFormat(revision_printer.REVISION_PRINTER_FORMAT) # End code that should be in Args conn_context = connection_context.GetConnectionContext( args, flags.Product.RUN, self.ReleaseTrack()) revision_ref = args.CONCEPTS.revision.Parse() with serverless_operations.Connect(conn_context) as client: wrapped_revision = client.GetRevision(revision_ref) if not wrapped_revision: raise flags.ArgumentError( 'Cannot find revision [{}]'.format(revision_ref.revisionsId)) return wrapped_revision
def GetConnectionContext(args): """Gets the regional or GKE connection context. Args: args: Namespace, the args namespace. Raises: ConfigurationError if cluster is specified without a location. Returns: A GKE or regional ConnectionInfo object. """ if flags.ValidateIsGKE(args): cluster_ref = args.CONCEPTS.cluster.Parse() return _GKEConnectionContext(cluster_ref) region = flags.GetRegion(args, prompt=True) if not region: raise flags.ArgumentError( 'You must specify either a cluster or a region.') return _RegionalConnectionContext(region)
def _ParseResourcesParameters(args): """Parses --resources flag.""" resources_flag = getattr(args, _RESOURCES_FLAG_NAME) if resources_flag is None: return {} result = [] for api_version_kind_string in resources_flag: avk_selector = api_version_kind_string.split(':') if len(avk_selector) != 3 and len(avk_selector) != 2: raise run_flags.ArgumentError( 'parameter flag resources expects Kind:' 'ApiVersion:LabelName=value, notation.') elif len(avk_selector) == 2: result.append({ 'kind': avk_selector[0], 'apiVersion': avk_selector[1], }) elif len(avk_selector) == 3: match_labels_obj = {} for match_labels_string in avk_selector[2].split(','): key, value = match_labels_string.split('=') match_labels_obj[key] = value result.append({ 'kind': avk_selector[0], 'apiVersion': avk_selector[1], 'selector': { 'matchLabels': match_labels_obj, } }) resources_params = {} if result: resources_params['resources'] = result return resources_params
def error(self, message='', context=None, reproduce=False): del context, reproduce raise flags.ArgumentError(message)
def _AllowedKeyString(key): if key in _DISALLOWED_SPECIAL_KEYS: raise run_flags.ArgumentError('Filter {} not allowed'.format(key)) return key