Skip to content

Commit

Permalink
update vmc doc
Browse files Browse the repository at this point in the history
  • Loading branch information
adeleporte committed Jan 20, 2021
1 parent cc50e70 commit 6779d5c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/resources/vmc.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ resource "hcx_site_pairing" "vmc" {

## Argument Reference

* `sddc_name` - (Required) Name of the SDDC.
* `sddc_name` - (Optional) Name of the SDDC. If not specified, sddc_id must be set.
* `sddc_id` - (Optional) ID of the SDDC. If not specified, sddc_name must be set.
* `token` - (Required) VMware Cloud Service API Token. Generated from the VMware Cloud Services Console / My account / API Tokens. Environment variable VMC_API_TOKEN can be used to avoid setting the token in the code.


Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ github.com/hashicorp/terraform-plugin-sdk v1.16.0 h1:NrkXMRjHErUPPTHQkZ6JIn6bByi
github.com/hashicorp/terraform-plugin-sdk/v2 v2.2.0 h1:2m4uKA97R8ijHGLwhHdpSJyI8Op1FpS/ozpoF21jK7s=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.2.0/go.mod h1:+12dJQebYjuU/yiq94iZUPuC66abfRBrXdpVJia3ojk=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.3.0 h1:Egv+R1tOOjPNz643KBTx3tLT6RdFGGYJcZlyLvrPcEU=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.0 h1:2c+vG46celrDCsfYEIzaXxvBaAXCqlVG77LwtFz8cfs=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/vault v0.10.4/go.mod h1:KfSyffbKxoVyspOdlaGVjIuwLobi07qD1bAbosPMpP0=
Expand Down
39 changes: 38 additions & 1 deletion hcx/vmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func HcxCloudAuthenticate(access_token string) (string, error) {

}

func GetSddc(hcx_auth, sddc_name string) (SDDC, error) {
func GetSddcByName(hcx_auth, sddc_name string) (SDDC, error) {

c := Client{
HTTPClient: &http.Client{Timeout: 60 * time.Second},
Expand Down Expand Up @@ -148,6 +148,43 @@ func GetSddc(hcx_auth, sddc_name string) (SDDC, error) {

}

func GetSddcByID(hcx_auth, sddcID string) (SDDC, error) {

c := Client{
HTTPClient: &http.Client{Timeout: 60 * time.Second},
HostURL: "https://connect.hcx.vmware.com/provider/csp/consumer",
Token: hcx_auth,
}

req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/sddcs", c.HostURL), nil)
if err != nil {
return SDDC{}, err
}

_, r, err := c.doVmcRequest(req)
if err != nil {
return SDDC{}, err
}

resp := GetSddcsResults{}
// parse response body
err = json.Unmarshal(r, &resp)
if err != nil {
fmt.Println(err)
return SDDC{}, err
}

for _, j := range resp.SDDCs {
if j.ID == sddcID {
return j, nil
}
}

// parse response header
return SDDC{}, errors.New("cant find the sddc")

}

func ActivateHcxOnSDDC(hcx_auth, sddc_id string) (ActivateHcxOnSDDCResults, error) {

resp := ActivateHcxOnSDDCResults{}
Expand Down
52 changes: 46 additions & 6 deletions resource_vmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ func resourceVmc() *schema.Resource {
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("VMC_API_TOKEN", nil),
},
"sddc_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"sddc_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
},
"cloud_url": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -49,6 +53,11 @@ func resourceVmcCreate(ctx context.Context, d *schema.ResourceData, m interface{

token := d.Get("token").(string)
sddc_name := d.Get("sddc_name").(string)
sddcID := d.Get("sddc_id").(string)

if sddc_name == "" && sddcID == "" {
return diag.Errorf("SDDC name or Id must be specified")
}

// Authenticate with VMware Cloud Services
access_token, err := hcx.VmcAuthenticate(token)
Expand All @@ -61,7 +70,13 @@ func resourceVmcCreate(ctx context.Context, d *schema.ResourceData, m interface{
return diag.FromErr(err)
}

sddc, err := hcx.GetSddc(hcx_auth, sddc_name)
var sddc hcx.SDDC
if sddcID != "" {
sddc, err = hcx.GetSddcByID(hcx_auth, sddcID)
} else {
sddc, err = hcx.GetSddcByName(hcx_auth, sddc_name)
}

if err != nil {
return diag.FromErr(err)
}
Expand All @@ -79,7 +94,11 @@ func resourceVmcCreate(ctx context.Context, d *schema.ResourceData, m interface{

// Wait for task to be completed
for {
sddc, err := hcx.GetSddc(hcx_auth, sddc_name)
if sddcID != "" {
sddc, err = hcx.GetSddcByID(hcx_auth, sddcID)
} else {
sddc, err = hcx.GetSddcByName(hcx_auth, sddc_name)
}
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -103,6 +122,11 @@ func resourceVmcRead(ctx context.Context, d *schema.ResourceData, m interface{})

token := d.Get("token").(string)
sddc_name := d.Get("sddc_name").(string)
sddcID := d.Get("sddc_id").(string)

if sddc_name == "" && sddcID == "" {
return diag.Errorf("SDDC name or Id must be specified")
}

// Authenticate with VMware Cloud Services
access_token, err := hcx.VmcAuthenticate(token)
Expand All @@ -115,7 +139,12 @@ func resourceVmcRead(ctx context.Context, d *schema.ResourceData, m interface{})
return diag.FromErr(err)
}

sddc, err := hcx.GetSddc(hcx_auth, sddc_name)
var sddc hcx.SDDC
if sddcID != "" {
sddc, err = hcx.GetSddcByID(hcx_auth, sddcID)
} else {
sddc, err = hcx.GetSddcByName(hcx_auth, sddc_name)
}
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -138,6 +167,7 @@ func resourceVmcDelete(ctx context.Context, d *schema.ResourceData, m interface{

token := d.Get("token").(string)
sddc_name := d.Get("sddc_name").(string)
sddcID := d.Get("sddc_id").(string)

// Authenticate with VMware Cloud Services
access_token, err := hcx.VmcAuthenticate(token)
Expand All @@ -150,7 +180,12 @@ func resourceVmcDelete(ctx context.Context, d *schema.ResourceData, m interface{
return diag.FromErr(err)
}

sddc, err := hcx.GetSddc(hcx_auth, sddc_name)
var sddc hcx.SDDC
if sddcID != "" {
sddc, err = hcx.GetSddcByID(hcx_auth, sddcID)
} else {
sddc, err = hcx.GetSddcByName(hcx_auth, sddc_name)
}
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -163,7 +198,12 @@ func resourceVmcDelete(ctx context.Context, d *schema.ResourceData, m interface{

// Wait for task to be completed
for {
sddc, err := hcx.GetSddc(hcx_auth, sddc_name)
var sddc hcx.SDDC
if sddcID != "" {
sddc, err = hcx.GetSddcByID(hcx_auth, sddcID)
} else {
sddc, err = hcx.GetSddcByName(hcx_auth, sddc_name)
}
if err != nil {
return diag.FromErr(err)
}
Expand Down

0 comments on commit 6779d5c

Please sign in to comment.