From 2b1d3df510fe99ea77819960f283aa7c75a9fa8c Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Thu, 30 May 2024 10:30:43 -0700 Subject: [PATCH 1/3] Ensure `roles` attribute has at least 1 item --- .../resource/resource_project_group.go | 3 +- .../resource/resource_project_group_test.go | 66 +++++++++++++++++++ pkg/project/resource/resource_project_user.go | 3 +- .../resource/resource_project_user_test.go | 64 ++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) diff --git a/pkg/project/resource/resource_project_group.go b/pkg/project/resource/resource_project_group.go index c2ee3488..fa38db0e 100644 --- a/pkg/project/resource/resource_project_group.go +++ b/pkg/project/resource/resource_project_group.go @@ -46,7 +46,8 @@ func ProjectGroupResource() *schema.Resource { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, - Description: "List of pre-defined Project or custom roles", + MinItems: 1, + Description: "List of pre-defined Project or custom roles. Must have at least 1 role, e.g. 'Viewer'", }, } diff --git a/pkg/project/resource/resource_project_group_test.go b/pkg/project/resource/resource_project_group_test.go index f955d45c..47cd91db 100644 --- a/pkg/project/resource/resource_project_group_test.go +++ b/pkg/project/resource/resource_project_group_test.go @@ -2,6 +2,7 @@ package project_test import ( "fmt" + "regexp" "strings" "testing" @@ -107,6 +108,71 @@ func TestAccProjectGroup(t *testing.T) { }) } +func TestAccProjectGroup_invalid_roles(t *testing.T) { + projectName := fmt.Sprintf("tftestprojects%s", acctest.RandSeq(10)) + projectKey := strings.ToLower(acctest.RandSeq(10)) + + group := fmt.Sprintf("group%s", strings.ToLower(acctest.RandSeq(5))) + + resourceName := "project_group." + group + + params := map[string]interface{}{ + "project_name": projectName, + "project_key": projectKey, + "group": group, + } + + template := ` + resource "artifactory_group" "{{ .group }}" { + name = "{{ .group }}" + } + + resource "project" "{{ .project_name }}" { + key = "{{ .project_key }}" + display_name = "{{ .project_name }}" + description = "test description" + admin_privileges { + manage_members = true + manage_resources = true + index_resources = true + } + max_storage_in_gibibytes = 1 + block_deployments_on_limit = true + email_notification = false + + use_project_group_resource = true + } + + resource "project_group" "{{ .group }}" { + project_key = project.{{ .project_name }}.key + name = artifactory_group.{{ .group }}.name + roles = [] + } + ` + + config := util.ExecuteTemplate("TestAccProjectGroup", template, params) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + CheckDestroy: acctest.VerifyDeleted(resourceName, func(id string, request *resty.Request) (*resty.Response, error) { + return verifyProjectGroup(group, projectKey, request) + }), + ProtoV6ProviderFactories: acctest.ProtoV6MuxProviderFactories, + ExternalProviders: map[string]resource.ExternalProvider{ + "artifactory": { + Source: "jfrog/artifactory", + VersionConstraint: "10.1.4", + }, + }, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`.*Attribute roles requires 1 item minimum, but config has only 0 declared.*`), + }, + }, + }) +} + func verifyProjectGroup(name string, projectKey string, request *resty.Request) (*resty.Response, error) { return request. SetPathParams(map[string]string{ diff --git a/pkg/project/resource/resource_project_user.go b/pkg/project/resource/resource_project_user.go index 2907a234..76553914 100644 --- a/pkg/project/resource/resource_project_user.go +++ b/pkg/project/resource/resource_project_user.go @@ -47,7 +47,8 @@ func ProjectUserResource() *schema.Resource { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, - Description: "List of pre-defined Project or custom roles", + MinItems: 1, + Description: "List of pre-defined Project or custom roles. Must have at least 1 role, e.g. 'Viewer'", }, "ignore_missing_user": { Type: schema.TypeBool, diff --git a/pkg/project/resource/resource_project_user_test.go b/pkg/project/resource/resource_project_user_test.go index fe17d826..8fe7c004 100644 --- a/pkg/project/resource/resource_project_user_test.go +++ b/pkg/project/resource/resource_project_user_test.go @@ -116,6 +116,70 @@ func TestAccProjectUser(t *testing.T) { }) } +func TestAccProjectUser_invalid_roles(t *testing.T) { + projectName := fmt.Sprintf("tftestprojects%s", acctest.RandSeq(10)) + projectKey := strings.ToLower(acctest.RandSeq(10)) + + username := fmt.Sprintf("not_existing%s", strings.ToLower(acctest.RandSeq(5))) + email := username + "@tempurl.org" + + params := map[string]interface{}{ + "project_name": projectName, + "project_key": projectKey, + "username": username, + "email": email, + } + + template := ` + resource "artifactory_managed_user" "{{ .username }}" { + name = "{{ .username }}" + email = "{{ .email }}" + password = "Password1!" + admin = false + } + + resource "project" "{{ .project_name }}" { + key = "{{ .project_key }}" + display_name = "{{ .project_name }}" + description = "test description" + admin_privileges { + manage_members = true + manage_resources = true + index_resources = true + } + max_storage_in_gibibytes = 1 + block_deployments_on_limit = true + email_notification = false + + use_project_user_resource = true + } + + resource "project_user" "{{ .username }}" { + project_key = project.{{ .project_name }}.key + name = artifactory_managed_user.{{ .username }}.name + roles = [] + ignore_missing_user = false + } + ` + + config := util.ExecuteTemplate("TestAccProjectUser", template, params) + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProtoV6ProviderFactories: acctest.ProtoV6MuxProviderFactories, + ExternalProviders: map[string]resource.ExternalProvider{ + "artifactory": { + Source: "jfrog/artifactory", + }, + }, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`.*Attribute roles requires 1 item minimum, but config has only 0 declared.*`), + }, + }, + }) +} + func TestAccProjectUser_missing_user_fails(t *testing.T) { projectName := fmt.Sprintf("tftestprojects%s", acctest.RandSeq(10)) projectKey := strings.ToLower(acctest.RandSeq(10)) From f630a00f07f68491189277d1105d2915c3523065 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Thu, 30 May 2024 10:31:20 -0700 Subject: [PATCH 2/3] Upgrade go modules Update makefile to call tfplugindocs binary directly --- GNUmakefile | 2 +- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index f06294be..1f174c4a 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -86,6 +86,6 @@ fmt: doc: rm -rfv docs/* - go generate + ./bin/tfplugindocs generate .PHONY: build fmt \ No newline at end of file diff --git a/go.mod b/go.mod index 6df74e86..186e22b9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ module github.com/jfrog/terraform-provider-project require ( github.com/go-resty/resty/v2 v2.13.1 - github.com/hashicorp/terraform-plugin-docs v0.19.2 + github.com/hashicorp/terraform-plugin-docs v0.19.3 github.com/hashicorp/terraform-plugin-framework v1.8.0 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-log v0.9.0 @@ -43,8 +43,8 @@ require ( github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.0 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect - github.com/hashicorp/go-version v1.6.0 // indirect - github.com/hashicorp/hc-install v0.6.4 // indirect + github.com/hashicorp/go-version v1.7.0 // indirect + github.com/hashicorp/hc-install v0.7.0 // indirect github.com/hashicorp/hcl/v2 v2.20.1 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.21.0 // indirect @@ -76,7 +76,7 @@ require ( github.com/zclconf/go-cty v1.14.4 // indirect go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect golang.org/x/crypto v0.23.0 // indirect - golang.org/x/mod v0.16.0 // indirect + golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect diff --git a/go.sum b/go.sum index c1e726f6..fce6e623 100644 --- a/go.sum +++ b/go.sum @@ -89,10 +89,10 @@ github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= -github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/hc-install v0.6.4 h1:QLqlM56/+SIIGvGcfFiwMY3z5WGXT066suo/v9Km8e0= -github.com/hashicorp/hc-install v0.6.4/go.mod h1:05LWLy8TD842OtgcfBbOT0WMoInBMUSHjmDx10zuBIA= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/hc-install v0.7.0 h1:Uu9edVqjKQxxuD28mR5TikkKDd/p55S8vzPC1659aBk= +github.com/hashicorp/hc-install v0.7.0/go.mod h1:ELmmzZlGnEcqoUMKUuykHaPCIR1sYLYX+KSggWSKZuA= github.com/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc= github.com/hashicorp/hcl/v2 v2.20.1/go.mod h1:TZDqQ4kNKCbh1iJp99FdPiUaVDDUPivbqxZulxDYqL4= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= @@ -101,8 +101,8 @@ github.com/hashicorp/terraform-exec v0.21.0 h1:uNkLAe95ey5Uux6KJdua6+cv8asgILFVW github.com/hashicorp/terraform-exec v0.21.0/go.mod h1:1PPeMYou+KDUSSeRE9szMZ/oHf4fYUmB923Wzbq1ICg= github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7orfb5Ltvec= github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A= -github.com/hashicorp/terraform-plugin-docs v0.19.2 h1:YjdKa1vuqt9EnPYkkrv9HnGZz175HhSJ7Vsn8yZeWus= -github.com/hashicorp/terraform-plugin-docs v0.19.2/go.mod h1:gad2aP6uObFKhgNE8DR9nsEuEQnibp7il0jZYYOunWY= +github.com/hashicorp/terraform-plugin-docs v0.19.3 h1:xoxpeIuBfnoGxXY0dTajdj4GjEv6TihZdj0lHNXbKew= +github.com/hashicorp/terraform-plugin-docs v0.19.3/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA= github.com/hashicorp/terraform-plugin-framework v1.8.0 h1:P07qy8RKLcoBkCrY2RHJer5AEvJnDuXomBgou6fD8kI= github.com/hashicorp/terraform-plugin-framework v1.8.0/go.mod h1:/CpTukO88PcL/62noU7cuyaSJ4Rsim+A/pa+3rUVufY= github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc= @@ -226,8 +226,8 @@ golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= -golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= From 2e238f1165ef2fe1b7f9658b605f27bd93cf0e09 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Thu, 30 May 2024 10:33:13 -0700 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3f5f491..5b68489f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.6.1 (May 31, 2024) + +BUG FIXES: + +* Add validation to `roles` attribute for `project_group` and `project_user` resources to ensure at least one role is defined. Issue: [#126](https://github.com/jfrog/terraform-provider-project/issues/126) PR: [#129](https://github.com/jfrog/terraform-provider-project/pull/129) + ## 1.6.0 (May 22, 2024) FEATURES: