#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (C) 2017-2026 Google # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # ---------------------------------------------------------------------------- # # *** AUTO GENERATED CODE *** Type: MMv1 *** # # ---------------------------------------------------------------------------- # # This file is automatically generated by Magic Modules and manual # changes will be clobbered when the file is regenerated. # # ---------------------------------------------------------------------------- # from __future__ import absolute_import, division, print_function __metaclass__ = type ################################################################################ # Documentation ################################################################################ ANSIBLE_METADATA = { "metadata_version": "1.1", "status": ["preview"], "supported_by": "community", } DOCUMENTATION = r""" --- author: - Google Inc. (@googlecloudplatform) description: - A collection of DataItems and Annotations on them. extends_documentation_fragment: - google.cloud.gcp module: gcp_vertexai_featurestore notes: - 'API Reference: U(https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.featurestores)' - 'Official Documentation Guide: U(https://cloud.google.com/vertex-ai/docs)' options: encryption_spec: description: - If set, both of the online and offline data storage will be secured by this key. suboptions: kms_key_name: description: - The Cloud KMS resource identifier of the customer managed encryption key used to protect a resource. - 'Has the form: projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key.' - The key needs to be in the same region as where the compute resource is created. required: true type: str type: dict force_destroy: default: false description: - If set to true, any EntityTypes and Features for this Featurestore will also be deleted. type: bool labels: description: - A set of key/value label pairs to assign to this Featurestore. type: dict name: description: - The name of the Featurestore. - This value may be up to 60 characters, and valid characters are [a-z0-9_]. - The first character cannot be a number. required: true type: str online_serving_config: description: - Config for online serving resources. suboptions: fixed_node_count: description: - The number of nodes for each cluster. - The number of nodes will not scale automatically but can be scaled manually by providing different values when updating. type: int scaling: description: - Online serving scaling configuration. - Only one of fixedNodeCount and scaling can be set. - Setting one will reset the other. suboptions: max_node_count: description: - The maximum number of nodes to scale up to. - Must be greater than minNodeCount, and less than or equal to 10 times of 'minNodeCount'. required: true type: int min_node_count: description: - The minimum number of nodes to scale down to. - Must be greater than or equal to 1. required: true type: int type: dict type: dict online_storage_ttl_days: default: 4000 description: - TTL in days for feature values that will be stored in online serving storage. - The Feature Store online storage periodically removes obsolete feature values older than onlineStorageTtlDays since the feature generation time. - Note that onlineStorageTtlDays should be less than or equal to offlineStorageTtlDays for each EntityType under a featurestore. - If not set, default to 4000 days. type: int region: description: - The region of the dataset. - eg us-central1. type: str state: choices: - present - absent default: present description: - Whether the resource should exist in GCP. type: str requirements: - python >= 3.8 - requests >= 2.18.4 - google-auth >= 2.25.1 short_description: Creates a GCP VertexAI.Featurestore resource """ # noqa: E501 EXAMPLES = r""" - name: Create Featurestore google.cloud.gcp_vertexai_featurestore: state: present name: "{{ resource_name }}" region: us-central1 online_serving_config: fixed_node_count: 2 project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" register: _myfs - name: Delete Featurestore google.cloud.gcp_vertexai_featurestore: state: absent name: "{{ _myfs.name }}" region: us-central1 project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" ################################################################################ - name: Create Featurestore with scaling configuration google.cloud.gcp_vertexai_featurestore: state: present name: "{{ resource_name }}" region: us-central1 online_serving_config: scaling: min_node_count: 1 max_node_count: 10 project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file }}" """ # noqa: E501 RETURN = r""" changed: description: Whether the resource was changed. returned: always type: bool createTime: description: - The timestamp of when the featurestore was created in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. returned: success type: str etag: description: - Used to perform consistent read-modify-write updates. returned: success type: str state: description: The current state of the resource. returned: always type: str updateTime: description: - The timestamp of when the featurestore was last updated in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. returned: success type: str """ # noqa: E501 ################################################################################ # Imports ################################################################################ from ansible_collections.google.cloud.plugins.module_utils import gcp_utils as gcp import types # BEGIN Custom imports # END Custom imports def build_link(module_params, uri): params = module_params.copy() return ("https://{region}-aiplatform.googleapis.com/v1/" + uri).format(**params) class EncryptionSpec(gcp.Resource): def _request(self): return { "kmsKeyName": self.request.get("kms_key_name"), } def _response(self): return { "kmsKeyName": self.response.get("kmsKeyName"), } class OnlineServingConfig(gcp.Resource): def _request(self): return { "fixedNodeCount": self.request.get("fixed_node_count"), "scaling": gcp.remove_empties( OnlineServingConfigScaling(self.request.get("scaling", {})).to_request() ), # remove empty values } def _response(self): return { "fixedNodeCount": self.response.get("fixedNodeCount"), "scaling": OnlineServingConfigScaling().from_response(self.response.get("scaling", {})), } class OnlineServingConfigScaling(gcp.Resource): def _request(self): return { "maxNodeCount": self.request.get("max_node_count"), "minNodeCount": self.request.get("min_node_count"), } def _response(self): return { "maxNodeCount": self.response.get("maxNodeCount"), "minNodeCount": self.response.get("minNodeCount"), } class VertexAI(gcp.Resource): def _request(self): return { "encryptionSpec": gcp.remove_empties( EncryptionSpec(self.request.get("encryption_spec", {})).to_request() ), # remove empty values "labels": self.request.get("labels"), "onlineServingConfig": gcp.remove_empties( OnlineServingConfig(self.request.get("online_serving_config", {})).to_request() ), # remove empty values "onlineStorageTtlDays": self.request.get("online_storage_ttl_days"), } def _response(self): return { "createTime": self.response.get("createTime"), "encryptionSpec": EncryptionSpec().from_response(self.response.get("encryptionSpec", {})), "etag": self.response.get("etag"), "labels": self.response.get("labels"), "onlineServingConfig": OnlineServingConfig().from_response(self.response.get("onlineServingConfig", {})), "onlineStorageTtlDays": self.response.get("onlineStorageTtlDays"), "updateTime": self.response.get("updateTime"), } ################################################################################ # Main ################################################################################ def encode(self, obj): """ This is a function bound to the main resource object. Its input is the object returned from to_request() and it mutates it before it is sent to the API. """ return obj def decode(self, obj): """ This is a function bound to the main resource object. Its input is the object returned from from_response() and it mutates it before it is returned to the module caller. """ return obj def main(): """Main function""" module = gcp.Module( argument_spec=dict( name=dict( type="str", required=True, ), state=dict( type="str", default="present", choices=["present", "absent"], ), encryption_spec=dict( type="dict", options=dict( kms_key_name=dict( type="str", required=True, no_log=False, ) ), ), force_destroy=dict( type="bool", default=False, ), labels=dict( type="dict", ), online_serving_config=dict( type="dict", options=dict( fixed_node_count=dict( type="int", ), scaling=dict( type="dict", options=dict( max_node_count=dict( type="int", required=True, ), min_node_count=dict( type="int", required=True, ), ), ), ), mutually_exclusive=[["fixed_node_count", "scaling"]], required_one_of=[["fixed_node_count", "scaling"]], ), online_storage_ttl_days=dict( type="int", default=4000, ), region=dict( type="str", ), ) ) if not module.params["scopes"]: module.params["scopes"] = ["https://www.googleapis.com/auth/cloud-platform"] state = module.params["state"] changed = False op_configs = gcp.ResourceOpConfigs( { "base_url": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featurestores", "async_uri": "", "verb": "GET", "timeout_minutes": 0, } ), "create": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featurestores?featurestoreId={name}", "async_uri": "{op_id}", "verb": "POST", "timeout_minutes": 20, } ), "delete": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featurestores/{name}", "async_uri": "{op_id}", "verb": "DELETE", "timeout_minutes": 20, } ), "read": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featurestores/{name}", "async_uri": "", "verb": "GET", "timeout_minutes": 0, } ), "update": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featurestores/{name}", "async_uri": "{op_id}", "verb": "PATCH", "timeout_minutes": 20, } ), } ) params = gcp.remove_nones(module.params) resource = VertexAI(params, module=module, product="VertexAI", kind="vertexai#featurestore") read_uri = op_configs.read.uri resource._state = state # store the state in the resource object # Bind the encode and decode functions to the resource object resource.encode_func = types.MethodType(encode, resource) resource.decode_func = types.MethodType(decode, resource) custom_diff = None # Set this variable if you want to implement custom diff logic # --------- BEGIN pre-read custom code --------- # if this comes from a registered variable, strip down to base name params["name"] = params["name"].split("/")[-1] # --------- END pre-read custom code --------- read_url = build_link(params, read_uri) existing_obj = resource.get(read_url, allow_not_found=True) or {} new_obj = {} gcp.debug(module, existing=existing_obj, post=False) if custom_diff is not None: is_different = custom_diff else: is_different = resource.diff(gcp.remove_empties(existing_obj)) gcp.debug( module, request=gcp.remove_empties(resource.to_request()), existing=existing_obj, post=True, is_different=is_different, ) if gcp.empty(existing_obj): if state == "present": create_uri = op_configs.create.uri create_async_uri = op_configs.create.async_uri try: # --------- BEGIN create code --------- is_async = create_async_uri != "" create_link = build_link(params, create_uri) create_retries = op_configs.create.timeout create_func = getattr(resource, op_configs.create.verb) async_create_func = getattr(resource, op_configs.create.verb + "_async") async_create_link = build_link(params, "") + create_async_uri gcp.debug( module, msg="Creating resource", create_link=create_link, async_create_link=async_create_link, is_async=is_async, ) if is_async: new_obj = async_create_func(create_link, async_link=async_create_link, retries=create_retries) else: new_obj = create_func(create_link) gcp.debug(module, new=new_obj, action="create", post=False) gcp.debug(module, new=new_obj, action="create", post=True) # --------- END create code --------- except Exception as e: module.fail_json(msg=str(e)) changed = True else: pass # nothing to do else: if state == "absent": delete_uri = op_configs.delete.uri delete_async_uri = op_configs.delete.async_uri try: # --------- BEGIN delete code --------- # --------- BEGIN pre-delete custom code --------- if params.get("force_destroy"): delete_uri += "?force=true" # --------- END pre-delete custom code --------- is_async = delete_async_uri != "" delete_link = build_link(params, delete_uri) delete_retries = op_configs.delete.timeout delete_func = getattr(resource, op_configs.delete.verb) async_delete_func = getattr(resource, op_configs.delete.verb + "_async") async_delete_link = build_link(params, "") + delete_async_uri gcp.debug( module, msg="Destroying resource", delete_link=delete_link, async_delete_link=async_delete_link, is_async=is_async, ) if is_async: new_obj = async_delete_func(delete_link, async_link=async_delete_link, retries=delete_retries) else: new_obj = delete_func(delete_link) # --------- END delete code --------- except Exception as e: module.fail_json(msg=str(e)) changed = True else: if is_different: update_uri = op_configs.update.uri update_async_uri = op_configs.update.async_uri try: # --------- BEGIN update code --------- is_async = update_async_uri != "" update_link = build_link(params, update_uri) update_retries = op_configs.update.timeout update_func = getattr(resource, op_configs.update.verb) async_update_func = getattr(resource, op_configs.update.verb + "_async") async_update_link = build_link(params, "") + update_async_uri gcp.debug( module, msg="Updating resource", update_link=update_link, async_update_link=async_update_link, is_async=is_async, ) if is_async: new_obj = async_update_func(update_link, async_link=async_update_link, retries=update_retries) else: new_obj = update_func(update_link) gcp.debug(module, new=new_obj, action="update", post=False) gcp.debug(module, new=new_obj, action="update", post=True) # --------- END update code --------- except Exception as e: module.fail_json(msg=str(e)) changed = True else: new_obj = existing_obj new_obj.update({"changed": changed}) module.exit_json(**new_obj) if __name__ == "__main__": main()