-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_entitlement.go
273 lines (239 loc) · 9.3 KB
/
resource_entitlement.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package cloudsmith
import (
"context"
"fmt"
"strings"
"time"
"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 importEntitlement(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), ".")
if len(idParts) != 3 {
return nil, fmt.Errorf(
"invalid import ID, must be of the form <organization_slug>.<repository_slug>.<entitlement_slug>, got: %s", d.Id(),
)
}
d.Set("namespace", idParts[0])
d.Set("repository", idParts[1])
d.SetId(idParts[2])
return []*schema.ResourceData{d}, nil
}
func resourceEntitlementCreate(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
namespace := requiredString(d, "namespace")
repository := requiredString(d, "repository")
req := pc.APIClient.EntitlementsApi.EntitlementsCreate(pc.Auth, namespace, repository)
req = req.Data(cloudsmith.RepositoryTokenRequest{
IsActive: optionalBool(d, "is_active"),
LimitDateRangeFrom: nullableTime(d, "limit_date_range_from"),
LimitDateRangeTo: nullableTime(d, "limit_date_range_to"),
LimitNumClients: nullableInt64(d, "limit_num_clients"),
LimitNumDownloads: nullableInt64(d, "limit_num_downloads"),
LimitPackageQuery: nullableString(d, "limit_package_query"),
LimitPathQuery: nullableString(d, "limitPathQuery"),
Name: requiredString(d, "name"),
Token: optionalString(d, "token"),
})
req = req.ShowTokens(true)
entitlement, _, err := pc.APIClient.EntitlementsApi.EntitlementsCreateExecute(req)
if err != nil {
return err
}
d.SetId(entitlement.GetSlugPerm())
checkerFunc := func() error {
req := pc.APIClient.EntitlementsApi.EntitlementsRead(pc.Auth, namespace, repository, d.Id())
if _, resp, err := pc.APIClient.EntitlementsApi.EntitlementsReadExecute(req); err != nil {
if is404(resp) {
return errKeepWaiting
}
return err
}
return nil
}
if err := waiter(checkerFunc, defaultCreationTimeout, defaultCreationInterval); err != nil {
return fmt.Errorf("error waiting for entitlement (%s) to be created: %w", d.Id(), err)
}
return resourceEntitlementRead(d, m)
}
func resourceEntitlementRead(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
namespace := requiredString(d, "namespace")
repository := requiredString(d, "repository")
req := pc.APIClient.EntitlementsApi.EntitlementsRead(pc.Auth, namespace, repository, d.Id())
req = req.ShowTokens(true)
entitlement, resp, err := pc.APIClient.EntitlementsApi.EntitlementsReadExecute(req)
if err != nil {
if is404(resp) {
d.SetId("")
return nil
}
return err
}
d.Set("is_active", entitlement.GetIsActive())
d.Set("limit_date_range_from", timeToString(entitlement.GetLimitDateRangeFrom()))
d.Set("limit_date_range_to", timeToString(entitlement.GetLimitDateRangeTo()))
d.Set("limit_num_clients", entitlement.GetLimitNumClients())
d.Set("limit_num_downloads", entitlement.GetLimitNumDownloads())
d.Set("limit_package_query", entitlement.GetLimitPackageQuery())
d.Set("limit_path_query", entitlement.GetLimitPathQuery())
d.Set("name", entitlement.GetName())
d.Set("token", entitlement.GetToken())
// namespace and repository are not returned from the entitlement read
// endpoint, so we can use the values stored in resource state. We rely on
// ForceNew to ensure if either changes a new resource is created.
d.Set("namespace", namespace)
d.Set("repository", repository)
return nil
}
func resourceEntitlementUpdate(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
namespace := requiredString(d, "namespace")
repository := requiredString(d, "repository")
req := pc.APIClient.EntitlementsApi.EntitlementsPartialUpdate(pc.Auth, namespace, repository, d.Id())
req = req.Data(cloudsmith.RepositoryTokenRequestPatch{
IsActive: optionalBool(d, "is_active"),
LimitDateRangeFrom: nullableTime(d, "limit_date_range_from"),
LimitDateRangeTo: nullableTime(d, "limit_date_range_to"),
LimitNumClients: nullableInt64(d, "limit_num_clients"),
LimitNumDownloads: nullableInt64(d, "limit_num_downloads"),
LimitPackageQuery: nullableString(d, "limit_package_query"),
LimitPathQuery: nullableString(d, "limit_path_query"),
Name: optionalString(d, "name"),
Token: optionalString(d, "token"),
})
req = req.ShowTokens(true)
entitlement, _, err := pc.APIClient.EntitlementsApi.EntitlementsPartialUpdateExecute(req)
if err != nil {
return err
}
d.SetId(entitlement.GetSlugPerm())
checkerFunc := func() error {
// this is somewhat of a hack until we have a better way to poll for an
// entitlement being updated (changes incoming on the API side)
time.Sleep(time.Second * 5)
return nil
}
if err := waiter(checkerFunc, defaultUpdateTimeout, defaultUpdateInterval); err != nil {
return fmt.Errorf("error waiting for entitlement (%s) to be updated: %w", d.Id(), err)
}
return resourceEntitlementRead(d, m)
}
func resourceEntitlementDelete(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
namespace := requiredString(d, "namespace")
repository := requiredString(d, "repository")
req := pc.APIClient.EntitlementsApi.EntitlementsDelete(pc.Auth, namespace, repository, d.Id())
_, err := pc.APIClient.EntitlementsApi.EntitlementsDeleteExecute(req)
if err != nil {
return err
}
checkerFunc := func() error {
req := pc.APIClient.EntitlementsApi.EntitlementsRead(pc.Auth, namespace, repository, d.Id())
if _, resp, err := pc.APIClient.EntitlementsApi.EntitlementsReadExecute(req); err != nil {
if is404(resp) {
return nil
}
return err
}
return errKeepWaiting
}
if err := waiter(checkerFunc, defaultDeletionTimeout, defaultDeletionInterval); err != nil {
return fmt.Errorf("error waiting for entitlement (%s) to be deleted: %w", d.Id(), err)
}
return nil
}
//nolint:funlen
func resourceEntitlement() *schema.Resource {
return &schema.Resource{
Create: resourceEntitlementCreate,
Read: resourceEntitlementRead,
Update: resourceEntitlementUpdate,
Delete: resourceEntitlementDelete,
Importer: &schema.ResourceImporter{
StateContext: importEntitlement,
},
Schema: map[string]*schema.Schema{
"is_active": {
Type: schema.TypeBool,
Description: "If enabled, the token will allow downloads based on configured restrictions (if any).",
Optional: true,
Computed: true,
},
"limit_date_range_from": {
Type: schema.TypeString,
Description: "The starting date/time the token is allowed to be used from.",
Optional: true,
ValidateFunc: validation.IsRFC3339Time,
},
"limit_date_range_to": {
Type: schema.TypeString,
Description: "The ending date/time the token is allowed to be used until.",
Optional: true,
ValidateFunc: validation.IsRFC3339Time,
},
"limit_num_clients": {
Type: schema.TypeInt,
Description: "The maximum number of unique clients allowed for the token. Please " +
"note that since clients are calculated asynchronously (after the download " +
"happens), the limit may not be imposed immediately but at a later point.",
Optional: true,
Computed: true,
},
"limit_num_downloads": {
Type: schema.TypeInt,
Description: "The maximum number of downloads allowed for the token. Please note " +
"that since downloads are calculated asynchronously (after the download " +
"happens), the limit may not be imposed immediately but at a later point.",
Optional: true,
Computed: true,
},
"limit_package_query": {
Type: schema.TypeString,
Description: "The package-based search query to apply to restrict downloads to. " +
"This uses the same syntax as the standard search used for repositories, and " +
"also supports boolean logic operators such as OR/AND/NOT and parentheses for " +
"grouping. This will still allow access to non-package files, such as metadata.",
Optional: true,
},
"limit_path_query": {
Type: schema.TypeString,
Description: "The path-based search query to apply to restrict downloads to. This " +
"supports boolean logic operators such as OR/AND/NOT and parentheses for " +
"grouping. The path evaluated does not include the domain name, the namespace, " +
"the entitlement code used, the package format, etc. and it always starts with " +
"a forward slash.",
Optional: true,
},
"name": {
Type: schema.TypeString,
Description: "A descriptive name for the entitlement.",
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"namespace": {
Type: schema.TypeString,
Description: "Namespace to which this entitlement belongs.",
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"repository": {
Type: schema.TypeString,
Description: "Repository to which this entitlement belongs.",
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"token": {
Type: schema.TypeString,
Description: "The literal value of the token to be created.",
Optional: true,
Computed: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}