From 1b8db030a7c4556dfe8e2b2bf35eee4e7c399437 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Thu, 31 Oct 2024 10:26:51 +0100 Subject: [PATCH 1/8] Add cleanup policies --- .../local-development/.terraform.lock.hcl | 23 +++ examples/local-development/main.tf | 27 ++- go.mod | 4 + .../data_source_security_cleanup_policies.go | 68 +++++++ ...a_source_security_cleanup_policies_test.go | 55 ++++++ .../resource_security_cleanup_policies.go | 169 ++++++++++++++++++ ...resource_security_cleanup_policies_test.go | 92 ++++++++++ 7 files changed, 433 insertions(+), 5 deletions(-) create mode 100644 examples/local-development/.terraform.lock.hcl create mode 100644 internal/services/security/data_source_security_cleanup_policies.go create mode 100644 internal/services/security/data_source_security_cleanup_policies_test.go create mode 100644 internal/services/security/resource_security_cleanup_policies.go create mode 100644 internal/services/security/resource_security_cleanup_policies_test.go diff --git a/examples/local-development/.terraform.lock.hcl b/examples/local-development/.terraform.lock.hcl new file mode 100644 index 00000000..88427706 --- /dev/null +++ b/examples/local-development/.terraform.lock.hcl @@ -0,0 +1,23 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/datadrivers/nexus" { + version = "2.5.0" + hashes = [ + "h1:/xUNKX1nMoA3E3Sq1hyU64izf8vSefWmlziXufJFbQo=", + "zh:1a8b42a3070c7b2ddb83e4bcd8457f4ba67757bff130b7e75920c3dda115bdff", + "zh:1c38bbf057021cc0f6f7901125055ed062e409efb6ac34921078e263b48aef06", + "zh:45c7031135303a07e68d6924626148b43353e63f0d45ff893083791f7232882f", + "zh:6d63b4cdf390596c5f4d09b0f208d499da51e14d1f0124504fe180324b82409e", + "zh:79aa8ccd12a38f5215ea9533de7a98e42ab721886060dc40a4824a6dcebd9dd7", + "zh:86a01610ade5d667f33f3471f91ef5b65b251270f0e8379d50fba6553a2fa4c5", + "zh:8a1e7c91c4a076ee340ca666af952c45aaa22224ae94c6167b5930cfa273383b", + "zh:aea473919d87547fde75ff04b877b21fd9c30d0b23c87d2ae20dcf4f644ed860", + "zh:bb3312a81e428980cbe13d8fba4be0b8160344a218c39d489c9f23239c8bfc10", + "zh:c628cb8ba9c054991b6f23e4a85350116433070c14348605932568aede14f6cd", + "zh:d02a7bc711684d4a9e4df01192ce242396d237c75bfd58e892f49b7fb1d3db5e", + "zh:da50892a5b2a9f14951af2de7d512278e2493afc764eacadd4927758ec08f18d", + "zh:e01831062dfe87be0d382c5caca9c6ea3f3bb9ec37ed9ff9b64c3549f59fd6a7", + "zh:ec793834616941701e04b28cc3d09b4d9fb50b86ca12c140eb71634ea1ec676e", + ] +} diff --git a/examples/local-development/main.tf b/examples/local-development/main.tf index db05a04b..7fc49161 100644 --- a/examples/local-development/main.tf +++ b/examples/local-development/main.tf @@ -1,11 +1,28 @@ -# arbitrary example -resource "nexus_blobstore" "default" { - name = "blobstore-file" - type = "File" - path = "/nexus-data/blobstore-file" +resource "nexus_blobstore_file" "blobby" { + + name = "blobby" + path = "/nexus-data/blobby" soft_quota { limit = 1024000000 type = "spaceRemainingQuota" } } + +resource "nexus_repository_docker_hosted" "example" { + name = "example-container" + + storage { + blob_store_name = "default" + strict_content_type_validation = false + } + + docker { + v1_enabled = false + force_basic_auth = false + } +} + +# resource "nexus_security_cleanup_policy" "cleanup" { +# name = "CleanerUpper" +# } \ No newline at end of file diff --git a/go.mod b/go.mod index ac48d2cc..2d75c330 100644 --- a/go.mod +++ b/go.mod @@ -243,3 +243,7 @@ require ( mvdan.cc/gofumpt v0.7.0 // indirect mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect ) + +replace github.com/datadrivers/go-nexus-client => ../go-nexus-client +//replace github.com/datadrivers/go-nexus-client => C:\Develop\IdeaProjects\go-nexus-client +//replace github.com/datadrivers/go-nexus-client => /home/uitdeveloper/Develop/IdeaProjects/go-nexus-client diff --git a/internal/services/security/data_source_security_cleanup_policies.go b/internal/services/security/data_source_security_cleanup_policies.go new file mode 100644 index 00000000..c577265e --- /dev/null +++ b/internal/services/security/data_source_security_cleanup_policies.go @@ -0,0 +1,68 @@ +package security + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func DataSourceRepositoryCleanupPolicies() *schema.Resource { + return &schema.Resource{ + Description: "Manages a cleanup policy in Nexus Repository.", + + Read: dataSourceRepositoryCleanupPoliciesRead, + + Schema: map[string]*schema.Schema{ + "notes": { + Description: "any details on the specific cleanup policy", + Type: schema.TypeString, + Default: "string", + Optional: true, + }, + "criterialLastBlobUpdated": { + Description: "the age of the component in days", + Type: schema.TypeInt, + Default: 0, + Optional: false, + }, + "criteriaLastDownloaded": { + Description: "the last time the component had been downloaded in days", + Type: schema.TypeInt, + Default: 0, + Optional: false, + }, + "criteriaReleaseType": { + Description: "When needed, this is either PRELEASE or RELEASE", + Type: schema.TypeString, + Default: "RELEASES", + Optional: false, + }, + "criteriaAssetRegex": { + Description: "a regex string to filter for specific asset paths", + Type: schema.TypeString, + Default: "string", + Optional: false, + }, + "retain": { + Description: "number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", + Type: schema.TypeInt, + Default: nil, + Optional: false, + }, + "name": { + Description: "the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", + Type: schema.TypeString, + Default: "policy-name", + Optional: true, + }, + "format": { + Description: "the target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", + Type: schema.TypeString, + Default: "string", + Optional: true, + }, + }, + } +} + +func dataSourceRepositoryCleanupPoliciesRead(d *schema.ResourceData, m interface{}) error { + return resourceSecurityCleanupPoliciesRead(d, m) +} diff --git a/internal/services/security/data_source_security_cleanup_policies_test.go b/internal/services/security/data_source_security_cleanup_policies_test.go new file mode 100644 index 00000000..178c515e --- /dev/null +++ b/internal/services/security/data_source_security_cleanup_policies_test.go @@ -0,0 +1,55 @@ +package security_test + +import ( + "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "strconv" + "testing" + + "github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" + "github.com/datadrivers/terraform-provider-nexus/internal/acceptance" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceCleanupCleanupPolicy(t *testing.T) { + dataSourceName := "data.nexus_security_cleanup_policy.acceptance" + + cp := security.CleanupPolicy{ + Notes: tools.GetStringPointer(acctest.RandString(25)), + CriteriaLastBlobUpdated: tools.GetIntPointer(acctest.RandInt()), + CriteriaLastDownloaded: tools.GetIntPointer(acctest.RandInt()), + CriteriaReleaseType: tools.GetStringPointer(acctest.RandString(8)), + CriteriaAssetRegex: tools.GetStringPointer(acctest.RandString(15)), + Retain: acctest.RandInt(), + Name: acctest.RandString(10), + Format: acctest.RandString(5), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.AccPreCheck(t) }, + Providers: acceptance.TestAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccResourceSecurityCleanupPolicyConfig(cp) + testAccDataSourceCleanupCleanupPolicyConfig(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPtr(dataSourceName, "notes", cp.Notes), + resource.TestCheckResourceAttr(dataSourceName, "criteria_last_blob_updated", strconv.Itoa(*cp.CriteriaLastBlobUpdated)), + resource.TestCheckResourceAttr(dataSourceName, "criteria_last_downloaded", strconv.Itoa(*cp.CriteriaLastDownloaded)), + resource.TestCheckResourceAttrPtr(dataSourceName, "criteria_release_type", cp.CriteriaReleaseType), + resource.TestCheckResourceAttrPtr(dataSourceName, "criteria_asset_regex", cp.CriteriaAssetRegex), + resource.TestCheckResourceAttr(dataSourceName, "retain", strconv.Itoa(cp.Retain)), + resource.TestCheckResourceAttr(dataSourceName, "name", cp.Name), + resource.TestCheckResourceAttr(dataSourceName, "format", cp.Format), + ), + }, + }, + }) +} + +func testAccDataSourceCleanupCleanupPolicyConfig() string { + return ` +data "nexus_cleanup_cleanup_policy" "acceptance" { + name = nexus_cleanup_cleanup_policy.acceptance.name +} +` +} diff --git a/internal/services/security/resource_security_cleanup_policies.go b/internal/services/security/resource_security_cleanup_policies.go new file mode 100644 index 00000000..c7091847 --- /dev/null +++ b/internal/services/security/resource_security_cleanup_policies.go @@ -0,0 +1,169 @@ +package security + +import ( + nexus "github.com/datadrivers/go-nexus-client/nexus3" + "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func ResourceSecurityCleanupPolicies() *schema.Resource { + return &schema.Resource{ + Description: "Manages a cleanup policy in Nexus Repository.", + + Create: resourceSecurityCleanupPoliciesCreate, + Read: resourceSecurityCleanupPoliciesRead, + Update: resourceSecurityCleanupPoliciesUpdate, + Delete: resourceSecurityCleanupPoliciesDelete, + Exists: resourceSecurityCleanupPoliciesExists, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "notes": { + Description: "any details on the specific cleanup policy", + Type: schema.TypeString, + Default: "string", + Optional: true, + }, + "criterialLastBlobUpdated": { + Description: "the age of the component in days", + Type: schema.TypeInt, + Default: 0, + Optional: false, + }, + "criteriaLastDownloaded": { + Description: "the last time the component had been downloaded in days", + Type: schema.TypeInt, + Default: 0, + Optional: false, + }, + "criteriaReleaseType": { + Description: "When needed, this is either PRELEASE or RELEASE", + Type: schema.TypeString, + Default: "RELEASE", + Optional: false, + }, + "criteriaAssetRegex": { + Description: "a regex string to filter for specific asset paths", + Type: schema.TypeString, + Default: "string", + Optional: false, + }, + "retain": { + Description: "number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", + Type: schema.TypeInt, + Default: nil, + Optional: false, + }, + "name": { + Description: "the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", + Type: schema.TypeString, + Default: "policy-name", + Optional: true, + }, + "format": { + Description: "the target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", + Type: schema.TypeString, + Default: "string", + Optional: true, + }, + }, + } +} + +func getCleanupPolicyFromResourceData(d *schema.ResourceData) security.CleanupPolicy { + + notes, _ := d.Get("notes").(string) + criteriaLastBlobUpdated, _ := d.Get("criteria_last_blob_updated").(int) + criteriaLastDownloaded, _ := d.Get("criteria_last_downloaded").(int) + criteriaReleaseType, _ := d.Get("criteria_release_type").(string) + criteriaAssetRegex, _ := d.Get("criteria_asset_regex").(string) + retain, _ := d.Get("retain").(int) + name, _ := d.Get("name").(string) + format, _ := d.Get("format").(string) + + policy := security.CleanupPolicy{ + Notes: ¬es, + CriteriaLastBlobUpdated: &criteriaLastBlobUpdated, + CriteriaLastDownloaded: &criteriaLastDownloaded, + CriteriaReleaseType: &criteriaReleaseType, + CriteriaAssetRegex: &criteriaAssetRegex, + Retain: retain, + Name: name, + Format: format, + } + + return policy +} + +func setCleanupPolicyToResourceData(cleanupPolicy *security.CleanupPolicy, d *schema.ResourceData) error { + d.SetId(cleanupPolicy.Name) + d.Set("notes", cleanupPolicy.Notes) + d.Set("criteria_last_blob_updated", cleanupPolicy.CriteriaLastBlobUpdated) + d.Set("criteria_last_downloaded", cleanupPolicy.CriteriaLastDownloaded) + d.Set("criteria_release_type", cleanupPolicy.CriteriaReleaseType) + d.Set("criteria_asset_regex", cleanupPolicy.CriteriaAssetRegex) + d.Set("retain", cleanupPolicy.Retain) + d.Set("name", cleanupPolicy.Name) + d.Set("format", cleanupPolicy.Format) + return nil +} + +func resourceSecurityCleanupPoliciesCreate(d *schema.ResourceData, m interface{}) error { + client := m.(*nexus.NexusClient) + + cleanupPolicy := getCleanupPolicyFromResourceData(d) + + if err := client.Security.CleanupPolicy.Create(&cleanupPolicy); err != nil { + return err + } + + d.SetId(cleanupPolicy.Name) + + return resourceSecurityContentSelectorRead(d, m) +} + +func resourceSecurityCleanupPoliciesRead(d *schema.ResourceData, m interface{}) error { + client := m.(*nexus.NexusClient) + + cleanupPolicy, err := client.Security.CleanupPolicy.Get(d.Id()) + if err != nil { + return err + } + + if cleanupPolicy == nil { + d.SetId("") + return nil + } + + return setCleanupPolicyToResourceData(cleanupPolicy, d) +} + +func resourceSecurityCleanupPoliciesUpdate(d *schema.ResourceData, m interface{}) error { + client := m.(*nexus.NexusClient) + + cleanupPolicy := getCleanupPolicyFromResourceData(d) + if err := client.Security.CleanupPolicy.Update(&cleanupPolicy); err != nil { + return err + } + + return resourceSecurityCleanupPoliciesRead(d, m) +} + +func resourceSecurityCleanupPoliciesDelete(d *schema.ResourceData, m interface{}) error { + client := m.(*nexus.NexusClient) + + if err := client.Security.CleanupPolicy.Delete(d.Id()); err != nil { + return err + } + + d.SetId("") + return nil +} + +func resourceSecurityCleanupPoliciesExists(d *schema.ResourceData, m interface{}) (bool, error) { + client := m.(*nexus.NexusClient) + + cleanupPolicy, err := client.Security.CleanupPolicy.Get(d.Id()) + return cleanupPolicy != nil, err +} diff --git a/internal/services/security/resource_security_cleanup_policies_test.go b/internal/services/security/resource_security_cleanup_policies_test.go new file mode 100644 index 00000000..54501e39 --- /dev/null +++ b/internal/services/security/resource_security_cleanup_policies_test.go @@ -0,0 +1,92 @@ +package security_test + +import ( + "fmt" + "strconv" + "testing" + + nexus "github.com/datadrivers/go-nexus-client/nexus3" + "github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" + "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "github.com/datadrivers/terraform-provider-nexus/internal/acceptance" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccResourceSecurityCleanupPolicy(t *testing.T) { + var cleanupPolicy security.CleanupPolicy + + resName := "nexus_security_cleanup_policy.acceptance" + cp := security.CleanupPolicy{ + Notes: tools.GetStringPointer(acctest.RandString(25)), + CriteriaLastBlobUpdated: tools.GetIntPointer(acctest.RandInt()), + CriteriaLastDownloaded: tools.GetIntPointer(acctest.RandInt()), + CriteriaReleaseType: tools.GetStringPointer(acctest.RandString(8)), + CriteriaAssetRegex: tools.GetStringPointer(acctest.RandString(15)), + Retain: acctest.RandInt(), + Name: acctest.RandString(10), + Format: acctest.RandString(5), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.AccPreCheck(t) }, + Providers: acceptance.TestAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccResourceSecurityCleanupPolicyConfig(cp) + testAccDataSourceCleanupCleanupPolicyConfig(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resName, "notes", *cp.Notes), + resource.TestCheckResourceAttr(resName, "criteria_last_blob_updated", strconv.Itoa(*cp.CriteriaLastBlobUpdated)), + resource.TestCheckResourceAttr(resName, "criteria_last_downloaded", strconv.Itoa(*cp.CriteriaLastDownloaded)), + resource.TestCheckResourceAttr(resName, "criteria_release_type", *cp.CriteriaReleaseType), + resource.TestCheckResourceAttr(resName, "criteria_asset_regex", *cp.CriteriaAssetRegex), + resource.TestCheckResourceAttr(resName, "retain", strconv.Itoa(cp.Retain)), + resource.TestCheckResourceAttr(resName, "name", cp.Name), + resource.TestCheckResourceAttr(resName, "format", cp.Format), + ), + }, + { + ResourceName: resName, + ImportStateId: cleanupPolicy.Name, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccResourceSecurityCleanupPolicyConfig(cp security.CleanupPolicy) string { + return fmt.Sprintf(` +resource "nexus_security_cleanup_policy" "acceptance" { + notes = "%s" + criteria_last_blob_updated = "%s" + criteria_last_downloaded = "%s" + criteria_release_type = "%s" + criteria_asset_regex = "%s" + retain = "%s" + name = "%s" + format = "%s" +} +`, *cp.Notes, strconv.Itoa(*cp.CriteriaLastBlobUpdated), strconv.Itoa(*cp.CriteriaLastDownloaded), + *cp.CriteriaReleaseType, *cp.CriteriaAssetRegex, strconv.Itoa(cp.Retain), cp.Name, cp.Format) +} + +func testAccCheckCleanupPolicyResourceExists(name string, cleanupPolicy *security.CleanupPolicy) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + client := acceptance.TestAccProvider.Meta().(*nexus.NexusClient) + result, err := client.Security.CleanupPolicy.Get(rs.Primary.ID) + if err != nil { + return err + } + + *cleanupPolicy = *result + + return nil + } +} From efe51ee3a959e0d9373d1a1c4027e5429c3306d6 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Fri, 1 Nov 2024 10:30:37 +0100 Subject: [PATCH 2/8] 'Refactor cleanup policies to adhere naming & structures in go-nexus-client --- examples/local-development/main.tf | 2 +- .../data_source_security_cleanup_policies.go | 4 +- ...a_source_security_cleanup_policies_test.go | 18 ++++---- .../resource_security_cleanup_policies.go | 44 +++++++++---------- ...resource_security_cleanup_policies_test.go | 18 ++++---- 5 files changed, 43 insertions(+), 43 deletions(-) rename internal/services/{security => cleanuppolicies}/data_source_security_cleanup_policies.go (96%) rename internal/services/{security => cleanuppolicies}/data_source_security_cleanup_policies_test.go (77%) rename internal/services/{security => cleanuppolicies}/resource_security_cleanup_policies.go (73%) rename internal/services/{security => cleanuppolicies}/resource_security_cleanup_policies_test.go (84%) diff --git a/examples/local-development/main.tf b/examples/local-development/main.tf index 7fc49161..f6c28de1 100644 --- a/examples/local-development/main.tf +++ b/examples/local-development/main.tf @@ -25,4 +25,4 @@ resource "nexus_repository_docker_hosted" "example" { # resource "nexus_security_cleanup_policy" "cleanup" { # name = "CleanerUpper" -# } \ No newline at end of file +# } diff --git a/internal/services/security/data_source_security_cleanup_policies.go b/internal/services/cleanuppolicies/data_source_security_cleanup_policies.go similarity index 96% rename from internal/services/security/data_source_security_cleanup_policies.go rename to internal/services/cleanuppolicies/data_source_security_cleanup_policies.go index c577265e..c70edab6 100644 --- a/internal/services/security/data_source_security_cleanup_policies.go +++ b/internal/services/cleanuppolicies/data_source_security_cleanup_policies.go @@ -1,4 +1,4 @@ -package security +package cleanuppolicies import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -64,5 +64,5 @@ func DataSourceRepositoryCleanupPolicies() *schema.Resource { } func dataSourceRepositoryCleanupPoliciesRead(d *schema.ResourceData, m interface{}) error { - return resourceSecurityCleanupPoliciesRead(d, m) + return resourceCleanupPoliciesRead(d, m) } diff --git a/internal/services/security/data_source_security_cleanup_policies_test.go b/internal/services/cleanuppolicies/data_source_security_cleanup_policies_test.go similarity index 77% rename from internal/services/security/data_source_security_cleanup_policies_test.go rename to internal/services/cleanuppolicies/data_source_security_cleanup_policies_test.go index 178c515e..368d734b 100644 --- a/internal/services/security/data_source_security_cleanup_policies_test.go +++ b/internal/services/cleanuppolicies/data_source_security_cleanup_policies_test.go @@ -1,7 +1,7 @@ -package security_test +package cleanuppolicies_test import ( - "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" "strconv" "testing" @@ -11,10 +11,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestAccDataSourceCleanupCleanupPolicy(t *testing.T) { - dataSourceName := "data.nexus_security_cleanup_policy.acceptance" +func TestAccDataSourceCleanupPolicy(t *testing.T) { + dataSourceName := "data.nexus_cleanup_policy.acceptance" - cp := security.CleanupPolicy{ + cp := cleanuppolicies.CleanupPolicy{ Notes: tools.GetStringPointer(acctest.RandString(25)), CriteriaLastBlobUpdated: tools.GetIntPointer(acctest.RandInt()), CriteriaLastDownloaded: tools.GetIntPointer(acctest.RandInt()), @@ -30,7 +30,7 @@ func TestAccDataSourceCleanupCleanupPolicy(t *testing.T) { Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccResourceSecurityCleanupPolicyConfig(cp) + testAccDataSourceCleanupCleanupPolicyConfig(), + Config: testAccResourceCleanupPolicyConfig(cp) + testAccDataSourceCleanupPolicyConfig(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPtr(dataSourceName, "notes", cp.Notes), resource.TestCheckResourceAttr(dataSourceName, "criteria_last_blob_updated", strconv.Itoa(*cp.CriteriaLastBlobUpdated)), @@ -46,10 +46,10 @@ func TestAccDataSourceCleanupCleanupPolicy(t *testing.T) { }) } -func testAccDataSourceCleanupCleanupPolicyConfig() string { +func testAccDataSourceCleanupPolicyConfig() string { return ` -data "nexus_cleanup_cleanup_policy" "acceptance" { - name = nexus_cleanup_cleanup_policy.acceptance.name +data "nexus_cleanup_policy" "acceptance" { + name = nexus_cleanup_policy.acceptance.name } ` } diff --git a/internal/services/security/resource_security_cleanup_policies.go b/internal/services/cleanuppolicies/resource_security_cleanup_policies.go similarity index 73% rename from internal/services/security/resource_security_cleanup_policies.go rename to internal/services/cleanuppolicies/resource_security_cleanup_policies.go index c7091847..5c19213f 100644 --- a/internal/services/security/resource_security_cleanup_policies.go +++ b/internal/services/cleanuppolicies/resource_security_cleanup_policies.go @@ -1,8 +1,8 @@ -package security +package cleanuppolicies import ( nexus "github.com/datadrivers/go-nexus-client/nexus3" - "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -10,11 +10,11 @@ func ResourceSecurityCleanupPolicies() *schema.Resource { return &schema.Resource{ Description: "Manages a cleanup policy in Nexus Repository.", - Create: resourceSecurityCleanupPoliciesCreate, - Read: resourceSecurityCleanupPoliciesRead, - Update: resourceSecurityCleanupPoliciesUpdate, - Delete: resourceSecurityCleanupPoliciesDelete, - Exists: resourceSecurityCleanupPoliciesExists, + Create: resourceCleanupPoliciesCreate, + Read: resourceCleanupPoliciesRead, + Update: resourceCleanupPoliciesUpdate, + Delete: resourceCleanupPoliciesDelete, + Exists: resourceCleanupPoliciesExists, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -71,7 +71,7 @@ func ResourceSecurityCleanupPolicies() *schema.Resource { } } -func getCleanupPolicyFromResourceData(d *schema.ResourceData) security.CleanupPolicy { +func getCleanupPolicyFromResourceData(d *schema.ResourceData) cleanuppolicies.CleanupPolicy { notes, _ := d.Get("notes").(string) criteriaLastBlobUpdated, _ := d.Get("criteria_last_blob_updated").(int) @@ -82,7 +82,7 @@ func getCleanupPolicyFromResourceData(d *schema.ResourceData) security.CleanupPo name, _ := d.Get("name").(string) format, _ := d.Get("format").(string) - policy := security.CleanupPolicy{ + policy := cleanuppolicies.CleanupPolicy{ Notes: ¬es, CriteriaLastBlobUpdated: &criteriaLastBlobUpdated, CriteriaLastDownloaded: &criteriaLastDownloaded, @@ -96,7 +96,7 @@ func getCleanupPolicyFromResourceData(d *schema.ResourceData) security.CleanupPo return policy } -func setCleanupPolicyToResourceData(cleanupPolicy *security.CleanupPolicy, d *schema.ResourceData) error { +func setCleanupPolicyToResourceData(cleanupPolicy *cleanuppolicies.CleanupPolicy, d *schema.ResourceData) error { d.SetId(cleanupPolicy.Name) d.Set("notes", cleanupPolicy.Notes) d.Set("criteria_last_blob_updated", cleanupPolicy.CriteriaLastBlobUpdated) @@ -109,24 +109,24 @@ func setCleanupPolicyToResourceData(cleanupPolicy *security.CleanupPolicy, d *sc return nil } -func resourceSecurityCleanupPoliciesCreate(d *schema.ResourceData, m interface{}) error { +func resourceCleanupPoliciesCreate(d *schema.ResourceData, m interface{}) error { client := m.(*nexus.NexusClient) cleanupPolicy := getCleanupPolicyFromResourceData(d) - if err := client.Security.CleanupPolicy.Create(&cleanupPolicy); err != nil { + if err := client.CleanupPolicy.Create(&cleanupPolicy); err != nil { return err } d.SetId(cleanupPolicy.Name) - return resourceSecurityContentSelectorRead(d, m) + return resourceCleanupPoliciesRead(d, m) } -func resourceSecurityCleanupPoliciesRead(d *schema.ResourceData, m interface{}) error { +func resourceCleanupPoliciesRead(d *schema.ResourceData, m interface{}) error { client := m.(*nexus.NexusClient) - cleanupPolicy, err := client.Security.CleanupPolicy.Get(d.Id()) + cleanupPolicy, err := client.CleanupPolicy.Get(d.Id()) if err != nil { return err } @@ -139,21 +139,21 @@ func resourceSecurityCleanupPoliciesRead(d *schema.ResourceData, m interface{}) return setCleanupPolicyToResourceData(cleanupPolicy, d) } -func resourceSecurityCleanupPoliciesUpdate(d *schema.ResourceData, m interface{}) error { +func resourceCleanupPoliciesUpdate(d *schema.ResourceData, m interface{}) error { client := m.(*nexus.NexusClient) cleanupPolicy := getCleanupPolicyFromResourceData(d) - if err := client.Security.CleanupPolicy.Update(&cleanupPolicy); err != nil { + if err := client.CleanupPolicy.Update(&cleanupPolicy); err != nil { return err } - return resourceSecurityCleanupPoliciesRead(d, m) + return resourceCleanupPoliciesRead(d, m) } -func resourceSecurityCleanupPoliciesDelete(d *schema.ResourceData, m interface{}) error { +func resourceCleanupPoliciesDelete(d *schema.ResourceData, m interface{}) error { client := m.(*nexus.NexusClient) - if err := client.Security.CleanupPolicy.Delete(d.Id()); err != nil { + if err := client.CleanupPolicy.Delete(d.Id()); err != nil { return err } @@ -161,9 +161,9 @@ func resourceSecurityCleanupPoliciesDelete(d *schema.ResourceData, m interface{} return nil } -func resourceSecurityCleanupPoliciesExists(d *schema.ResourceData, m interface{}) (bool, error) { +func resourceCleanupPoliciesExists(d *schema.ResourceData, m interface{}) (bool, error) { client := m.(*nexus.NexusClient) - cleanupPolicy, err := client.Security.CleanupPolicy.Get(d.Id()) + cleanupPolicy, err := client.CleanupPolicy.Get(d.Id()) return cleanupPolicy != nil, err } diff --git a/internal/services/security/resource_security_cleanup_policies_test.go b/internal/services/cleanuppolicies/resource_security_cleanup_policies_test.go similarity index 84% rename from internal/services/security/resource_security_cleanup_policies_test.go rename to internal/services/cleanuppolicies/resource_security_cleanup_policies_test.go index 54501e39..b61e7115 100644 --- a/internal/services/security/resource_security_cleanup_policies_test.go +++ b/internal/services/cleanuppolicies/resource_security_cleanup_policies_test.go @@ -1,4 +1,4 @@ -package security_test +package cleanuppolicies_test import ( "fmt" @@ -7,18 +7,18 @@ import ( nexus "github.com/datadrivers/go-nexus-client/nexus3" "github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" - "github.com/datadrivers/go-nexus-client/nexus3/schema/security" + "github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" "github.com/datadrivers/terraform-provider-nexus/internal/acceptance" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccResourceSecurityCleanupPolicy(t *testing.T) { - var cleanupPolicy security.CleanupPolicy +func TestAccResourceCleanupPolicy(t *testing.T) { + var cleanupPolicy cleanuppolicies.CleanupPolicy resName := "nexus_security_cleanup_policy.acceptance" - cp := security.CleanupPolicy{ + cp := cleanuppolicies.CleanupPolicy{ Notes: tools.GetStringPointer(acctest.RandString(25)), CriteriaLastBlobUpdated: tools.GetIntPointer(acctest.RandInt()), CriteriaLastDownloaded: tools.GetIntPointer(acctest.RandInt()), @@ -34,7 +34,7 @@ func TestAccResourceSecurityCleanupPolicy(t *testing.T) { Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { - Config: testAccResourceSecurityCleanupPolicyConfig(cp) + testAccDataSourceCleanupCleanupPolicyConfig(), + Config: testAccResourceCleanupPolicyConfig(cp) + testAccDataSourceCleanupPolicyConfig(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resName, "notes", *cp.Notes), resource.TestCheckResourceAttr(resName, "criteria_last_blob_updated", strconv.Itoa(*cp.CriteriaLastBlobUpdated)), @@ -56,7 +56,7 @@ func TestAccResourceSecurityCleanupPolicy(t *testing.T) { }) } -func testAccResourceSecurityCleanupPolicyConfig(cp security.CleanupPolicy) string { +func testAccResourceCleanupPolicyConfig(cp cleanuppolicies.CleanupPolicy) string { return fmt.Sprintf(` resource "nexus_security_cleanup_policy" "acceptance" { notes = "%s" @@ -72,7 +72,7 @@ resource "nexus_security_cleanup_policy" "acceptance" { *cp.CriteriaReleaseType, *cp.CriteriaAssetRegex, strconv.Itoa(cp.Retain), cp.Name, cp.Format) } -func testAccCheckCleanupPolicyResourceExists(name string, cleanupPolicy *security.CleanupPolicy) resource.TestCheckFunc { +func testAccCheckCleanupPolicyResourceExists(name string, cleanupPolicy *cleanuppolicies.CleanupPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -80,7 +80,7 @@ func testAccCheckCleanupPolicyResourceExists(name string, cleanupPolicy *securit } client := acceptance.TestAccProvider.Meta().(*nexus.NexusClient) - result, err := client.Security.CleanupPolicy.Get(rs.Primary.ID) + result, err := client.CleanupPolicy.Get(rs.Primary.ID) if err != nil { return err } From 01a203b89f5cce9c9c5ef9a37e6da2047128b434 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Fri, 1 Nov 2024 10:30:37 +0100 Subject: [PATCH 3/8] 'Refactor cleanup policies to adhere naming & structures in go-nexus-client --- .../cleanuppolicies/resource_security_cleanup_policies.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/cleanuppolicies/resource_security_cleanup_policies.go b/internal/services/cleanuppolicies/resource_security_cleanup_policies.go index 5c19213f..e7f02288 100644 --- a/internal/services/cleanuppolicies/resource_security_cleanup_policies.go +++ b/internal/services/cleanuppolicies/resource_security_cleanup_policies.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func ResourceSecurityCleanupPolicies() *schema.Resource { +func ResourceCleanupPolicies() *schema.Resource { return &schema.Resource{ Description: "Manages a cleanup policy in Nexus Repository.", From f39ae73d074659de7d348ed74af671600e318822 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Fri, 1 Nov 2024 13:45:23 +0100 Subject: [PATCH 4/8] Add cleanup policies in main.go provider --- internal/provider/main.go | 3 +++ ...ity_cleanup_policies.go => data_source_cleanup_policies.go} | 0 ...p_policies_test.go => data_source_cleanup_policies_test.go} | 0 ...curity_cleanup_policies.go => resource_cleanup_policies.go} | 0 ...anup_policies_test.go => resource_cleanup_policies_test.go} | 0 5 files changed, 3 insertions(+) rename internal/services/cleanuppolicies/{data_source_security_cleanup_policies.go => data_source_cleanup_policies.go} (100%) rename internal/services/cleanuppolicies/{data_source_security_cleanup_policies_test.go => data_source_cleanup_policies_test.go} (100%) rename internal/services/cleanuppolicies/{resource_security_cleanup_policies.go => resource_cleanup_policies.go} (100%) rename internal/services/cleanuppolicies/{resource_security_cleanup_policies_test.go => resource_cleanup_policies_test.go} (100%) diff --git a/internal/provider/main.go b/internal/provider/main.go index 0b0b70e3..ef956b19 100755 --- a/internal/provider/main.go +++ b/internal/provider/main.go @@ -4,6 +4,7 @@ import ( nexus "github.com/datadrivers/go-nexus-client/nexus3" "github.com/datadrivers/go-nexus-client/nexus3/pkg/client" "github.com/datadrivers/terraform-provider-nexus/internal/services/blobstore" + "github.com/datadrivers/terraform-provider-nexus/internal/services/cleanuppolicies" "github.com/datadrivers/terraform-provider-nexus/internal/services/other" "github.com/datadrivers/terraform-provider-nexus/internal/services/repository" "github.com/datadrivers/terraform-provider-nexus/internal/services/security" @@ -79,6 +80,7 @@ func Provider() *schema.Provider { "nexus_privilege_repository_view": security.DataSourceSecurityPrivilegeRepositoryView(), "nexus_privilege_repository_admin": security.DataSourceSecurityPrivilegeRepositoryAdmin(), "nexus_privilege_repository_content_selector": security.DataSourceSecurityPrivilegeRepositoryContentSelector(), + "nexus_cleanup_policy": cleanuppolicies.DataSourceRepositoryCleanupPolicies(), }, ResourcesMap: map[string]*schema.Resource{ "nexus_blobstore_azure": blobstore.ResourceBlobstoreAzure(), @@ -145,6 +147,7 @@ func Provider() *schema.Provider { "nexus_privilege_repository_admin": security.ResourceSecurityPrivilegeRepositoryAdmin(), "nexus_privilege_repository_content_selector": security.ResourceSecurityPrivilegeRepositoryContentSelector(), "nexus_privilege_wildcard": security.ResourceSecurityPrivilegeWildcard(), + "nexus_cleanup_policy": cleanuppolicies.ResourceCleanupPolicies(), }, Schema: map[string]*schema.Schema{ "insecure": { diff --git a/internal/services/cleanuppolicies/data_source_security_cleanup_policies.go b/internal/services/cleanuppolicies/data_source_cleanup_policies.go similarity index 100% rename from internal/services/cleanuppolicies/data_source_security_cleanup_policies.go rename to internal/services/cleanuppolicies/data_source_cleanup_policies.go diff --git a/internal/services/cleanuppolicies/data_source_security_cleanup_policies_test.go b/internal/services/cleanuppolicies/data_source_cleanup_policies_test.go similarity index 100% rename from internal/services/cleanuppolicies/data_source_security_cleanup_policies_test.go rename to internal/services/cleanuppolicies/data_source_cleanup_policies_test.go diff --git a/internal/services/cleanuppolicies/resource_security_cleanup_policies.go b/internal/services/cleanuppolicies/resource_cleanup_policies.go similarity index 100% rename from internal/services/cleanuppolicies/resource_security_cleanup_policies.go rename to internal/services/cleanuppolicies/resource_cleanup_policies.go diff --git a/internal/services/cleanuppolicies/resource_security_cleanup_policies_test.go b/internal/services/cleanuppolicies/resource_cleanup_policies_test.go similarity index 100% rename from internal/services/cleanuppolicies/resource_security_cleanup_policies_test.go rename to internal/services/cleanuppolicies/resource_cleanup_policies_test.go From 0fbbf2314d9263486b1cfe01181bb09482a7d360 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Wed, 20 Nov 2024 13:24:00 +0100 Subject: [PATCH 5/8] Update Schema & update tests --- examples/local-development/main.tf | 27 +++--------- go.mod | 2 - .../data_source_cleanup_policies.go | 44 ++++++++----------- .../resource_cleanup_policies.go | 43 ++++++++---------- .../resource_cleanup_policies_test.go | 21 --------- 5 files changed, 41 insertions(+), 96 deletions(-) diff --git a/examples/local-development/main.tf b/examples/local-development/main.tf index f6c28de1..db05a04b 100644 --- a/examples/local-development/main.tf +++ b/examples/local-development/main.tf @@ -1,28 +1,11 @@ -resource "nexus_blobstore_file" "blobby" { - - name = "blobby" - path = "/nexus-data/blobby" +# arbitrary example +resource "nexus_blobstore" "default" { + name = "blobstore-file" + type = "File" + path = "/nexus-data/blobstore-file" soft_quota { limit = 1024000000 type = "spaceRemainingQuota" } } - -resource "nexus_repository_docker_hosted" "example" { - name = "example-container" - - storage { - blob_store_name = "default" - strict_content_type_validation = false - } - - docker { - v1_enabled = false - force_basic_auth = false - } -} - -# resource "nexus_security_cleanup_policy" "cleanup" { -# name = "CleanerUpper" -# } diff --git a/go.mod b/go.mod index 2d75c330..f27df43d 100644 --- a/go.mod +++ b/go.mod @@ -245,5 +245,3 @@ require ( ) replace github.com/datadrivers/go-nexus-client => ../go-nexus-client -//replace github.com/datadrivers/go-nexus-client => C:\Develop\IdeaProjects\go-nexus-client -//replace github.com/datadrivers/go-nexus-client => /home/uitdeveloper/Develop/IdeaProjects/go-nexus-client diff --git a/internal/services/cleanuppolicies/data_source_cleanup_policies.go b/internal/services/cleanuppolicies/data_source_cleanup_policies.go index c70edab6..4ccadb41 100644 --- a/internal/services/cleanuppolicies/data_source_cleanup_policies.go +++ b/internal/services/cleanuppolicies/data_source_cleanup_policies.go @@ -12,52 +12,44 @@ func DataSourceRepositoryCleanupPolicies() *schema.Resource { Schema: map[string]*schema.Schema{ "notes": { - Description: "any details on the specific cleanup policy", + Description: "Any details on the specific cleanup policy", Type: schema.TypeString, - Default: "string", - Optional: true, + Computed: true, }, - "criterialLastBlobUpdated": { - Description: "the age of the component in days", + "criteria_last_blob_updated": { + Description: "The age of the component in days", Type: schema.TypeInt, - Default: 0, - Optional: false, + Computed: true, }, - "criteriaLastDownloaded": { + "criteria_last_downloaded": { Description: "the last time the component had been downloaded in days", Type: schema.TypeInt, - Default: 0, - Optional: false, + Computed: true, }, - "criteriaReleaseType": { + "criteria_release_type": { Description: "When needed, this is either PRELEASE or RELEASE", Type: schema.TypeString, - Default: "RELEASES", - Optional: false, + Computed: true, }, - "criteriaAssetRegex": { - Description: "a regex string to filter for specific asset paths", + "criteria_asset_regex": { + Description: "A regex string to filter for specific asset paths", Type: schema.TypeString, - Default: "string", - Optional: false, + Computed: true, }, "retain": { - Description: "number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", + Description: "Number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", Type: schema.TypeInt, - Default: nil, - Optional: false, + Required: true, }, "name": { - Description: "the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", + Description: "The name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", Type: schema.TypeString, - Default: "policy-name", - Optional: true, + Required: true, }, "format": { - Description: "the target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", + Description: "The target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", Type: schema.TypeString, - Default: "string", - Optional: true, + Required: true, }, }, } diff --git a/internal/services/cleanuppolicies/resource_cleanup_policies.go b/internal/services/cleanuppolicies/resource_cleanup_policies.go index e7f02288..b9e49eaf 100644 --- a/internal/services/cleanuppolicies/resource_cleanup_policies.go +++ b/internal/services/cleanuppolicies/resource_cleanup_policies.go @@ -20,52 +20,45 @@ func ResourceCleanupPolicies() *schema.Resource { }, Schema: map[string]*schema.Schema{ "notes": { - Description: "any details on the specific cleanup policy", + Description: "Any details on the specific cleanup policy", Type: schema.TypeString, - Default: "string", Optional: true, }, - "criterialLastBlobUpdated": { - Description: "the age of the component in days", + "criteria_last_blob_updated": { + Description: "The age of the component in days", Type: schema.TypeInt, - Default: 0, - Optional: false, + Optional: true, }, - "criteriaLastDownloaded": { + "criteria_last_downloaded": { Description: "the last time the component had been downloaded in days", Type: schema.TypeInt, - Default: 0, - Optional: false, + Optional: true, }, - "criteriaReleaseType": { + "criteria_release_type": { Description: "When needed, this is either PRELEASE or RELEASE", Type: schema.TypeString, - Default: "RELEASE", - Optional: false, + Optional: true, }, - "criteriaAssetRegex": { - Description: "a regex string to filter for specific asset paths", + "criteria_asset_regex": { + Description: "A regex string to filter for specific asset paths", Type: schema.TypeString, - Default: "string", - Optional: false, + Optional: true, }, "retain": { - Description: "number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", + Description: "Number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", Type: schema.TypeInt, - Default: nil, - Optional: false, + Required: true, }, "name": { - Description: "the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", + Description: "The name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", Type: schema.TypeString, - Default: "policy-name", - Optional: true, + Required: true, + ForceNew: true, }, "format": { - Description: "the target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", + Description: "The target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats", Type: schema.TypeString, - Default: "string", - Optional: true, + Required: true, }, }, } diff --git a/internal/services/cleanuppolicies/resource_cleanup_policies_test.go b/internal/services/cleanuppolicies/resource_cleanup_policies_test.go index b61e7115..bd341490 100644 --- a/internal/services/cleanuppolicies/resource_cleanup_policies_test.go +++ b/internal/services/cleanuppolicies/resource_cleanup_policies_test.go @@ -5,13 +5,11 @@ import ( "strconv" "testing" - nexus "github.com/datadrivers/go-nexus-client/nexus3" "github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" "github.com/datadrivers/go-nexus-client/nexus3/schema/cleanuppolicies" "github.com/datadrivers/terraform-provider-nexus/internal/acceptance" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccResourceCleanupPolicy(t *testing.T) { @@ -71,22 +69,3 @@ resource "nexus_security_cleanup_policy" "acceptance" { `, *cp.Notes, strconv.Itoa(*cp.CriteriaLastBlobUpdated), strconv.Itoa(*cp.CriteriaLastDownloaded), *cp.CriteriaReleaseType, *cp.CriteriaAssetRegex, strconv.Itoa(cp.Retain), cp.Name, cp.Format) } - -func testAccCheckCleanupPolicyResourceExists(name string, cleanupPolicy *cleanuppolicies.CleanupPolicy) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] - if !ok { - return fmt.Errorf("Not found: %s", name) - } - - client := acceptance.TestAccProvider.Meta().(*nexus.NexusClient) - result, err := client.CleanupPolicy.Get(rs.Primary.ID) - if err != nil { - return err - } - - *cleanupPolicy = *result - - return nil - } -} From d98b120c51d5846e0d9824d22e253692a1466c24 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Wed, 20 Nov 2024 14:51:53 +0100 Subject: [PATCH 6/8] remove replace in go.mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index f27df43d..ac48d2cc 100644 --- a/go.mod +++ b/go.mod @@ -243,5 +243,3 @@ require ( mvdan.cc/gofumpt v0.7.0 // indirect mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect ) - -replace github.com/datadrivers/go-nexus-client => ../go-nexus-client From 02b43bf59f0fd75ac803303ba94da939296f3854 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Wed, 20 Nov 2024 15:02:44 +0100 Subject: [PATCH 7/8] remove terraform.lock.hcl --- .../local-development/.terraform.lock.hcl | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 examples/local-development/.terraform.lock.hcl diff --git a/examples/local-development/.terraform.lock.hcl b/examples/local-development/.terraform.lock.hcl deleted file mode 100644 index 88427706..00000000 --- a/examples/local-development/.terraform.lock.hcl +++ /dev/null @@ -1,23 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/datadrivers/nexus" { - version = "2.5.0" - hashes = [ - "h1:/xUNKX1nMoA3E3Sq1hyU64izf8vSefWmlziXufJFbQo=", - "zh:1a8b42a3070c7b2ddb83e4bcd8457f4ba67757bff130b7e75920c3dda115bdff", - "zh:1c38bbf057021cc0f6f7901125055ed062e409efb6ac34921078e263b48aef06", - "zh:45c7031135303a07e68d6924626148b43353e63f0d45ff893083791f7232882f", - "zh:6d63b4cdf390596c5f4d09b0f208d499da51e14d1f0124504fe180324b82409e", - "zh:79aa8ccd12a38f5215ea9533de7a98e42ab721886060dc40a4824a6dcebd9dd7", - "zh:86a01610ade5d667f33f3471f91ef5b65b251270f0e8379d50fba6553a2fa4c5", - "zh:8a1e7c91c4a076ee340ca666af952c45aaa22224ae94c6167b5930cfa273383b", - "zh:aea473919d87547fde75ff04b877b21fd9c30d0b23c87d2ae20dcf4f644ed860", - "zh:bb3312a81e428980cbe13d8fba4be0b8160344a218c39d489c9f23239c8bfc10", - "zh:c628cb8ba9c054991b6f23e4a85350116433070c14348605932568aede14f6cd", - "zh:d02a7bc711684d4a9e4df01192ce242396d237c75bfd58e892f49b7fb1d3db5e", - "zh:da50892a5b2a9f14951af2de7d512278e2493afc764eacadd4927758ec08f18d", - "zh:e01831062dfe87be0d382c5caca9c6ea3f3bb9ec37ed9ff9b64c3549f59fd6a7", - "zh:ec793834616941701e04b28cc3d09b4d9fb50b86ca12c140eb71634ea1ec676e", - ] -} From f30dbf469dc8660b1bab23d6efea1364b01d69d4 Mon Sep 17 00:00:00 2001 From: Tim-orius Date: Thu, 21 Nov 2024 17:05:22 +0100 Subject: [PATCH 8/8] Update criteria & policy creation --- .../resource_cleanup_policies.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/services/cleanuppolicies/resource_cleanup_policies.go b/internal/services/cleanuppolicies/resource_cleanup_policies.go index b9e49eaf..c699ec21 100644 --- a/internal/services/cleanuppolicies/resource_cleanup_policies.go +++ b/internal/services/cleanuppolicies/resource_cleanup_policies.go @@ -28,26 +28,31 @@ func ResourceCleanupPolicies() *schema.Resource { Description: "The age of the component in days", Type: schema.TypeInt, Optional: true, + Default: nil, }, "criteria_last_downloaded": { Description: "the last time the component had been downloaded in days", Type: schema.TypeInt, Optional: true, + Default: nil, }, "criteria_release_type": { Description: "When needed, this is either PRELEASE or RELEASE", Type: schema.TypeString, Optional: true, + Default: nil, }, "criteria_asset_regex": { Description: "A regex string to filter for specific asset paths", Type: schema.TypeString, Optional: true, + Default: nil, }, "retain": { Description: "Number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments", Type: schema.TypeInt, - Required: true, + Optional: true, + Default: 1, }, "name": { Description: "The name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot", @@ -79,13 +84,21 @@ func getCleanupPolicyFromResourceData(d *schema.ResourceData) cleanuppolicies.Cl Notes: ¬es, CriteriaLastBlobUpdated: &criteriaLastBlobUpdated, CriteriaLastDownloaded: &criteriaLastDownloaded, - CriteriaReleaseType: &criteriaReleaseType, CriteriaAssetRegex: &criteriaAssetRegex, - Retain: retain, Name: name, Format: format, } + // Only set CriteriaReleaseType for applicable formats + if format == "maven2" || format == "npm" || format == "yum" { + policy.CriteriaReleaseType = &criteriaReleaseType + } + + // Only set CriteriaReleaseType for applicable formats + if format == "maven2" || format == "docker" { + policy.Retain = retain + } + return policy }