Skip to content

Commit

Permalink
Add VCenter Credentials to Vmwareengine (#9572) (#16709)
Browse files Browse the repository at this point in the history
[upstream:9b9aa20fc8ac98a3fe9ac350b6c108822fb435d9]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Dec 7, 2023
1 parent 4b55213 commit 412608b
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/9572.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_vmwareengine_vcenter_credentials`
```
1 change: 1 addition & 0 deletions google/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_vmwareengine_nsx_credentials": vmwareengine.DataSourceVmwareengineNsxCredentials(),
"google_vmwareengine_private_cloud": vmwareengine.DataSourceVmwareenginePrivateCloud(),
"google_vmwareengine_subnet": vmwareengine.DataSourceVmwareengineSubnet(),
"google_vmwareengine_vcenter_credentials": vmwareengine.DataSourceVmwareengineVcenterCredentials(),

// ####### END handwritten datasources ###########
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package vmwareengine

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceVmwareengineVcenterCredentials() *schema.Resource {
return &schema.Resource{
Read: dataSourceVmwareengineVcenterCredentialsRead,
Schema: map[string]*schema.Schema{
"parent": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The resource name of the private cloud which contains vcenter.
Resource names are schemeless URIs that follow the conventions in https://cloud.google.com/apis/design/resource_names.
For example: projects/my-project/locations/us-west1-a/privateClouds/my-cloud`,
},
"username": {
Type: schema.TypeString,
Computed: true,
Description: `Initial username.`,
},
"password": {
Type: schema.TypeString,
Computed: true,
Description: `Initial password.`,
},
},
}
}

func dataSourceVmwareengineVcenterCredentialsRead(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, "{{VmwareengineBasePath}}{{parent}}:showVcenterCredentials")
if err != nil {
return err
}

billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
ErrorAbortPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.Is429QuotaError},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("VmwareengineVcenterCredentials %q", d.Id()))
}

if err := d.Set("username", flattenVmwareengineVcenterCredentailsUsername(res["username"], d, config)); err != nil {
return fmt.Errorf("Error reading VcenterCredentails: %s", err)
}
if err := d.Set("password", flattenVmwareengineVcenterCredentailsPassword(res["password"], d, config)); err != nil {
return fmt.Errorf("Error reading VcenterCredentails: %s", err)
}

id, err := tpgresource.ReplaceVars(d, config, "{{parent}}:vcenter-credentials")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return nil
}

func flattenVmwareengineVcenterCredentailsUsername(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenVmwareengineVcenterCredentailsPassword(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_vmwareengine_private_cloud.ds", "google_vmwareengine_private_cloud.vmw-engine-pc", map[string]struct{}{}),
testAccCheckGoogleVmwareengineNsxCredentialsMeta("data.google_vmwareengine_nsx_credentials.nsx-ds"),
testAccCheckGoogleVmwareengineVcenterCredentialsMeta("data.google_vmwareengine_vcenter_credentials.vcenter-ds"),
),
},
{
Expand Down Expand Up @@ -101,9 +102,12 @@ data "google_vmwareengine_private_cloud" "ds" {
]
}
# 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
# 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
data "google_vmwareengine_nsx_credentials" "nsx-ds" {
parent = google_vmwareengine_private_cloud.vmw-engine-pc
data "google_vmwareengine_vcenter_credentials" "vcenter-ds" {
parent = google_vmwareengine_private_cloud.vmw-engine-pc.id
}
`, context)
Expand All @@ -127,6 +131,24 @@ func testAccCheckGoogleVmwareengineNsxCredentialsMeta(n string) resource.TestChe
}
}

func testAccCheckGoogleVmwareengineVcenterCredentialsMeta(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find vcenter credentials data source: %s", n)
}
_, ok = rs.Primary.Attributes["username"]
if !ok {
return fmt.Errorf("can't find 'username' attribute in data source: %s", n)
}
_, ok = rs.Primary.Attributes["password"]
if !ok {
return fmt.Errorf("can't find 'password' attribute in data source: %s", n)
}
return nil
}
}

func testAccCheckVmwareenginePrivateCloudDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/vmwareengine_vcenter_credentials.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Cloud VMware Engine"
description: |-
Get Vcenter Credentials of a Private Cloud.
---

# google\_vmwareengine\_vcenter_credentials

Use this data source to get Vcenter credentials for a Private Cloud.

To get more information about private cloud Vcenter credentials, see:
* [API documentation](https://cloud.google.com/vmware-engine/docs/reference/rest/v1/projects.locations.privateClouds/showVcenterCredentials)

## Example Usage

```hcl
data "google_vmwareengine_vcenter_credentials" "ds" {
parent = "projects/my-project/locations/us-west1-a/privateClouds/my-cloud"
}
```

## Argument Reference

The following arguments are supported:

* `parent` - (Required) The resource name of the private cloud which contains the Vcenter.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

* `username` - The username of the Vcenter Credential.
* `password` - The password of the Vcenter Credential.

0 comments on commit 412608b

Please sign in to comment.