Skip to content

Commit

Permalink
Merge pull request #24 from jfrog/GH-23-unable-to-update-project-key
Browse files Browse the repository at this point in the history
GH-23 Add “ForceNew” field to “key” attribute
  • Loading branch information
alexhung authored Feb 23, 2022
2 parents 8c19160 + 897a12c commit 4a4d228
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.0.3 (Feb 24, 2022)

BUG FIXES:

* resource/project: Add `ForceNew` field to `key` attribute to ensure resource is destroyed and recreated when key is changed. [GH-24]
44 changes: 30 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,28 @@ terraform {
required_providers {
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
version = "2.6.24"
version = "2.20.0"
}
project = {
source = "registry.terraform.io/jfrog/project"
version = "0.9.2"
version = "1.0.3"
}
}
}
variable "artifactory_url" {
type = string
default = "http://localhost:8081"
}
provider "artifactory" {
url = "${var.artifactory_url}"
}
provider "project" {
url = "${var.artifactory_url}"
}
variable "qa_roles" {
type = list(string)
default = ["READ_REPOSITORY","READ_RELEASE_BUNDLE", "READ_BUILD", "READ_SOURCES_PIPELINE", "READ_INTEGRATIONS_PIPELINE", "READ_POOLS_PIPELINE", "TRIGGER_PIPELINE"]
Expand All @@ -56,6 +69,8 @@ variable "devop_roles" {
default = ["READ_REPOSITORY", "ANNOTATE_REPOSITORY", "DEPLOY_CACHE_REPOSITORY", "DELETE_OVERWRITE_REPOSITORY", "TRIGGER_PIPELINE", "READ_INTEGRATIONS_PIPELINE", "READ_POOLS_PIPELINE", "MANAGE_INTEGRATIONS_PIPELINE", "MANAGE_SOURCES_PIPELINE", "MANAGE_POOLS_PIPELINE", "READ_BUILD", "ANNOTATE_BUILD", "DEPLOY_BUILD", "DELETE_BUILD",]
}
# Artifactory resources
resource "artifactory_user" "user1" {
name = "user1"
email = "[email protected]"
Expand All @@ -82,20 +97,21 @@ resource "artifactory_group" "release-group" {
admin_privileges = false
}
resource "artifactory_local_repository" "docker-local" {
key = "docker-local"
package_type = "docker"
xray_index = false
description = "hello docker-local"
resource "artifactory_local_docker_v2_repository" "docker-local" {
key = "docker-local"
description = "hello docker-local"
tag_retention = 3
max_unique_tags = 5
}
resource "artifactory_remote_repository" "npm-remote" {
key = "npm-remote"
package_type = "npm"
url = "https://registry.npmjs.org"
xray_index = true
resource "artifactory_remote_npm_repository" "npm-remote" {
key = "npm-remote"
url = "https://registry.npmjs.org"
mismatching_mime_types_override_list = "application/json,application/xml"
}
# Project resources
resource "project" "myproject" {
key = "myproj"
display_name = "My Project"
Expand Down Expand Up @@ -152,8 +168,8 @@ resource "project" "myproject" {
artifactory_user.user2,
artifactory_group.qa-group,
artifactory_group.release-group,
artifactory_local_repository.docker-local,
artifactory_remote_repository.npm-remote,
artifactory_local_docker_v2_repository.docker-local,
artifactory_remote_npm_repository.npm-remote,
]
}
```
Expand Down
4 changes: 1 addition & 3 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ resource "project" "myproject" {

- **admin_privileges** (Block Set, Min: 1) (see [below for nested schema](#nestedblock--admin_privileges))
- **display_name** (String) Also known as project name on the UI
- **key** (String) The Project Key is added as a prefix to resources created within a Project. This field is mandatory and supports only 3 - 6 lowercase alphanumeric characters. Must begin with a letter. For example: us1a.
- **key** (String, ForceNew) The Project Key is added as a prefix to resources created within a Project. This field is mandatory and supports only 3 - 6 lowercase alphanumeric characters. Must begin with a letter. For example: us1a.

### Optional

Expand Down Expand Up @@ -128,5 +128,3 @@ Required:
Optional:

- **description** (String)


140 changes: 140 additions & 0 deletions examples/full.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Required for Terraform 0.13 and up (https://www.terraform.io/upgrade-guides/0-13.html)
terraform {
required_providers {
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
version = "2.20.0"
}
project = {
source = "registry.terraform.io/jfrog/project"
version = "1.0.3"
}
}
}

variable "artifactory_url" {
type = string
default = "http://localhost:8081"
}

provider "artifactory" {
url = "${var.artifactory_url}"
}

provider "project" {
url = "${var.artifactory_url}"
}

variable "qa_roles" {
type = list(string)
default = ["READ_REPOSITORY","READ_RELEASE_BUNDLE", "READ_BUILD", "READ_SOURCES_PIPELINE", "READ_INTEGRATIONS_PIPELINE", "READ_POOLS_PIPELINE", "TRIGGER_PIPELINE"]
}

variable "devop_roles" {
type = list(string)
default = ["READ_REPOSITORY", "ANNOTATE_REPOSITORY", "DEPLOY_CACHE_REPOSITORY", "DELETE_OVERWRITE_REPOSITORY", "TRIGGER_PIPELINE", "READ_INTEGRATIONS_PIPELINE", "READ_POOLS_PIPELINE", "MANAGE_INTEGRATIONS_PIPELINE", "MANAGE_SOURCES_PIPELINE", "MANAGE_POOLS_PIPELINE", "READ_BUILD", "ANNOTATE_BUILD", "DEPLOY_BUILD", "DELETE_BUILD",]
}

# Artifactory resources

resource "artifactory_user" "user1" {
name = "user1"
email = "[email protected]"
groups = ["readers"]
password = "Passw0rd!"
}

resource "artifactory_user" "user2" {
name = "user2"
email = "[email protected]"
groups = ["readers"]
password = "Passw0rd!"
}

resource "artifactory_group" "qa-group" {
name = "qa"
description = "QA group"
admin_privileges = false
}

resource "artifactory_group" "release-group" {
name = "release"
description = "release group"
admin_privileges = false
}

resource "artifactory_local_docker_v2_repository" "docker-local" {
key = "docker-local"
description = "hello docker-local"
tag_retention = 3
max_unique_tags = 5
}

resource "artifactory_remote_npm_repository" "npm-remote" {
key = "npm-remote"
url = "https://registry.npmjs.org"
mismatching_mime_types_override_list = "application/json,application/xml"
}

# Project resources

resource "project" "myproject" {
key = "myproj"
display_name = "My Project"
description = "My Project"
admin_privileges {
manage_members = true
manage_resources = true
index_resources = true
}
max_storage_in_gibibytes = 10
block_deployments_on_limit = false
email_notification = true

member {
name = "user1"
roles = ["Developer", "Project Admin"]
}

member {
name = "user2"
roles = ["Developer"]
}

group {
name = "qa"
roles = ["qa"]
}

group {
name = "release"
roles = ["Release Manager"]
}

role {
name = "qa"
description = "QA role"
type = "CUSTOM"
environments = ["DEV"]
actions = var.qa_roles
}

role {
name = "devop"
description = "DevOp role"
type = "CUSTOM"
environments = ["DEV", "PROD"]
actions = var.devop_roles
}

repos = ["docker-local", "npm-remote"]

depends_on = [
artifactory_user.user1,
artifactory_user.user2,
artifactory_group.qa-group,
artifactory_group.release-group,
artifactory_local_docker_v2_repository.docker-local,
artifactory_remote_npm_repository.npm-remote,
]
}
1 change: 1 addition & 0 deletions pkg/projects/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func projectResource() *schema.Resource {
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringMatch(regexp.MustCompile("^[a-z0-9]{3,6}$"), "key must be 3 - 6 lowercase alphanumeric characters"),
),
Expand Down
47 changes: 41 additions & 6 deletions pkg/projects/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ func TestAccProjectInvalidProjectKey(t *testing.T) {
}
}

func TestAccProjectInvalidDisplayName(t *testing.T) {
name := fmt.Sprintf("tftestprojects%s", randSeq(20))
resourceName := fmt.Sprintf("project.%s", name)

func testProjectConfig(name, key string) string {
params := map[string]interface{}{
"max_storage_in_gibibytes": rand.Intn(100),
"block_deployments_on_limit": randBool(),
Expand All @@ -94,9 +91,9 @@ func TestAccProjectInvalidDisplayName(t *testing.T) {
"manage_resources": randBool(),
"index_resources": randBool(),
"name": name,
"project_key": strings.ToLower(randSeq(6)),
"project_key": key,
}
project := executeTemplate("TestAccProjects", `
return executeTemplate("TestAccProjects", `
resource "project" "{{ .name }}" {
key = "{{ .project_key }}"
display_name = "{{ .name }}"
Expand All @@ -111,6 +108,12 @@ func TestAccProjectInvalidDisplayName(t *testing.T) {
email_notification = {{ .email_notification }}
}
`, params)
}

func TestAccProjectInvalidDisplayName(t *testing.T) {
name := fmt.Sprintf("invalidtestprojects%s", randSeq(20))
resourceName := fmt.Sprintf("project.%s", name)
project := testProjectConfig(name, strings.ToLower(randSeq(6)))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -125,6 +128,38 @@ func TestAccProjectInvalidDisplayName(t *testing.T) {
})
}

func TestAccProjectUpdateKey(t *testing.T) {
name := fmt.Sprintf("testprojects%s", randSeq(20))
resourceName := fmt.Sprintf("project.%s", name)
key1 := strings.ToLower(randSeq(6))
config := testProjectConfig(name, key1)

key2 := strings.ToLower(randSeq(6))
configWithNewKey := testProjectConfig(name, key2)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: verifyDeleted(resourceName, verifyProject),
ProviderFactories: testAccProviders(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "key", key1),
resource.TestCheckResourceAttr(resourceName, "display_name", name),
),
},
{
Config: configWithNewKey,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "key", key2),
resource.TestCheckResourceAttr(resourceName, "display_name", name),
),
},
},
})
}

func TestAccProject(t *testing.T) {
name := fmt.Sprintf("tftestprojects%s", randSeq(10))
resourceName := fmt.Sprintf("project.%s", name)
Expand Down
2 changes: 1 addition & 1 deletion templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The following 3 license types (`jq .type`) do **NOT** support APIs:

## Example Usage

{{tffile "sample.tf"}}
{{tffile "examples/full.tf"}}

## Authentication

Expand Down

0 comments on commit 4a4d228

Please sign in to comment.