From 51a2cda2b689a280a2b09d1d8f126dc0f021c108 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 2 Oct 2024 09:50:26 -0700 Subject: [PATCH] Added dialogflow_agent_to_link field to the google_discovery_engine_chat_engine resource (#11889) (#8333) [upstream:6637e268b8624441ec3542867ef5b6923bbc1a90] Signed-off-by: Modular Magician --- .changelog/11889.txt | 3 + .../resource_discovery_engine_chat_engine.go | 42 +++++++++++-- ...overy_engine_chat_engine_generated_test.go | 60 +++++++++++++++++++ ...discovery_engine_chat_engine.html.markdown | 48 ++++++++++++++- 4 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 .changelog/11889.txt diff --git a/.changelog/11889.txt b/.changelog/11889.txt new file mode 100644 index 0000000000..1aaf01889a --- /dev/null +++ b/.changelog/11889.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +discoveryengine: added `chat_engine_config.dialogflow_agent_to_link` field to `google_discovery_engine_chat_engine` resource +``` \ No newline at end of file diff --git a/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine.go b/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine.go index 06c722bbb2..b439406e62 100644 --- a/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine.go +++ b/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine.go @@ -64,10 +64,12 @@ func ResourceDiscoveryEngineChatEngine() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "agent_creation_config": { - Type: schema.TypeList, - Required: true, - Description: `The configuration to generate the Dialogflow agent that is associated to this Engine.`, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Description: `The configuration to generate the Dialogflow agent that is associated to this Engine. +Exactly one of 'agent_creation_config' or 'dialogflow_agent_to_link' must be set.`, + MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "default_language_code": { @@ -92,6 +94,16 @@ func ResourceDiscoveryEngineChatEngine() *schema.Resource { }, }, }, + ExactlyOneOf: []string{"chat_engine_config.0.agent_creation_config", "chat_engine_config.0.dialogflow_agent_to_link"}, + }, + "dialogflow_agent_to_link": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: verify.ValidateRegexp(`^projects\/[a-zA-Z0-9-]+(?:\/locations\/[a-zA-Z0-9-]+)?\/agents\/[a-zA-Z0-9-]+$`), + Description: `The resource name of an existing Dialogflow agent to link to this Chat Engine. Format: 'projects//locations//agents/'. +Exactly one of 'agent_creation_config' or 'dialogflow_agent_to_link' must be set.`, + ExactlyOneOf: []string{"chat_engine_config.0.agent_creation_config", "chat_engine_config.0.dialogflow_agent_to_link"}, }, }, }, @@ -105,7 +117,6 @@ func ResourceDiscoveryEngineChatEngine() *schema.Resource { "data_store_ids": { Type: schema.TypeList, Required: true, - ForceNew: true, Description: `The data stores associated with this engine. Multiple DataStores in the same Collection can be associated here. All listed DataStores must be 'SOLUTION_TYPE_CHAT'. Adding or removing data stores will force recreation.`, MinItems: 1, Elem: &schema.Schema{ @@ -403,6 +414,12 @@ func resourceDiscoveryEngineChatEngineUpdate(d *schema.ResourceData, meta interf } else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { obj["displayName"] = displayNameProp } + dataStoreIdsProp, err := expandDiscoveryEngineChatEngineDataStoreIds(d.Get("data_store_ids"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("data_store_ids"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, dataStoreIdsProp)) { + obj["dataStoreIds"] = dataStoreIdsProp + } obj, err = resourceDiscoveryEngineChatEngineEncoder(d, meta, obj) if err != nil { @@ -421,6 +438,10 @@ func resourceDiscoveryEngineChatEngineUpdate(d *schema.ResourceData, meta interf if d.HasChange("display_name") { updateMask = append(updateMask, "displayName") } + + if d.HasChange("data_store_ids") { + updateMask = append(updateMask, "dataStoreIds") + } // updateMask is a URL parameter but not present in the schema, so ReplaceVars // won't set it url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) @@ -619,6 +640,13 @@ func expandDiscoveryEngineChatEngineChatEngineConfig(v interface{}, d tpgresourc transformed["agentCreationConfig"] = transformedAgentCreationConfig } + transformedDialogflowAgentToLink, err := expandDiscoveryEngineChatEngineChatEngineConfigDialogflowAgentToLink(original["dialogflow_agent_to_link"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDialogflowAgentToLink); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["dialogflowAgentToLink"] = transformedDialogflowAgentToLink + } + return transformed, nil } @@ -678,6 +706,10 @@ func expandDiscoveryEngineChatEngineChatEngineConfigAgentCreationConfigLocation( return v, nil } +func expandDiscoveryEngineChatEngineChatEngineConfigDialogflowAgentToLink(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + func expandDiscoveryEngineChatEngineCommonConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { l := v.([]interface{}) if len(l) == 0 || l[0] == nil { diff --git a/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine_generated_test.go b/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine_generated_test.go index 72bca7cb64..3f0ae99ee0 100644 --- a/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine_generated_test.go +++ b/google-beta/services/discoveryengine/resource_discovery_engine_chat_engine_generated_test.go @@ -96,6 +96,66 @@ resource "google_discovery_engine_chat_engine" "primary" { `, context) } +func TestAccDiscoveryEngineChatEngine_discoveryengineChatEngineExistingDialogflowAgentExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckDiscoveryEngineChatEngineDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDiscoveryEngineChatEngine_discoveryengineChatEngineExistingDialogflowAgentExample(context), + }, + { + ResourceName: "google_discovery_engine_chat_engine.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"chat_engine_config", "collection_id", "engine_id", "location"}, + }, + }, + }) +} + +func testAccDiscoveryEngineChatEngine_discoveryengineChatEngineExistingDialogflowAgentExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_discovery_engine_data_store" "test_data_store" { + location = "global" + data_store_id = "tf-test-data-store%{random_suffix}" + display_name = "Structured datastore" + industry_vertical = "GENERIC" + content_config = "NO_CONTENT" + solution_types = ["SOLUTION_TYPE_CHAT"] +} + +resource "google_dialogflow_cx_agent" "agent" { + display_name = "dialogflowcx-agent" + location = "global" + default_language_code = "en" + time_zone = "America/Los_Angeles" +} + +resource "google_discovery_engine_chat_engine" "primary" { + engine_id = "tf-test-chat-engine-id%{random_suffix}" + collection_id = "default_collection" + location = google_discovery_engine_data_store.test_data_store.location + display_name = "Chat engine" + industry_vertical = "GENERIC" + data_store_ids = [google_discovery_engine_data_store.test_data_store.data_store_id] + common_config { + company_name = "test-company" + } + chat_engine_config { + dialogflow_agent_to_link = google_dialogflow_cx_agent.agent.id + } +} +`, context) +} + func testAccCheckDiscoveryEngineChatEngineDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { for name, rs := range s.RootModule().Resources { diff --git a/website/docs/r/discovery_engine_chat_engine.html.markdown b/website/docs/r/discovery_engine_chat_engine.html.markdown index 2d75f6a598..8495fc613d 100644 --- a/website/docs/r/discovery_engine_chat_engine.html.markdown +++ b/website/docs/r/discovery_engine_chat_engine.html.markdown @@ -74,6 +74,46 @@ resource "google_discovery_engine_chat_engine" "primary" { } } ``` + +## Example Usage - Discoveryengine Chat Engine Existing Dialogflow Agent + + +```hcl +resource "google_discovery_engine_data_store" "test_data_store" { + location = "global" + data_store_id = "data-store" + display_name = "Structured datastore" + industry_vertical = "GENERIC" + content_config = "NO_CONTENT" + solution_types = ["SOLUTION_TYPE_CHAT"] +} + +resource "google_dialogflow_cx_agent" "agent" { + display_name = "dialogflowcx-agent" + location = "global" + default_language_code = "en" + time_zone = "America/Los_Angeles" +} + +resource "google_discovery_engine_chat_engine" "primary" { + engine_id = "chat-engine-id" + collection_id = "default_collection" + location = google_discovery_engine_data_store.test_data_store.location + display_name = "Chat engine" + industry_vertical = "GENERIC" + data_store_ids = [google_discovery_engine_data_store.test_data_store.data_store_id] + common_config { + company_name = "test-company" + } + chat_engine_config { + dialogflow_agent_to_link = google_dialogflow_cx_agent.agent.id + } +} +``` ## Argument Reference @@ -109,10 +149,16 @@ The following arguments are supported: The `chat_engine_config` block supports: * `agent_creation_config` - - (Required) + (Optional) The configuration to generate the Dialogflow agent that is associated to this Engine. + Exactly one of `agent_creation_config` or `dialogflow_agent_to_link` must be set. Structure is [documented below](#nested_agent_creation_config). +* `dialogflow_agent_to_link` - + (Optional) + The resource name of an existing Dialogflow agent to link to this Chat Engine. Format: `projects//locations//agents/`. + Exactly one of `agent_creation_config` or `dialogflow_agent_to_link` must be set. + The `agent_creation_config` block supports: