Skip to content

Commit 618af44

Browse files
Documentai Processor (#6083) (#4389)
* Adding documentai processor * Document AI Processor, not working due to unimplemented API methods? * Remove default * Remove default * Add default version * Fix test * Use id over name, remove unused file * Add note on delete Signed-off-by: Modular Magician <[email protected]>
1 parent e926497 commit 618af44

11 files changed

+958
-2
lines changed

.changelog/6083.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:new-resource
2+
google_documentai_processor
3+
```
4+
```release-note:new-resource
5+
google_documentai_processor_default_version
6+
```

google-beta/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ type Config struct {
197197
DialogflowBasePath string
198198
DialogflowCXBasePath string
199199
DNSBasePath string
200+
DocumentAIBasePath string
200201
EssentialContactsBasePath string
201202
FilestoreBasePath string
202203
FirebaseBasePath string
@@ -310,6 +311,7 @@ const DeploymentManagerBasePathKey = "DeploymentManager"
310311
const DialogflowBasePathKey = "Dialogflow"
311312
const DialogflowCXBasePathKey = "DialogflowCX"
312313
const DNSBasePathKey = "DNS"
314+
const DocumentAIBasePathKey = "DocumentAI"
313315
const EssentialContactsBasePathKey = "EssentialContacts"
314316
const FilestoreBasePathKey = "Filestore"
315317
const FirebaseBasePathKey = "Firebase"
@@ -404,6 +406,7 @@ var DefaultBasePaths = map[string]string{
404406
DialogflowBasePathKey: "https://dialogflow.googleapis.com/v2/",
405407
DialogflowCXBasePathKey: "https://{{location}}-dialogflow.googleapis.com/v3/",
406408
DNSBasePathKey: "https://dns.googleapis.com/dns/v1beta2/",
409+
DocumentAIBasePathKey: "https://documentai.googleapis.com/v1/",
407410
EssentialContactsBasePathKey: "https://essentialcontacts.googleapis.com/v1/",
408411
FilestoreBasePathKey: "https://file.googleapis.com/v1beta1/",
409412
FirebaseBasePathKey: "https://firebase.googleapis.com/v1beta1/",
@@ -1274,6 +1277,7 @@ func ConfigureBasePaths(c *Config) {
12741277
c.DialogflowBasePath = DefaultBasePaths[DialogflowBasePathKey]
12751278
c.DialogflowCXBasePath = DefaultBasePaths[DialogflowCXBasePathKey]
12761279
c.DNSBasePath = DefaultBasePaths[DNSBasePathKey]
1280+
c.DocumentAIBasePath = DefaultBasePaths[DocumentAIBasePathKey]
12771281
c.EssentialContactsBasePath = DefaultBasePaths[EssentialContactsBasePathKey]
12781282
c.FilestoreBasePath = DefaultBasePaths[FilestoreBasePathKey]
12791283
c.FirebaseBasePath = DefaultBasePaths[FirebaseBasePathKey]

google-beta/provider.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ func Provider() *schema.Provider {
437437
"GOOGLE_DNS_CUSTOM_ENDPOINT",
438438
}, DefaultBasePaths[DNSBasePathKey]),
439439
},
440+
"document_ai_custom_endpoint": {
441+
Type: schema.TypeString,
442+
Optional: true,
443+
ValidateFunc: validateCustomEndpoint,
444+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
445+
"GOOGLE_DOCUMENT_AI_CUSTOM_ENDPOINT",
446+
}, DefaultBasePaths[DocumentAIBasePathKey]),
447+
},
440448
"essential_contacts_custom_endpoint": {
441449
Type: schema.TypeString,
442450
Optional: true,
@@ -939,9 +947,9 @@ func Provider() *schema.Provider {
939947
return provider
940948
}
941949

942-
// Generated resources: 250
950+
// Generated resources: 252
943951
// Generated IAM resources: 141
944-
// Total generated resources: 391
952+
// Total generated resources: 393
945953
func ResourceMap() map[string]*schema.Resource {
946954
resourceMap, _ := ResourceMapWithErrors()
947955
return resourceMap
@@ -1175,6 +1183,8 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
11751183
"google_dns_policy": resourceDNSPolicy(),
11761184
"google_dns_response_policy": resourceDNSResponsePolicy(),
11771185
"google_dns_response_policy_rule": resourceDNSResponsePolicyRule(),
1186+
"google_document_ai_processor": resourceDocumentAIProcessor(),
1187+
"google_document_ai_processor_default_version": resourceDocumentAIProcessorDefaultVersion(),
11781188
"google_essential_contacts_contact": resourceEssentialContactsContact(),
11791189
"google_filestore_instance": resourceFilestoreInstance(),
11801190
"google_firebase_project": resourceFirebaseProject(),
@@ -1637,6 +1647,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
16371647
config.DialogflowBasePath = d.Get("dialogflow_custom_endpoint").(string)
16381648
config.DialogflowCXBasePath = d.Get("dialogflow_cx_custom_endpoint").(string)
16391649
config.DNSBasePath = d.Get("dns_custom_endpoint").(string)
1650+
config.DocumentAIBasePath = d.Get("document_ai_custom_endpoint").(string)
16401651
config.EssentialContactsBasePath = d.Get("essential_contacts_custom_endpoint").(string)
16411652
config.FilestoreBasePath = d.Get("filestore_custom_endpoint").(string)
16421653
config.FirebaseBasePath = d.Get("firebase_custom_endpoint").(string)
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
15+
package google
16+
17+
import (
18+
"fmt"
19+
"log"
20+
"reflect"
21+
"time"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
24+
)
25+
26+
func resourceDocumentAIProcessor() *schema.Resource {
27+
return &schema.Resource{
28+
Create: resourceDocumentAIProcessorCreate,
29+
Read: resourceDocumentAIProcessorRead,
30+
Delete: resourceDocumentAIProcessorDelete,
31+
32+
Importer: &schema.ResourceImporter{
33+
State: resourceDocumentAIProcessorImport,
34+
},
35+
36+
Timeouts: &schema.ResourceTimeout{
37+
Create: schema.DefaultTimeout(20 * time.Minute),
38+
Delete: schema.DefaultTimeout(20 * time.Minute),
39+
},
40+
41+
Schema: map[string]*schema.Schema{
42+
"display_name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
ForceNew: true,
46+
Description: `The display name. Must be unique.`,
47+
},
48+
"location": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
ForceNew: true,
52+
Description: `The location of the resource.`,
53+
},
54+
"type": {
55+
Type: schema.TypeString,
56+
Required: true,
57+
ForceNew: true,
58+
Description: `The type of processor. For possible types see the [official list](https://cloud.google.com/document-ai/docs/reference/rest/v1/projects.locations/fetchProcessorTypes#google.cloud.documentai.v1.DocumentProcessorService.FetchProcessorTypes)`,
59+
},
60+
"kms_key_name": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
ForceNew: true,
64+
Description: `The KMS key used for encryption/decryption in CMEK scenarios. See https://cloud.google.com/security-key-management.`,
65+
},
66+
"name": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: `The resource name of the processor.`,
70+
},
71+
"project": {
72+
Type: schema.TypeString,
73+
Optional: true,
74+
Computed: true,
75+
ForceNew: true,
76+
},
77+
},
78+
UseJSONNumber: true,
79+
}
80+
}
81+
82+
func resourceDocumentAIProcessorCreate(d *schema.ResourceData, meta interface{}) error {
83+
config := meta.(*Config)
84+
userAgent, err := generateUserAgentString(d, config.userAgent)
85+
if err != nil {
86+
return err
87+
}
88+
89+
obj := make(map[string]interface{})
90+
typeProp, err := expandDocumentAIProcessorType(d.Get("type"), d, config)
91+
if err != nil {
92+
return err
93+
} else if v, ok := d.GetOkExists("type"); !isEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) {
94+
obj["type"] = typeProp
95+
}
96+
displayNameProp, err := expandDocumentAIProcessorDisplayName(d.Get("display_name"), d, config)
97+
if err != nil {
98+
return err
99+
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
100+
obj["displayName"] = displayNameProp
101+
}
102+
kmsKeyNameProp, err := expandDocumentAIProcessorKmsKeyName(d.Get("kms_key_name"), d, config)
103+
if err != nil {
104+
return err
105+
} else if v, ok := d.GetOkExists("kms_key_name"); !isEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
106+
obj["kmsKeyName"] = kmsKeyNameProp
107+
}
108+
109+
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}projects/{{project}}/locations/{{location}}/processors")
110+
if err != nil {
111+
return err
112+
}
113+
114+
log.Printf("[DEBUG] Creating new Processor: %#v", obj)
115+
billingProject := ""
116+
117+
project, err := getProject(d, config)
118+
if err != nil {
119+
return fmt.Errorf("Error fetching project for Processor: %s", err)
120+
}
121+
billingProject = project
122+
123+
// err == nil indicates that the billing_project value was found
124+
if bp, err := getBillingProject(d, config); err == nil {
125+
billingProject = bp
126+
}
127+
128+
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
129+
if err != nil {
130+
return fmt.Errorf("Error creating Processor: %s", err)
131+
}
132+
if err := d.Set("name", flattenDocumentAIProcessorName(res["name"], d, config)); err != nil {
133+
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
134+
}
135+
136+
// Store the ID now
137+
id, err := replaceVars(d, config, "{{name}}")
138+
if err != nil {
139+
return fmt.Errorf("Error constructing id: %s", err)
140+
}
141+
d.SetId(id)
142+
143+
log.Printf("[DEBUG] Finished creating Processor %q: %#v", d.Id(), res)
144+
145+
return resourceDocumentAIProcessorRead(d, meta)
146+
}
147+
148+
func resourceDocumentAIProcessorRead(d *schema.ResourceData, meta interface{}) error {
149+
config := meta.(*Config)
150+
userAgent, err := generateUserAgentString(d, config.userAgent)
151+
if err != nil {
152+
return err
153+
}
154+
155+
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
156+
if err != nil {
157+
return err
158+
}
159+
160+
billingProject := ""
161+
162+
project, err := getProject(d, config)
163+
if err != nil {
164+
return fmt.Errorf("Error fetching project for Processor: %s", err)
165+
}
166+
billingProject = project
167+
168+
// err == nil indicates that the billing_project value was found
169+
if bp, err := getBillingProject(d, config); err == nil {
170+
billingProject = bp
171+
}
172+
173+
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
174+
if err != nil {
175+
return handleNotFoundError(err, d, fmt.Sprintf("DocumentAIProcessor %q", d.Id()))
176+
}
177+
178+
if err := d.Set("project", project); err != nil {
179+
return fmt.Errorf("Error reading Processor: %s", err)
180+
}
181+
182+
if err := d.Set("name", flattenDocumentAIProcessorName(res["name"], d, config)); err != nil {
183+
return fmt.Errorf("Error reading Processor: %s", err)
184+
}
185+
if err := d.Set("type", flattenDocumentAIProcessorType(res["type"], d, config)); err != nil {
186+
return fmt.Errorf("Error reading Processor: %s", err)
187+
}
188+
if err := d.Set("display_name", flattenDocumentAIProcessorDisplayName(res["displayName"], d, config)); err != nil {
189+
return fmt.Errorf("Error reading Processor: %s", err)
190+
}
191+
if err := d.Set("kms_key_name", flattenDocumentAIProcessorKmsKeyName(res["kmsKeyName"], d, config)); err != nil {
192+
return fmt.Errorf("Error reading Processor: %s", err)
193+
}
194+
195+
return nil
196+
}
197+
198+
func resourceDocumentAIProcessorDelete(d *schema.ResourceData, meta interface{}) error {
199+
config := meta.(*Config)
200+
userAgent, err := generateUserAgentString(d, config.userAgent)
201+
if err != nil {
202+
return err
203+
}
204+
205+
billingProject := ""
206+
207+
project, err := getProject(d, config)
208+
if err != nil {
209+
return fmt.Errorf("Error fetching project for Processor: %s", err)
210+
}
211+
billingProject = project
212+
213+
url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}")
214+
if err != nil {
215+
return err
216+
}
217+
218+
var obj map[string]interface{}
219+
log.Printf("[DEBUG] Deleting Processor %q", d.Id())
220+
221+
// err == nil indicates that the billing_project value was found
222+
if bp, err := getBillingProject(d, config); err == nil {
223+
billingProject = bp
224+
}
225+
226+
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
227+
if err != nil {
228+
return handleNotFoundError(err, d, "Processor")
229+
}
230+
231+
log.Printf("[DEBUG] Finished deleting Processor %q: %#v", d.Id(), res)
232+
return nil
233+
}
234+
235+
func resourceDocumentAIProcessorImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
236+
config := meta.(*Config)
237+
if err := parseImportId([]string{
238+
"(?P<name>.+)",
239+
}, d, config); err != nil {
240+
return nil, err
241+
}
242+
243+
// Replace import id for the resource id
244+
id, err := replaceVars(d, config, "{{name}}")
245+
if err != nil {
246+
return nil, fmt.Errorf("Error constructing id: %s", err)
247+
}
248+
d.SetId(id)
249+
250+
return []*schema.ResourceData{d}, nil
251+
}
252+
253+
func flattenDocumentAIProcessorName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
254+
return v
255+
}
256+
257+
func flattenDocumentAIProcessorType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
258+
return v
259+
}
260+
261+
func flattenDocumentAIProcessorDisplayName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
262+
return v
263+
}
264+
265+
func flattenDocumentAIProcessorKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
266+
return v
267+
}
268+
269+
func expandDocumentAIProcessorType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
270+
return v, nil
271+
}
272+
273+
func expandDocumentAIProcessorDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
274+
return v, nil
275+
}
276+
277+
func expandDocumentAIProcessorKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
278+
return v, nil
279+
}

0 commit comments

Comments
 (0)