Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Added org_id as a required parameter in terraform schema #38

Merged
merged 5 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions examples/main.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
provider "vmc" {
refresh_token = var.api_token
}

data "vmc_org" "my_org" {
id = var.org_id
org_id = var.org_id
}

data "vmc_connected_accounts" "my_accounts" {
org_id = data.vmc_org.my_org.id
account_number = var.aws_account_number
}

data "vmc_customer_subnets" "my_subnets" {
org_id = data.vmc_org.my_org.id
connected_account_id = data.vmc_connected_accounts.my_accounts.ids[0]
region = var.sddc_region
}

resource "vmc_sddc" "sddc_1" {
org_id = data.vmc_org.my_org.id

sddc_name = var.sddc_name
vpc_cidr = var.vpc_cidr
num_host = 3
Expand Down
8 changes: 1 addition & 7 deletions vmc/data_source_vmc_connected_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ func dataSourceVmcConnectedAccounts() *schema.Resource {
Read: dataSourceVmcConnectedAccountsRead,

Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Description: "Organization identifier.",
Required: true,
},
"provider_type": {
Type: schema.TypeString,
Description: "The cloud provider of the SDDC (AWS or ZeroCloud).",
Expand All @@ -41,8 +36,7 @@ func dataSourceVmcConnectedAccounts() *schema.Resource {
}

func dataSourceVmcConnectedAccountsRead(d *schema.ResourceData, m interface{}) error {

orgID := d.Get("org_id").(string)
orgID := (m.(*ConnectorWrapper)).OrgID
providerType := d.Get("provider_type").(string)
accountNumber := d.Get("account_number").(string)

Expand Down
6 changes: 0 additions & 6 deletions vmc/data_source_vmc_connected_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ func TestAccDataSourceVmcConnectedAccounts_basic(t *testing.T) {

func testAccDataSourceVmcConnectedAccountsConfig() string {
return fmt.Sprintf(`
data "vmc_org" "my_org" {
id = %q
}

data "vmc_connected_accounts" "my_accounts" {
org_id = "${data.vmc_org.my_org.id}"
account_number = %q
}
`,
os.Getenv("ORG_ID"),
os.Getenv("AWS_ACCOUNT_NUMBER"),
)
}
11 changes: 1 addition & 10 deletions vmc/data_source_vmc_customer_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ func dataSourceVmcCustomerSubnets() *schema.Resource {
Read: dataSourceVmcCustomerSubnetsRead,

Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Description: "Organization identifier.",
Required: true,
},
"connected_account_id": {
Type: schema.TypeString,
Description: "The linked connected account identifier.",
Expand Down Expand Up @@ -77,7 +72,7 @@ func dataSourceVmcCustomerSubnets() *schema.Resource {

func dataSourceVmcCustomerSubnetsRead(d *schema.ResourceData, m interface{}) error {

orgID := d.Get("org_id").(string)
orgID := m.(*ConnectorWrapper).OrgID
accountID := d.Get("connected_account_id").(string)
sddcID := d.Get("sddc_id").(string)
region := d.Get("region").(string)
Expand All @@ -103,10 +98,6 @@ func dataSourceVmcCustomerSubnetsRead(d *schema.ResourceData, m interface{}) err
ids = append(ids, *subnet.SubnetId)
}
}

// for _, subnet := range subnets.VpcMap["VpcInfoSubnets"].Subnets {
// ids = append(ids, subnet.SubnetId)
// }
log.Printf("[DEBUG] Subnet IDs are %v\n", ids)

if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions vmc/data_source_vmc_customer_subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,15 @@ func TestAccDataSourceVmcCustomerSubnets_basic(t *testing.T) {

func testAccDataSourceVmcCustomerSubnetsConfig() string {
return fmt.Sprintf(`
data "vmc_org" "my_org" {
id = %q
}

data "vmc_connected_accounts" "my_accounts" {
org_id = "${data.vmc_org.my_org.id}"
account_number = %q
}

data "vmc_customer_subnets" "my_subnets" {
org_id = "${data.vmc_org.my_org.id}"
connected_account_id = "${data.vmc_connected_accounts.my_accounts.ids.0}"
region = "US_WEST_2"
}
`,
os.Getenv("ORG_ID"),
)
os.Getenv("AWS_ACCOUNT_NUMBER"))
}
12 changes: 4 additions & 8 deletions vmc/data_source_vmc_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func dataSourceVmcOrg() *schema.Resource {
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique ID of this resource",
Required: true,
Description: "Organization identifier.",
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Expand All @@ -34,19 +34,15 @@ func dataSourceVmcOrg() *schema.Resource {
}

func dataSourceVmcOrgRead(d *schema.ResourceData, m interface{}) error {
orgID := d.Get("id").(string)
if orgID == "" {
return fmt.Errorf("org ID is a required parameter and cannot be empty")
}

orgID := (m.(*ConnectorWrapper)).OrgID
connector := (m.(*ConnectorWrapper)).Connector
orgClient := vmc.NewDefaultOrgsClient(connector)
org, err := orgClient.Get(orgID)

if err != nil {
return fmt.Errorf("Error while reading org information for %s: %v", orgID, err)
}
d.SetId(org.Id)
d.SetId(orgID)
d.Set("display_name", org.DisplayName)
d.Set("name", org.Name)

Expand Down
3 changes: 0 additions & 3 deletions vmc/data_source_vmc_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func TestAccDataSourceVmcOrg_basic(t *testing.T) {
func testAccDataSourceVmcOrgConfig() string {
return fmt.Sprintf(`
data "vmc_org" "my_org" {
id = %q

}
`,
os.Getenv("ORG_ID"),
)
}
9 changes: 8 additions & 1 deletion vmc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type ConnectorWrapper struct {
client.Connector
RefreshToken string
OrgID string
VmcURL string
CspURL string
}
Expand All @@ -37,6 +38,11 @@ func Provider() terraform.ResourceProvider {
Required: true,
DefaultFunc: schema.EnvDefaultFunc("API_TOKEN", nil),
},
"org_id": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ORG_ID", nil),
},
"vmc_url": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -67,11 +73,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
refreshToken := d.Get("refresh_token").(string)
vmcURL := d.Get("vmc_url").(string)
cspURL := d.Get("csp_url").(string)
orgID := d.Get("org_id").(string)
httpClient := http.Client{}
connector, err := NewVmcConnectorByRefreshToken(refreshToken, vmcURL, cspURL, httpClient)
if err != nil {
return nil, fmt.Errorf("Error creating connector : %v ", err)
}

return &ConnectorWrapper{connector, refreshToken, vmcURL, cspURL}, nil
return &ConnectorWrapper{connector, refreshToken, orgID, vmcURL, cspURL}, nil
}
3 changes: 3 additions & 0 deletions vmc/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("TEST_SDDC_ID"); v == "" {
t.Fatal("TEST_SDDC_ID must be set for acceptance tests")
}
if v := os.Getenv("AWS_ACCOUNT_NUMBER"); v == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test needed? We don't want to enforce users to provide account number since it is optional - if account number is not provided, we return a list of all accounts.

t.Fatal("AWS_ACCOUNT_NUMBER must be set for acceptance tests")
}
}
14 changes: 4 additions & 10 deletions vmc/resource_vmc_sddc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func resourceSddc() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of this resource",
},
"storage_capacity": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -166,8 +161,7 @@ func resourceSddc() *schema.Resource {
func resourceSddcCreate(d *schema.ResourceData, m interface{}) error {
connectorWrapper := m.(*ConnectorWrapper)
sddcClient := orgs.NewDefaultSddcsClient(connectorWrapper)

orgID := d.Get("org_id").(string)
orgID := connectorWrapper.OrgID
storageCapacity := d.Get("storage_capacity").(int)
storageCapacityConverted := int64(storageCapacity)
sddcName := d.Get("sddc_name").(string)
Expand Down Expand Up @@ -256,7 +250,7 @@ func resourceSddcCreate(d *schema.ResourceData, m interface{}) error {
func resourceSddcRead(d *schema.ResourceData, m interface{}) error {
connector := (m.(*ConnectorWrapper)).Connector
sddcID := d.Id()
orgID := d.Get("org_id").(string)
orgID := (m.(*ConnectorWrapper)).OrgID
sddc, err := getSDDC(connector, orgID, sddcID)
if err != nil {
if err.Error() == errors.NewNotFound().Error() {
Expand Down Expand Up @@ -304,7 +298,7 @@ func resourceSddcDelete(d *schema.ResourceData, m interface{}) error {
connector := (m.(*ConnectorWrapper)).Connector
sddcClient := orgs.NewDefaultSddcsClient(connector)
sddcID := d.Id()
orgID := d.Get("org_id").(string)
orgID := (m.(*ConnectorWrapper)).OrgID

task, err := sddcClient.Delete(orgID, sddcID, nil, nil, nil)
if err != nil {
Expand Down Expand Up @@ -332,7 +326,7 @@ func resourceSddcUpdate(d *schema.ResourceData, m interface{}) error {
connector := (m.(*ConnectorWrapper)).Connector
esxsClient := sddcs.NewDefaultEsxsClient(connector)
sddcID := d.Id()
orgID := d.Get("org_id").(string)
orgID := (m.(*ConnectorWrapper)).OrgID

// Add,remove hosts
if d.HasChange("num_host") {
Expand Down
37 changes: 18 additions & 19 deletions vmc/resource_vmc_sddc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,21 @@ func testCheckVmcSddcDestroy(s *terraform.State) error {

func testAccVmcSddcConfigBasic(sddcName string) string {
return fmt.Sprintf(`
data "vmc_org" "my_org" {
id = %q

data "vmc_connected_accounts" "my_accounts" {
account_number = %q
}

data "vmc_connected_accounts" "accounts" {
org_id = "${data.vmc_org.my_org.id}"
data "vmc_customer_subnets" "my_subnets" {
connected_account_id = data.vmc_connected_accounts.my_accounts.ids[0]
region = "US_WEST_2"
}

resource "vmc_sddc" "sddc_1" {
org_id = "${data.vmc_org.my_org.id}"

# storage_capacity = 100
sddc_name = %q

vpc_cidr = "10.2.0.0/16"
num_host = 1
provider_type = "ZEROCLOUD"
num_host = 3
provider_type = "AWS"

region = "US_WEST_2"

Expand All @@ -135,17 +133,18 @@ resource "vmc_sddc" "sddc_1" {
sso_domain = "vmc.local"

deployment_type = "SingleAZ"

# TODO raise exception here need to debug
#account_link_sddc_config = [
# {
# customer_subnet_ids = ["subnet-13a0c249"]
# connected_account_id = "${data.vmc_connected_accounts.accounts.ids.0}"
# },
# ]
account_link_sddc_config {
customer_subnet_ids = [data.vmc_customer_subnets.my_subnets.ids[0]]
connected_account_id = data.vmc_connected_accounts.my_accounts.ids[0]
}
timeouts {
create = "300m"
update = "300m"
delete = "180m"
}
}
`,
os.Getenv("ORG_ID"),
os.Getenv("AWS_ACCOUNT_NUMBER"),
sddcName,
)
}
4 changes: 2 additions & 2 deletions website/docs/d/connected_accounts.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ The connected accounts data source get a list of connected accounts.

```hcl
data "vmc_connected_accounts" "my_accounts" {
org_id = data.vmc_org.my_org.id
account_number = var.aws_account_number
}
```

## Argument Reference

* `org_id` - (Required) Organization identifier.
* `org_id` - (Computed) Organization identifier.

* `account_number` - (Optional) AWS account number.

## Attributes Reference
Expand Down
3 changes: 1 addition & 2 deletions website/docs/d/customer_subnets.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ The customer subnets data source provides information about customer's compatibl

```hcl
data "vmc_customer_subnets" "my_subnets" {
org_id = data.vmc_org.my_org.id
connected_account_id = data.vmc_connected_accounts.my_accounts.ids[0]
region = var.sddc_region
}
```

## Argument Reference

* `org_id` - (Required) Organization identifier.
* `org_id` - (Computed) Organization identifier.

* `region` - (Required) The region of the cloud resources to work in.

Expand Down
10 changes: 1 addition & 9 deletions website/docs/d/org.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ description: A organization data source.

This data source provides information about an organization.

## Example Usage

```hcl
data "vmc_org" "my_org" {
id = var.org_id
}
```

## Argument Reference

* `id` - (Required) ID of the organization.
* `id` - (Computed) ID of the organization.
Loading