def GetPatchRequestFromResourceType(resource_type, name, local_value, etag): """Returns the GoogleCloudResourcesettingsV1Setting from the user-specified arguments. Args: resource_type: A String object that contains the resource type name: The resource name of the setting and has the following syntax: [organizations|folders|projects]/{resource_id}/settings/{setting_name}. local_value: The configured value of the setting at the given parent resource etag: A fingerprint used for optimistic concurrency. """ setting = settings_service.ResourceSettingsMessages( ).GoogleCloudResourcesettingsV1Setting(name=name, localValue=local_value, etag=etag) if resource_type == ORGANIZATION: request = settings_service.ResourceSettingsMessages( ).ResourcesettingsOrganizationsSettingsPatchRequest( name=name, googleCloudResourcesettingsV1Setting=setting) elif resource_type == FOLDER: request = settings_service.ResourceSettingsMessages( ).ResourcesettingsFoldersSettingsPatchRequest( name=name, googleCloudResourcesettingsV1Setting=setting) else: request = settings_service.ResourceSettingsMessages( ).ResourcesettingsProjectsSettingsPatchRequest( name=name, googleCloudResourcesettingsV1Setting=setting) return request
def GetListRequestFromArgs(args, parent_resource, show_value): """Returns the get_request from the user-specified arguments. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. parent_resource: resource location such as `organizations/123` show_value: if true, show all setting values set on the resource; if false, show all available settings. """ messages = settings_service.ResourceSettingsMessages() if args.organization: view = messages.ResourcesettingsOrganizationsSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE if show_value else messages.ResourcesettingsOrganizationsSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_BASIC get_request = messages.ResourcesettingsOrganizationsSettingsListRequest( parent=parent_resource, view=view) elif args.folder: view = messages.ResourcesettingsFoldersSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE if show_value else messages.ResourcesettingsFoldersSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_BASIC get_request = messages.ResourcesettingsFoldersSettingsListRequest( parent=parent_resource, view=view) else: view = messages.ResourcesettingsProjectsSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE if show_value else messages.ResourcesettingsProjectsSettingsListRequest.ViewValueValuesEnum.SETTING_VIEW_BASIC get_request = messages.ResourcesettingsProjectsSettingsListRequest( parent=parent_resource, view=view) return get_request
def GetCreateRequestFromResourceType(resource_type, parent_resource, setting_id, setting_value): """Returns the get_request from the user-specified arguments. Args: resource_type: string value object contains resource type parent_resource: resource location such as `organizations/123` setting_id: setting id such as `iam-projectCreatorRoles` setting_value: setting value object contains name, value and (optional) etag """ messages = settings_service.ResourceSettingsMessages() if resource_type == ORGANIZATION: message_type = messages.ResourcesettingsOrganizationsSettingsValueCreateRequest elif resource_type == FOLDER: message_type = messages.ResourcesettingsFoldersSettingsValueCreateRequest else: message_type = messages.ResourcesettingsProjectsSettingsValueCreateRequest get_request = message_type( parent=parent_resource, settingsId=setting_id, googleCloudResourcesettingsV1SettingValue=setting_value) return get_request
def GetRequestFromResourceType(resource_type, setting_name, is_effective): """Returns the get_request from the user-specified arguments. Args: resource_type: A String object that contains the resource type setting_name: setting name such as `settings/iam-projectCreatorRoles` is_effective: indicate if it is requesting for an effective setting """ messages = settings_service.ResourceSettingsMessages() if resource_type == ORGANIZATION: view = messages.ResourcesettingsOrganizationsSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_EFFECTIVE_VALUE if is_effective else messages.ResourcesettingsOrganizationsSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE get_request = messages.ResourcesettingsOrganizationsSettingsGetRequest( name=setting_name, view=view) elif resource_type == FOLDER: view = messages.ResourcesettingsFoldersSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_EFFECTIVE_VALUE if is_effective else messages.ResourcesettingsFoldersSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE get_request = messages.ResourcesettingsFoldersSettingsGetRequest( name=setting_name, view=view) else: view = messages.ResourcesettingsProjectsSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_EFFECTIVE_VALUE if is_effective else messages.ResourcesettingsProjectsSettingsGetRequest.ViewValueValuesEnum.SETTING_VIEW_LOCAL_VALUE get_request = messages.ResourcesettingsProjectsSettingsGetRequest( name=setting_name, view=view) return get_request
def GetCreateRequestFromArgs(args, parent_resource, setting_id, setting_value): """Returns the get_request from the user-specified arguments. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. parent_resource: resource location such as `organizations/123` setting_id: setting id such as `iam-projectCreatorRoles` setting_value: setting value object contains name, value and (optional) etag """ messages = settings_service.ResourceSettingsMessages() if args.organization: message_type = messages.ResourcesettingsOrganizationsSettingsValueCreateRequest elif args.folder: message_type = messages.ResourcesettingsFoldersSettingsValueCreateRequest else: message_type = messages.ResourcesettingsProjectsSettingsValueCreateRequest get_request = message_type( parent=parent_resource, settingsId=setting_id, googleCloudResourcesettingsV1SettingValue=setting_value) return get_request
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 _MultiTenantProjectIds(project): setting_name = 'projects/{}/settings/cloudrun-multiTenancy'.format(project) messages = resourcesettings_service.ResourceSettingsMessages() get_request = messages.ResourcesettingsProjectsSettingsLookupEffectiveValueRequest( parent=setting_name) settings_service = resourcesettings_service.ProjectsSettingsService() service_value = settings_service.LookupEffectiveValue(get_request) return [ _MulitTenantProjectId(project) for project in service_value.value.stringSetValue.values ]
def _MultiTenantProjectIds(project): setting_name = 'projects/{}/settings/cloudrun-multiTenancy'.format(project) messages = resourcesettings_service.ResourceSettingsMessages() get_request = messages.ResourcesettingsProjectsSettingsGetRequest( name=setting_name, view=messages.ResourcesettingsProjectsSettingsGetRequest. ViewValueValuesEnum.SETTING_VIEW_EFFECTIVE_VALUE) settings_service = resourcesettings_service.ProjectsSettingsService() service_value = settings_service.LookupEffectiveValue(get_request) return [ _MulitTenantProjectId(project) for project in service_value.localValue.stringSetValue.values ]
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 GetGetValueRequestFromResourceType(resource_type, setting_name): """Returns the get_request from the user-specified arguments. Args: resource_type: A String object that contains the resource type setting_name: setting name such as `settings/iam-projectCreatorRoles` """ messages = settings_service.ResourceSettingsMessages() if resource_type == ORGANIZATION: get_request = messages.ResourcesettingsOrganizationsSettingsGetValueRequest( name=setting_name) elif resource_type == FOLDER: get_request = messages.ResourcesettingsFoldersSettingsGetValueRequest( name=setting_name) else: get_request = messages.ResourcesettingsProjectsSettingsGetValueRequest( name=setting_name) return get_request
def GetLookupEffectiveValueRequestFromArgs(args, parent_resource): """Returns the get_request from the user-specified arguments. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. parent_resource: resource location such as `organizations/123` """ messages = settings_service.ResourceSettingsMessages() if args.organization: get_request = messages.ResourcesettingsOrganizationsSettingsValueLookupEffectiveValueRequest( name=parent_resource) elif args.folder: get_request = messages.ResourcesettingsFoldersSettingsValueLookupEffectiveValueRequest( name=parent_resource) else: get_request = messages.ResourcesettingsProjectsSettingsValueLookupEffectiveValueRequest( name=parent_resource) return get_request
def GetGetValueRequestFromArgs(args, setting_name): """Returns the get_request from the user-specified arguments. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. setting_name: setting name such as `settings/iam-projectCreatorRoles` """ messages = settings_service.ResourceSettingsMessages() if args.organization: get_request = messages.ResourcesettingsOrganizationsSettingsGetValueRequest( name=setting_name) elif args.folder: get_request = messages.ResourcesettingsFoldersSettingsGetValueRequest( name=setting_name) else: get_request = messages.ResourcesettingsProjectsSettingsGetValueRequest( name=setting_name) return get_request
def GetUpdateValueRequestFromResourceType(resource_type, setting_value): """Returns the get_request from the user-specified arguments. Args: resource_type: A String object that contains the resource type setting_value: setting value object contains name, value """ messages = settings_service.ResourceSettingsMessages() if resource_type == ORGANIZATION: message_type = messages.ResourcesettingsOrganizationsSettingsUpdateValueRequest elif resource_type == FOLDER: message_type = messages.ResourcesettingsFoldersSettingsUpdateValueRequest else: message_type = messages.ResourcesettingsProjectsSettingsUpdateValueRequest get_request = message_type( name=setting_value.name, googleCloudResourcesettingsV1SettingValue=setting_value) return get_request
def GetUpdateValueRequestFromArgs(args, setting_value): """Returns the get_request from the user-specified arguments. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the Args method. setting_value: setting value object contains name, value """ messages = settings_service.ResourceSettingsMessages() if args.organization: message_type = messages.ResourcesettingsOrganizationsSettingsUpdateValueRequest elif args.folder: message_type = messages.ResourcesettingsFoldersSettingsUpdateValueRequest else: message_type = messages.ResourcesettingsProjectsSettingsUpdateValueRequest get_request = message_type( name=setting_value.name, googleCloudResourcesettingsV1SettingValue=setting_value) return get_request