Skip to content

Commit 0d89fcf

Browse files
dialogflow: support non-global endpoints for google_dialogflow_conversation_profile (#15037)
1 parent 13543ac commit 0d89fcf

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

mmv1/products/dialogflow/ConversationProfile.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ timeouts:
3232
update_minutes: 40
3333
delete_minutes: 20
3434
custom_code:
35+
pre_create: 'templates/terraform/pre_create/dialogflow_set_endpoint.go.tmpl'
36+
pre_read: 'templates/terraform/pre_create/dialogflow_set_endpoint.go.tmpl'
37+
pre_update: 'templates/terraform/pre_create/dialogflow_set_endpoint.go.tmpl'
38+
pre_delete: 'templates/terraform/pre_create/dialogflow_set_endpoint.go.tmpl'
3539
post_create: 'templates/terraform/post_create/set_computed_name.tmpl'
36-
custom_import: 'templates/terraform/custom_import/self_link_as_name_set_project.go.tmpl'
40+
custom_import: 'templates/terraform/custom_import/self_link_as_name_set_project_and_location.go.tmpl'
3741
exclude_sweeper: true
3842
examples:
3943
- name: 'dialogflow_conversation_profile_basic'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
config := meta.(*transport_tpg.Config)
3+
4+
// current import_formats can't import fields with forward slashes in their value
5+
if err := tpgresource.ParseImportId([]string{"(?P<name>.+)"}, d, config); err != nil {
6+
return nil, err
7+
}
8+
9+
stringParts := strings.Split(d.Get("name").(string), "/")
10+
if len(stringParts) < 2 {
11+
return nil, fmt.Errorf(
12+
"Could not split project from name: %s",
13+
d.Get("name"),
14+
)
15+
}
16+
17+
if err := d.Set("project", stringParts[1]); err != nil {
18+
return nil, fmt.Errorf("Error setting project: %s", err)
19+
}
20+
21+
var location string
22+
for i, part := range stringParts {
23+
if part == "locations" && i+1 < len(stringParts) {
24+
location = stringParts[i+1]
25+
break
26+
}
27+
}
28+
if location == "" {
29+
return nil, fmt.Errorf("Could not extract location from name: %s", d.Get("name"))
30+
}
31+
if err := d.Set("location", location); err != nil {
32+
return nil, fmt.Errorf("Error setting location: %s", err)
33+
}
34+
35+
return []*schema.ResourceData{d}, nil
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
location := d.Get("location").(string)
2+
universeDomain := config.UniverseDomain
23

3-
// insert location into url for a different endpoint.
4-
if strings.HasPrefix(url, "https://dialogflow.googleapis.com/v2/") {
5-
url = strings.Replace(url, "https://dialogflow", fmt.Sprintf("https://%s-dialogflow", location), 1)
4+
if universeDomain != "" && universeDomain != "googleapis.com" {
5+
url = strings.Replace(url, "googleapis.com", universeDomain, 1)
6+
}
7+
8+
if strings.HasPrefix(url, "https://dialogflow") {
9+
if location != "" && location != "global" {
10+
url = strings.Replace(url, "https://dialogflow", fmt.Sprintf("https://%s-dialogflow", location), 1)
11+
}
612
}

mmv1/third_party/terraform/services/dialogflow/resource_dialogflow_conversation_profile_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ func TestAccDialogflowConversationProfile_update(t *testing.T) {
6363
})
6464
}
6565

