Skip to content
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
59 changes: 59 additions & 0 deletions tencentcloud/resource_tc_tcr_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,67 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func init() {
resource.AddTestSweepers("tencentcloud_tcr_repository", &resource.Sweeper{
Name: "tencentcloud_tcr_repository",
F: testSweepTCRRepository,
})
}

// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_tcr_repository
func testSweepTCRRepository(r string) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
cli, _ := sharedClientForRegion(r)
tcrService := TCRService{client: cli.(*TencentCloudClient).apiV3Conn}

var filters []*tcr.Filter
filters = append(filters, &tcr.Filter{
Name: helper.String("RegistryName"),
Values: []*string{helper.String(defaultTCRInstanceName)},
})

instances, err := tcrService.DescribeTCRInstances(ctx, "", filters)

if err != nil {
return err
}

if len(instances) == 0 {
return fmt.Errorf("instance %s not exist", defaultTCRInstanceName)
}

instanceId := *instances[0].RegistryId
// the non-keep namespace will be removed directly when run sweeper tencentcloud_tcr_namespace
// so... only need to care about the repos under the keep namespace
repos, err := tcrService.DescribeTCRRepositories(ctx, instanceId, "", "")

if err != nil {
return err
}

for i := range repos {
n := repos[i]
names := strings.Split(*n.Name, "/")
if len(names) != 2 {
continue
}
repoName := names[1]
if isResourcePersist(repoName, nil) {
continue
}
err = tcrService.DeleteTCRRepository(ctx, instanceId, *n.Namespace, repoName)
if err != nil {
continue
}
}
return nil
}

func TestAccTencentCloudTCRRepository_basic_and_update(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Expand Down
34 changes: 28 additions & 6 deletions tencentcloud/resource_tc_tcr_tag_retention_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
)

func TestAccTencentCloudTCRTagRetentionRuleResource_basic(t *testing.T) {
Expand Down Expand Up @@ -76,6 +77,11 @@ func testAccCheckTCRTagRetentionRuleDestroy(s *terraform.State) error {

rule, err := service.DescribeTcrTagRetentionRuleById(ctx, registryId, namespaceName, &retentionId)
if err != nil {
if ee, ok := err.(*sdkErrors.TencentCloudSDKError); ok {
if ee.Code == "ResourceNotFound" {
return nil
}
}
return err
}

Expand Down Expand Up @@ -110,6 +116,11 @@ func testAccCheckTCRTagRetentionRuleExists(re string) resource.TestCheckFunc {

rule, err := service.DescribeTcrTagRetentionRuleById(ctx, registryId, namespaceName, &retentionId)
if err != nil {
if ee, ok := err.(*sdkErrors.TencentCloudSDKError); ok {
if ee.Code == "ResourceNotFound" {
return fmt.Errorf("Tcr Tag Retention Rule not found[ResourceNotFound], Id: %v", rs.Primary.ID)
}
}
return err
}

Expand All @@ -120,10 +131,21 @@ func testAccCheckTCRTagRetentionRuleExists(re string) resource.TestCheckFunc {
}
}

const testAccTcrTagRetentionRule = defaultTCRInstanceData + `
const testAccTCRInstance_retention = `
resource "tencentcloud_tcr_instance" "mytcr_retention" {
name = "tf-test-tcr-retention"
instance_type = "basic"
delete_bucket = true

tags ={
test = "test"
}
}`

const testAccTcrTagRetentionRule = testAccTCRInstance_retention + `

resource "tencentcloud_tcr_namespace" "my_ns" {
instance_id = local.tcr_id
instance_id = tencentcloud_tcr_instance.mytcr_retention.id
name = "tf_test_ns_retention"
is_public = true
is_auto_scan = true
Expand All @@ -135,7 +157,7 @@ resource "tencentcloud_tcr_namespace" "my_ns" {
}

resource "tencentcloud_tcr_tag_retention_rule" "my_rule" {
registry_id = local.tcr_id
registry_id = tencentcloud_tcr_instance.mytcr_retention.id
namespace_name = tencentcloud_tcr_namespace.my_ns.name
retention_rule {
key = "nDaysSinceLastPush"
Expand All @@ -147,10 +169,10 @@ resource "tencentcloud_tcr_tag_retention_rule" "my_rule" {

`

const testAccTcrTagRetentionRule_update = defaultTCRInstanceData + `
const testAccTcrTagRetentionRule_update = testAccTCRInstance_retention + `

resource "tencentcloud_tcr_namespace" "my_ns" {
instance_id = local.tcr_id
instance_id = tencentcloud_tcr_instance.mytcr_retention.id
name = "tf_test_ns_retention"
is_public = true
is_auto_scan = true
Expand All @@ -162,7 +184,7 @@ resource "tencentcloud_tcr_namespace" "my_ns" {
}

resource "tencentcloud_tcr_tag_retention_rule" "my_rule" {
registry_id = local.tcr_id
registry_id = tencentcloud_tcr_instance.mytcr_retention.id
namespace_name = tencentcloud_tcr_namespace.my_ns.name
retention_rule {
key = "nDaysSinceLastPush"
Expand Down