Skip to content

Commit

Permalink
Merge pull request #158 from jfrog/add-read-only-attribute-for-projec…
Browse files Browse the repository at this point in the history
…t-sharing

Add `read_only` attribute for project sharing resources
  • Loading branch information
alexhung authored Sep 16, 2024
2 parents 4764fe5 + 4a7e07d commit 71aa688
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.8.0 (September 17, 2024)

IMPROVEMENTS:

* resource/project_share_repository, resource/project_share_repository_with_all: Add `read_only` attribute to support sharing repository with project(s) in read-only mode. Issue: [#156](https://github.com/jfrog/terraform-provider-project/issues/156) PR: [#158](https://github.com/jfrog/terraform-provider-project/pull/158)

## 1.7.2 (August 8, 2024). Tested on Artifactory 7.90.6 with Terraform 1.9.3 and OpenTofu 1.8.1

IMPROVEMENTS:
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/share_repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ resource "project_share_repository" "myprojectsharerepo" {

- `repo_key` (String) The key of the repository.
- `target_project_key` (String) The project key to which the repository should be shared with.
- `read_only` (Boolean) Share repository with a Project in Read-Only mode to avoid any changes or modifications of the shared content.

->Only available for Artifactory 7.94.0 or later.

## Import

Expand Down
3 changes: 3 additions & 0 deletions docs/resources/share_repository_with_all.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ resource "project_share_repository_with_all" "myprojectsharerepo" {
### Required

- `repo_key` (String) The key of the repository.
- `read_only` (Boolean) Share repository with a Project in Read-Only mode to avoid any changes or modifications of the shared content.

->Only available for Artifactory 7.94.0 or later.

## Import

Expand Down
17 changes: 17 additions & 0 deletions pkg/project/resource/resource_project_share_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand All @@ -35,6 +37,7 @@ type ProjectShareRepositoryResource struct {
type ProjectShareRepositoryResourceModel struct {
RepoKey types.String `tfsdk:"repo_key"`
TargetProjectKey types.String `tfsdk:"target_project_key"`
ReadOnly types.Bool `tfsdk:"read_only"`
}

func (r *ProjectShareRepositoryResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -64,6 +67,16 @@ func (r *ProjectShareRepositoryResource) Schema(ctx context.Context, req resourc
},
Description: "The project key to which the repository should be shared with.",
},
"read_only": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.RequiresReplace(),
},
Description: "Share repository with a Project in Read-Only mode to avoid any changes or modifications of the shared content.\n\n" +
"->Only available for Artifactory 7.94.0 or later.",
},
},
Description: "Share a local or remote repository with a list of projects. Project Members of the target project are granted actions to the shared repository according to their Roles and Role actions assigned in the target Project. Requires a user assigned with the 'Administer the Platform' role.\n\n" +
"->Only available for Artifactory 7.90.1 or later.",
Expand Down Expand Up @@ -114,6 +127,7 @@ func (r *ProjectShareRepositoryResource) Create(ctx context.Context, req resourc
"repo_key": plan.RepoKey.ValueString(),
"target_project_key": plan.TargetProjectKey.ValueString(),
}).
SetQueryParam("readOnly", fmt.Sprintf("%t", plan.ReadOnly.ValueBool())).
SetError(&projectError).
Put(shareWithTargetProject)

Expand Down Expand Up @@ -174,6 +188,8 @@ func (r *ProjectShareRepositoryResource) Read(ctx context.Context, req resource.
state.TargetProjectKey = types.StringNull()
}

