Skip to content

Commit c53d322

Browse files
fix documentai so eu region is supported (#6229) (#12074)
Signed-off-by: Modular Magician <[email protected]>
1 parent 490b306 commit c53d322

8 files changed

+90
-16
lines changed

.changelog/6229.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
documentai: fixed bug where eu region could not be utilized for documentai resources
3+
```

google/common_diff_suppress.go

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net"
1010
"reflect"
11+
"regexp"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -219,3 +220,15 @@ func lastSlashDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
219220
}
220221
return new == old
221222
}
223+
224+
// Suppress diffs when the value read from api
225+
// has the project number instead of the project name
226+
func projectNumberDiffSupress(_, old, new string, _ *schema.ResourceData) bool {
227+
var a2, b2 string
228+
reN := regexp.MustCompile("projects/\\d+")
229+
re := regexp.MustCompile("projects/[^/]+")
230+
replacement := []byte("projects/equal")
231+
a2 = string(reN.ReplaceAll([]byte(old), replacement))
232+
b2 = string(re.ReplaceAll([]byte(new), replacement))
233+
return a2 == b2
234+
}

google/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ var DefaultBasePaths = map[string]string{
375375
DialogflowBasePathKey: "https://dialogflow.googleapis.com/v2/",
376376
DialogflowCXBasePathKey: "https://{{location}}-dialogflow.googleapis.com/v3/",
377377
DNSBasePathKey: "https://dns.googleapis.com/dns/v1/",
378-
DocumentAIBasePathKey: "https://documentai.googleapis.com/v1/",
378+
DocumentAIBasePathKey: "https://{{location}}-documentai.googleapis.com/v1/",
379379
EssentialContactsBasePathKey: "https://essentialcontacts.googleapis.com/v1/",
380380
FilestoreBasePathKey: "https://file.googleapis.com/v1/",
381381
FirestoreBasePathKey: "https://firestore.googleapis.com/v1/",

google/resource_document_ai_processor.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func resourceDocumentAIProcessorCreate(d *schema.ResourceData, meta interface{})
134134
}
135135

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

155-
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
155+
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
156156
if err != nil {
157157
return err
158158
}
@@ -210,7 +210,7 @@ func resourceDocumentAIProcessorDelete(d *schema.ResourceData, meta interface{})
210210
}
211211
billingProject = project
212212

213-
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
213+
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
214214
if err != nil {
215215
return err
216216
}
@@ -235,13 +235,15 @@ func resourceDocumentAIProcessorDelete(d *schema.ResourceData, meta interface{})
235235
func resourceDocumentAIProcessorImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
236236
config := meta.(*Config)
237237
if err := parseImportId([]string{
238-
"(?P<name>.+)",
238+
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/processors/(?P<name>[^/]+)",
239+
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)",
240+
"(?P<location>[^/]+)/(?P<name>[^/]+)",
239241
}, d, config); err != nil {
240242
return nil, err
241243
}
242244

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

253255
func flattenDocumentAIProcessorName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
254-
return v
256+
if v == nil {
257+
return v
258+
}
259+
return NameFromSelfLinkStateFunc(v)
255260
}
256261

257262
func flattenDocumentAIProcessorType(v interface{}, d *schema.ResourceData, config *Config) interface{} {

google/resource_document_ai_processor_default_version.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"reflect"
21+
"strings"
2122
"time"
2223

2324
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -46,10 +47,11 @@ func resourceDocumentAIProcessorDefaultVersion() *schema.Resource {
4647
Description: `The processor to set the version on.`,
4748
},
4849
"version": {
49-
Type: schema.TypeString,
50-
Required: true,
51-
ForceNew: true,
52-
Description: `The version to set`,
50+
Type: schema.TypeString,
51+
Required: true,
52+
ForceNew: true,
53+
DiffSuppressFunc: projectNumberDiffSupress,
54+
Description: `The version to set`,
5355
},
5456
},
5557
UseJSONNumber: true,
@@ -90,6 +92,11 @@ func resourceDocumentAIProcessorDefaultVersionCreate(d *schema.ResourceData, met
9092
billingProject = bp
9193
}
9294

95+
if strings.Contains(url, "https://-") {
96+
location := GetRegionFromRegionalSelfLink(url)
97+
url = strings.TrimPrefix(url, "https://")
98+
url = "https://" + location + url
99+
}
93100
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
94101
if err != nil {
95102
return fmt.Errorf("Error creating ProcessorDefaultVersion: %s", err)
@@ -126,6 +133,11 @@ func resourceDocumentAIProcessorDefaultVersionRead(d *schema.ResourceData, meta
126133
billingProject = bp
127134
}
128135

136+
if strings.Contains(url, "https://-") {
137+
location := GetRegionFromRegionalSelfLink(url)
138+
url = strings.TrimPrefix(url, "https://")
139+
url = "https://" + location + url
140+
}
129141
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
130142
if err != nil {
131143
return handleNotFoundError(err, d, fmt.Sprintf("DocumentAIProcessorDefaultVersion %q", d.Id()))

google/resource_document_ai_processor_generated_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ resource "google_document_ai_processor" "processor" {
5858
`, context)
5959
}
6060

