/
resource_zpa_app_server_controller.go
226 lines (195 loc) · 6.82 KB
/
resource_zpa_app_server_controller.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
217
218
219
220
221
222
223
224
225
226
package zpa
import (
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
client "github.com/zscaler/zscaler-sdk-go/v2/zpa"
"github.com/zscaler/zscaler-sdk-go/v2/zpa/services/appservercontroller"
)
func resourceApplicationServer() *schema.Resource {
return &schema.Resource{
Create: resourceApplicationServerCreate,
Read: resourceApplicationServerRead,
Update: resourceApplicationServerUpdate,
Delete: resourceApplicationServerDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
zClient := m.(*Client).appservercontroller.WithMicroTenant(GetString(d.Get("microtenant_id")))
id := d.Id()
_, parseIDErr := strconv.ParseInt(id, 10, 64)
if parseIDErr == nil {
// assume if the passed value is an int
_ = d.Set("id", id)
} else {
resp, _, err := zClient.GetByName(id)
if err == nil {
d.SetId(resp.ID)
_ = d.Set("id", resp.ID)
} else {
return []*schema.ResourceData{d}, err
}
}
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "This field defines the name of the server.",
ValidateFunc: validation.StringIsNotEmpty,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "This field defines the description of the server.",
},
"address": {
Type: schema.TypeString,
Required: true,
Description: "This field defines the domain or IP address of the server.",
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "This field defines the status of the server.",
},
// App Server Group ID can only be attached if Dynamic Server Discovery in Server Group is False
"app_server_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "This field defines the list of server groups IDs.",
Optional: true,
},
"config_space": {
Type: schema.TypeString,
Optional: true,
Default: "DEFAULT",
ValidateFunc: validation.StringInSlice([]string{
"DEFAULT",
"SIEM",
}, false),
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"microtenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceApplicationServerCreate(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).appservercontroller.WithMicroTenant(GetString(d.Get("microtenant_id")))
req := expandCreateAppServerRequest(d)
log.Printf("[INFO] Creating zpa application server with request\n%+v\n", req)
resp, _, err := service.Create(req)
if err != nil {
return err
}
log.Printf("[INFO] Created application server request. ID: %v\n", resp)
d.SetId(resp.ID)
return resourceApplicationServerRead(d, m)
}
func resourceApplicationServerRead(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).appservercontroller.WithMicroTenant(GetString(d.Get("microtenant_id")))
resp, _, err := service.Get(d.Id())
if err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
log.Printf("[WARN] Removing application server %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting application server:\n%+v\n", resp)
_ = d.Set("address", resp.Address)
_ = d.Set("app_server_group_ids", resp.AppServerGroupIds)
_ = d.Set("config_space", resp.ConfigSpace)
_ = d.Set("description", resp.Description)
_ = d.Set("enabled", resp.Enabled)
_ = d.Set("name", resp.Name)
_ = d.Set("microtenant_id", resp.MicroTenantID)
return nil
}
func resourceApplicationServerUpdate(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).appservercontroller.WithMicroTenant(GetString(d.Get("microtenant_id")))
log.Println("An updated occurred")
if d.HasChange("app_server_group_ids") || d.HasChange("name") || d.HasChange("description") || d.HasChange("address") || d.HasChange("enabled") {
log.Println("The AppServerGroupID, name, description or address has been changed")
if _, _, err := service.Get(d.Id()); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
}
if _, err := service.Update(d.Id(), appservercontroller.ApplicationServer{
AppServerGroupIds: SetToStringSlice(d.Get("app_server_group_ids").(*schema.Set)),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Address: d.Get("address").(string),
Enabled: d.Get("enabled").(bool),
MicroTenantID: d.Get("microtenant_id").(string),
}); err != nil {
return err
}
}
return nil
}
func resourceApplicationServerDelete(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).appservercontroller.WithMicroTenant(GetString(d.Get("microtenant_id")))
log.Printf("[INFO] Deleting application server ID: %v\n", d.Id())
err := removeServerFromGroup(service, d.Id())
if err != nil {
return err
}
if _, err = service.Delete(d.Id()); err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
d.SetId("")
return nil
}
return err
}
return nil
}
func removeServerFromGroup(service *appservercontroller.Service, serverID string) error {
// Remove the reference to this server from server groups.
resp, _, err := service.Get(serverID)
if err != nil {
if respErr, ok := err.(*client.ErrorResponse); ok && respErr.IsObjectNotFound() {
return nil
}
return err
}
if len(resp.AppServerGroupIds) != 0 {
log.Printf("[INFO] Removing server group ID/s from application server: %s", serverID)
resp.AppServerGroupIds = make([]string, 0)
log.Printf("[INFO] Updating server group ID: %s", serverID)
_, err = service.Update(serverID, *resp)
if err != nil {
log.Printf("[ERROR] Failed to update application server ID: %s", serverID)
return err
}
}
return nil
}
func expandCreateAppServerRequest(d *schema.ResourceData) appservercontroller.ApplicationServer {
applicationServer := appservercontroller.ApplicationServer{
ID: d.Id(),
Address: d.Get("address").(string),
ConfigSpace: d.Get("config_space").(string),
AppServerGroupIds: SetToStringSlice(d.Get("app_server_group_ids").(*schema.Set)),
Description: d.Get("description").(string),
Enabled: d.Get("enabled").(bool),
Name: d.Get("name").(string),
MicroTenantID: d.Get("microtenant_id").(string),
}
return applicationServer
}