Skip to content

Added dialogflow_agent_to_link field to the google_discovery_engine_chat_engine resource #8333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11889.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
discoveryengine: added `chat_engine_config.dialogflow_agent_to_link` field to `google_discovery_engine_chat_engine` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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/<Project_ID>/locations/<Location_ID>/agents/<Agent_ID>'.
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"},
},
},
},
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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, ",")})
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 47 additions & 1 deletion website/docs/r/discovery_engine_chat_engine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ resource "google_discovery_engine_chat_engine" "primary" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=discoveryengine_chat_engine_existing_dialogflow_agent&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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

Expand Down Expand Up @@ -109,10 +149,16 @@ The following arguments are supported:
<a name="nested_chat_engine_config"></a>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/<Project_ID>/locations/<Location_ID>/agents/<Agent_ID>`.
Exactly one of `agent_creation_config` or `dialogflow_agent_to_link` must be set.


<a name="nested_agent_creation_config"></a>The `agent_creation_config` block supports:

Expand Down