-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_client_profile.go
225 lines (204 loc) · 7.43 KB
/
resource_appgate_client_profile.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
package appgate
import (
"context"
"fmt"
"log"
"math/rand"
"strings"
"time"
"github.com/appgate/sdp-api-client-go/api/v16/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateClientProfile() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateClientProfileCreate,
Read: resourceAppgateClientProfileRead,
Delete: resourceAppgateClientProfileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"spa_key_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"identity_provider_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func getIdFromProfile(profile openapi.ClientConnectionsProfiles) string {
// names are case sensitive and the controller only allows 1 per case type.
// so it will be suitable as the identitfer.
return profile.GetName()
}
func resourceAppgateClientProfileCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Create Client Profile %s", d.Get("name"))
ctx := context.Background()
api := meta.(*Client).API.ClientConnectionsApi
err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
rand.Seed(time.Now().UnixNano())
duration := rand.Intn(10)
time.Sleep(time.Duration(duration) * time.Second)
// before we create a profile, make sure the controllers are in a healthy state
if err := applianceStatsRetryable(ctx, meta); err != nil {
return err
}
token, err := meta.(*Client).GetToken()
if err != nil {
return resource.RetryableError(err)
}
clientConnections, _, err := api.ClientConnectionsGet(ctx).Authorization(token).Execute()
if err != nil {
return resource.RetryableError(err)
}
existingProfiles := clientConnections.GetProfiles()
profile := openapi.ClientConnectionsProfiles{}
if v, ok := d.GetOk("name"); ok {
profile.SetName(v.(string))
}
if v, ok := d.GetOk("spa_key_name"); ok {
profile.SetSpaKeyName(v.(string))
}
if v, ok := d.GetOk("identity_provider_name"); ok {
profile.SetIdentityProviderName(v.(string))
}
d.SetId(getIdFromProfile(profile))
existingProfiles = append(existingProfiles, profile)
clientConnections.SetProfiles(existingProfiles)
_, _, err = api.ClientConnectionsPut(ctx).ClientConnections(clientConnections).Authorization(token).Execute()
if err != nil {
return resource.NonRetryableError(fmt.Errorf("Error updating client connection profile %s: %s", d.Id(), err))
}
// check number of client profiles again and verify that is existingProfiles+1
newConnections, _, err := api.ClientConnectionsGet(ctx).Authorization(token).Execute()
if err != nil {
return resource.RetryableError(err)
}
newProfiles := newConnections.GetProfiles()
beep := false
for _, existingProfile := range newProfiles {
log.Printf("[DEBUG] Creation Read Found profile %q - Looking for %s", profile.GetName(), d.Id())
if strings.EqualFold(existingProfile.GetName(), profile.GetName()) {
log.Printf("[DEBUG] Found profile %s after create, OK!", profile.GetName())
beep = true
}
}
if !beep {
return resource.RetryableError(fmt.Errorf("Profile %q did not get created", profile.GetName()))
}
// give the controller a moment before we check the initial status
time.Sleep(time.Duration(duration) * time.Second)
if err := waitForControllers(ctx, meta); err != nil {
return resource.NonRetryableError(fmt.Errorf("1 or more controller never reached a healthy state after creating a client_profile: %s", err))
}
return nil
})
if err != nil {
return fmt.Errorf("Error create: %s", err)
}
return resourceAppgateClientProfileRead(d, meta)
}
func resourceAppgateClientProfileRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading Client Profile id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.ClientConnectionsApi
ctx := context.Background()
clientConnections, _, err := api.ClientConnectionsGet(ctx).Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not read Client Connections %+v", prettyPrintAPIError(err))
}
existingProfiles := clientConnections.GetProfiles()
var p *openapi.ClientConnectionsProfiles
for _, profile := range existingProfiles {
log.Printf("[DEBUG] Reading Found profile %q - Looking for %s", profile.GetName(), d.Id())
if strings.EqualFold(profile.GetName(), d.Id()) && profile.GetName() == d.Id() {
p = &profile
d.Set("name", p.GetName())
d.Set("spa_key_name", p.GetSpaKeyName())
d.Set("identity_provider_name", p.GetIdentityProviderName())
d.Set("url", p.GetUrl())
break
}
}
if p == nil {
log.Printf("[DEBUG] Client Profile id %q not found in client connections profiles", d.Id())
d.SetId("")
}
return nil
}
func resourceAppgateClientProfileDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Delete client profile %+v", d.Id())
ctx := context.Background()
api := meta.(*Client).API.ClientConnectionsApi
err := resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
rand.Seed(time.Now().UnixNano())
duration := rand.Intn(10) // n will be between 0 and 20
log.Printf("[DEBUG] Create Client Profile %s Sleep %d", d.Get("name"), duration)
time.Sleep(time.Duration(duration) * time.Second)
if err := applianceStatsRetryable(ctx, meta); err != nil {
return err
}
token, err := meta.(*Client).GetToken()
if err != nil {
return resource.RetryableError(err)
}
clientConnections, _, err := api.ClientConnectionsGet(ctx).Authorization(token).Execute()
if err != nil {
return resource.RetryableError(fmt.Errorf("Could not read Client Connections during delete %+v", prettyPrintAPIError(err)))
}
existingProfiles := clientConnections.GetProfiles()
var p *openapi.ClientConnectionsProfiles
var newProfiles []openapi.ClientConnectionsProfiles
for i, profile := range existingProfiles {
if strings.EqualFold(profile.GetName(), d.Id()) && profile.GetName() == d.Id() {
p = &profile
// remove the profile from the list and maintain order.
newProfiles = append(existingProfiles[:i], existingProfiles[i+1:]...)
break
}
}
if p == nil {
diag.FromErr(fmt.Errorf("could not find client profile %s during delete", d.Id()))
}
clientConnections.SetProfiles(newProfiles)
_, _, err = api.ClientConnectionsPut(ctx).ClientConnections(clientConnections).Authorization(token).Execute()
if err != nil {
return resource.NonRetryableError(err)
}
// give the controller a moment before we check the initial status
time.Sleep(time.Duration(duration) * time.Second)
if err := waitForControllers(ctx, meta); err != nil {
return resource.NonRetryableError(fmt.Errorf("1 or more controller never reached a healthy state after deleting a client_profile: %s", err))
}
return nil
})
if err != nil {
return fmt.Errorf("Could not delete Client profile %s after retry %+v", d.Id(), err)
}
d.SetId("")
return nil
}