/
resource_cluster_addons.go
216 lines (191 loc) · 5.92 KB
/
resource_cluster_addons.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package nirmata
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/nirmata/go-client/pkg/client"
)
func resoureClusterAddOn() *schema.Resource {
return &schema.Resource{
Create: resourceClusterAddOnCreate,
Read: resourceClusterAddOnRead,
Update: resourceClusterAddOnUpdate,
Delete: resourceClusterAddOnDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateName,
},
"cluster": {
Type: schema.TypeString,
Required: true,
},
"application": {
Type: schema.TypeString,
Required: true,
},
"environment": {
Type: schema.TypeString,
Optional: true,
},
"catalog": {
Type: schema.TypeString,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Optional: true,
},
"channel": {
Type: schema.TypeString,
Optional: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceClusterAddOnCreate(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
name := d.Get("name").(string)
clusterNameOrID := d.Get("cluster").(string)
namespace := d.Get("namespace").(string)
application := d.Get("application").(string)
channel := d.Get("channel").(string)
catalog := d.Get("catalog").(string)
environment := d.Get("environment").(string)
labels := d.Get("labels")
isAppPresent := false
clusterID, err := apiClient.QueryByName(client.ServiceClusters, "KubernetesCluster", clusterNameOrID)
if err != nil {
log.Printf("[ERROR] - failed to resolve cluster %s %v", clusterNameOrID, err)
return err
}
catalogID, catalogErr := fetchID(apiClient, client.ServiceCatalogs, "Catalogs", catalog)
if catalogErr != nil {
log.Printf("[ERROR] - failed to get catalog %s %v", catalog, catalogErr)
return catalogErr
}
fields := []string{"name", "id"}
applicationList, appErr := apiClient.GetDescendants(catalogID, "Application", &client.GetOptions{Fields: fields})
if appErr != nil {
log.Printf("[ERROR] - failed to get application - %v", appErr)
return err
}
for _, apps := range applicationList {
if application == apps["name"] {
isAppPresent = true
}
}
if !isAppPresent {
log.Printf("[ERROR] - no application found with name %v in catalog - %v", application, catalog)
return fmt.Errorf("[ERROR] - no application found with name %v in catalog - %v", application, catalog)
}
addOnId, err := apiClient.GetRelationID(clusterID, "ClusterAddOns")
if err != nil {
log.Printf("[ERROR] - failed to resolve cluster %s %v", addOnId, err)
return err
}
if namespace == "" {
namespace = application
}
if environment == "" {
if isUUID(clusterNameOrID) {
clusterData, err := apiClient.Get(clusterID, nil)
if err != nil {
log.Printf("[ERROR] - failed to get cluster details %s %v", clusterID, err)
return err
}
clusterNameOrID = application + "-" + clusterData["name"].(string)
}
environment = application + "-" + clusterNameOrID
}
data := map[string]interface{}{
"name": name,
"parent": addOnId.UUID(),
"namespace": namespace,
"application": application,
"channel": channel,
"environment": environment,
"labels": labels,
"catalog": catalog,
"modelIndex": "ClusterAddOn",
}
log.Printf("[DEBUG] - creating cluster add-on %s with %+v", name, data)
addonData, err := apiClient.PostFromJSON(client.ServiceClusters, "ClusterAddOn", data, nil)
if err != nil {
log.Printf("[ERROR] - failed to create cluster add-on %s with data %v: %v", name, data, err)
return err
}
addOnUUID := addonData["id"].(string)
addonID := client.NewID(client.ServiceClusters, "ClusterAddOn", addOnUUID)
state, waitErr := waitForAddonState(apiClient, d.Timeout(schema.TimeoutCreate), addonID)
if waitErr != nil {
log.Printf("[ERROR] - failed to check add-on status. Error - %v", waitErr)
return waitErr
}
if strings.EqualFold("failed", state) {
return fmt.Errorf("cluster add-on creation failed")
}
d.SetId(addOnUUID)
log.Printf("[INFO] - created cluster addon %s %s", name, addOnUUID)
return nil
}
func resourceClusterAddOnRead(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
cluster := d.Get("cluster").(string)
_, err := fetchID(apiClient, client.ServiceClusters, "KubernetesCluster", cluster)
if err != nil {
log.Printf("[INFO] cluster %+v not found", err)
return err
}
d.SetId("")
return nil
}
var addOnMap = map[string]string{
"labels": "labels",
}
func resourceClusterAddOnUpdate(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
id := client.NewID(client.ServiceClusters, "clusteraddon", d.Id())
addOnChanges := buildChanges(d, addOnMap, "labels")
if len(addOnChanges) > 0 {
addOnData, err := apiClient.Get(id, &client.GetOptions{})
if err != nil {
log.Printf("[ERROR] - failed to retrieve %s from %v: %v", "cluster add-on", id.Map(), err)
return err
}
d, plainErr := client.NewObject(addOnData)
if plainErr != nil {
log.Printf("[ERROR] - failed to decode %s %v: %v", "cluster ad-don", d, plainErr)
return err
}
_, plainErr = apiClient.PutWithIDFromJSON(d.ID(), addOnChanges)
if plainErr != nil {
log.Printf("[ERROR] - failed to update %s %v: %v", "cluster", d.ID().Map(), plainErr)
return err
}
log.Printf("[DEBUG] updated %v %v", d.ID().Map(), addOnChanges)
}
return nil
}
func resourceClusterAddOnDelete(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
name := d.Get("name").(string)
id := client.NewID(client.ServiceClusters, "ClusterAddOn", d.Id())
if err := apiClient.Delete(id, nil); err != nil {
return err
}
log.Printf("[INFO] - deleted add-on %s %s", name, id.UUID())
return nil
}