def Run(self, args): """Creates or updates a setting from a JSON or YAML file. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. Returns: The created or updated setting. """ settings_message = service.ResourceSettingsMessages() input_setting = utils.GetMessageFromFile( args.value_file, settings_message.GoogleCloudResourcesettingsV1Setting) if not input_setting.name: raise exceptions.InvalidInputError( 'Name field not present in the resource setting.') if not utils.ValidateSettingPath(input_setting.name): raise exceptions.InvalidInputError('Name field has invalid syntax') resource_type = utils.GetResourceTypeFromString(input_setting.name) settings_service = api_utils.GetServiceFromResourceType(resource_type) # Syntax: [organizations|folders|projects]/{resource_id}/ # settings/{setting_name} setting_path = input_setting.name etag = input.etag if hasattr(input, 'etag') else None update_request = api_utils.GetPatchRequestFromResourceType( resource_type, setting_path, input_setting.localValue, etag) update_response = settings_service.Patch(update_request) return update_response
def Run(self, args): """Creates or updates a setting from a JSON or YAML file. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. Returns: The created or updated setting. """ settings_message = service.ResourceSettingsMessages() input_setting = utils.GetMessageFromFile( args.value_file, settings_message.GoogleCloudResourcesettingsV1SettingValue) if not input_setting.name: raise exceptions.InvalidInputError( 'Name field not present in the resource setting.') if not utils.ValidateSettingPath(input_setting.name): raise exceptions.InvalidInputError('Name field has invalid syntax') resource_type = utils.GetResourceTypeFromString(input_setting.name) settings_service = api_utils.GetServiceFromResourceType(resource_type) value_service = api_utils.GetValueServiceFromResourceType( resource_type) # Syntax: [organizations|folders|projects]/{resource_id}/ # settings/{setting_name}/value get_request = api_utils.GetGetValueRequestFromResourceType( resource_type, input_setting.name) try: setting_value = settings_service.GetValue(get_request) except api_exceptions.HttpNotFoundError: parent_resource = utils.GetParentResourceFromString( input_setting.name) setting_id = utils.GetSettingNameFromString(input_setting.name) create_request = api_utils.GetCreateRequestFromResourceType( resource_type, parent_resource, setting_id, input_setting) create_response = value_service.Create(create_request) return create_response if setting_value == input_setting: return setting_value update_request = api_utils.GetUpdateValueRequestFromResourceType( resource_type, input_setting) update_response = settings_service.UpdateValue(update_request) return update_response
def Run(self, args): """Creates or updates a setting from a JSON or YAML file. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. Returns: The created or updated setting. """ settings_service = api_utils.GetServiceFromArgs(args) value_service = api_utils.GetValueServiceFromArgs(args) settings_message = service.ResourceSettingsMessages() input_setting = utils.GetMessageFromFile( args.value_file, settings_message.GoogleCloudResourcesettingsV1alpha1SettingValue) if not input_setting.name: raise exceptions.InvalidInputError( 'Name field not present in the resource setting.') setting_name = '{}/value'.format(utils.GetSettingsPathFromArgs(args)) get_request = api_utils.GetGetValueRequestFromArgs(args, setting_name) try: setting_value = settings_service.GetValue(get_request) except api_exceptions.HttpNotFoundError: create_request = api_utils.GetCreateRequestFromArgs( args, input_setting) create_response = value_service.Create(create_request) return create_response if setting_value == input_setting: return setting_value update_request = api_utils.GetUpdateValueRequestFromArgs( args, input_setting) update_response = settings_service.UpdateValue(update_request) return update_response
def GetMessageFromFile(filepath, message): """Returns a message populated from the JSON or YAML file. Args: filepath: str, A local path to an object specification in JSON or YAML format. message: messages.Message, The message class to populate from the file. """ file_contents = files.ReadFileContents(filepath) try: yaml_obj = yaml.load(file_contents) json_str = json.dumps(yaml_obj) except yaml.YAMLParseError: json_str = file_contents try: return encoding.JsonToMessage(message, json_str) except Exception as e: raise exceptions.InvalidInputError('Unable to parse file [{}]: {}.'.format( filepath, e))