|
| 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 code is generated by Magic Modules using the following: |
| 11 | +// |
| 12 | +// Configuration: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/products/apihub/HostProjectRegistration.yaml |
| 13 | +// Template: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/templates/terraform/resource.go.tmpl |
| 14 | +// |
| 15 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 16 | +// overwritten during the next generation cycle. |
| 17 | +// |
| 18 | +// ---------------------------------------------------------------------------- |
| 19 | + |
| 20 | +package apihub |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "net/http" |
| 26 | + "reflect" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 31 | + |
| 32 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 33 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 34 | +) |
| 35 | + |
| 36 | +func ResourceApihubHostProjectRegistration() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceApihubHostProjectRegistrationCreate, |
| 39 | + Read: resourceApihubHostProjectRegistrationRead, |
| 40 | + Delete: resourceApihubHostProjectRegistrationDelete, |
| 41 | + |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: resourceApihubHostProjectRegistrationImport, |
| 44 | + }, |
| 45 | + |
| 46 | + Timeouts: &schema.ResourceTimeout{ |
| 47 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 48 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 49 | + }, |
| 50 | + |
| 51 | + CustomizeDiff: customdiff.All( |
| 52 | + tpgresource.DefaultProviderProject, |
| 53 | + ), |
| 54 | + |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "gcp_project": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + ForceNew: true, |
| 60 | + DiffSuppressFunc: tpgresource.ProjectNumberDiffSuppress, |
| 61 | + Description: `Required. Immutable. Google cloud project name in the format: "projects/abc" or "projects/123". |
| 62 | +As input, project name with either project id or number are accepted. |
| 63 | +As output, this field will contain project number.`, |
| 64 | + }, |
| 65 | + "host_project_registration_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Required: true, |
| 68 | + ForceNew: true, |
| 69 | + Description: `Required. The ID to use for the Host Project Registration, which will become the |
| 70 | +final component of the host project registration's resource name. The ID |
| 71 | +must be the same as the Google cloud project specified in the |
| 72 | +host_project_registration.gcp_project field.`, |
| 73 | + }, |
| 74 | + "location": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + ForceNew: true, |
| 78 | + Description: `Part of 'parent'. See documentation of 'projectsId'.`, |
| 79 | + }, |
| 80 | + "create_time": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: `Output only. The time at which the host project registration was created.`, |
| 84 | + }, |
| 85 | + "name": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: `Identifier. The name of the host project registration. |
| 89 | +Format: |
| 90 | +"projects/{project}/locations/{location}/hostProjectRegistrations/{host_project_registration}".`, |
| 91 | + }, |
| 92 | + "project": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Optional: true, |
| 95 | + Computed: true, |
| 96 | + ForceNew: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + UseJSONNumber: true, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func resourceApihubHostProjectRegistrationCreate(d *schema.ResourceData, meta interface{}) error { |
| 104 | + config := meta.(*transport_tpg.Config) |
| 105 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + obj := make(map[string]interface{}) |
| 111 | + gcpProjectProp, err := expandApihubHostProjectRegistrationGcpProject(d.Get("gcp_project"), d, config) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } else if v, ok := d.GetOkExists("gcp_project"); !tpgresource.IsEmptyValue(reflect.ValueOf(gcpProjectProp)) && (ok || !reflect.DeepEqual(v, gcpProjectProp)) { |
| 115 | + obj["gcpProject"] = gcpProjectProp |
| 116 | + } |
| 117 | + |
| 118 | + url, err := tpgresource.ReplaceVars(d, config, "{{ApihubBasePath}}projects/{{project}}/locations/{{location}}/hostProjectRegistrations?hostProjectRegistrationId={{host_project_registration_id}}") |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + log.Printf("[DEBUG] Creating new HostProjectRegistration: %#v", obj) |
| 124 | + billingProject := "" |
| 125 | + |
| 126 | + project, err := tpgresource.GetProject(d, config) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("Error fetching project for HostProjectRegistration: %s", err) |
| 129 | + } |
| 130 | + billingProject = project |
| 131 | + |
| 132 | + // err == nil indicates that the billing_project value was found |
| 133 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 134 | + billingProject = bp |
| 135 | + } |
| 136 | + |
| 137 | + headers := make(http.Header) |
| 138 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 139 | + Config: config, |
| 140 | + Method: "POST", |
| 141 | + Project: billingProject, |
| 142 | + RawURL: url, |
| 143 | + UserAgent: userAgent, |
| 144 | + Body: obj, |
| 145 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 146 | + Headers: headers, |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("Error creating HostProjectRegistration: %s", err) |
| 150 | + } |
| 151 | + if err := d.Set("name", flattenApihubHostProjectRegistrationName(res["name"], d, config)); err != nil { |
| 152 | + return fmt.Errorf(`Error setting computed identity field "name": %s`, err) |
| 153 | + } |
| 154 | + |
| 155 | + // Store the ID now |
| 156 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}") |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("Error constructing id: %s", err) |
| 159 | + } |
| 160 | + d.SetId(id) |
| 161 | + |
| 162 | + log.Printf("[DEBUG] Finished creating HostProjectRegistration %q: %#v", d.Id(), res) |
| 163 | + |
| 164 | + return resourceApihubHostProjectRegistrationRead(d, meta) |
| 165 | +} |
| 166 | + |
| 167 | +func resourceApihubHostProjectRegistrationRead(d *schema.ResourceData, meta interface{}) error { |
| 168 | + config := meta.(*transport_tpg.Config) |
| 169 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + url, err := tpgresource.ReplaceVars(d, config, "{{ApihubBasePath}}projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}") |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + |
| 179 | + billingProject := "" |
| 180 | + |
| 181 | + project, err := tpgresource.GetProject(d, config) |
| 182 | + if err != nil { |
| 183 | + return fmt.Errorf("Error fetching project for HostProjectRegistration: %s", err) |
| 184 | + } |
| 185 | + billingProject = project |
| 186 | + |
| 187 | + // err == nil indicates that the billing_project value was found |
| 188 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 189 | + billingProject = bp |
| 190 | + } |
| 191 | + |
| 192 | + headers := make(http.Header) |
| 193 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 194 | + Config: config, |
| 195 | + Method: "GET", |
| 196 | + Project: billingProject, |
| 197 | + RawURL: url, |
| 198 | + UserAgent: userAgent, |
| 199 | + Headers: headers, |
| 200 | + }) |
| 201 | + if err != nil { |
| 202 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApihubHostProjectRegistration %q", d.Id())) |
| 203 | + } |
| 204 | + |
| 205 | + if err := d.Set("project", project); err != nil { |
| 206 | + return fmt.Errorf("Error reading HostProjectRegistration: %s", err) |
| 207 | + } |
| 208 | + |
| 209 | + if err := d.Set("name", flattenApihubHostProjectRegistrationName(res["name"], d, config)); err != nil { |
| 210 | + return fmt.Errorf("Error reading HostProjectRegistration: %s", err) |
| 211 | + } |
| 212 | + if err := d.Set("gcp_project", flattenApihubHostProjectRegistrationGcpProject(res["gcpProject"], d, config)); err != nil { |
| 213 | + return fmt.Errorf("Error reading HostProjectRegistration: %s", err) |
| 214 | + } |
| 215 | + if err := d.Set("create_time", flattenApihubHostProjectRegistrationCreateTime(res["createTime"], d, config)); err != nil { |
| 216 | + return fmt.Errorf("Error reading HostProjectRegistration: %s", err) |
| 217 | + } |
| 218 | + |
| 219 | + return nil |
| 220 | +} |
| 221 | + |
| 222 | +func resourceApihubHostProjectRegistrationDelete(d *schema.ResourceData, meta interface{}) error { |
| 223 | + log.Printf("[WARNING] Apihub HostProjectRegistration resources"+ |
| 224 | + " cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ |
| 225 | + " state, but will still be present on Google Cloud.", d.Id()) |
| 226 | + d.SetId("") |
| 227 | + |
| 228 | + return nil |
| 229 | +} |
| 230 | + |
| 231 | +func resourceApihubHostProjectRegistrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 232 | + config := meta.(*transport_tpg.Config) |
| 233 | + if err := tpgresource.ParseImportId([]string{ |
| 234 | + "^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/hostProjectRegistrations/(?P<host_project_registration_id>[^/]+)$", |
| 235 | + "^(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<host_project_registration_id>[^/]+)$", |
| 236 | + "^(?P<location>[^/]+)/(?P<host_project_registration_id>[^/]+)$", |
| 237 | + }, d, config); err != nil { |
| 238 | + return nil, err |
| 239 | + } |
| 240 | + |
| 241 | + // Replace import id for the resource id |
| 242 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/hostProjectRegistrations/{{host_project_registration_id}}") |
| 243 | + if err != nil { |
| 244 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 245 | + } |
| 246 | + d.SetId(id) |
| 247 | + |
| 248 | + return []*schema.ResourceData{d}, nil |
| 249 | +} |
| 250 | + |
| 251 | +func flattenApihubHostProjectRegistrationName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 252 | + return v |
| 253 | +} |
| 254 | + |
| 255 | +func flattenApihubHostProjectRegistrationGcpProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 256 | + return v |
| 257 | +} |
| 258 | + |
| 259 | +func flattenApihubHostProjectRegistrationCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 260 | + return v |
| 261 | +} |
| 262 | + |
| 263 | +func expandApihubHostProjectRegistrationGcpProject(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 264 | + return v, nil |
| 265 | +} |
0 commit comments