-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_saml.go
235 lines (202 loc) · 6.29 KB
/
resource_saml.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
227
228
229
230
231
232
233
234
235
package cloudsmith
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/cloudsmith-io/cloudsmith-api-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func samlImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ".")
if len(idParts) != 2 {
return nil, fmt.Errorf(
"invalid import ID, must be of the form <organization_slug>.<saml_slug_perm>, got: %s", d.Id(),
)
}
d.Set("organization", idParts[0])
d.SetId(idParts[1])
return []*schema.ResourceData{d}, nil
}
func samlCreate(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
organization := requiredString(d, "organization")
req := pc.APIClient.OrgsApi.OrgsSamlGroupSyncCreate(pc.Auth, organization)
req = req.Data(cloudsmith.OrganizationGroupSyncRequest{
IdpKey: requiredString(d, "idp_key"),
IdpValue: requiredString(d, "idp_value"),
Role: optionalString(d, "role"), // default to Member
Team: requiredString(d, "team"),
Organization: requiredString(d, "organization"),
})
saml, _, err := pc.APIClient.OrgsApi.OrgsSamlGroupSyncCreateExecute(req)
if err != nil {
return err
}
d.SetId(saml.GetSlugPerm())
checkerFunc := func() error {
req := pc.APIClient.OrgsApi.OrgsSamlGroupSyncList(pc.Auth, organization)
_, resp, err := pc.APIClient.OrgsApi.OrgsSamlGroupSyncListExecute(req)
if err != nil {
if resp != nil {
if is404(resp) {
return errKeepWaiting
}
if resp.StatusCode == 422 {
return fmt.Errorf("team does not exist, please check that the team exist")
}
}
return err
}
return nil
}
if err := waiter(checkerFunc, defaultCreationTimeout, defaultCreationInterval); err != nil {
return fmt.Errorf("error waiting for SAML group sync (%s) to be created: %w", d.Id(), err)
}
return samlRead(d, m)
}
func retrieveSAMLSyncListPage(pc *providerConfig, organization string, pageSize int64, pageCount int64) ([]cloudsmith.OrganizationGroupSync, int64, error) {
req := pc.APIClient.OrgsApi.OrgsSamlGroupSyncList(pc.Auth, organization)
req = req.Page(pageCount)
req = req.PageSize(pageSize)
samlPage, resp, err := pc.APIClient.OrgsApi.OrgsSamlGroupSyncListExecute(req)
if err != nil {
if is404(resp) {
return nil, 0, nil
}
return nil, 0, err
}
pageTotal, err := strconv.ParseInt(resp.Header.Get("X-Pagination-Pagetotal"), 10, 64)
if err != nil {
return nil, 0, err
}
return samlPage, pageTotal, nil
}
func retrieveSAMLSyncListPages(pc *providerConfig, organization string, pageSize int64, pageCount int64) ([]cloudsmith.OrganizationGroupSync, error) {
var pageCurrentCount int64 = 1
// A negative or zero count is assumed to mean retrieve the largest size page
samlList := []cloudsmith.OrganizationGroupSync{}
if pageSize == -1 || pageSize == 0 {
pageSize = 100
}
// If no count is supplied assumed to mean retrieve all pages
// we have to retrieve a page to get this count
if pageCount == -1 || pageCount == 0 {
var samlPage []cloudsmith.OrganizationGroupSync
var err error
samlPage, pageCount, err = retrieveSAMLSyncListPage(pc, organization, pageSize, 1)
if err != nil {
return nil, err
}
samlList = append(samlList, samlPage...)
pageCurrentCount++
}
for pageCurrentCount <= pageCount {
samlPage, _, err := retrieveSAMLSyncListPage(pc, organization, pageSize, pageCount)
if err != nil {
return nil, err
}
samlList = append(samlList, samlPage...)
pageCurrentCount++
}
return samlList, nil
}
func samlRead(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
organization := requiredString(d, "organization")
var pageCount, pageSize int64 = -1, -1
samlList, err := retrieveSAMLSyncListPages(pc, organization, pageSize, pageCount)
if err != nil {
return err
}
// Iterate over the saml array to find the matching item
for _, item := range samlList {
if item.GetSlugPerm() == d.Id() {
d.Set("idp_key", item.IdpKey)
d.Set("idp_value", item.IdpValue)
d.Set("role", item.Role)
d.Set("team", item.Team)
d.Set("slug_perm", item.SlugPerm)
// namespace is not returned from the saml group endpoint so we rely on the input value
d.Set("organization", organization)
return nil
}
}
// If no matching item is found, unset the ID and return
d.SetId("")
return nil
}
func samlDelete(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
organization := requiredString(d, "organization")
req := pc.APIClient.OrgsApi.OrgsSamlGroupSyncDelete(pc.Auth, organization, d.Id())
_, err := pc.APIClient.OrgsApi.OrgsSamlGroupSyncDeleteExecute(req)
if err != nil {
return err
}
checkerFunc := func() error {
req := pc.APIClient.OrgsApi.OrgsSamlGroupSyncList(pc.Auth, organization)
_, resp, err := pc.APIClient.OrgsApi.OrgsSamlGroupSyncListExecute(req)
if err != nil {
if resp != nil {
if is404(resp) {
return nil
}
}
return err
}
return nil
}
if err := waiter(checkerFunc, defaultDeletionTimeout, defaultDeletionInterval); err != nil {
return fmt.Errorf("error waiting for SAML group sync (%s) to be deleted: %w", d.Id(), err)
}
return nil
}
// This is a workaround for not having a proper update endpoint for SAML group sync, we are recreating the entry based on new+old values
func samlUpdate(d *schema.ResourceData, m interface{}) error {
if err := samlDelete(d, m); err != nil {
return err
}
return samlCreate(d, m)
}
func resourceSAML() *schema.Resource {
return &schema.Resource{
Create: samlCreate,
Read: samlRead,
Update: samlUpdate,
Delete: samlDelete,
Importer: &schema.ResourceImporter{
StateContext: samlImport,
},
Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"idp_key": {
Type: schema.TypeString,
Required: true,
},
"idp_value": {
Type: schema.TypeString,
Required: true,
},
"role": {
Type: schema.TypeString,
Optional: true,
Default: "Member",
ValidateFunc: validation.StringInSlice([]string{"Member", "Manager"}, false),
},
"team": {
Type: schema.TypeString,
Required: true,
},
"slug_perm": {
Type: schema.TypeString,
Computed: true,
},
},
}
}