|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// ---------------------------------------------------------------------------- |
| 5 | +// |
| 6 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 7 | +// |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | +// |
| 10 | +// This file is automatically generated by Magic Modules and manual |
| 11 | +// changes will be clobbered when the file is regenerated. |
| 12 | +// |
| 13 | +// Please read more about how to change this file in |
| 14 | +// .github/CONTRIBUTING.md. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +package publicca |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 28 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 29 | +) |
| 30 | + |
| 31 | +func ResourcePublicCAExternalAccountKey() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Create: resourcePublicCAExternalAccountKeyCreate, |
| 34 | + Read: resourcePublicCAExternalAccountKeyRead, |
| 35 | + Delete: resourcePublicCAExternalAccountKeyDelete, |
| 36 | + |
| 37 | + Timeouts: &schema.ResourceTimeout{ |
| 38 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 39 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 40 | + }, |
| 41 | + |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "location": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + ForceNew: true, |
| 47 | + Description: `Location for the externalAccountKey. Currently only 'global' is supported.`, |
| 48 | + Default: "global", |
| 49 | + }, |
| 50 | + "b64_mac_key": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: `Base64-URL-encoded HS256 key. It is generated by the PublicCertificateAuthorityService |
| 54 | +when the ExternalAccountKey is created.`, |
| 55 | + Sensitive: true, |
| 56 | + }, |
| 57 | + "key_id": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: `It is generated by the PublicCertificateAuthorityService when the ExternalAccountKey is created.`, |
| 61 | + Sensitive: true, |
| 62 | + }, |
| 63 | + "name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: `Resource name. projects/{project}/locations/{location}/externalAccountKeys/{keyId}.`, |
| 67 | + }, |
| 68 | + "project": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Computed: true, |
| 72 | + ForceNew: true, |
| 73 | + }, |
| 74 | + }, |
| 75 | + UseJSONNumber: true, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func resourcePublicCAExternalAccountKeyCreate(d *schema.ResourceData, meta interface{}) error { |
| 80 | + config := meta.(*transport_tpg.Config) |
| 81 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + obj := make(map[string]interface{}) |
| 87 | + |
| 88 | + url, err := tpgresource.ReplaceVars(d, config, "{{PublicCABasePath}}projects/{{project}}/locations/{{location}}/externalAccountKeys") |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + log.Printf("[DEBUG] Creating new ExternalAccountKey: %#v", obj) |
| 94 | + billingProject := "" |
| 95 | + |
| 96 | + project, err := tpgresource.GetProject(d, config) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("Error fetching project for ExternalAccountKey: %s", err) |
| 99 | + } |
| 100 | + billingProject = project |
| 101 | + |
| 102 | + // err == nil indicates that the billing_project value was found |
| 103 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 104 | + billingProject = bp |
| 105 | + } |
| 106 | + |
| 107 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 108 | + Config: config, |
| 109 | + Method: "POST", |
| 110 | + Project: billingProject, |
| 111 | + RawURL: url, |
| 112 | + UserAgent: userAgent, |
| 113 | + Body: obj, |
| 114 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 115 | + }) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("Error creating ExternalAccountKey: %s", err) |
| 118 | + } |
| 119 | + if err := d.Set("name", flattenPublicCAExternalAccountKeyName(res["name"], d, config)); err != nil { |
| 120 | + return fmt.Errorf(`Error setting computed identity field "name": %s`, err) |
| 121 | + } |
| 122 | + if err := d.Set("key_id", flattenPublicCAExternalAccountKeyKeyId(res["keyId"], d, config)); err != nil { |
| 123 | + return fmt.Errorf(`Error setting computed identity field "key_id": %s`, err) |
| 124 | + } |
| 125 | + if err := d.Set("b64_mac_key", flattenPublicCAExternalAccountKeyB64MacKey(res["b64MacKey"], d, config)); err != nil { |
| 126 | + return fmt.Errorf(`Error setting computed identity field "b64_mac_key": %s`, err) |
| 127 | + } |
| 128 | + |
| 129 | + // Store the ID now |
| 130 | + id, err := tpgresource.ReplaceVars(d, config, "{{name}}") |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("Error constructing id: %s", err) |
| 133 | + } |
| 134 | + d.SetId(id) |
| 135 | + |
| 136 | + log.Printf("[DEBUG] Finished creating ExternalAccountKey %q: %#v", d.Id(), res) |
| 137 | + |
| 138 | + return resourcePublicCAExternalAccountKeyRead(d, meta) |
| 139 | +} |
| 140 | + |
| 141 | +func resourcePublicCAExternalAccountKeyRead(d *schema.ResourceData, meta interface{}) error { |
| 142 | + // This resource could not be read from the API. |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func resourcePublicCAExternalAccountKeyDelete(d *schema.ResourceData, meta interface{}) error { |
| 147 | + log.Printf("[WARNING] PublicCA ExternalAccountKey resources"+ |
| 148 | + " cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ |
| 149 | + " state, but will still be present on Google Cloud.", d.Id()) |
| 150 | + d.SetId("") |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func flattenPublicCAExternalAccountKeyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 156 | + return v |
| 157 | +} |
| 158 | + |
| 159 | +func flattenPublicCAExternalAccountKeyKeyId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 160 | + return v |
| 161 | +} |
| 162 | + |
| 163 | +func flattenPublicCAExternalAccountKeyB64MacKey(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 164 | + return v |
| 165 | +} |
0 commit comments