def Lister(*unused_args): """Returns a list of the zones for a given region.""" if region: filter_expr = 'name eq {0}.*'.format(region) else: filter_expr = None errors = [] global_resources = lister.GetGlobalResources( service=compute_client.apitools_client.zones, project=project, filter_expr=filter_expr, http=compute_client.apitools_client.http, batch_url=compute_client.batch_url, errors=errors) choices = [resource for resource in global_resources] if errors or not choices: punctuation = ':' if errors else '.' utils.RaiseToolException( errors, 'Unable to fetch a list of zones. Specifying [{0}] may fix this ' 'issue{1}'.format(', or '.join(flag_names), punctuation)) return {compute_scope.ScopeEnum.ZONE: choices}
def FetchChoiceResources(self, attribute, service, flag_names, prefix_filter=None): """Returns a list of choices used to prompt with.""" if prefix_filter: filter_expr = 'name eq {0}.*'.format(prefix_filter) else: filter_expr = None errors = [] global_resources = lister.GetGlobalResources(service=service, project=self.project, filter_expr=filter_expr, http=self.http, batch_url=self.batch_url, errors=errors) choices = [resource for resource in global_resources] if errors or not choices: punctuation = ':' if errors else '.' utils.RaiseToolException( errors, 'Unable to fetch a list of {0}s. Specifying [{1}] may fix this ' 'issue{2}'.format(attribute, ', or '.join(flag_names), punctuation)) return choices
def GetResources(self, args, errors): return lister.GetGlobalResources(service=self.service, project=self.project, filter_expr=self.GetFilterExpr(args), http=self.http, batch_url=self.batch_url, errors=errors)
def GetResources(self, args, errors): health_checks = lister.GetGlobalResources( service=self.service, project=self.project, filter_expr=self.GetFilterExpr(args), http=self.http, batch_url=self.batch_url, errors=errors) # If a protocol is specified, check that it is one we support, and convert # it to a number. protocol_value = None if args.protocol is not None: protocol_whitelist = [ self.messages.HealthCheck.TypeValueValuesEnum.HTTP.number, self.messages.HealthCheck.TypeValueValuesEnum.HTTPS.number, self.messages.HealthCheck.TypeValueValuesEnum.HTTP2.number, self.messages.HealthCheck.TypeValueValuesEnum.TCP.number, self.messages.HealthCheck.TypeValueValuesEnum.SSL.number ] protocol_value = self._ConvertProtocolArgToValue(args) if protocol_value not in protocol_whitelist: raise exceptions.ToolException( 'Invalid health check protocol ' + args.protocol + '.') for health_check in health_checks: if protocol_value is None or health_check.type.number == protocol_value: yield health_check
def GetResources(self, args, errors): return lister.GetGlobalResources( service=self.service, project=self.project, filter_expr=self.GetFilterExpr(args), http=self.http, batch_url='https://www.googleapis.com/batch/', errors=errors)
def GetGlobalResources(self, **kwargs): args = dict( service=self.compute.networks, project='my-project', filter_expr=None, http=self.mock_http, batch_url=self.batch_url, errors=[]) args.update(kwargs) return lister.GetGlobalResources(**args)
def GetResources(self, args, errors): health_checks = lister.GetGlobalResources( service=self.service, project=self.project, filter_expr=self.GetFilterExpr(args), http=self.http, batch_url=self.batch_url, errors=errors) # TODO(user): Need to add protocol-specific columns. For example, if # --protocol http is used, we need to add columns like HOST, REQUEST_PATH, # etc. Need to wait for some work to be done by gsfowler@, then (according # to his comments in CL/97712492) we should do something like this: # Display() method: # def Format(self, args, unused_resource): # if args.filter_on_http: # return 'table(foo, bar, baz.http_special_field:label=HTTP_SPECIAL, ..) # elif ...: # ... # else: # return 'table(generic.field, bla, bla.bla, ...)' # If a protocol is specified, check that it is one we support, and convert # it to a number. protocol_value = None if args.protocol is not None: protocol_whitelist = [ self.messages.HealthCheck.TypeValueValuesEnum.HTTP.number, self.messages.HealthCheck.TypeValueValuesEnum.HTTPS.number, self.messages.HealthCheck.TypeValueValuesEnum.HTTP2.number, self.messages.HealthCheck.TypeValueValuesEnum.TCP.number, self.messages.HealthCheck.TypeValueValuesEnum.SSL.number ] # Get the dictionary that maps strings to numbers, e.g. "HTTP" to 0. protocol_dict = self.messages.HealthCheck.TypeValueValuesEnum.to_dict( ) protocol_value = protocol_dict.get(args.protocol.upper()) if protocol_value not in protocol_whitelist: raise exceptions.ToolException( 'Invalid health check protocol ' + args.protocol + '.') for health_check in health_checks: if protocol_value is None or health_check.type.number == protocol_value: yield health_check
def GetResources(self, args, errors): """Gets a list of global healthcheck resources.""" health_checks = lister.GetGlobalResources( service=self.service, project=self.project, filter_expr=self.GetFilterExpr(args), http=self.http, batch_url=self.batch_url, errors=errors) # If a protocol is specified, check that it is one we support, and convert # it to a number. protocol_value = None if args.protocol is not None: protocol_value = self._ConvertProtocolArgToValue(args) if protocol_value not in self._ProtocolWhitelist(): raise exceptions.ToolException( 'Invalid health check protocol ' + args.protocol + '.') for health_check in health_checks: if protocol_value is None or health_check.type.number == protocol_value: yield health_check
def Run(self, args): """Yields zonal, regional, and/or global resources.""" compute_holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) compute_client = compute_holder.client # This is True if the user provided no flags indicating scope. no_scope_flags = self.NoArguments(args) requests = [] request_data = lister.ParseNamesAndRegexpFlags(args, compute_holder.resources) # TODO(b/36050874): Start using aggregatedList for zones and regions when # the operations list API supports them. if no_scope_flags: requests.append( (compute_client.apitools_client.globalOperations, 'AggregatedList', compute_client.apitools_client.globalOperations.GetRequestType( 'AggregatedList')( filter=request_data.filter, maxResults=request_data.max_results, project=list(request_data.scope_set)[0].project))) else: if getattr(args, 'global'): requests.append( (compute_client.apitools_client.globalOperations, 'List', compute_client.apitools_client.globalOperations.GetRequestType( 'List')( filter=request_data.filter, maxResults=request_data.max_results, project=list(request_data.scope_set)[0].project))) if args.regions is not None: args_region_names = [ compute_holder.resources.Parse( # pylint:disable=g-complex-comprehension region, params={'project': properties.VALUES.core.project.GetOrFail}, collection='compute.regions').Name() for region in args.regions or []] # If no regions were provided by the user, fetch a list. errors = [] region_names = ( args_region_names or [res.name for res in lister.GetGlobalResources( # pylint:disable=g-complex-comprehension service=compute_client.apitools_client.regions, project=properties.VALUES.core.project.GetOrFail(), filter_expr=None, http=compute_client.apitools_client.http, batch_url=compute_client.batch_url, errors=errors)]) if errors: utils.RaiseToolException( errors, 'Unable to fetch a list of regions. Specifying [--regions] may ' 'fix this issue:') for region_name in region_names: requests.append( (compute_client.apitools_client.regionOperations, 'List', compute_client.apitools_client.regionOperations.GetRequestType( 'List')( filter=request_data.filter, maxResults=request_data.max_results, region=region_name, project=list(request_data.scope_set)[0].project))) if args.zones is not None: args_zone_names = [ compute_holder.resources.Parse( # pylint:disable=g-complex-comprehension zone, params={ 'project': properties.VALUES.core.project.GetOrFail, }, collection='compute.zones').Name() for zone in args.zones or []] # If no zones were provided by the user, fetch a list. errors = [] zone_names = ( args_zone_names or [res.name for res in lister.GetGlobalResources( # pylint:disable=g-complex-comprehension service=compute_client.apitools_client.zones, project=properties.VALUES.core.project.GetOrFail(), filter_expr=None, http=compute_client.apitools_client.http, batch_url=compute_client.batch_url, errors=errors)]) if errors: utils.RaiseToolException( errors, 'Unable to fetch a list of zones. Specifying [--zones] may ' 'fix this issue:') for zone_name in zone_names: requests.append( (compute_client.apitools_client.zoneOperations, 'List', compute_client.apitools_client.zoneOperations.GetRequestType( 'List')( filter=request_data.filter, maxResults=request_data.max_results, zone=zone_name, project=list(request_data.scope_set)[0].project))) errors = [] results = list( request_helper.ListJson( requests=requests, http=compute_client.apitools_client.http, batch_url=compute_client.batch_url, errors=errors)) if errors: utils.RaiseToolException(errors) return results