Skip to content

Commit 50d9f1e

Browse files
committed
resource/gitlab_managed_license: Make GitLab 15.0 compatible
1 parent 585c284 commit 50d9f1e

File tree

4 files changed

+54
-65
lines changed

4 files changed

+54
-65
lines changed

docs/resources/managed_license.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ resource "gitlab_managed_license" "mit" {
3737

3838
### Required
3939

40-
- `approval_status` (String) The approval status of the license. Valid values are: `approved`, `blacklisted`, `allowed`, `denied`. "approved" and "blacklisted"
41-
have been deprecated in favor of "allowed" and "denied"; use "allowed" and "denied" for GitLab versions 15.0 and higher.
40+
- `approval_status` (String) The approval status of the license. Valid values are: `approved`, `blacklisted`, `allowed`, `denied`. "approved" and "blacklisted"
41+
have been deprecated in favor of "allowed" and "denied"; use "allowed" and "denied" for GitLab versions 15.0 and higher.
4242
Prior to version 15.0 and after 14.6, the values are equivalent.
4343
- `name` (String) The name of the managed license (I.e., 'Apache License 2.0' or 'MIT license')
4444
- `project` (String) The ID of the project under which the managed license will be created.

internal/provider/helper_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,6 @@ func isRunningInCE() (bool, error) {
6868
return !isEE, err
6969
}
7070

71-
// orSkipFunc accepts many skipFunc and returns "true" if any returns true.
72-
func orSkipFunc(input ...SkipFunc) SkipFunc {
73-
return func() (bool, error) {
74-
for _, item := range input {
75-
result, err := item()
76-
if err != nil {
77-
return false, err
78-
}
79-
if result {
80-
return result, nil
81-
}
82-
}
83-
return false, nil
84-
}
85-
}
86-
8771
// testAccCheckEE is a test helper that skips the current test if the GitLab version is not GitLab Enterprise.
8872
// This is useful when the version needs to be checked during setup, before the Terraform acceptance test starts.
8973
func testAccCheckEE(t *testing.T) {

internal/provider/resource_gitlab_managed_license.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ var _ = registerResource("gitlab_managed_license", func() *schema.Resource {
5353
ForceNew: false,
5454
ValidateFunc: validation.StringInSlice(managedLicenseAllowedValues, true),
5555
DiffSuppressFunc: checkDeprecatedValuesForDiff,
56-
Description: fmt.Sprintf(`The approval status of the license. Valid values are: %s. "approved" and "blacklisted"
57-
have been deprecated in favor of "allowed" and "denied"; use "allowed" and "denied" for GitLab versions 15.0 and higher.
56+
Description: fmt.Sprintf(`The approval status of the license. Valid values are: %s. "approved" and "blacklisted"
57+
have been deprecated in favor of "allowed" and "denied"; use "allowed" and "denied" for GitLab versions 15.0 and higher.
5858
Prior to version 15.0 and after 14.6, the values are equivalent.`, renderValueListForDocs(managedLicenseAllowedValues)),
5959
},
6060
},
@@ -65,9 +65,14 @@ func resourceGitlabManagedLicenseCreate(ctx context.Context, d *schema.ResourceD
6565
client := meta.(*gitlab.Client)
6666
project := d.Get("project").(string)
6767

68+
approvalStatus, err := stringToApprovalStatus(ctx, client, d.Get("approval_status").(string))
69+
if err != nil {
70+
return diag.FromErr(err)
71+
}
72+
6873
options := &gitlab.AddManagedLicenseOptions{
6974
Name: gitlab.String(d.Get("name").(string)),
70-
ApprovalStatus: stringToApprovalStatus(d.Get("approval_status").(string)),
75+
ApprovalStatus: approvalStatus,
7176
}
7277

7378
log.Printf("[DEBUG] create gitlab Managed License on Project %s, with Name %s", project, *options.Name)
@@ -131,12 +136,13 @@ func resourceGitlabManagedLicenseUpdate(ctx context.Context, d *schema.ResourceD
131136
return diag.FromErr(err)
132137
}
133138

134-
opts := &gitlab.EditManagedLicenceOptions{
135-
ApprovalStatus: stringToApprovalStatus(d.Get("approval_status").(string)),
139+
approvalStatus, err := stringToApprovalStatus(ctx, client, d.Get("approval_status").(string))
140+
if err != nil {
141+
return diag.FromErr(err)
136142
}
137143

138-
if d.HasChange("approval_status") {
139-
opts.ApprovalStatus = stringToApprovalStatus(d.Get("approval_status").(string))
144+
opts := &gitlab.EditManagedLicenceOptions{
145+
ApprovalStatus: approvalStatus,
140146
}
141147

142148
log.Printf("[DEBUG] update gitlab Managed License %s", d.Id())
@@ -152,22 +158,32 @@ func resourceGitlabManagedLicenseUpdate(ctx context.Context, d *schema.ResourceD
152158
}
153159

154160
// Convert the incoming string into the proper constant value for passing into the API.
155-
func stringToApprovalStatus(s string) *gitlab.LicenseApprovalStatusValue {
156-
lookup := map[string]gitlab.LicenseApprovalStatusValue{
157-
"approved": gitlab.LicenseApproved,
158-
"blacklisted": gitlab.LicenseBlacklisted,
159-
160-
// This is counter-intuitive, but currently the API response from the non-deprecated
161-
// values is the deprecated values. So we have to map them here.
162-
"allowed": gitlab.LicenseApproved,
163-
"denied": gitlab.LicenseBlacklisted,
164-
}
161+
func stringToApprovalStatus(ctx context.Context, client *gitlab.Client, s string) (*gitlab.LicenseApprovalStatusValue, error) {
162+
var value gitlab.LicenseApprovalStatusValue
163+
notSupported, err := isGitLabVersionAtLeast(ctx, client, "15.0")()
164+
if err != nil {
165+
return nil, fmt.Errorf("failed to fetch GitLab version: %+v", err)
166+
}
167+
if notSupported {
168+
value = gitlab.LicenseApprovalStatusValue(s)
169+
} else {
170+
lookup := map[string]gitlab.LicenseApprovalStatusValue{
171+
"approved": gitlab.LicenseApproved,
172+
"blacklisted": gitlab.LicenseBlacklisted,
173+
174+
// This is counter-intuitive, but currently the API response from the non-deprecated
175+
// values is the deprecated values. So we have to map them here.
176+
"allowed": gitlab.LicenseApproved,
177+
"denied": gitlab.LicenseBlacklisted,
178+
}
165179

166-
value, ok := lookup[s]
167-
if !ok {
168-
return nil
180+
v, ok := lookup[s]
181+
if !ok {
182+
return nil, fmt.Errorf("invalid approval status value %q", s)
183+
}
184+
value = v
169185
}
170-
return &value
186+
return &value, nil
171187
}
172188

173189
func projectIdAndLicenseIdFromId(id string) (string, int, error) {

internal/provider/resource_gitlab_managed_license_test.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,21 @@ func TestAccGitlabManagedLicense_basic(t *testing.T) {
2020
rInt := acctest.RandInt()
2121

2222
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { testAccCheckEE(t) },
2324
ProviderFactories: providerFactories,
2425
CheckDestroy: testAccCheckManagedLicenseDestroy,
2526
Steps: []resource.TestStep{
2627
{
2728
// Create a managed license with an "allowed" state
28-
SkipFunc: isRunningInCE,
29-
Config: testManagedLicenseConfig(rInt, "allowed"),
30-
Check: resource.ComposeTestCheckFunc(
31-
testAccCheckGitlabManagedLicenseExists("gitlab_managed_license.fixme", &managedLicense),
32-
),
29+
Config: testManagedLicenseConfig(rInt, "allowed"),
30+
Check: testAccCheckGitlabManagedLicenseExists("gitlab_managed_license.fixme", &managedLicense),
3331
},
3432
{
3533
// Update the managed license to have a denied state
36-
SkipFunc: isRunningInCE,
37-
Config: testManagedLicenseConfig(rInt, "denied"),
38-
Check: resource.ComposeTestCheckFunc(
39-
//Even though the API accepts "denied", it still returns "blacklisted" until 15.0
40-
testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", "blacklisted", &managedLicense),
41-
),
34+
Config: testManagedLicenseConfig(rInt, "denied"),
35+
Check: testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", "denied", &managedLicense),
4236
},
4337
{
44-
SkipFunc: isRunningInCE,
4538
ResourceName: "gitlab_managed_license.fixme",
4639
ImportState: true,
4740
ImportStateVerify: true,
@@ -55,36 +48,28 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
5548
rInt := acctest.RandInt()
5649

5750
resource.Test(t, resource.TestCase{
51+
PreCheck: func() {
52+
testAccRequiresLessThan(t, "15.0")
53+
testAccCheckEE(t)
54+
},
5855
ProviderFactories: providerFactories,
5956
CheckDestroy: testAccCheckManagedLicenseDestroy,
6057
Steps: []resource.TestStep{
6158
{
6259
// Create a managed license with an "approved" state
63-
SkipFunc: orSkipFunc(
64-
isRunningInCE,
65-
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
66-
),
6760
Config: testManagedLicenseConfig(rInt, "approved"),
6861
Check: resource.ComposeTestCheckFunc(
6962
testAccCheckGitlabManagedLicenseExists("gitlab_managed_license.fixme", &managedLicense),
7063
),
7164
},
7265
{
73-
// Update the managed license to have a blacklisted state
74-
SkipFunc: orSkipFunc(
75-
isRunningInCE,
76-
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
77-
),
66+
// Update the managed license to have a "blacklisted" state
7867
Config: testManagedLicenseConfig(rInt, "blacklisted"),
7968
Check: resource.ComposeTestCheckFunc(
8069
testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", "blacklisted", &managedLicense),
8170
),
8271
},
8372
{
84-
SkipFunc: orSkipFunc(
85-
isRunningInCE,
86-
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
87-
),
8873
ResourceName: "gitlab_managed_license.fixme",
8974
ImportState: true,
9075
ImportStateVerify: true,
@@ -133,7 +118,11 @@ func testAccCheckGitlabManagedLicenseStatus(resource string, status string, lice
133118
}
134119

135120
for _, gotLicense := range licenses {
136-
if gotLicense.ApprovalStatus == *stringToApprovalStatus(status) {
121+
approvalStatus, err := stringToApprovalStatus(context.TODO(), testGitlabClient, status)
122+
if err != nil {
123+
return err
124+
}
125+
if gotLicense.ApprovalStatus == *approvalStatus {
137126
*license = *gotLicense
138127
return nil
139128
}

0 commit comments

Comments
 (0)