#!/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: - Vertex AI Feature Group. extends_documentation_fragment: - google.cloud.gcp module: gcp_vertexai_feature_group notes: - 'API Reference: U(https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.featureGroups)' - 'Creating a Feature Group Guide: U(https://cloud.google.com/vertex-ai/docs/featurestore/latest/create-featuregroup)' options: big_query: description: - Indicates that features for this group come from BigQuery Table/View. - By default treats the source as a sparse time series source, which is required to have an entityId and a feature_timestamp column in the source. suboptions: big_query_source: description: - The BigQuery source URI that points to either a BigQuery Table or View. required: true suboptions: input_uri: description: - BigQuery URI to a table, up to 2000 characters long. - 'For example: `bq://projectId.bqDatasetId.bqTableId.`.' required: true type: str type: dict entity_id_columns: description: - Columns to construct entityId / row keys. - If not provided defaults to entityId. elements: str type: list type: dict description: description: - The description of the FeatureGroup. type: str labels: description: - The labels with user-defined metadata to organize your FeatureGroup. type: dict name: description: - The resource name of the Feature Group. type: str region: description: - The region of feature group. - 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.FeatureGroup resource """ # noqa: E501 EXAMPLES = r""" - name: Create feature group google.cloud.gcp_vertexai_feature_group: state: present name: my_feature_group description: "My feature group" region: us-central1 big_query: big_query_source: input_uri: "bq://{{ gcp_project }}.my_dataset.my_table" 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 FeatureGroup 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 FeatureGroup 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 import copy # END Custom imports def build_link(module_params, uri): params = module_params.copy() return ("https://{region}-aiplatform.googleapis.com/v1/" + uri).format(**params) class BigQuery(gcp.Resource): def _request(self): return { "bigQuerySource": gcp.remove_empties( BigQueryBigQuerySource(self.request.get("big_query_source", {})).to_request() ), # remove empty values "entityIdColumns": self.request.get("entity_id_columns"), } def _response(self): return { "bigQuerySource": BigQueryBigQuerySource().from_response(self.response.get("bigQuerySource", {})), "entityIdColumns": self.response.get("entityIdColumns"), } class BigQueryBigQuerySource(gcp.Resource): def _request(self): return { "inputUri": self.request.get("input_uri"), } def _response(self): return { "inputUri": self.response.get("inputUri"), } class VertexAI(gcp.Resource): def _request(self): return { "bigQuery": gcp.remove_empties( BigQuery(self.request.get("big_query", {})).to_request() ), # remove empty values "description": self.request.get("description"), "labels": self.request.get("labels"), "name": self.request.get("name"), } def _response(self): return { "bigQuery": BigQuery().from_response(self.response.get("bigQuery", {})), "createTime": self.response.get("createTime"), "description": self.response.get("description"), "etag": self.response.get("etag"), "labels": self.response.get("labels"), "name": self.response.get("name"), "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. """ # --------- BEGIN custom decoder code --------- r = copy.deepcopy(obj) if "name" in r: r["name"] = r.pop("name").split("/")[-1] return r # --------- END custom decoder code --------- def main(): """Main function""" module = gcp.Module( argument_spec=dict( name=dict( type="str", ), state=dict( type="str", default="present", choices=["present", "absent"], ), big_query=dict( type="dict", options=dict( big_query_source=dict( type="dict", required=True, options=dict( input_uri=dict( type="str", required=True, ) ), ), entity_id_columns=dict( type="list", elements="str", ), ), ), description=dict( type="str", ), labels=dict( type="dict", ), 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}/featureGroups", "async_uri": "", "verb": "GET", "timeout_minutes": 0, } ), "create": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featureGroups?featureGroupId={name}", "async_uri": "{op_id}", "verb": "POST", "timeout_minutes": 20, } ), "delete": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featureGroups/{name}", "async_uri": "{op_id}", "verb": "DELETE", "timeout_minutes": 20, } ), "read": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featureGroups/{name}", "async_uri": "", "verb": "GET", "timeout_minutes": 0, } ), "update": gcp.ResourceOpConfig( **{ "uri": "projects/{project}/locations/{region}/featureGroups/{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#featureGroup") 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 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 --------- 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()