61+
func TestAccDocumentAIProcessor_documentaiProcessorEuExample(t *testing.T) {
62+
t.Parallel()
63+
64+
context := map[string]interface{}{
65+
"random_suffix": randString(t, 10),
66+
}
67+
68+
vcrTest(t, resource.TestCase{
69+
PreCheck: func() { testAccPreCheck(t) },
70+
Providers: testAccProviders,
71+
CheckDestroy: testAccCheckDocumentAIProcessorDestroyProducer(t),
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testAccDocumentAIProcessor_documentaiProcessorEuExample(context),
75+
},
76+
{
77+
ResourceName: "google_document_ai_processor.processor",
78+
ImportState: true,
79+
ImportStateVerify: true,
80+
ImportStateVerifyIgnore: []string{"location"},
81+
},
82+
},
83+
})
84+
}
85+
86+
func testAccDocumentAIProcessor_documentaiProcessorEuExample(context map[string]interface{}) string {
87+
return Nprintf(`
88+
resource "google_document_ai_processor" "processor" {
89+
location = "eu"
90+
display_name = "tf-test-test-processor%{random_suffix}"
91+
type = "OCR_PROCESSOR"
92+
}
93+
`, context)
94+
}
95+
6196
func testAccCheckDocumentAIProcessorDestroyProducer(t *testing.T) func(s *terraform.State) error {
6297
return func(s *terraform.State) error {
6398
for name, rs := range s.RootModule().Resources {
@@ -70,7 +105,7 @@ func testAccCheckDocumentAIProcessorDestroyProducer(t *testing.T) func(s *terraf
70105

71106
config := googleProviderConfig(t)
72107

73-
url, err := replaceVarsForTest(config, rs, "{{DocumentAIBasePath}}{{name}}")
108+
url, err := replaceVarsForTest(config, rs, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors/{{name}}")
74109
if err != nil {
75110
return err
76111
}

google/resource_document_ai_processor_sweeper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func testSweepDocumentAIProcessor(region string) error {
6161
},
6262
}
6363

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

102-
deleteTemplate := "https://documentai.googleapis.com/v1/{{name}}"
102+
deleteTemplate := "https://{{location}}-documentai.googleapis.com/v1/projects/{{project}}/locations/{{location}}/processors/{{name}}"
103103
deleteUrl, err := replaceVars(d, config, deleteTemplate)
104104
if err != nil {
105105
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)

website/docs/r/document_ai_processor.html.markdown

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ The following arguments are supported:
8080

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

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

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

102102
```
103-
$ terraform import google_document_ai_processor.default {{name}}
103+
$ terraform import google_document_ai_processor.default projects/{{project}}/locations/{{location}}/processors/{{name}}
104+
$ terraform import google_document_ai_processor.default {{project}}/{{location}}/{{name}}
105+
$ terraform import google_document_ai_processor.default {{location}}/{{name}}
104106
```
107+
108+
## User Project Overrides
109+
110+
This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override).

0 commit comments

Comments
 (0)