forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabling management of default Dialogflow resources without terraform…
… import: `is_default_x` fields (GoogleCloudPlatform#9336) * Enabling management of Default Start Flow + Default Welcome Intent + Default Negative Intent, without terraform import Fixes hashicorp/terraform-provider-google#16308 * Basic tests for default flow + intents * Tests asserting resource IDs * Making Flow + Intent import respect is_default_X; fixing description on examples * Bah, making isFallback output-only might break users that'd imported the Default Negative Intent :( * Update mmv1/templates/terraform/pre_create/dialogflowcx_set_location_skip_default_obj.go.erb Co-authored-by: Sarah French <[email protected]> * Adding callout notes about multiple is_default_x resources * More words around the pre-create + pre-delete code * Adding more tests around Default Start Flow * Adding more tests around Default Welcome Intent and Default Negative Intent --------- Co-authored-by: Sarah French <[email protected]>
- Loading branch information
Showing
13 changed files
with
592 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
mmv1/templates/terraform/examples/dialogflowcx_flow_default_start_flow.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
resource "google_dialogflow_cx_agent" "agent" { | ||
display_name = "<%= ctx[:vars]["agent_name"] %>" | ||
location = "global" | ||
default_language_code = "en" | ||
time_zone = "America/New_York" | ||
} | ||
|
||
resource "google_dialogflow_cx_intent" "default_welcome_intent" { | ||
parent = google_dialogflow_cx_agent.agent.id | ||
is_default_welcome_intent = true | ||
display_name = "Default Welcome Intent" | ||
priority = 1 | ||
training_phrases { | ||
parts { | ||
text = "Hello" | ||
} | ||
repeat_count = 1 | ||
} | ||
} | ||
|
||
|
||
resource "google_dialogflow_cx_flow" "<%= ctx[:primary_resource_id] %>" { | ||
parent = google_dialogflow_cx_agent.agent.id | ||
is_default_start_flow = true | ||
display_name = "Default Start Flow" | ||
description = "A start flow created along with the agent" | ||
|
||
nlu_settings { | ||
classification_threshold = 0.3 | ||
model_type = "MODEL_TYPE_STANDARD" | ||
} | ||
|
||
transition_routes { | ||
intent = google_dialogflow_cx_intent.default_welcome_intent.id | ||
trigger_fulfillment { | ||
messages { | ||
text { | ||
text = ["Response to default welcome intent."] | ||
} | ||
} | ||
} | ||
} | ||
|
||
event_handlers { | ||
event = "custom-event" | ||
trigger_fulfillment { | ||
messages { | ||
text { | ||
text = ["This is a default flow."] | ||
} | ||
} | ||
} | ||
} | ||
|
||
event_handlers { | ||
event = "sys.no-match-default" | ||
trigger_fulfillment { | ||
messages { | ||
text { | ||
text = ["We've updated the default flow no-match response!"] | ||
} | ||
} | ||
} | ||
} | ||
|
||
event_handlers { | ||
event = "sys.no-input-default" | ||
trigger_fulfillment { | ||
messages { | ||
text { | ||
text = ["We've updated the default flow no-input response!"] | ||
} | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
mmv1/templates/terraform/examples/dialogflowcx_intent_default_negative_intent.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
resource "google_dialogflow_cx_agent" "agent" { | ||
display_name = "<%= ctx[:vars]["agent_name"] %>" | ||
location = "global" | ||
default_language_code = "en" | ||
time_zone = "America/New_York" | ||
} | ||
|
||
|
||
resource "google_dialogflow_cx_intent" "<%= ctx[:primary_resource_id] %>" { | ||
parent = google_dialogflow_cx_agent.agent.id | ||
is_default_negative_intent = true | ||
display_name = "Default Negative Intent" | ||
priority = 1 | ||
is_fallback = true | ||
training_phrases { | ||
parts { | ||
text = "Never match this phrase" | ||
} | ||
repeat_count = 1 | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
mmv1/templates/terraform/examples/dialogflowcx_intent_default_welcome_intent.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource "google_dialogflow_cx_agent" "agent" { | ||
display_name = "<%= ctx[:vars]["agent_name"] %>" | ||
location = "global" | ||
default_language_code = "en" | ||
time_zone = "America/New_York" | ||
} | ||
|
||
|
||
resource "google_dialogflow_cx_intent" "<%= ctx[:primary_resource_id] %>" { | ||
parent = google_dialogflow_cx_agent.agent.id | ||
is_default_welcome_intent = true | ||
display_name = "Default Welcome Intent" | ||
priority = 1 | ||
training_phrases { | ||
parts { | ||
text = "Hello" | ||
} | ||
repeat_count = 1 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
mmv1/templates/terraform/pre_create/dialogflowcx_set_location_skip_default_obj.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
// extract location from the parent | ||
location := "" | ||
|
||
if parts := regexp.MustCompile(`locations\/([^\/]*)\/`).FindStringSubmatch(d.Get("parent").(string)); parts != nil { | ||
location = parts[1] | ||
} else { | ||
return fmt.Errorf( | ||
"Saw %s when the parent is expected to contains location %s", | ||
d.Get("parent"), | ||
"projects/{{project}}/locations/{{location}}/...", | ||
) | ||
} | ||
|
||
url = strings.Replace(url,"-dialogflow",fmt.Sprintf("%s-dialogflow",location),1) | ||
|
||
// if it's a default object Dialogflow creates for you, "Update" instead of "Create" | ||
// Note: below we try to access fields that aren't present in the resource, because this custom code is reused across multiple Dialogflow resources that contain different fields. When the field isn't present, we deliberately ignore the error and the boolean is false. | ||
isDefaultStartFlow, _ := d.Get("is_default_start_flow").(bool) | ||
isDefaultWelcomeIntent, _ := d.Get("is_default_welcome_intent").(bool) | ||
isDefaultNegativeIntent, _ := d.Get("is_default_negative_intent").(bool) | ||
if isDefaultStartFlow || isDefaultWelcomeIntent || isDefaultNegativeIntent { | ||
// hardcode the default object ID: | ||
var defaultObjName string | ||
if isDefaultStartFlow || isDefaultWelcomeIntent { | ||
defaultObjName = "00000000-0000-0000-0000-000000000000" | ||
} | ||
if isDefaultNegativeIntent { | ||
defaultObjName = "00000000-0000-0000-0000-000000000001" | ||
} | ||
|
||
// Store the ID | ||
d.Set("name", defaultObjName) | ||
id, err := tpgresource.ReplaceVars(d, config, "<%= id_format(object) -%>") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
// and defer to the Update method: | ||
log.Printf("[DEBUG] Updating default <%= resource_name -%>") | ||
return resource<%= resource_name -%>Update(d, meta) | ||
} |
26 changes: 26 additions & 0 deletions
26
mmv1/templates/terraform/pre_delete/dialogflowcx_set_location_skip_default_obj.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
// extract location from the parent | ||
location := "" | ||
|
||
if parts := regexp.MustCompile(`locations\/([^\/]*)\/`).FindStringSubmatch(d.Get("parent").(string)); parts != nil { | ||
location = parts[1] | ||
} else { | ||
return fmt.Errorf( | ||
"Saw %s when the parent is expected to contains location %s", | ||
d.Get("parent"), | ||
"projects/{{project}}/locations/{{location}}/...", | ||
) | ||
} | ||
|
||
url = strings.Replace(url,"-dialogflow",fmt.Sprintf("%s-dialogflow",location),1) | ||
|
||
// if it's a default object Dialogflow creates for you, skip deletion | ||
// Note: below we try to access fields that aren't present in the resource, because this custom code is reused across multiple Dialogflow resources that contain different fields. When the field isn't present, we deliberately ignore the error and the boolean is false. | ||
isDefaultStartFlow, _ := d.Get("is_default_start_flow").(bool) | ||
isDefaultWelcomeIntent, _ := d.Get("is_default_welcome_intent").(bool) | ||
isDefaultNegativeIntent, _ := d.Get("is_default_negative_intent").(bool) | ||
if isDefaultStartFlow || isDefaultWelcomeIntent || isDefaultNegativeIntent { | ||
// we can't delete these resources so do nothing | ||
log.Printf("[DEBUG] Not deleting default <%= resource_name -%>") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.