Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for project group and project user roles attribute #129

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ fmt:

doc:
rm -rfv docs/*
go generate
./bin/tfplugindocs generate

.PHONY: build fmt
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion pkg/project/resource/resource_project_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
},
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/project/resource/resource_project_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package project_test

import (
"fmt"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion pkg/project/resource/resource_project_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions pkg/project/resource/resource_project_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading