def CompletionCallback(parsed_args): resource_ref = resources.Parse( getattr(parsed_args, completion_resource_arg), collection=completion_resource_collection) resource_uri = resource_ref.SelfLink() return ['beta', 'iam', 'list-grantable-roles', '--format=value(name)', resource_uri]
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Some value that we want to have printed later. """ client = self.context['container_client'] messages = self.context['container_messages'] project_id = properties.VALUES.core.project.Get(required=True) zone_id = None if args.zone: zone_id = resources.Parse(args.zone, collection='compute.zones').zone try: if zone_id: # Zone-filtered list req = messages.ContainerProjectsZonesOperationsListRequest( projectId=project_id, zoneId=zone_id) return client.projects_zones_operations.List(req) else: # Global list req = messages.ContainerProjectsOperationsListRequest( projectId=project_id) return client.projects_operations.List(req) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def __init__(self, bucket_url): """Constructor for BucketReference. Args: bucket_url: str, The bucket to reference. Format: gs://<bucket_name> """ self._bucket_url = bucket_url bucket_name = bucket_url.replace('gs://', '').rstrip('/') self._ref = resources.Parse(bucket_name, collection='storage.buckets')
def StoreInCache(self, self_links): """Store names of resources listed in cache. Args: self_links: A list of resource instance references Returns: None """ if platforms.OperatingSystem.Current( ) == platforms.OperatingSystem.WINDOWS: return None paths = {} collection = None for ref in self_links: if not collection: try: instance_ref = resources.Parse(ref) collection = instance_ref.Collection() except resources.InvalidResourceException: # construct collection from self link lst = ref.split('/') collection = lst[3] + '.' + lst[-2] lst = RemoteCompletion.CachePath(ref) path = lst[0] name = lst[1] if path in paths: paths[path].append(name) else: paths[path] = [name] if not collection: return for path in paths: abs_path = os.path.join(self.cache_dir, path) dirname = os.path.dirname(abs_path) try: if not os.path.isdir(dirname): files.MakeDir(dirname) tempname = tempfile.NamedTemporaryFile(dir=dirname).name with open(tempname, 'w') as f: f.write('\n'.join(paths[path])) # note that atomic rename does't work on windows os.rename(tempname, abs_path) now = time.time() timeout = RemoteCompletion._TIMEOUTS.get(collection, 300) os.utime(abs_path, (now, now + timeout)) except Exception: # pylint: disable=broad-except return
def GetOrganizationRef(self, organization_id): # This is fun stuff here. # Gcloud's apitools client generator doesn't support one platform {+name} # URL template patterns. To work around this, a differnt discovery doc for # CRM is used in cloud/sdk/apis/BUILD and that doc autogenerates an ID field # for the name field that doesn't have the prefix. This autogenerated field # is the collection name folowed by "Id", e.g. "organizationsId". Our API # already has an "organizationId" field that's deprecated and was used # before we switched to the one platform "name" convention. # "organizationsId=123" gets turned into "name=organizations/123" under the # hood in the client. We don't pass organizationId because it's deprecated. return resources.Parse(None, params={ 'organizationsId': organization_id, 'organizationId': organization_id }, collection=self.Collection())
def StoreInCache(self, self_links): """Store names of resources listed in cache. Args: self_links: A list of resource instance references Returns: None """ paths = {} collection = None for ref in self_links: if not collection: try: instance_ref = resources.Parse(ref) collection = instance_ref.Collection() except resources.InvalidResourceException: # construct collection from self link lst = ref.split('/') collection = lst[3] + '.' + lst[-2] lst = RemoteCompletion.CachePath(ref) path = lst[0] name = lst[1] if path in paths: paths[path].append(name) else: paths[path] = [name] if not collection: return for path in paths: abs_path = os.path.join(self.cache_dir, path) dirname = os.path.dirname(abs_path) try: if not os.path.isdir(dirname): files.MakeDir(dirname) with open(abs_path, 'w') as f: f.write('\n'.join(paths[path])) now = time.time() timeout = RemoteCompletion._TIMEOUTS.get(collection, 300) os.utime(abs_path, (now, now+timeout)) except Exception: # pylint: disable=broad-except return
def __init__(self, project, bucket_name, unique_obj_name, tr_client, tr_messages, storage_client): """Construct a ResultsBucketOps object to be used with a single matrix run. Args: project: string containing the Google Developers Console project id. bucket_name: string with the user-supplied name of a GCS bucket, or None. unique_obj_name: the name of a unique GCS object to hold the raw test results within the supplied bucket_name. tr_client: ToolResults API client library generated by Apitools. tr_messages: ToolResults API messages library generated by Apitools. storage_client: Cloud Storage API client library generated by Apitools. Attributes: gcs_results_root: string containing the root path for the test results in 'gs://{bucket}/{timestamp-suffix}' format. """ self._project = project self._storage_client = storage_client self._storage_messages = core_apis.GetMessagesModule('storage', 'v1') self._gcs_object_name = unique_obj_name # If the user supplied a results bucket, make sure it exists. Otherwise, # call the SettingsService to get the project's existing default bucket. if bucket_name: self.EnsureBucketExists(bucket_name) else: bucket_name = self._GetDefaultBucket(tr_client, tr_messages) bucket_ref = resources.Parse(bucket_name, collection='storage.buckets') self._results_bucket = bucket_ref.bucket self._gcs_results_url = ( 'https://console.developers.google.com/storage/browser/{b}/{t}/'. format(b=bucket_name, t=self._gcs_object_name)) self.gcs_results_root = ('gs://{b}/{t}/'.format( b=bucket_name, t=self._gcs_object_name)) log.info('Raw results root path is: [{0}]'.format( self.gcs_results_root))
def RemoteCompleter(parsed_args, **unused_kwargs): """Run list command on resource to generates completion options.""" resource = ro_resource command_line = ro_command_line options = [] try: if hasattr(parsed_args, 'property'): if parsed_args.property == 'compute/region': resource = 'compute.regions' command_line = resource elif parsed_args.property == 'compute/zone': resource = 'compute.zones' command_line = resource elif parsed_args.property != 'project': return options line = os.getenv('COMP_LINE') prefix = '' if line: for i in range(len(line)-1, -1, -1): c = line[i] if c == ' ' or c == '\t': break prefix = c + prefix command = command_line.split('.') + ['list'] project = properties.VALUES.core.project.Get(required=True) parms = {} if command[0] in _OPTIONAL_PARMS: for arg in _OPTIONAL_PARMS[command[0]]: for attrib in dict(arg): if hasattr(parsed_args, attrib): fun = arg[attrib] value = fun(parsed_args) if value: parms[attrib] = value command.append('--' + attrib) command.append(value) parms['project'] = project resource_link = resources.Parse('+', parms, resource, resolve=False) resource_link = resource_link.WeakSelfLink() lst = resource_link.split('*') resource_missing = len(lst) > 1 ccache = RemoteCompletion() options = ccache.GetFromCache(resource_link, prefix) if options is None: properties.VALUES.core.user_output_enabled.Set(False) ofile = RemoteCompletion.GetTickerStream() with CompletionProgressTracker(ofile): items = list(cli().Execute(command, call_arg_complete=False)) options = [] self_links = [] for item in items: # Get a selflink for the item if command[0] == 'compute': if 'selfLink' in item: instance_ref = resources.Parse(item['selfLink']) selflink = instance_ref.SelfLink() elif resource_link: selflink = resource_link.rstrip('+') + item['name'] elif _GETINSTANCEFUN: # List command provides a function to get the selflink selflink = _GETINSTANCEFUN(item) else: instance_ref = resources.Create(resource, project=item.project, instance=item.instance) selflink = instance_ref.SelfLink() self_links.append(selflink) lst = selflink.split('/') name = lst[-1] if not prefix or name.startswith(prefix): options.append(name) if self_links: ccache.StoreInCache(self_links) if resource_missing: options = ccache.GetFromCache(resource_link, prefix) if options: RemoteCompletion.CACHE_HITS -= 1 else: options = [] except Exception: # pylint:disable=broad-except logging.error(resource + 'completion command failed', exc_info=True) return [] return options
def RemoteCompleter(parsed_args, **unused_kwargs): """Run list command on resource to generates completion options.""" options = [] try: line = os.getenv('COMP_LINE') prefix = '' if line: for i in range(len(line) - 1, -1, -1): c = line[i] if c == ' ' or c == '\t': break prefix = c + prefix command = command_line.split('.') + ['list'] project = properties.VALUES.core.project.Get(required=True) parms = {} if command[0] in _OPTIONAL_PARMS: for arg in _OPTIONAL_PARMS[command[0]]: for attrib in dict(arg): if hasattr(parsed_args, attrib): fun = arg[attrib] value = fun(parsed_args) if value: parms[attrib] = value command.append('--' + attrib) command.append(value) parms['project'] = project resource_link = resources.Parse('+', parms, resource, resolve=False) resource_link = resource_link.WeakSelfLink() lst = resource_link.split('*') resource_missing = len(lst) > 1 ccache = RemoteCompletion() options = ccache.GetFromCache(resource_link, prefix) pid = os.getpid() if options is None: properties.VALUES.core.user_output_enabled.Set(False) ofile = RemoteCompletion.GetTickerStream() tracker = CompletionProgressTracker(ofile) with tracker: items = RemoteCompletion.RunListCommand(cli, command) options = [] # the following is true if tracker forked and this is the parent if tracker.has_forked and os.getpid() == pid: return options self_links = [] for item in items: # Get a selflink for the item if command[0] == 'compute': if 'selfLink' in item: instance_ref = resources.Parse( item['selfLink']) selflink = instance_ref.SelfLink() elif resource_link: selflink = resource_link.rstrip( '+') + item['name'] elif _GETINSTANCEFUN: # List command provides a function to get the selflink selflink = _GETINSTANCEFUN(item) else: instance_ref = resources.Create( resource, project=item.project, instance=item.instance) selflink = instance_ref.SelfLink() self_links.append(selflink) lst = selflink.split('/') name = lst[-1] if not prefix or name.startswith(prefix): options.append(name) if self_links: ccache.StoreInCache(self_links) if tracker.has_forked: # the parent already exited, so exit the child sys.exit(0) if resource_missing: options = ccache.GetFromCache( resource_link, prefix, increment_counters=False) or [] except Exception: # pylint:disable=broad-except logging.error(resource + 'completion command failed', exc_info=True) return [] return options
def ProjectIdToLink(item): instance_ref = resources.Parse(item.projectId, collection='cloudresourcemanager.projects') return instance_ref.SelfLink()
def RemoteCompleter(parsed_args, **unused_kwargs): """Runs list command on resource to generate completion data.""" resource = ro_resource command_line = ro_command_line list_command_callback_fn = ro_list_command_callback_fn list_command_updates_cache = False info = resource_registry.Get(resource) if info.cache_command: command = info.cache_command.split(' ') elif list_command_callback_fn: command = list_command_callback_fn(parsed_args) else: command = command_line.split('.') + ['list'] if info.bypass_cache: # Don't cache - use the cache_command results directly. return RemoteCompletion.RunListCommand( cli, command, parse_output=True) list_command_updates_cache = True options = [] try: if hasattr(parsed_args, 'property'): if parsed_args.property == 'compute/region': resource = 'compute.regions' command = resource.split('.') + ['list'] elif parsed_args.property == 'compute/zone': resource = 'compute.zones' command = resource.split('.') + ['list'] elif parsed_args.property != 'project': return options line = os.getenv('COMP_LINE') prefix = '' if line: for i in range(len(line)-1, -1, -1): c = line[i] if c == ' ' or c == '\t': break prefix = c + prefix project = properties.VALUES.core.project.Get(required=True) parms = {} if command[0] in _OPTIONAL_PARMS: for arg in _OPTIONAL_PARMS[command[0]]: for attrib in dict(arg): if hasattr(parsed_args, attrib): fun = arg[attrib] value = fun(parsed_args) if value: parms[attrib] = value command.append('--' + attrib) command.append(value) parms['project'] = project resource_link = resources.Parse('+', parms, resource, resolve=False) resource_link = resource_link.WeakSelfLink() lst = resource_link.split('*') resource_missing = len(lst) > 1 ccache = RemoteCompletion() options = ccache.GetFromCache(resource_link, prefix) if options is not None: return options items = RemoteCompletion.RunListCommand( cli, command, list_command_updates_cache=list_command_updates_cache) if list_command_updates_cache: options = ccache.GetFromCache(resource_link, prefix) or [] if options: RemoteCompletion.CACHE_HITS -= 1 return options # This part can be dropped when all commands are subclassed. options = [] self_links = [] for item in items: # Get a selflink for the item if command[0] == 'compute': if 'selfLink' in item: instance_ref = resources.Parse(item['selfLink']) selflink = instance_ref.SelfLink() elif resource_link: selflink = resource_link.rstrip('+') + item['name'] else: instance_ref = resources.Create(resource, project=item.project, instance=item.instance) selflink = instance_ref.SelfLink() self_links.append(selflink) lst = selflink.split('/') name = lst[-1] if not prefix or name.startswith(prefix): options.append(name) if self_links: ccache.StoreInCache(self_links) if resource_missing: options = ccache.GetFromCache(resource_link, prefix, increment_counters=False) or [] except Exception: # pylint:disable=broad-except log.error(resource + 'completion command failed', exc_info=True) return [] return options
def _GetUri(resource): ref = resources.Parse(resource.projectId, collection=self.Collection()) return ref.SelfLink()
def GetProject(self, project_id): return resources.Parse(project_id, collection=self.Collection())