/
resource_license_policy.go
258 lines (224 loc) · 8.02 KB
/
resource_license_policy.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
package cloudsmith
import (
"context"
"fmt"
"net/http"
"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"
)
const (
CreatedAt string = "created_at"
Description string = "description"
Name string = "name"
AllowUnknownLicenses string = "allow_unknown_licenses"
OnViolationQuarantine string = "on_violation_quarantine"
SlugPerm string = "slug_perm"
SpdxIdentifiers string = "spdx_identifiers"
PackageQueryString string = "package_query_string"
UpdatedAt string = "updated_at"
Organization string = "organization"
)
func importLicensePolicy(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>.<policy_slug_perm>, got: %s", d.Id(),
)
}
d.Set(Organization, idParts[0])
d.SetId(idParts[1])
return []*schema.ResourceData{d}, nil
}
func resourceLicensePolicyCreate(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
org := requiredString(d, Organization)
req := pc.APIClient.OrgsApi.OrgsLicensePolicyCreate(pc.Auth, org)
req = req.Data(cloudsmith.OrganizationPackageLicensePolicyRequest{
AllowUnknownLicenses: optionalBool(d, AllowUnknownLicenses),
Description: nullableString(d, Description),
Name: requiredString(d, Name),
OnViolationQuarantine: optionalBool(d, OnViolationQuarantine),
SpdxIdentifiers: expandStrings(d, SpdxIdentifiers),
PackageQueryString: nullableString(d, PackageQueryString),
})
licensePolicy, resp, err := pc.APIClient.OrgsApi.OrgsLicensePolicyCreateExecute(req)
if err != nil {
if resp.StatusCode == http.StatusUnprocessableEntity {
return fmt.Errorf("invalid spdx_identifiers: %v", expandStrings(d, SpdxIdentifiers))
}
return err
}
d.SetId(licensePolicy.GetSlugPerm())
checkerFunc := func() error {
req := pc.APIClient.OrgsApi.OrgsLicensePolicyRead(pc.Auth, org, d.Id())
if _, resp, err := pc.APIClient.OrgsApi.OrgsLicensePolicyReadExecute(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 license policy (%s) to be created: %s", d.Id(), err)
}
return resourceLicensePolicyRead(d, m)
}
func resourceLicensePolicyUpdate(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
org := requiredString(d, Organization)
req := pc.APIClient.OrgsApi.OrgsLicensePolicyUpdate(pc.Auth, org, d.Id())
req = req.Data(cloudsmith.OrganizationPackageLicensePolicyRequest{
AllowUnknownLicenses: optionalBool(d, AllowUnknownLicenses),
Description: nullableString(d, Description),
Name: requiredString(d, Name),
OnViolationQuarantine: optionalBool(d, OnViolationQuarantine),
SpdxIdentifiers: expandStrings(d, SpdxIdentifiers),
PackageQueryString: nullableString(d, PackageQueryString),
})
licensePolicy, resp, err := pc.APIClient.OrgsApi.OrgsLicensePolicyUpdateExecute(req)
if err != nil {
if resp.StatusCode == http.StatusUnprocessableEntity {
return fmt.Errorf("invalid spdx_identifiers: %v", expandStrings(d, SpdxIdentifiers))
}
return err
}
d.SetId(licensePolicy.GetSlugPerm())
checkerFunc := func() error {
// this is somewhat of a hack until we have a better way to poll for a
// policy 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 license policy (%s) to be updated: %w", d.Id(), err)
}
return resourceLicensePolicyRead(d, m)
}
func resourceLicensePolicyDelete(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
org := requiredString(d, Organization)
req := pc.APIClient.OrgsApi.OrgsLicensePolicyDelete(pc.Auth, org, d.Id())
_, err := pc.APIClient.OrgsApi.OrgsLicensePolicyDeleteExecute(req)
if err != nil {
return err
}
checkerFunc := func() error {
req := pc.APIClient.OrgsApi.OrgsLicensePolicyRead(pc.Auth, org, d.Id())
if _, resp, err := pc.APIClient.OrgsApi.OrgsLicensePolicyReadExecute(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 license policy (%s) to be deleted: %w", d.Id(), err)
}
return nil
}
func resourceLicensePolicyRead(d *schema.ResourceData, m interface{}) error {
pc := m.(*providerConfig)
org := requiredString(d, Organization)
req := pc.APIClient.OrgsApi.OrgsLicensePolicyRead(pc.Auth, org, d.Id())
licensePolicy, resp, err := pc.APIClient.OrgsApi.OrgsLicensePolicyReadExecute(req)
if err != nil {
if is404(resp) {
d.SetId("")
return nil
}
return err
}
_ = d.Set(CreatedAt, licensePolicy.GetCreatedAt().String())
_ = d.Set(Description, licensePolicy.GetDescription())
_ = d.Set(Name, licensePolicy.GetName())
_ = d.Set(OnViolationQuarantine, licensePolicy.GetOnViolationQuarantine())
_ = d.Set(AllowUnknownLicenses, licensePolicy.GetAllowUnknownLicenses())
_ = d.Set(SlugPerm, licensePolicy.GetSlugPerm())
_ = d.Set(SpdxIdentifiers, flattenStrings(licensePolicy.GetSpdxIdentifiers()))
_ = d.Set(PackageQueryString, licensePolicy.GetPackageQueryString())
_ = d.Set(UpdatedAt, licensePolicy.GetUpdatedAt().String())
// organization is not returned from the 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(Organization, org)
return nil
}
//nolint:funlen
func resourceLicensePolicy() *schema.Resource {
return &schema.Resource{
Create: resourceLicensePolicyCreate,
Read: resourceLicensePolicyRead,
Update: resourceLicensePolicyUpdate,
Delete: resourceLicensePolicyDelete,
Importer: &schema.ResourceImporter{
StateContext: importLicensePolicy,
},
Schema: map[string]*schema.Schema{
CreatedAt: {
Type: schema.TypeString,
Description: "The time the policy was created at.",
Computed: true,
},
Description: {
Type: schema.TypeString,
Description: "The description of the license policy.",
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
Name: {
Type: schema.TypeString,
Description: "The name of the license policy.",
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
OnViolationQuarantine: {
Type: schema.TypeBool,
Description: "On violation of the license policy, quarantine violating packages.",
Optional: true,
Computed: true,
},
AllowUnknownLicenses: {
Type: schema.TypeBool,
Description: "Allow unknown licenses within the policy.",
Optional: true,
Computed: true,
},
SlugPerm: {
Type: schema.TypeString,
Description: "Slug-perm of the license policy",
Computed: true,
},
SpdxIdentifiers: {
Type: schema.TypeSet,
Description: "The licenses to deny.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
Required: true,
},
PackageQueryString: {
Type: schema.TypeString,
Description: "A search / filter string of packages to include in the policy.",
Optional: true,
},
UpdatedAt: {
Type: schema.TypeString,
Description: "The time the policy last updated at.",
Computed: true,
},
Organization: {
Type: schema.TypeString,
Description: "Organization to which this policy belongs.",
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}