-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_vcenter.go
123 lines (96 loc) · 2.51 KB
/
resource_vcenter.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"time"
b64 "encoding/base64"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
hcx "github.com/adeleporte/terraform-provider-hcx/hcx"
)
func resourcevCenter() *schema.Resource {
return &schema.Resource{
CreateContext: resourcevCenterCreate,
ReadContext: resourcevCenterRead,
UpdateContext: resourcevCenterUpdate,
DeleteContext: resourcevCenterDelete,
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
},
"username": {
Type: schema.TypeString,
Required: true,
},
"password": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourcevCenterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcx.Client)
url := d.Get("url").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
body := hcx.InsertvCenterBody{
Data: hcx.InsertvCenterData{
Items: []hcx.InsertvCenterDataItem{
{
Config: hcx.InsertvCenterDataItemConfig{
Username: username,
Password: b64.StdEncoding.EncodeToString([]byte(password)),
URL: url,
},
},
},
},
}
res, err := hcx.InsertvCenter(client, body)
if err != nil {
return diag.FromErr(err)
}
d.SetId(res.InsertvCenterData.Items[0].Config.UUID)
// Restart App Deamon
hcx.AppEngineStop(client)
// Wait for App Deamon to be stopped
for {
jr, err := hcx.GetAppEngineStatus(client)
if err != nil {
return diag.FromErr(err)
}
if jr.Result == "STOPPED" {
break
}
time.Sleep(5 * time.Second)
}
hcx.AppEngineStart(client)
// Wait for App Deamon to be started
for {
jr, err := hcx.GetAppEngineStatus(client)
if err != nil {
return diag.FromErr(err)
}
if jr.Result == "RUNNING" {
break
}
time.Sleep(5 * time.Second)
}
// Seems that we need to wait a bit
time.Sleep(60 * time.Second)
return resourcevCenterRead(ctx, d, m)
}
func resourcevCenterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
return diags
}
func resourcevCenterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourcevCenterRead(ctx, d, m)
}
func resourcevCenterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
hcx.DeletevCenter(client, d.Id())
return diags
}