-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_activation.go
98 lines (75 loc) · 2.12 KB
/
resource_activation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"github.com/adeleporte/terraform-provider-hcx/hcx"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceActivation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceActivationCreate,
ReadContext: resourceActivationRead,
UpdateContext: resourceActivationUpdate,
DeleteContext: resourceActivationDelete,
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Optional: true,
Default: "https://connect.hcx.vmware.com",
},
"activationkey": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceActivationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcx.Client)
url := d.Get("url").(string)
activationkey := d.Get("activationkey").(string)
body := hcx.ActivateBody{
Data: hcx.ActivateData{
Items: []hcx.ActivateDataItem{
{
Config: hcx.ActivateDataItemConfig{
URL: url,
ActivationKey: activationkey,
},
},
},
},
}
// First, check if already activated
res, err := hcx.GetActivate(client)
if err != nil {
return diag.FromErr(err)
}
if len(res.Data.Items) == 0 {
// No activation config found
_, err := hcx.PostActivate(client, body)
if err != nil {
return diag.FromErr(err)
}
return resourceActivationRead(ctx, d, m)
}
d.SetId(res.Data.Items[0].Config.UUID)
return resourceActivationRead(ctx, d, m)
}
func resourceActivationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
res, err := hcx.GetActivate(client)
if err != nil {
return diag.FromErr(err)
}
d.SetId(res.Data.Items[0].Config.UUID)
return diags
}
func resourceActivationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceActivationRead(ctx, d, m)
}
func resourceActivationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}