Skip to content
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

fix documentai so eu region is supported #12074

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/6229.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
documentai: fixed bug where eu region could not be utilized for documentai resources
```
13 changes: 13 additions & 0 deletions google/common_diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -219,3 +220,15 @@ func lastSlashDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
}
return new == old
}

// Suppress diffs when the value read from api
// has the project number instead of the project name
func projectNumberDiffSupress(_, old, new string, _ *schema.ResourceData) bool {
var a2, b2 string
reN := regexp.MustCompile("projects/\\d+")
re := regexp.MustCompile("projects/[^/]+")
replacement := []byte("projects/equal")
a2 = string(reN.ReplaceAll([]byte(old), replacement))
b2 = string(re.ReplaceAll([]byte(new), replacement))
return a2 == b2
}
2 changes: 1 addition & 1 deletion google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ var DefaultBasePaths = map[string]string{
DialogflowBasePathKey: "https://dialogflow.googleapis.com/v2/",
DialogflowCXBasePathKey: "https://{{location}}-dialogflow.googleapis.com/v3/",
DNSBasePathKey: "https://dns.googleapis.com/dns/v1/",
DocumentAIBasePathKey: "https://documentai.googleapis.com/v1/",
DocumentAIBasePathKey: "https://{{location}}-documentai.googleapis.com/v1/",
EssentialContactsBasePathKey: "https://essentialcontacts.googleapis.com/v1/",
FilestoreBasePathKey: "https://file.googleapis.com/v1/",
FirestoreBasePathKey: "https://firestore.googleapis.com/v1/",
Expand Down
17 changes: 11 additions & 6 deletions google/resource_document_ai_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func resourceDocumentAIProcessorCreate(d *schema.ResourceData, meta interface{})
}

// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/processors/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -152,7 +152,7 @@ func resourceDocumentAIProcessorRead(d *schema.ResourceData, meta interface{}) e
return err
}

url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func resourceDocumentAIProcessorDelete(d *schema.ResourceData, meta interface{})
}
billingProject = project

url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
if err != nil {
return err
}
Expand All @@ -235,13 +235,15 @@ func resourceDocumentAIProcessorDelete(d *schema.ResourceData, meta interface{})
func resourceDocumentAIProcessorImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"(?P<name>.+)",
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/processors/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)",
"(?P<location>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/processors/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -251,7 +253,10 @@ func resourceDocumentAIProcessorImport(d *schema.ResourceData, meta interface{})
}

func flattenDocumentAIProcessorName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
if v == nil {
return v
}
return NameFromSelfLinkStateFunc(v)
}

func flattenDocumentAIProcessorType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand Down
20 changes: 16 additions & 4 deletions google/resource_document_ai_processor_default_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -46,10 +47,11 @@ func resourceDocumentAIProcessorDefaultVersion() *schema.Resource {
Description: `The processor to set the version on.`,
},
"version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The version to set`,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: projectNumberDiffSupress,
Description: `The version to set`,
},
},
UseJSONNumber: true,
Expand Down Expand Up @@ -90,6 +92,11 @@ func resourceDocumentAIProcessorDefaultVersionCreate(d *schema.ResourceData, met
billingProject = bp
}

if strings.Contains(url, "https://-") {
location := GetRegionFromRegionalSelfLink(url)
url = strings.TrimPrefix(url, "https://")
url = "https://" + location + url
}
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating ProcessorDefaultVersion: %s", err)
Expand Down Expand Up @@ -126,6 +133,11 @@ func resourceDocumentAIProcessorDefaultVersionRead(d *schema.ResourceData, meta
billingProject = bp
}

if strings.Contains(url, "https://-") {
location := GetRegionFromRegionalSelfLink(url)
url = strings.TrimPrefix(url, "https://")
url = "https://" + location + url
}
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DocumentAIProcessorDefaultVersion %q", d.Id()))
Expand Down
37 changes: 36 additions & 1 deletion google/resource_document_ai_processor_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ resource "google_document_ai_processor" "processor" {
`, context)
}

func TestAccDocumentAIProcessor_documentaiProcessorEuExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDocumentAIProcessorDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDocumentAIProcessor_documentaiProcessorEuExample(context),
},
{
ResourceName: "google_document_ai_processor.processor",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}

func testAccDocumentAIProcessor_documentaiProcessorEuExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_document_ai_processor" "processor" {
location = "eu"
display_name = "tf-test-test-processor%{random_suffix}"
type = "OCR_PROCESSOR"
}
`, context)
}

func testAccCheckDocumentAIProcessorDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand All @@ -70,7 +105,7 @@ func testAccCheckDocumentAIProcessorDestroyProducer(t *testing.T) func(s *terraf

config := googleProviderConfig(t)

url, err := replaceVarsForTest(config, rs, "{{DocumentAIBasePath}}{{name}}")
url, err := replaceVarsForTest(config, rs, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions google/resource_document_ai_processor_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testSweepDocumentAIProcessor(region string) error {
},
}

listTemplate := strings.Split("https://documentai.googleapis.com/v1/{{name}}", "?")[0]
listTemplate := strings.Split("https://{{location}}-documentai.googleapis.com/v1/projects/{{project}}/locations/{{location}}/processors", "?")[0]
listUrl, err := replaceVars(d, config, listTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err)
Expand Down Expand Up @@ -99,7 +99,7 @@ func testSweepDocumentAIProcessor(region string) error {
continue
}

deleteTemplate := "https://documentai.googleapis.com/v1/{{name}}"
deleteTemplate := "https://{{location}}-documentai.googleapis.com/v1/projects/{{project}}/locations/{{location}}/processors/{{name}}"
deleteUrl, err := replaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
Expand Down
10 changes: 8 additions & 2 deletions website/docs/r/document_ai_processor.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ The following arguments are supported:

In addition to the arguments listed above, the following computed attributes are exported:

* `id` - an identifier for the resource with format `{{name}}`
* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{location}}/processors/{{name}}`

* `name` -
The resource name of the processor.
Expand All @@ -100,5 +100,11 @@ This resource provides the following
Processor can be imported using any of these accepted formats:

```
$ terraform import google_document_ai_processor.default {{name}}
$ terraform import google_document_ai_processor.default projects/{{project}}/locations/{{location}}/processors/{{name}}
$ terraform import google_document_ai_processor.default {{project}}/{{location}}/{{name}}
$ terraform import google_document_ai_processor.default {{location}}/{{name}}
```

## User Project Overrides

This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override).