Skip to content

Commit

Permalink
Add NSX Credentials to Vmwareengine
Browse files Browse the repository at this point in the history
  • Loading branch information
swamitagupta committed Nov 28, 2023
1 parent 5fbe3a5 commit 1c1d6fd
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mmv1/third_party/terraform/acctest/bootstrap_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ func BootstrapVmwareengineNetwork(t *testing.T, networkName string) string {

func BootstrapVmwareenginePrivateCloud(t *testing.T, privateCloudName string, networkName string) string {
project := envvar.GetTestProjectFromEnv()
region := "southamerica-west1" // Using a low utilisation region
region := "southamerica-west1" // Using a low utilisation region
config := BootstrapConfig(t)
if config == nil {
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_vmwareengine_cluster": vmwareengine.DataSourceVmwareengineCluster(),
<% end -%>
"google_vmwareengine_network": vmwareengine.DataSourceVmwareengineNetwork(),
"google_vmwareengine_nsx_credentials": vmwareengine.DataSourceVmwareengineNsxCredentials(),
"google_vmwareengine_private_cloud": vmwareengine.DataSourceVmwareenginePrivateCloud(),

// ####### END handwritten datasources ###########
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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 DataSourceVmwareengineNsxCredentials() *schema.Resource {
return &schema.Resource{
Read: dataSourceVmwareengineNsxCredentialsRead,
Schema: map[string]*schema.Schema{
"parent": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The resource name of the private cloud which contains NSX.
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 dataSourceVmwareengineNsxCredentialsRead(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}}:showNsxCredentials")
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("VmwareengineNsxCredentials %q", d.Id()))
}

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

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

return nil
}

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

func flattenVmwareengineNsxCredentailsPassword(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package vmwareengine_test

import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccDataSourceVmwareengineNsxCredentials_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"private_cloud_id": acctest.BootstrapVmwareenginePrivateCloud(t, "test-pc", acctest.BootstrapVmwareengineNetwork(t, "test-nw")),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleVmwareengineNsxCredentialsConfig(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleVmwareengineNsxCredentialsMeta("data.google_vmwareengine_nsx_credentials.ds"),
),
},
},
})
}

func testAccCheckGoogleVmwareengineNsxCredentialsConfig(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_vmwareengine_nsx_credentials" "ds" {
parent = "%{private_cloud_id}"
}
`, context)
}

func testAccCheckGoogleVmwareengineNsxCredentialsMeta(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find nsx credentials data source: %s", n)
}

_, ok = rs.Primary.Attributes["username"]
if !ok {
return errors.New("can't find 'username' attribute")
}

_, ok = rs.Primary.Attributes["password"]
if !ok {
return errors.New("can't find 'password' attribute")
}

return nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Cloud VMware Engine"
description: |-
Get NSX Credentials of a Private Cloud.
---

# google\_vmwareengine\_nsx_credentials

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

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

## Example Usage

```hcl
data "google_vmwareengine_nsx_credentials" "ds" {
parent = "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 NSX.

## Attributes Reference

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

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

0 comments on commit 1c1d6fd

Please sign in to comment.