/
resource_host_group_direct_connect.go
120 lines (99 loc) · 2.81 KB
/
resource_host_group_direct_connect.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
package nirmata
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/nirmata/go-client/pkg/client"
)
func resourceHostGroupDirectConnect() *schema.Resource {
return &schema.Resource{
Create: resourceHostGroupDirectConnectCreate,
Read: resourceHostGroupDirectConnectRead,
Update: resourceHostGroupDirectConnectUpdate,
Delete: resourceHostGroupDirectConnectDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
"curl_script": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
},
}
}
func resourceHostGroupDirectConnectCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(client.Client)
name := d.Get("name").(string)
labels := d.Get("labels").(map[string]interface{})
cpID, err := apiClient.QueryByName(client.ServiceConfig, "CloudProvider", "Direct Connect")
if err != nil {
return err
}
hg := map[string]interface{}{
"name": name,
"parent": cpID.UUID(),
"labels": labels,
}
data, err := apiClient.PostFromJSON(client.ServiceConfig, "HostGroup", hg, nil)
if err != nil {
return err
}
hgID := data["id"].(string)
d.SetId(hgID)
scriptPath := "nirmata-host-agent/setup-nirmata-agent.sh"
curlScript := fmt.Sprintf("sudo curl -sSL %s/%s | sudo sh -s -- --cloud other --hostgroup %s",
apiClient.Address(), scriptPath, hgID)
d.Set("curl_script", curlScript)
updateHostGroupData(d, data)
return nil
}
func updateHostGroupData(d *schema.ResourceData, data map[string]interface{}) {
updateStateAndStatus(d, data)
}
func updateStateAndStatus(d *schema.ResourceData, data map[string]interface{}) {
if data["state"] != nil {
clusterState := data["state"].(string)
d.Set("state", clusterState)
}
if data["status"] != nil {
clusterStatus := data["status"].([]interface{})
d.Set("status", clusterStatus)
}
}
func resourceHostGroupDirectConnectRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(client.Client)
id := clientID(d, client.ServiceConfig, "HostGroup")
data, err := apiClient.Get(id, nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
return err
}
updateClusterData(d, data)
return nil
}
func resourceHostGroupDirectConnectUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourceHostGroupDirectConnectDelete(d *schema.ResourceData, m interface{}) error {
return delete(d, m, client.ServiceConfig, "HostGroup", nil)
}