Skip to content

Commit

Permalink
Dialogflow CX location (#5922) (#11470)
Browse files Browse the repository at this point in the history
* Change to use region API url for Dialogflow CX

* change regexp in location extraction

* Update operation location

* Order custom functions by CRUD

Co-authored-by: “Javier <javier-canizalez@outlook.com>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: “Javier <javier-canizalez@outlook.com>
  • Loading branch information
modular-magician and jcanizalez committed Apr 11, 2022
1 parent bb5beeb commit 5290a9f
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/5922.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
dialogflow: added support for location based dialogflow resources
```
2 changes: 1 addition & 1 deletion google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ var DefaultBasePaths = map[string]string{
DatastoreBasePathKey: "https://datastore.googleapis.com/v1/",
DeploymentManagerBasePathKey: "https://www.googleapis.com/deploymentmanager/v2/",
DialogflowBasePathKey: "https://dialogflow.googleapis.com/v2/",
DialogflowCXBasePathKey: "https://dialogflow.googleapis.com/v3/",
DialogflowCXBasePathKey: "https://{{location}}-dialogflow.googleapis.com/v3/",
DNSBasePathKey: "https://dns.googleapis.com/dns/v1/",
EssentialContactsBasePathKey: "https://essentialcontacts.googleapis.com/v1/",
FilestoreBasePathKey: "https://file.googleapis.com/v1/",
Expand Down
2 changes: 1 addition & 1 deletion google/dialogflow_cx_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (w *DialogflowCXOperationWaiter) QueryOp() (interface{}, error) {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
// Returns the proper get.
url := fmt.Sprintf("%s%s", w.Config.DialogflowCXBasePath, w.CommonOperationWaiter.Op.Name)
url := fmt.Sprintf("https://dialogflow.googleapis.com/v3/%s", w.CommonOperationWaiter.Op.Name)

return sendRequest(w.Config, "GET", "", url, w.UserAgent, nil)
}
Expand Down
59 changes: 59 additions & 0 deletions google/resource_dialogflow_cx_entity_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -208,6 +209,20 @@ func resourceDialogflowCXEntityTypeCreate(d *schema.ResourceData, meta interface
billingProject = bp
}

// 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)
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating EntityType: %s", err)
Expand Down Expand Up @@ -247,6 +262,20 @@ func resourceDialogflowCXEntityTypeRead(d *schema.ResourceData, meta interface{}
billingProject = bp
}

// 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)
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DialogflowCXEntityType %q", d.Id()))
Expand Down Expand Up @@ -375,6 +404,21 @@ func resourceDialogflowCXEntityTypeUpdate(d *schema.ResourceData, meta interface
return err
}

// 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)

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down Expand Up @@ -406,6 +450,21 @@ func resourceDialogflowCXEntityTypeDelete(d *schema.ResourceData, meta interface
}

var obj map[string]interface{}

// 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)
log.Printf("[DEBUG] Deleting EntityType %q", d.Id())

// err == nil indicates that the billing_project value was found
Expand Down
59 changes: 59 additions & 0 deletions google/resource_dialogflow_cx_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -131,6 +132,20 @@ func resourceDialogflowCXEnvironmentCreate(d *schema.ResourceData, meta interfac
billingProject = bp
}

// 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)
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating Environment: %s", err)
Expand Down Expand Up @@ -190,6 +205,20 @@ func resourceDialogflowCXEnvironmentRead(d *schema.ResourceData, meta interface{
billingProject = bp
}

// 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)
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DialogflowCXEnvironment %q", d.Id()))
Expand Down Expand Up @@ -269,6 +298,21 @@ func resourceDialogflowCXEnvironmentUpdate(d *schema.ResourceData, meta interfac
return err
}

// 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)

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down Expand Up @@ -308,6 +352,21 @@ func resourceDialogflowCXEnvironmentDelete(d *schema.ResourceData, meta interfac
}

var obj map[string]interface{}

// 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)
log.Printf("[DEBUG] Deleting Environment %q", d.Id())

// err == nil indicates that the billing_project value was found
Expand Down
59 changes: 59 additions & 0 deletions google/resource_dialogflow_cx_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -384,6 +385,20 @@ func resourceDialogflowCXFlowCreate(d *schema.ResourceData, meta interface{}) er
billingProject = bp
}

// 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)
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating Flow: %s", err)
Expand Down Expand Up @@ -423,6 +438,20 @@ func resourceDialogflowCXFlowRead(d *schema.ResourceData, meta interface{}) erro
billingProject = bp
}

// 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)
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DialogflowCXFlow %q", d.Id()))
Expand Down Expand Up @@ -541,6 +570,21 @@ func resourceDialogflowCXFlowUpdate(d *schema.ResourceData, meta interface{}) er
return err
}

// 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)

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down Expand Up @@ -572,6 +616,21 @@ func resourceDialogflowCXFlowDelete(d *schema.ResourceData, meta interface{}) er
}

var obj map[string]interface{}

// 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)
log.Printf("[DEBUG] Deleting Flow %q", d.Id())

// err == nil indicates that the billing_project value was found
Expand Down
59 changes: 59 additions & 0 deletions google/resource_dialogflow_cx_intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -247,6 +248,20 @@ func resourceDialogflowCXIntentCreate(d *schema.ResourceData, meta interface{})
billingProject = bp
}

// 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)
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating Intent: %s", err)
Expand Down Expand Up @@ -286,6 +301,20 @@ func resourceDialogflowCXIntentRead(d *schema.ResourceData, meta interface{}) er
billingProject = bp
}

// 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)
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DialogflowCXIntent %q", d.Id()))
Expand Down Expand Up @@ -417,6 +446,21 @@ func resourceDialogflowCXIntentUpdate(d *schema.ResourceData, meta interface{})
return err
}

// 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)

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down Expand Up @@ -448,6 +492,21 @@ func resourceDialogflowCXIntentDelete(d *schema.ResourceData, meta interface{})
}

var obj map[string]interface{}

// 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)
log.Printf("[DEBUG] Deleting Intent %q", d.Id())

// err == nil indicates that the billing_project value was found
Expand Down
Loading

0 comments on commit 5290a9f

Please sign in to comment.