diff --git a/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go b/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go index 0abc67817f84..269fb9d8d07e 100644 --- a/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go +++ b/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go @@ -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 "" diff --git a/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb b/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb index a1f905b01b1d..a28702f8c4dd 100644 --- a/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb +++ b/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb @@ -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 ########### diff --git a/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials.go b/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials.go new file mode 100644 index 000000000000..2a22d6db7eb2 --- /dev/null +++ b/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials.go @@ -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 +} diff --git a/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials_test.go b/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials_test.go new file mode 100644 index 000000000000..98d0132d6f20 --- /dev/null +++ b/mmv1/third_party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials_test.go @@ -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 + } +} diff --git a/mmv1/third_party/terraform/website/docs/d/vmwareengine_nsx_credentials.html.markdown b/mmv1/third_party/terraform/website/docs/d/vmwareengine_nsx_credentials.html.markdown new file mode 100644 index 000000000000..b1360bfda510 --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/vmwareengine_nsx_credentials.html.markdown @@ -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. \ No newline at end of file