66+
func TestAccDialogflowConversationProfile_regional(t *testing.T) {
67+
t.Parallel()
68+
69+
context := map[string]interface{}{
70+
"org_id": envvar.GetTestOrgFromEnv(t),
71+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
72+
"random_suffix": acctest.RandString(t, 10),
73+
}
74+
75+
acctest.VcrTest(t, resource.TestCase{
76+
PreCheck: func() { acctest.AccTestPreCheck(t) },
77+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
78+
ExternalProviders: map[string]resource.ExternalProvider{
79+
"time": {},
80+
},
81+
Steps: []resource.TestStep{
82+
{
83+
Config: testAccDialogflowConversationProfile_dialogflowRegional(context),
84+
},
85+
{
86+
ResourceName: "google_dialogflow_conversation_profile.profile",
87+
ImportState: true,
88+
ImportStateVerify: true,
89+
ImportStateVerifyIgnore: []string{"location", "logging_config", "logging_config.0", "logging_config.0.enable_stackdriver_logging"},
90+
},
91+
},
92+
})
93+
}
94+
6695
func testAccDialogflowConversationProfile_dialogflowAgentFull1(context map[string]interface{}) string {
6796
return acctest.Nprintf(`
6897
resource "google_project" "agent_project" {
@@ -246,6 +275,7 @@ func testAccDialogflowConversationProfile_dialogflowAgentFull1(context map[strin
246275
}
247276
`, context)
248277
}
278+
249279
func testAccDialogflowConversationProfile_dialogflowAgentFull2(context map[string]interface{}) string {
250280
return acctest.Nprintf(`
251281
resource "google_project" "agent_project" {
@@ -409,3 +439,63 @@ func testAccDialogflowConversationProfile_dialogflowAgentFull2(context map[strin
409439
}
410440
`, context)
411441
}
442+
443+
func testAccDialogflowConversationProfile_dialogflowRegional(context map[string]interface{}) string {
444+
return acctest.Nprintf(`
445+
resource "google_project" "agent_project" {
446+
name = "tf-test-dialogflow-%{random_suffix}"
447+
project_id = "tf-test-dialogflow-%{random_suffix}"
448+
org_id = "%{org_id}"
449+
billing_account = "%{billing_account}"
450+
deletion_policy = "DELETE"
451+
}
452+
resource "google_project_service" "agent_project" {
453+
project = "${google_project.agent_project.id}"
454+
service = "dialogflow.googleapis.com"
455+
disable_dependent_services = false
456+
}
457+
458+
resource "google_service_account" "dialogflow_service_account" {
459+
account_id = "tf-test-dialogflow-%{random_suffix}"
460+
}
461+
462+
resource "google_project_iam_member" "agent_create" {
463+
project = "${google_project.agent_project.id}"
464+
role = "roles/dialogflow.admin"
465+
member = "serviceAccount:${google_service_account.dialogflow_service_account.email}"
466+
}
467+
468+
resource "google_dialogflow_agent" "agent" {
469+
display_name = "tf-test-agent-%{random_suffix}"
470+
default_language_code = "en-us"
471+
time_zone = "America/New_York"
472+
project = google_project.agent_project.name
473+
}
474+
475+
resource "google_pubsub_topic" "topic_diff" {
476+
name = "tf-test-topic-%{random_suffix}-diff"
477+
project = google_project.agent_project.project_id
478+
depends_on = [google_project.agent_project, time_sleep.wait_120_seconds]
479+
message_retention_duration = "8000s"
480+
}
481+
resource "google_dialogflow_cx_security_settings" "security_setting_diff" {
482+
display_name = "tf-test-setting-%{random_suffix}-diff"
483+
location = "us-central1"
484+
purge_data_types = []
485+
retention_window_days = 7
486+
project = google_project.agent_project.project_id
487+
depends_on = [time_sleep.wait_120_seconds]
488+
}
489+
resource "time_sleep" "wait_120_seconds" {
490+
create_duration = "120s"
491+
depends_on = [google_dialogflow_agent.agent]
492+
}
493+
resource "google_dialogflow_conversation_profile" "profile" {
494+
depends_on = [google_dialogflow_agent.agent, google_dialogflow_cx_security_settings.security_setting_diff, time_sleep.wait_120_seconds]
495+
project = "${google_project.agent_project.name}"
496+
display_name = "tf-test-conversation-profile-%{random_suffix}-new"
497+
location = "us-central1"
498+
language_code = "en-US"
499+
}
500+
`, context)
501+
}

0 commit comments

Comments
 (0)