| // Copyright (c) HashiCorp, Inc. |
| // SPDX-License-Identifier: MPL-2.0 |
| |
| // ---------------------------------------------------------------------------- |
| // |
| // *** AUTO GENERATED CODE *** Type: MMv1 *** |
| // |
| // ---------------------------------------------------------------------------- |
| // |
| // This file is automatically generated by Magic Modules and manual |
| // changes will be clobbered when the file is regenerated. |
| // |
| // Please read more about how to change this file in |
| // .github/CONTRIBUTING.md. |
| // |
| // ---------------------------------------------------------------------------- |
| |
| package kms |
| |
| import ( |
| "fmt" |
| "log" |
| "net/http" |
| "reflect" |
| "time" |
| |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| |
| "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| ) |
| |
| func ResourceKMSKeyHandle() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceKMSKeyHandleCreate, |
| Read: resourceKMSKeyHandleRead, |
| Delete: resourceKMSKeyHandleDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceKMSKeyHandleImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(20 * time.Minute), |
| Delete: schema.DefaultTimeout(20 * time.Minute), |
| }, |
| |
| CustomizeDiff: customdiff.All( |
| tpgresource.DefaultProviderProject, |
| ), |
| |
| Schema: map[string]*schema.Schema{ |
| "location": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The location for the KeyHandle. |
| A full list of valid locations can be found by running 'gcloud kms locations list'.`, |
| }, |
| "name": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The resource name for the KeyHandle.`, |
| }, |
| "resource_type_selector": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `Selector of the resource type where we want to protect resources. |
| For example, 'storage.googleapis.com/Bucket'.`, |
| }, |
| "kms_key": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `A reference to a Cloud KMS CryptoKey that can be used for CMEK in the requested |
| product/project/location, for example |
| 'projects/1/locations/us-east1/keyRings/foo/cryptoKeys/bar-ffffff'`, |
| }, |
| "project": { |
| Type: schema.TypeString, |
| Optional: true, |
| Computed: true, |
| ForceNew: true, |
| }, |
| }, |
| UseJSONNumber: true, |
| } |
| } |
| |
| func resourceKMSKeyHandleCreate(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| obj := make(map[string]interface{}) |
| nameProp, err := expandKMSKeyHandleName(d.Get("name"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { |
| obj["name"] = nameProp |
| } |
| resourceTypeSelectorProp, err := expandKMSKeyHandleResourceTypeSelector(d.Get("resource_type_selector"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("resource_type_selector"); !tpgresource.IsEmptyValue(reflect.ValueOf(resourceTypeSelectorProp)) && (ok || !reflect.DeepEqual(v, resourceTypeSelectorProp)) { |
| obj["resourceTypeSelector"] = resourceTypeSelectorProp |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{KMSBasePath}}projects/{{project}}/locations/{{location}}/keyHandles?keyHandleId={{name}}") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Creating new KeyHandle: %#v", obj) |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for KeyHandle: %s", err) |
| } |
| billingProject = project |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| headers := make(http.Header) |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "POST", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Body: obj, |
| Timeout: d.Timeout(schema.TimeoutCreate), |
| Headers: headers, |
| }) |
| if err != nil { |
| return fmt.Errorf("Error creating KeyHandle: %s", err) |
| } |
| |
| // Store the ID now |
| id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/keyHandles/{{name}}") |
| if err != nil { |
| return fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| // Use the resource in the operation response to populate |
| // identity fields and d.Id() before read |
| var opRes map[string]interface{} |
| err = KMSOperationWaitTimeWithResponse( |
| config, res, &opRes, project, "Creating KeyHandle", userAgent, |
| d.Timeout(schema.TimeoutCreate)) |
| if err != nil { |
| // The resource didn't actually create |
| d.SetId("") |
| |
| return fmt.Errorf("Error waiting to create KeyHandle: %s", err) |
| } |
| |
| opRes, err = resourceKMSKeyHandleDecoder(d, meta, opRes) |
| if err != nil { |
| return fmt.Errorf("Error decoding response from operation: %s", err) |
| } |
| if opRes == nil { |
| return fmt.Errorf("Error decoding response from operation, could not find object") |
| } |
| |
| if err := d.Set("name", flattenKMSKeyHandleName(opRes["name"], d, config)); err != nil { |
| return err |
| } |
| |
| // This may have caused the ID to update - update it if so. |
| id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/keyHandles/{{name}}") |
| if err != nil { |
| return fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| log.Printf("[DEBUG] Finished creating KeyHandle %q: %#v", d.Id(), res) |
| |
| return resourceKMSKeyHandleRead(d, meta) |
| } |
| |
| func resourceKMSKeyHandleRead(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{KMSBasePath}}projects/{{project}}/locations/{{location}}/keyHandles/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for KeyHandle: %s", err) |
| } |
| billingProject = project |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| headers := make(http.Header) |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "GET", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Headers: headers, |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("KMSKeyHandle %q", d.Id())) |
| } |
| |
| res, err = resourceKMSKeyHandleDecoder(d, meta, res) |
| if err != nil { |
| return err |
| } |
| |
| if res == nil { |
| // Decoding the object has resulted in it being gone. It may be marked deleted |
| log.Printf("[DEBUG] Removing KMSKeyHandle because it no longer exists.") |
| d.SetId("") |
| return nil |
| } |
| |
| if err := d.Set("project", project); err != nil { |
| return fmt.Errorf("Error reading KeyHandle: %s", err) |
| } |
| |
| if err := d.Set("name", flattenKMSKeyHandleName(res["name"], d, config)); err != nil { |
| return fmt.Errorf("Error reading KeyHandle: %s", err) |
| } |
| if err := d.Set("kms_key", flattenKMSKeyHandleKmsKey(res["kmsKey"], d, config)); err != nil { |
| return fmt.Errorf("Error reading KeyHandle: %s", err) |
| } |
| if err := d.Set("resource_type_selector", flattenKMSKeyHandleResourceTypeSelector(res["resourceTypeSelector"], d, config)); err != nil { |
| return fmt.Errorf("Error reading KeyHandle: %s", err) |
| } |
| |
| return nil |
| } |
| |
| func resourceKMSKeyHandleDelete(d *schema.ResourceData, meta interface{}) error { |
| log.Printf("[WARNING] KMS KeyHandle resources"+ |
| " cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ |
| " state, but will still be present on Google Cloud.", d.Id()) |
| d.SetId("") |
| |
| return nil |
| } |
| |
| func resourceKMSKeyHandleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| if err := tpgresource.ParseImportId([]string{ |
| "^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/keyHandles/(?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 := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/keyHandles/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |
| |
| func flattenKMSKeyHandleName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenKMSKeyHandleKmsKey(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenKMSKeyHandleResourceTypeSelector(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func expandKMSKeyHandleName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandKMSKeyHandleResourceTypeSelector(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func resourceKMSKeyHandleDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { |
| // Modify the name to be the user specified form. |
| // We can't just ignore_read on `name` as the linter will |
| // complain that the returned `res` is never used afterwards. |
| // Some field needs to be actually set, and we chose `name`. |
| res["name"] = d.Get("name").(string) |
| return res, nil |
| } |