From d1aceca358fa7b97d7d886a20e9591320748e599 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Mon, 23 May 2022 14:16:32 -0700 Subject: [PATCH 1/8] Adding documentai processor --- mmv1/products/documentai/api.yaml | 74 +++++++++++++++++++ mmv1/products/documentai/terraform.yaml | 30 ++++++++ .../examples/documentai_processor.tf.erb | 5 ++ 3 files changed, 109 insertions(+) create mode 100644 mmv1/products/documentai/api.yaml create mode 100644 mmv1/products/documentai/terraform.yaml create mode 100644 mmv1/templates/terraform/examples/documentai_processor.tf.erb diff --git a/mmv1/products/documentai/api.yaml b/mmv1/products/documentai/api.yaml new file mode 100644 index 000000000000..7aeab387ae82 --- /dev/null +++ b/mmv1/products/documentai/api.yaml @@ -0,0 +1,74 @@ +# Copyright 2021 Google Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- !ruby/object:Api::Product +name: DocumentAI +display_name: Essential Contacts +versions: + - !ruby/object:Api::Product::Version + name: ga + base_url: https://documentai.googleapis.com/v1/ +scopes: + - https://www.googleapis.com/auth/cloud-platform +apis_required: + - !ruby/object:Api::Product::ApiReference + name: Document AI API + url: https://console.cloud.google.com/apis/api/documentai.googleapis.com/overview +objects: + - !ruby/object:Api::Resource + name: 'Processor' + create_url: 'projects/{{project}}/locations/{{location}}/processors' + self_link: '{{name}}' + base_url: '{{name}}' + references: !ruby/object:Api::Resource::ReferenceLinks + guides: + 'Official Documentation': + 'https://cloud.google.com/document-ai/docs/overview' + api: 'https://cloud.google.com/document-ai/docs/reference/rest/v1/projects.locations.processors' + description: | + The first-class citizen for Document AI. Each processor defines how to extract structural information from a document. + parameters: + - !ruby/object:Api::Type::String + name: location + description: | + The location of the resource. + input: true + url_param_only: true + required: true + properties: + - !ruby/object:Api::Type::String + name: 'name' + description: | + The resource name of the processor. + output: true + - !ruby/object:Api::Type::String + name: 'type' + required: true + input: true + 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) + - !ruby/object:Api::Type::String + name: 'displayName' + required: true + description: | + The display name. Must be unique. + - !ruby/object:Api::Type::String + name: 'defaultProcessorVersion' + output: true + description: | + The default processor version. + - !ruby/object:Api::Type::String + name: 'kmsKeyName' + description: | + The KMS key used for encryption/decryption in CMEK scenarios. See https://cloud.google.com/security-key-management. + diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml new file mode 100644 index 000000000000..98f7b387c346 --- /dev/null +++ b/mmv1/products/documentai/terraform.yaml @@ -0,0 +1,30 @@ +# Copyright 2021 Google Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- !ruby/object:Provider::Terraform::Config +overrides: !ruby/object:Overrides::ResourceOverrides + Processor: !ruby/object:Overrides::Terraform::ResourceOverride + import_format: ["{{%name}}"] + examples: + - !ruby/object:Provider::Terraform::Examples + name: "documentai_processor" + primary_resource_id: "processor" + vars: + processor_name: "test-processor" + +# This is for copying files over +files: !ruby/object:Provider::Config::Files + # These files have templating (ERB) code that will be run. + # This is usually to add licensing info, autogeneration notices, etc. + compile: +<%= lines(indent(compile('provider/terraform/product~compile.yaml'), 4)) -%> diff --git a/mmv1/templates/terraform/examples/documentai_processor.tf.erb b/mmv1/templates/terraform/examples/documentai_processor.tf.erb new file mode 100644 index 000000000000..7dfbec6b7ac2 --- /dev/null +++ b/mmv1/templates/terraform/examples/documentai_processor.tf.erb @@ -0,0 +1,5 @@ +resource "google_document_ai_processor" "<%= ctx[:primary_resource_id] %>" { + location = "us" + display_name = "<%= ctx[:vars]['processor_name'] %>" + type = "OCR_PROCESSOR" +} From 67bf519015570e8837d83d36d1abe7ec70d6ba3a Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Thu, 26 May 2022 13:03:22 -0700 Subject: [PATCH 2/8] Document AI Processor, not working due to unimplemented API methods? --- mmv1/products/documentai/api.yaml | 5 +++- mmv1/products/documentai/terraform.yaml | 10 +++++++ .../documentai_processor_with_default.tf.erb | 6 ++++ .../post_create/documentai_processor.go.erb | 29 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb create mode 100644 mmv1/templates/terraform/post_create/documentai_processor.go.erb diff --git a/mmv1/products/documentai/api.yaml b/mmv1/products/documentai/api.yaml index 7aeab387ae82..42dbcd3e9fdb 100644 --- a/mmv1/products/documentai/api.yaml +++ b/mmv1/products/documentai/api.yaml @@ -60,15 +60,18 @@ objects: - !ruby/object:Api::Type::String name: 'displayName' required: true + input: true description: | The display name. Must be unique. - !ruby/object:Api::Type::String name: 'defaultProcessorVersion' - output: true + update_verb: :POST + update_url: '{{name}}:setDefaultProcessorVersion' description: | The default processor version. - !ruby/object:Api::Type::String name: 'kmsKeyName' + input: true description: | The KMS key used for encryption/decryption in CMEK scenarios. See https://cloud.google.com/security-key-management. diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml index 98f7b387c346..deba038c0c33 100644 --- a/mmv1/products/documentai/terraform.yaml +++ b/mmv1/products/documentai/terraform.yaml @@ -15,12 +15,22 @@ overrides: !ruby/object:Overrides::ResourceOverrides Processor: !ruby/object:Overrides::Terraform::ResourceOverride import_format: ["{{%name}}"] + properties: + defaultProcessorVersion: !ruby/object:Overrides::Terraform::PropertyOverride + default_from_api: true examples: - !ruby/object:Provider::Terraform::Examples name: "documentai_processor" primary_resource_id: "processor" vars: processor_name: "test-processor" + - !ruby/object:Provider::Terraform::Examples + name: "documentai_processor_with_default" + primary_resource_id: "processor" + vars: + processor_name: "test-processor" + custom_code: !ruby/object:Provider::Terraform::CustomCode + post_create: templates/terraform/post_create/documentai_processor.go.erb # This is for copying files over files: !ruby/object:Provider::Config::Files diff --git a/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb b/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb new file mode 100644 index 000000000000..fede1e920bf9 --- /dev/null +++ b/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb @@ -0,0 +1,6 @@ +resource "google_document_ai_processor" "<%= ctx[:primary_resource_id] %>" { + location = "us" + display_name = "<%= ctx[:vars]['processor_name'] %>" + type = "OCR_PROCESSOR" + default_processor_version = "pretrained-next" +} diff --git a/mmv1/templates/terraform/post_create/documentai_processor.go.erb b/mmv1/templates/terraform/post_create/documentai_processor.go.erb new file mode 100644 index 000000000000..7f4c8fe91dc0 --- /dev/null +++ b/mmv1/templates/terraform/post_create/documentai_processor.go.erb @@ -0,0 +1,29 @@ +// default_processor_version is not set during create +if o, n := d.GetChange("default_processor_version"); o.(string) != n.(string) { + obj := make(map[string]interface{}) + + defaultProcessorVersionProp, err := expandDocumentAIProcessorDefaultProcessorVersion(d.Get("default_processor_version"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("default_processor_version"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, defaultProcessorVersionProp)) { + obj["defaultProcessorVersion"] = defaultProcessorVersionProp + } + + url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}:setDefaultProcessorVersion") + if err != nil { + return err + } + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return fmt.Errorf("Error updating Processor %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Processor %q: %#v", d.Id(), res) + } + +} \ No newline at end of file From d36a729ec2e52ccbc537c9ab6bddc3a6a60d0b36 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 1 Jun 2022 16:47:58 -0700 Subject: [PATCH 3/8] Remove default --- mmv1/products/documentai/api.yaml | 7 +------ mmv1/products/documentai/terraform.yaml | 7 ------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/mmv1/products/documentai/api.yaml b/mmv1/products/documentai/api.yaml index 42dbcd3e9fdb..c1e375a23ebb 100644 --- a/mmv1/products/documentai/api.yaml +++ b/mmv1/products/documentai/api.yaml @@ -30,6 +30,7 @@ objects: create_url: 'projects/{{project}}/locations/{{location}}/processors' self_link: '{{name}}' base_url: '{{name}}' + input: true references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Official Documentation': @@ -63,12 +64,6 @@ objects: input: true description: | The display name. Must be unique. - - !ruby/object:Api::Type::String - name: 'defaultProcessorVersion' - update_verb: :POST - update_url: '{{name}}:setDefaultProcessorVersion' - description: | - The default processor version. - !ruby/object:Api::Type::String name: 'kmsKeyName' input: true diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml index deba038c0c33..5e9994881ab3 100644 --- a/mmv1/products/documentai/terraform.yaml +++ b/mmv1/products/documentai/terraform.yaml @@ -24,13 +24,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides primary_resource_id: "processor" vars: processor_name: "test-processor" - - !ruby/object:Provider::Terraform::Examples - name: "documentai_processor_with_default" - primary_resource_id: "processor" - vars: - processor_name: "test-processor" - custom_code: !ruby/object:Provider::Terraform::CustomCode - post_create: templates/terraform/post_create/documentai_processor.go.erb # This is for copying files over files: !ruby/object:Provider::Config::Files From 4bf758885af1826a0439f2487eb06652ce7ec6b3 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 1 Jun 2022 16:57:14 -0700 Subject: [PATCH 4/8] Remove default --- mmv1/products/documentai/terraform.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml index 5e9994881ab3..98f7b387c346 100644 --- a/mmv1/products/documentai/terraform.yaml +++ b/mmv1/products/documentai/terraform.yaml @@ -15,9 +15,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides Processor: !ruby/object:Overrides::Terraform::ResourceOverride import_format: ["{{%name}}"] - properties: - defaultProcessorVersion: !ruby/object:Overrides::Terraform::PropertyOverride - default_from_api: true examples: - !ruby/object:Provider::Terraform::Examples name: "documentai_processor" From b9c04df95ec9bae33e8f7875febba135d6cbc217 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Thu, 9 Jun 2022 08:38:46 -0700 Subject: [PATCH 5/8] Add default version --- mmv1/products/documentai/api.yaml | 27 ++++++++++++++++++- mmv1/products/documentai/terraform.yaml | 8 ++++++ .../documentai_default_version.tf.erb | 11 ++++++++ .../documentai_processor_with_default.tf.erb | 6 ----- 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 mmv1/templates/terraform/examples/documentai_default_version.tf.erb delete mode 100644 mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb diff --git a/mmv1/products/documentai/api.yaml b/mmv1/products/documentai/api.yaml index c1e375a23ebb..038700412833 100644 --- a/mmv1/products/documentai/api.yaml +++ b/mmv1/products/documentai/api.yaml @@ -69,4 +69,29 @@ objects: input: true description: | The KMS key used for encryption/decryption in CMEK scenarios. See https://cloud.google.com/security-key-management. - + - !ruby/object:Api::Resource + name: 'ProcessorDefaultVersion' + input: true + base_url: '{{processor}}' + create_url: '{{processor}}:setDefaultProcessorVersion' + create_verb: :POST + self_link: '{{processor}}' + identity: + - processor + description: | + The default version for the processor + parameters: + - !ruby/object:Api::Type::String + name: 'processor' + description: | + The processor to set the version on. + required: true + input: true + properties: + - !ruby/object:Api::Type::String + name: 'version' + api_name: 'defaultProcessorVersion' + description: | + The version to set + required: true + input: true diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml index 98f7b387c346..f31a833e0037 100644 --- a/mmv1/products/documentai/terraform.yaml +++ b/mmv1/products/documentai/terraform.yaml @@ -21,6 +21,14 @@ overrides: !ruby/object:Overrides::ResourceOverrides primary_resource_id: "processor" vars: processor_name: "test-processor" + ProcessorDefaultVersion: !ruby/object:Overrides::Terraform::ResourceOverride + skip_delete: true + examples: + - !ruby/object:Provider::Terraform::Examples + name: "documentai_default_version" + primary_resource_id: "processor" + vars: + processor_name: "test-processor" # This is for copying files over files: !ruby/object:Provider::Config::Files diff --git a/mmv1/templates/terraform/examples/documentai_default_version.tf.erb b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb new file mode 100644 index 000000000000..648aaa94fe5f --- /dev/null +++ b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb @@ -0,0 +1,11 @@ +resource "google_document_ai_processor" "processor" { + location = "us" + display_name = "<%= ctx[:vars]['processor_name'] %>" + type = "OCR_PROCESSOR" + default_processor_version = "pretrained-next" +} + +resource "google_document_ai_processor_default_version" "<%= ctx[:primary_resource_id] %>" { + processor = google_document_ai_processor.processor.name + version = "${google_document_ai_processor.processor.name}/versions/pretrained-next" +} diff --git a/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb b/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb deleted file mode 100644 index fede1e920bf9..000000000000 --- a/mmv1/templates/terraform/examples/documentai_processor_with_default.tf.erb +++ /dev/null @@ -1,6 +0,0 @@ -resource "google_document_ai_processor" "<%= ctx[:primary_resource_id] %>" { - location = "us" - display_name = "<%= ctx[:vars]['processor_name'] %>" - type = "OCR_PROCESSOR" - default_processor_version = "pretrained-next" -} From 98d5d240b531df63bcee80b8f54c5e023509c33c Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Thu, 9 Jun 2022 08:54:23 -0700 Subject: [PATCH 6/8] Fix test --- mmv1/products/documentai/terraform.yaml | 4 ++++ .../terraform/examples/documentai_default_version.tf.erb | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mmv1/products/documentai/terraform.yaml b/mmv1/products/documentai/terraform.yaml index f31a833e0037..d1cb7da4e0c6 100644 --- a/mmv1/products/documentai/terraform.yaml +++ b/mmv1/products/documentai/terraform.yaml @@ -23,6 +23,10 @@ overrides: !ruby/object:Overrides::ResourceOverrides processor_name: "test-processor" ProcessorDefaultVersion: !ruby/object:Overrides::Terraform::ResourceOverride skip_delete: true + import_format: ["{{%processor}}"] + properties: + processor: !ruby/object:Overrides::Terraform::PropertyOverride + ignore_read: true examples: - !ruby/object:Provider::Terraform::Examples name: "documentai_default_version" diff --git a/mmv1/templates/terraform/examples/documentai_default_version.tf.erb b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb index 648aaa94fe5f..144bc487e38d 100644 --- a/mmv1/templates/terraform/examples/documentai_default_version.tf.erb +++ b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb @@ -2,10 +2,9 @@ resource "google_document_ai_processor" "processor" { location = "us" display_name = "<%= ctx[:vars]['processor_name'] %>" type = "OCR_PROCESSOR" - default_processor_version = "pretrained-next" } resource "google_document_ai_processor_default_version" "<%= ctx[:primary_resource_id] %>" { processor = google_document_ai_processor.processor.name - version = "${google_document_ai_processor.processor.name}/versions/pretrained-next" + version = "${google_document_ai_processor.processor.name}/processorVersions/pretrained-next" } From 7b9b6db3ff67503d8ad983dc6e0efae85c70cea2 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Mon, 13 Jun 2022 14:47:59 -0700 Subject: [PATCH 7/8] Use id over name, remove unused file --- .../documentai_default_version.tf.erb | 4 +-- .../post_create/documentai_processor.go.erb | 29 ------------------- 2 files changed, 2 insertions(+), 31 deletions(-) delete mode 100644 mmv1/templates/terraform/post_create/documentai_processor.go.erb diff --git a/mmv1/templates/terraform/examples/documentai_default_version.tf.erb b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb index 144bc487e38d..496780cb45c7 100644 --- a/mmv1/templates/terraform/examples/documentai_default_version.tf.erb +++ b/mmv1/templates/terraform/examples/documentai_default_version.tf.erb @@ -5,6 +5,6 @@ resource "google_document_ai_processor" "processor" { } resource "google_document_ai_processor_default_version" "<%= ctx[:primary_resource_id] %>" { - processor = google_document_ai_processor.processor.name - version = "${google_document_ai_processor.processor.name}/processorVersions/pretrained-next" + processor = google_document_ai_processor.processor.id + version = "${google_document_ai_processor.processor.id}/processorVersions/pretrained-next" } diff --git a/mmv1/templates/terraform/post_create/documentai_processor.go.erb b/mmv1/templates/terraform/post_create/documentai_processor.go.erb deleted file mode 100644 index 7f4c8fe91dc0..000000000000 --- a/mmv1/templates/terraform/post_create/documentai_processor.go.erb +++ /dev/null @@ -1,29 +0,0 @@ -// default_processor_version is not set during create -if o, n := d.GetChange("default_processor_version"); o.(string) != n.(string) { - obj := make(map[string]interface{}) - - defaultProcessorVersionProp, err := expandDocumentAIProcessorDefaultProcessorVersion(d.Get("default_processor_version"), d, config) - if err != nil { - return err - } else if v, ok := d.GetOkExists("default_processor_version"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, defaultProcessorVersionProp)) { - obj["defaultProcessorVersion"] = defaultProcessorVersionProp - } - - url, err := replaceVars(d, config, "{{DocumentAIBasePath}}{{name}}:setDefaultProcessorVersion") - if err != nil { - return err - } - - // err == nil indicates that the billing_project value was found - if bp, err := getBillingProject(d, config); err == nil { - billingProject = bp - } - - res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate)) - if err != nil { - return fmt.Errorf("Error updating Processor %q: %s", d.Id(), err) - } else { - log.Printf("[DEBUG] Finished updating Processor %q: %#v", d.Id(), res) - } - -} \ No newline at end of file From 93283aa749beb1be31c5cb7b39757cbbe74ebec6 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Tue, 14 Jun 2022 11:31:27 -0700 Subject: [PATCH 8/8] Add note on delete --- mmv1/products/documentai/api.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/products/documentai/api.yaml b/mmv1/products/documentai/api.yaml index 038700412833..39e1e7e295cc 100644 --- a/mmv1/products/documentai/api.yaml +++ b/mmv1/products/documentai/api.yaml @@ -79,7 +79,7 @@ objects: identity: - processor description: | - The default version for the processor + The default version for the processor. Deleting this resource is a no-op, and does not unset the default version. parameters: - !ruby/object:Api::Type::String name: 'processor'