Skip to content

Commit 412608b

Browse files
Add VCenter Credentials to Vmwareengine (#9572) (#16709)
[upstream:9b9aa20fc8ac98a3fe9ac350b6c108822fb435d9] Signed-off-by: Modular Magician <[email protected]>
1 parent 4b55213 commit 412608b

5 files changed

+152
-1
lines changed

.changelog/9572.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_vmwareengine_vcenter_credentials`
3+
```

google/provider/provider_mmv1_resources.go

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
268268
"google_vmwareengine_nsx_credentials": vmwareengine.DataSourceVmwareengineNsxCredentials(),
269269
"google_vmwareengine_private_cloud": vmwareengine.DataSourceVmwareenginePrivateCloud(),
270270
"google_vmwareengine_subnet": vmwareengine.DataSourceVmwareengineSubnet(),
271+
"google_vmwareengine_vcenter_credentials": vmwareengine.DataSourceVmwareengineVcenterCredentials(),
271272

272273
// ####### END handwritten datasources ###########
273274
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

google/services/vmwareengine/resource_vmwareengine_private_cloud_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T
3333
Check: resource.ComposeTestCheckFunc(
3434
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_vmwareengine_private_cloud.ds", "google_vmwareengine_private_cloud.vmw-engine-pc", map[string]struct{}{}),
3535
testAccCheckGoogleVmwareengineNsxCredentialsMeta("data.google_vmwareengine_nsx_credentials.nsx-ds"),
36+
testAccCheckGoogleVmwareengineVcenterCredentialsMeta("data.google_vmwareengine_vcenter_credentials.vcenter-ds"),
3637
),
3738
},
3839
{
@@ -101,9 +102,12 @@ data "google_vmwareengine_private_cloud" "ds" {
101102
]
102103
}
103104
104-
# NSX Credentials is a child datasource of PC and is included in the PC test due to the high deployment time involved in the Creation and deletion of a PC
105+
# NSX and Vcenter Credentials are child datasources of PC and are included in the PC test due to the high deployment time involved in the Creation and deletion of a PC
105106
data "google_vmwareengine_nsx_credentials" "nsx-ds" {
106107
parent = google_vmwareengine_private_cloud.vmw-engine-pc
108+
109+
data "google_vmwareengine_vcenter_credentials" "vcenter-ds" {
110+
parent = google_vmwareengine_private_cloud.vmw-engine-pc.id
107111
}
108112
109113
`, context)
@@ -127,6 +131,24 @@ func testAccCheckGoogleVmwareengineNsxCredentialsMeta(n string) resource.TestChe
127131
}
128132
}
129133

134+
func testAccCheckGoogleVmwareengineVcenterCredentialsMeta(n string) resource.TestCheckFunc {
135+
return func(s *terraform.State) error {
136+
rs, ok := s.RootModule().Resources[n]
137+
if !ok {
138+
return fmt.Errorf("Can't find vcenter credentials data source: %s", n)
139+
}
140+
_, ok = rs.Primary.Attributes["username"]
141+
if !ok {
142+
return fmt.Errorf("can't find 'username' attribute in data source: %s", n)
143+
}
144+
_, ok = rs.Primary.Attributes["password"]
145+
if !ok {
146+
return fmt.Errorf("can't find 'password' attribute in data source: %s", n)
147+
}
148+
return nil
149+
}
150+
}
151+
130152
func testAccCheckVmwareenginePrivateCloudDestroyProducer(t *testing.T) func(s *terraform.State) error {
131153
return func(s *terraform.State) error {
132154
for name, rs := range s.RootModule().Resources {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Cloud VMware Engine"
3+
description: |-
4+
Get Vcenter Credentials of a Private Cloud.
5+
---
6+
7+
# google\_vmwareengine\_vcenter_credentials
8+
9+
Use this data source to get Vcenter credentials for a Private Cloud.
10+
11+
To get more information about private cloud Vcenter credentials, see:
12+
* [API documentation](https://cloud.google.com/vmware-engine/docs/reference/rest/v1/projects.locations.privateClouds/showVcenterCredentials)
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "google_vmwareengine_vcenter_credentials" "ds" {
18+
parent = "projects/my-project/locations/us-west1-a/privateClouds/my-cloud"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `parent` - (Required) The resource name of the private cloud which contains the Vcenter.
27+
28+
## Attributes Reference
29+
30+
In addition to the arguments listed above, the following computed attributes are exported:
31+
32+
* `username` - The username of the Vcenter Credential.
33+
* `password` - The password of the Vcenter Credential.

0 commit comments

Comments
 (0)