def _GetToken(self): try: return store.GetFreshAccessToken( account=self._account, allow_account_impersonation=self._use_account_impersonation) except Exception as e: # pylint: disable=broad-except raise client_base.ClientException( 'Error Configuring KCC Client: [{}]'.format(e))
def _GetToken(self): try: cred = store.LoadFreshCredential( self._account, allow_account_impersonation=self._use_account_impersonation) return cred.access_token except Exception as e: # pylint: disable=broad-except raise client_base.ClientException( 'Error Configuring KCC Client: [{}]'.format(e))
def ListResources(self, output_format='table'): cmd = [ self._export_service, 'print-resources', '--output-format', output_format ] exit_code, output_value, error_value = _ExecuteBinary(cmd) if exit_code != 0: raise client_base.ClientException( 'Error occured while listing available resources: [{}]'.format( error_value)) else: return output_value
def SerializeAll(self, resources, path=None, scope_field=None): if path: if not os.path.isdir(path): raise client_base.ClientException( 'Error executing export: "{}" must be an existing directory when --all is specified.' .format(path)) for resource in resources: file_name = resource['metadata']['name'] if scope_field: scope = resource['spec'][scope_field] file_name = '{}_{}'.format(scope, file_name) file_path = os.path.join(path, '{}.yaml'.format(file_name)) try: with files.FileWriter(file_path) as stream: yaml.dump(resource, stream=stream) except (EnvironmentError, yaml.Error) as e: raise client_base.ClientException( 'Error while serializing resource to file [{}]: "{}"'. format(file_path, e)) else: yaml.dump_all(documents=resources, stream=sys.stdout)
def Serialize(self, resource, path=None): if path: if os.path.isdir(path): path = os.path.join( path, '{}.yaml'.format(resource['metadata']['name'])) try: with files.FileWriter(path) as stream: yaml.dump(resource, stream=stream) except (EnvironmentError, yaml.Error) as e: raise client_base.ClientException( 'Error while serializing resource to file [{}]: "{}"'. format(path, e)) else: yaml.dump(resource, stream=sys.stdout)
def BulkExport(self, args): cmd = self._GetBinaryCommand(args, 'bulk-export') if self._OutputToFileOrDir(args): with progress_tracker.ProgressTracker( message='Exporting resources', aborted_message='Aborted Export.'): exit_code, _, error_value = _ExecuteBinary(cmd) if exit_code != 0: raise client_base.ClientException( 'Error executing export:: [{}]'.format(error_value)) return exit_code else: return _ExecuteBinaryWithStreaming(cmd)
def ExportAll(self, args, resource_uris, resource_type): normalized_resource_uris = _NormalizeUris(resource_uris) cmd = self._GetBinaryCommand(args, 'bulk-export') asset_inventory_input = _GetAssetInventoryInput( resource_uris=normalized_resource_uris, resource_type=resource_type) if self._OutputToFileOrDir(args): with progress_tracker.ProgressTracker( message='Exporting resource configurations', aborted_message='Aborted Export.'): exit_code, _, error_value = _ExecuteBinary( cmd=cmd, in_str=asset_inventory_input) if exit_code != 0: raise client_base.ClientException( 'Error executing export:: [{}]'.format(error_value)) return exit_code else: return _ExecuteBinaryWithStreaming(cmd=cmd, in_str=asset_inventory_input)
def _CallBulkExport(self, cmd, args, asset_list_input=None): """Execute actual bulk-export command on config-connector binary.""" if self._OutputToFileOrDir(args): preexisting_file_count = len(os.listdir(args.path)) with progress_tracker.ProgressTracker( message='Exporting resource configurations to [{}]'.format(args.path), aborted_message='Aborted Export.'): exit_code, _, error_value = _ExecuteBinary(cmd=cmd, in_str=asset_list_input) if exit_code != 0: raise client_base.ClientException( 'Error executing export:: [{}]'.format(error_value)) else: _BulkExportPostStatus(preexisting_file_count, args.path) return exit_code else: log.status.write('Exporting resource configurations...\n') return _ExecuteBinaryWithStreaming(cmd=cmd, in_str=asset_list_input)
def Export(self, args, resource_uri): normalized_resource_uri = _NormalizeUri(resource_uri) with progress_tracker.ProgressTracker( message='Exporting resources', aborted_message='Aborted Export.'): cmd = self._GetBinaryCommand(args=args, command_name='export', resource_uri=normalized_resource_uri) exit_code, output_value, error_value = _ExecuteBinary(cmd) if exit_code != 0: if 'resource not found' in error_value: raise client_base.ResourceNotFoundException( 'Could not fetch resource: \n - The resource [{}] does not exist.' .format(normalized_resource_uri)) else: raise client_base.ClientException( 'Error executing export:: [{}]'.format(error_value)) if not self._OutputToFileOrDir(args): log.out.Print(output_value) log.status.Print('Exported successfully.') return exit_code
def _GetHelper(self, resource_uri): cmd = [self._export_service, '--oauth2-token', self._GetToken()] cmd.extend(['export', resource_uri]) output_value = io.StringIO() error_value = io.StringIO() exit_code = exec_utils.Exec(args=cmd, no_exit=True, out_func=output_value.write, err_func=error_value.write) if exit_code != 0: error = error_value.getvalue() if 'resource not found' in error: raise client_base.ResourceNotFoundException( 'Could not fetch resource: \n - The resource [{}] does not exist.' .format(resource_uri)) else: raise client_base.ClientException( 'Error executing export:: [{}]'.format( error_value.getvalue())) resource = yaml.load(output_value.getvalue()) return resource
def ValidateAllPathArgs(args): if args.IsSpecified('all'): if args.IsSpecified('path') and not os.path.isdir(args.path): raise client_base.ClientException( 'Error executing export: "{}" must be a directory when --all is specified.' .format(args.path))
def _ExecuteBinaryWithStreaming(cmd, in_str=None): exit_code = execution_utils.ExecWithStreamingOutput(args=cmd, in_str=in_str) if exit_code != 0: raise client_base.ClientException( 'The bulk-export command could not finish correctly.')
def _GetAssetInventoryListInput(folder, project, org, file_path=None, asset_types_filter=None, filter_expression=None): """Generate a AssetInventory export file from api list call. Calls AssetInventory List API via shared api client (AssetListClient) and writes the list of exportable assets to a temp file on disk. If the `filter_func` parameter is set will filter out non-matching resources in the result. Args: folder: string, folder parent for resource export. project: string, project parent for resource export. org: string, organization parent for resource export. file_path: string, path to write AssetInventory export file to. If None, results are returned as string. asset_types_filter: [string], list of asset types to include in the output file. filter_expression: string, a valid gcloud filter expression. See `gcloud topic filter` for more details. Returns: string: file path where AssetInventory data has been written or raw data if `temp_file_path` is None. Returns None if no results returned from API. Raises: RequiredArgumentException: If none of folder, project or org is provided. ResourceNotFoundException: If no resources are found or returned from filtering. ClientException: Writing file to disk. """ root_asset = asset_utils.GetParentNameForExport(organization=org, project=project, folder=folder) asset_client = client_util.AssetListClient(root_asset) filter_func = (resource_filter.Compile(filter_expression.strip()).Evaluate if filter_expression else None) args = ApiClientArgs(snapshot_time=None, limit=None, page_size=None, content_type=None, asset_types=asset_types_filter or [], parent=root_asset, filter_func=filter_func) asset_results = asset_client.List(args, do_filter=True) asset_string_array = [] for item in asset_results: # list of apitools Asset messages. item_str = encoding.MessageToJson(item) item_str = item_str.replace('"assetType"', '"asset_type"') asset_string_array.append(item_str) if not asset_string_array: if asset_types_filter: asset_msg = '\n With resource types in [{}].'.format(asset_types_filter) else: asset_msg = '' if filter_expression: filter_msg = '\n Matching provided filter [{}].'.format(filter_expression) else: filter_msg = '' raise ResourceNotFoundException( 'No matching resources found for [{parent}] {assets} {filter}'.format( parent=root_asset, assets=asset_msg, filter=filter_msg)) if not file_path: return '\n'.join(asset_string_array) else: try: files.WriteFileAtomically(file_path, '\n'.join(asset_string_array)) except (ValueError, TypeError) as e: raise client_base.ClientException(e) return file_path