Skip to content

Commit 918becc

Browse files
authored
Merge pull request #952 from PatrickRice-KSC/update_managed_license_values
Update managed_license resources to deprecate existing values and add GitLab 15+ values
2 parents dc70175 + 90b522b commit 918becc

File tree

5 files changed

+133
-18
lines changed

5 files changed

+133
-18
lines changed

docs/resources/managed_license.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "gitlab_project" "foo" {
2828
resource "gitlab_managed_license" "mit" {
2929
project = gitlab_project.foo.id
3030
name = "MIT license"
31-
approval_status = "approved"
31+
approval_status = "allowed"
3232
}
3333
```
3434

@@ -37,7 +37,9 @@ resource "gitlab_managed_license" "mit" {
3737

3838
### Required
3939

40-
- **approval_status** (String) Whether the license is approved or not. Only 'approved' or 'blacklisted' allowed.
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.
42+
Prior to version 15.0 and after 14.6, the values are equivalent.
4143
- **name** (String) The name of the managed license (I.e., 'Apache License 2.0' or 'MIT license')
4244
- **project** (String) The ID of the project under which the managed license will be created.
4345

examples/resources/gitlab_managed_license/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ resource "gitlab_project" "foo" {
77
resource "gitlab_managed_license" "mit" {
88
project = gitlab_project.foo.id
99
name = "MIT license"
10-
approval_status = "approved"
10+
approval_status = "allowed"
1111
}

internal/provider/helper_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/xanzy/go-gitlab"
1616
)
1717

18+
type SkipFunc = func() (bool, error)
19+
1820
func init() {
1921
// We are using the gomega package for its matchers only, but it requires us to register a handler anyway.
2022
gomega.RegisterFailHandler(func(_ string, _ ...int) {
@@ -71,6 +73,22 @@ func testAccCheck(t *testing.T) {
7173
}
7274
}
7375

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

internal/provider/resource_gitlab_managed_license.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/xanzy/go-gitlab"
1313
)
1414

15+
var managedLicenseAllowedValues = []string{
16+
"approved", "blacklisted", "allowed", "denied",
17+
}
18+
1519
var _ = registerResource("gitlab_managed_license", func() *schema.Resource {
1620
return &schema.Resource{
1721
Description: `The ` + "`" + `gitlab_managed_license` + "`" + ` resource allows to manage the lifecycle of a managed license.
@@ -44,11 +48,14 @@ var _ = registerResource("gitlab_managed_license", func() *schema.Resource {
4448
Description: "The name of the managed license (I.e., 'Apache License 2.0' or 'MIT license')",
4549
},
4650
"approval_status": {
47-
Type: schema.TypeString,
48-
Required: true,
49-
ForceNew: false,
50-
ValidateFunc: validation.StringInSlice([]string{"approved", "blacklisted"}, true),
51-
Description: "Whether the license is approved or not. Only 'approved' or 'blacklisted' allowed.",
51+
Type: schema.TypeString,
52+
Required: true,
53+
ForceNew: false,
54+
ValidateFunc: validation.StringInSlice(managedLicenseAllowedValues, true),
55+
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.
58+
Prior to version 15.0 and after 14.6, the values are equivalent.`, renderValueListForDocs(managedLicenseAllowedValues)),
5259
},
5360
},
5461
}
@@ -149,6 +156,11 @@ func stringToApprovalStatus(s string) *gitlab.LicenseApprovalStatusValue {
149156
lookup := map[string]gitlab.LicenseApprovalStatusValue{
150157
"approved": gitlab.LicenseApproved,
151158
"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,
152164
}
153165

154166
value, ok := lookup[s]
@@ -171,3 +183,20 @@ func projectIdAndLicenseIdFromId(id string) (string, int, error) {
171183

172184
return projectId, licenseId, nil
173185
}
186+
187+
func checkDeprecatedValuesForDiff(k, oldValue, newValue string, d *schema.ResourceData) bool {
188+
approvedValues := []string{"approved", "allowed"}
189+
deniedValues := []string{"blacklisted", "denied"}
190+
191+
// While we could technically combine these two "if" blocks, this seems more readable
192+
// and should have the same execution pattern.
193+
if contains(approvedValues, oldValue) && contains(approvedValues, newValue) {
194+
return true
195+
}
196+
197+
if contains(deniedValues, oldValue) && contains(deniedValues, newValue) {
198+
return true
199+
}
200+
201+
return false
202+
}

internal/provider/resource_gitlab_managed_license_test.go

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ func TestAccGitlabManagedLicense_basic(t *testing.T) {
2121
CheckDestroy: testAccCheckManagedLicenseDestroy,
2222
Steps: []resource.TestStep{
2323
{
24-
// Create a managed license with an "approved" state
24+
// Create a managed license with an "allowed" state
2525
SkipFunc: isRunningInCE,
26-
Config: testManagedLicenseConfig(rInt, "approved"),
26+
Config: testManagedLicenseConfig(rInt, "allowed"),
2727
Check: resource.ComposeTestCheckFunc(
2828
testAccCheckGitlabManagedLicenseExists("gitlab_managed_license.fixme", &managedLicense),
2929
),
3030
},
3131
{
32-
// Update the managed license to have a blacklisted state
32+
// Update the managed license to have a denied state
3333
SkipFunc: isRunningInCE,
34-
Config: testManagedLicenseConfig(rInt, "blacklisted"),
34+
Config: testManagedLicenseConfig(rInt, "denied"),
3535
Check: resource.ComposeTestCheckFunc(
36-
testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", &managedLicense),
36+
//Even though the API accepts "denied", it still returns "blacklisted" until 15.0
37+
testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", "blacklisted", &managedLicense),
3738
),
3839
},
3940
{
@@ -46,14 +47,79 @@ func TestAccGitlabManagedLicense_basic(t *testing.T) {
4647
})
4748
}
4849

49-
func testAccCheckGitlabManagedLicenseStatus(n string, license *gitlab.ManagedLicense) resource.TestCheckFunc {
50+
func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
51+
var managedLicense gitlab.ManagedLicense
52+
rInt := acctest.RandInt()
53+
54+
resource.Test(t, resource.TestCase{
55+
PreCheck: func() {},
56+
ProviderFactories: providerFactories,
57+
CheckDestroy: testAccCheckManagedLicenseDestroy,
58+
Steps: []resource.TestStep{
59+
{
60+
// Create a managed license with an "approved" state
61+
SkipFunc: orSkipFunc(
62+
isRunningInCE,
63+
isGitLabVersionLessThan(testGitlabClient, "15.0"),
64+
),
65+
Config: testManagedLicenseConfig(rInt, "approved"),
66+
Check: resource.ComposeTestCheckFunc(
67+
testAccCheckGitlabManagedLicenseExists("gitlab_managed_license.fixme", &managedLicense),
68+
),
69+
},
70+
{
71+
// Update the managed license to have a blacklisted state
72+
SkipFunc: orSkipFunc(
73+
isRunningInCE,
74+
isGitLabVersionLessThan(testGitlabClient, "15.0"),
75+
),
76+
Config: testManagedLicenseConfig(rInt, "blacklisted"),
77+
Check: resource.ComposeTestCheckFunc(
78+
testAccCheckGitlabManagedLicenseStatus("gitlab_managed_license.fixme", "blacklisted", &managedLicense),
79+
),
80+
},
81+
{
82+
SkipFunc: orSkipFunc(
83+
isRunningInCE,
84+
isGitLabVersionLessThan(testGitlabClient, "15.0"),
85+
),
86+
ResourceName: "gitlab_managed_license.fixme",
87+
ImportState: true,
88+
ImportStateVerify: true,
89+
},
90+
},
91+
})
92+
}
93+
94+
func TestCheckManagedLicenseStatusDiffFunc(t *testing.T) {
95+
if !checkDeprecatedValuesForDiff("", "approved", "allowed", nil) {
96+
t.Log("approved and allowed should be suppressed")
97+
t.Fail()
98+
}
99+
100+
if !checkDeprecatedValuesForDiff("", "denied", "blacklisted", nil) {
101+
t.Log("denied and blacklisted should be suppressed")
102+
t.Fail()
103+
}
104+
105+
if checkDeprecatedValuesForDiff("", "denied", "approved", nil) {
106+
t.Log("denied and approved should not be suppressed")
107+
t.Fail()
108+
}
109+
110+
if checkDeprecatedValuesForDiff("", "allowed", "blacklisted", nil) {
111+
t.Log("allowed and blacklisted should not be suppressed")
112+
t.Fail()
113+
}
114+
}
115+
116+
func testAccCheckGitlabManagedLicenseStatus(resource string, status string, license *gitlab.ManagedLicense) resource.TestCheckFunc {
50117
return func(s *terraform.State) error {
51-
rs, ok := s.RootModule().Resources[n]
118+
rs, ok := s.RootModule().Resources[resource]
52119
if !ok {
53-
return fmt.Errorf("not Found: %s", n)
120+
return fmt.Errorf("not Found: %s", resource)
54121
}
55122

56-
licenseStatus := rs.Primary.Attributes["approval_status"]
57123
project := rs.Primary.Attributes["project"]
58124
if project == "" {
59125
return fmt.Errorf("no project ID is set")
@@ -65,7 +131,7 @@ func testAccCheckGitlabManagedLicenseStatus(n string, license *gitlab.ManagedLic
65131
}
66132

67133
for _, gotLicense := range licenses {
68-
if gotLicense.ApprovalStatus == *stringToApprovalStatus(licenseStatus) {
134+
if gotLicense.ApprovalStatus == *stringToApprovalStatus(status) {
69135
*license = *gotLicense
70136
return nil
71137
}

0 commit comments

Comments
 (0)