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