forked from apache/cloudstack-terraform-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_cloudstack_cni_configuration.go
More file actions
231 lines (192 loc) · 6.92 KB
/
resource_cloudstack_cni_configuration.go
File metadata and controls
231 lines (192 loc) · 6.92 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"fmt"
"log"
"strings"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudStackCniConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackCniConfigurationCreate,
Read: resourceCloudStackCniConfigurationRead,
Delete: resourceCloudStackCniConfigurationDelete,
Importer: &schema.ResourceImporter{
State: importStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the CNI configuration",
},
"cni_config": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "CNI Configuration content to be registered",
},
"account": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "An optional account for the CNI configuration. Must be used with domain_id.",
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "An optional domain ID for the CNI configuration. If the account parameter is used, domain_id must also be used.",
},
"project_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "An optional project for the CNI configuration",
},
"params": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Description: "List of variables declared in CNI configuration content",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceCloudStackCniConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
log.Printf("[DEBUG] Creating CNI configuration: %s", name)
p := cs.Configuration.NewRegisterCniConfigurationParams(name)
if v, ok := d.GetOk("cni_config"); ok {
cniConfig := v.(string)
log.Printf("[DEBUG] CNI config data length: %d bytes", len(cniConfig))
p.SetCniconfig(cniConfig)
} else {
return fmt.Errorf("CNI configuration content is required but not provided")
}
if account := d.Get("account").(string); account != "" {
log.Printf("[DEBUG] Setting account: %s", account)
p.SetAccount(account)
}
if domainID := d.Get("domain_id").(string); domainID != "" {
log.Printf("[DEBUG] Setting domain ID: %s", domainID)
p.SetDomainid(domainID)
}
if projectID := d.Get("project_id").(string); projectID != "" {
log.Printf("[DEBUG] Setting project ID: %s", projectID)
p.SetProjectid(projectID)
}
if params, ok := d.GetOk("params"); ok {
paramsList := []string{}
for _, param := range params.(*schema.Set).List() {
paramsList = append(paramsList, param.(string))
}
if len(paramsList) > 0 {
paramsStr := strings.Join(paramsList, ",")
log.Printf("[DEBUG] Setting params: %s", paramsStr)
p.SetParams(paramsStr)
}
}
resp, err := cs.Configuration.RegisterCniConfiguration(p)
if err != nil {
return fmt.Errorf("Error creating CNI configuration %s: %s", name, err)
}
log.Printf("[DEBUG] CNI configuration creation response: %+v", resp)
// List configurations to find the created one by name since direct ID access is not available
listParams := cs.Configuration.NewListCniConfigurationParams()
listParams.SetName(name)
// Add context parameters if available
if account := d.Get("account").(string); account != "" {
listParams.SetAccount(account)
}
if domainID := d.Get("domain_id").(string); domainID != "" {
listParams.SetDomainid(domainID)
}
if projectID := d.Get("project_id").(string); projectID != "" {
listParams.SetProjectid(projectID)
}
listResp, err := cs.Configuration.ListCniConfiguration(listParams)
if err != nil {
return fmt.Errorf("Error listing CNI configurations after creation: %s", err)
}
if listResp.Count == 0 {
return fmt.Errorf("CNI configuration %s was created but could not be found", name)
}
// Use the first (and should be only) result
config := listResp.CniConfiguration[0]
d.SetId(config.Id)
log.Printf("[DEBUG] CNI configuration %s successfully created with ID: %s", name, d.Id())
return resourceCloudStackCniConfigurationRead(d, meta)
}
func resourceCloudStackCniConfigurationRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] Reading CNI configuration: %s", d.Id())
p := cs.Configuration.NewListCniConfigurationParams()
p.SetId(d.Id())
config, err := cs.Configuration.ListCniConfiguration(p)
if err != nil {
return fmt.Errorf("Error listing CNI configuration: %s", err)
}
if config.Count == 0 {
log.Printf("[DEBUG] CNI configuration %s no longer exists", d.Id())
d.SetId("")
return nil
}
d.Set("name", config.CniConfiguration[0].Name)
d.Set("cni_config", config.CniConfiguration[0].Userdata)
// Only set account and domain_id if they were originally provided by the user
// to avoid drift when CloudStack returns default values
if _, ok := d.GetOk("account"); ok {
d.Set("account", config.CniConfiguration[0].Account)
}
if _, ok := d.GetOk("domain_id"); ok {
d.Set("domain_id", config.CniConfiguration[0].Domainid)
}
if _, ok := d.GetOk("project_id"); ok {
d.Set("project_id", config.CniConfiguration[0].Projectid)
}
if config.CniConfiguration[0].Params != "" {
paramsList := strings.Split(config.CniConfiguration[0].Params, ",")
d.Set("params", paramsList)
}
return nil
}
func resourceCloudStackCniConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] Deleting CNI configuration: %s", d.Id())
p := cs.Configuration.NewDeleteCniConfigurationParams(d.Id())
_, err := cs.Configuration.DeleteCniConfiguration(p)
if err != nil {
if strings.Contains(err.Error(), "does not exist") ||
strings.Contains(err.Error(), "not found") {
log.Printf("[DEBUG] CNI configuration %s already deleted", d.Id())
return nil
}
return fmt.Errorf("Error deleting CNI configuration %s: %s", d.Id(), err)
}
log.Printf("[DEBUG] CNI configuration %s deleted", d.Id())
return nil
}