state.ReadOnly = types.BoolValue(status.SharedReadOnly)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand Down Expand Up @@ -203,6 +219,7 @@ func (r *ProjectShareRepositoryResource) Delete(ctx context.Context, req resourc
"repo_key": state.RepoKey.ValueString(),
"target_project_key": state.TargetProjectKey.ValueString(),
}).
SetQueryParam("readOnly", fmt.Sprintf("%t", state.ReadOnly.ValueBool())).
SetError(&projectError).
Delete(shareWithTargetProject)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestAccProjectShareRepository_full(t *testing.T) {
resource "project_share_repository" "{{ .resource_name }}" {
repo_key = artifactory_local_generic_repository.{{ .repo_key }}.key
target_project_key = project.{{ .project_name_1 }}.key
read_only = true
}
`

Expand Down Expand Up @@ -123,13 +124,15 @@ func TestAccProjectShareRepository_full(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "repo_key", params["repo_key"]),
resource.TestCheckResourceAttr(fqrn, "target_project_key", params["project_key_1"]),
resource.TestCheckResourceAttr(fqrn, "read_only", "true"),
),
},
{
Config: configUpdated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "repo_key", updateParams["repo_key"]),
resource.TestCheckResourceAttr(fqrn, "target_project_key", updateParams["project_key_2"]),
resource.TestCheckResourceAttr(fqrn, "read_only", "false"),
),
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand All @@ -31,7 +33,8 @@ type ProjectShareRepositoryWithAllResource struct {
}

type ProjectShareRepositoryWithAllResourceModel struct {
RepoKey types.String `tfsdk:"repo_key"`
RepoKey types.String `tfsdk:"repo_key"`
ReadOnly types.Bool `tfsdk:"read_only"`
}

func (r *ProjectShareRepositoryWithAllResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand All @@ -51,6 +54,16 @@ func (r *ProjectShareRepositoryWithAllResource) Schema(ctx context.Context, req
},
Description: "The key of the repository.",
},
"read_only": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.RequiresReplace(),
},
Description: "Share repository with all Projects in Read-Only mode to avoid any changes or modifications of the shared content.\n\n" +
"->Only available for Artifactory 7.94.0 or later.",
},
},
Description: "Share a local or remote repository with all projects. Project Members of the target project are granted actions to the shared repository according to their Roles and Role actions assigned in the target Project. Requires a user assigned with the 'Administer the Platform' role.\n\n" +
"->Only available for Artifactory 7.90.1 or later.",
Expand Down Expand Up @@ -98,6 +111,7 @@ func (r *ProjectShareRepositoryWithAllResource) Create(ctx context.Context, req

response, err := r.ProviderData.Client.R().
SetPathParam("repo_key", plan.RepoKey.ValueString()).
SetQueryParam("readOnly", fmt.Sprintf("%t", plan.ReadOnly.ValueBool())).
SetError(&projectError).
Put(shareWithAllProjectsEndpoint)

Expand Down Expand Up @@ -161,6 +175,8 @@ func (r *ProjectShareRepositoryWithAllResource) Read(ctx context.Context, req re
return
}

state.ReadOnly = types.BoolValue(status.SharedReadOnly)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand All @@ -187,6 +203,7 @@ func (r *ProjectShareRepositoryWithAllResource) Delete(ctx context.Context, req

response, err := r.ProviderData.Client.R().
SetPathParam("repo_key", state.RepoKey.ValueString()).
SetQueryParam("readOnly", fmt.Sprintf("%t", state.ReadOnly.ValueBool())).
SetError(&projectError).
Delete(shareWithAllProjectsEndpoint)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestAccProjectShareWithAllRepository_full(t *testing.T) {
resource "project_share_repository_with_all" "{{ .resource_name }}" {
repo_key = artifactory_local_generic_repository.{{ .repo_key }}.key
read_only = true
depends_on = [
project.{{ .project_name }}
Expand All @@ -74,6 +75,40 @@ func TestAccProjectShareWithAllRepository_full(t *testing.T) {

config := util.ExecuteTemplate("TestAccProjectShareRepository", temp, params)

updatedTemp := `
resource "artifactory_local_generic_repository" "{{ .repo_key }}" {
key = "{{ .repo_key }}"
lifecycle {
ignore_changes = ["project_key"]
}
}
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
}
resource "project_share_repository_with_all" "{{ .resource_name }}" {
repo_key = artifactory_local_generic_repository.{{ .repo_key }}.key
depends_on = [
project.{{ .project_name }}
]
}
`

updatedConfig := util.ExecuteTemplate("TestAccProjectShareRepository", updatedTemp, params)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Expand All @@ -87,6 +122,14 @@ func TestAccProjectShareWithAllRepository_full(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "repo_key", params["repo_key"].(string)),
resource.TestCheckResourceAttr(fqrn, "read_only", "true"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "repo_key", params["repo_key"].(string)),
resource.TestCheckResourceAttr(fqrn, "read_only", "false"),
),
},
{
Expand Down

0 comments on commit 71aa688

Please sign in to comment.