|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package vmwareengine |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceVmwareengineVcenterCredentials() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceVmwareengineVcenterCredentialsRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "parent": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + ForceNew: true, |
| 21 | + Description: `The resource name of the private cloud which contains vcenter. |
| 22 | +Resource names are schemeless URIs that follow the conventions in https://cloud.google.com/apis/design/resource_names. |
| 23 | +For example: projects/my-project/locations/us-west1-a/privateClouds/my-cloud`, |
| 24 | + }, |
| 25 | + "username": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Description: `Initial username.`, |
| 29 | + }, |
| 30 | + "password": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: `Initial password.`, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func dataSourceVmwareengineVcenterCredentialsRead(d *schema.ResourceData, meta interface{}) error { |
| 40 | + config := meta.(*transport_tpg.Config) |
| 41 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + url, err := tpgresource.ReplaceVars(d, config, "{{VmwareengineBasePath}}{{parent}}:showVcenterCredentials") |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + billingProject := "" |
| 52 | + |
| 53 | + // err == nil indicates that the billing_project value was found |
| 54 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 55 | + billingProject = bp |
| 56 | + } |
| 57 | + |
| 58 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 59 | + Config: config, |
| 60 | + Method: "GET", |
| 61 | + Project: billingProject, |
| 62 | + RawURL: url, |
| 63 | + UserAgent: userAgent, |
| 64 | + ErrorAbortPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.Is429QuotaError}, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("VmwareengineVcenterCredentials %q", d.Id())) |
| 68 | + } |
| 69 | + |
| 70 | + if err := d.Set("username", flattenVmwareengineVcenterCredentailsUsername(res["username"], d, config)); err != nil { |
| 71 | + return fmt.Errorf("Error reading VcenterCredentails: %s", err) |
| 72 | + } |
| 73 | + if err := d.Set("password", flattenVmwareengineVcenterCredentailsPassword(res["password"], d, config)); err != nil { |
| 74 | + return fmt.Errorf("Error reading VcenterCredentails: %s", err) |
| 75 | + } |
| 76 | + |
| 77 | + id, err := tpgresource.ReplaceVars(d, config, "{{parent}}:vcenter-credentials") |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("Error constructing id: %s", err) |
| 80 | + } |
| 81 | + d.SetId(id) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func flattenVmwareengineVcenterCredentailsUsername(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 87 | + return v |
| 88 | +} |
| 89 | + |
| 90 | +func flattenVmwareengineVcenterCredentailsPassword(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 91 | + return v |
| 92 | +} |
0 commit comments