-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fbe3a5
commit 1c1d6fd
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
..._party/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
62 changes: 62 additions & 0 deletions
62
...y/terraform/services/vmwareengine/data_source_google_vmwareengine_nsx_credentials_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...third_party/terraform/website/docs/d/vmwareengine_nsx_credentials.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |