|
| 1 | +//nolint:testpackage |
| 2 | +package cloudsmith |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +// TestAccEntitlementControl_basic spins up a repository and uses its default entitlement token, |
| 14 | +// creates an entitlement control with the token disabled, verifies it exists and checks |
| 15 | +// the enabled state is set correctly. Then it changes the enabled state to true, |
| 16 | +// and verifies it's been set correctly before tearing down the resources and |
| 17 | +// verifying deletion. |
| 18 | +func TestAccEntitlementControl_basic(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + resource.Test(t, resource.TestCase{ |
| 22 | + PreCheck: func() { testAccPreCheck(t) }, |
| 23 | + Providers: testAccProviders, |
| 24 | + CheckDestroy: testAccEntitlementControlCheckDestroy("cloudsmith_entitlement_control.test"), |
| 25 | + Steps: []resource.TestStep{ |
| 26 | + { |
| 27 | + Config: testAccEntitlementControlConfigBasic, |
| 28 | + Check: resource.ComposeTestCheckFunc( |
| 29 | + testAccEntitlementControlCheckExists("cloudsmith_entitlement_control.test"), |
| 30 | + resource.TestCheckResourceAttr("cloudsmith_entitlement_control.test", "namespace", os.Getenv("CLOUDSMITH_NAMESPACE")), |
| 31 | + resource.TestCheckResourceAttr("cloudsmith_entitlement_control.test", "enabled", "false"), |
| 32 | + ), |
| 33 | + }, |
| 34 | + { |
| 35 | + Config: testAccEntitlementControlConfigBasicUpdate, |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + testAccEntitlementControlCheckExists("cloudsmith_entitlement_control.test"), |
| 38 | + resource.TestCheckResourceAttr("cloudsmith_entitlement_control.test", "namespace", os.Getenv("CLOUDSMITH_NAMESPACE")), |
| 39 | + resource.TestCheckResourceAttr("cloudsmith_entitlement_control.test", "enabled", "true"), |
| 40 | + ), |
| 41 | + }, |
| 42 | + { |
| 43 | + ResourceName: "cloudsmith_entitlement_control.test", |
| 44 | + ImportState: true, |
| 45 | + ImportStateVerify: true, |
| 46 | + ImportStateIdFunc: func(s *terraform.State) (string, error) { |
| 47 | + resourceState := s.RootModule().Resources["cloudsmith_entitlement_control.test"] |
| 48 | + return fmt.Sprintf( |
| 49 | + "%s.%s.%s", |
| 50 | + resourceState.Primary.Attributes["namespace"], |
| 51 | + resourceState.Primary.Attributes["repository"], |
| 52 | + resourceState.Primary.ID, |
| 53 | + ), nil |
| 54 | + }, |
| 55 | + ImportStateVerifyIgnore: []string{ |
| 56 | + "identifier", // Ignore identifier as it's used for creation but not returned in read |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +//nolint:goerr113 |
| 64 | +func testAccEntitlementControlCheckDestroy(resourceName string) resource.TestCheckFunc { |
| 65 | + return func(s *terraform.State) error { |
| 66 | + resourceState, ok := s.RootModule().Resources[resourceName] |
| 67 | + if !ok { |
| 68 | + return fmt.Errorf("resource not found: %s", resourceName) |
| 69 | + } |
| 70 | + |
| 71 | + if resourceState.Primary.ID == "" { |
| 72 | + return fmt.Errorf("resource id not set") |
| 73 | + } |
| 74 | + |
| 75 | + pc := testAccProvider.Meta().(*providerConfig) |
| 76 | + |
| 77 | + namespace := os.Getenv("CLOUDSMITH_NAMESPACE") |
| 78 | + repository := resourceState.Primary.Attributes["repository"] |
| 79 | + identifier := resourceState.Primary.ID |
| 80 | + |
| 81 | + req := pc.APIClient.EntitlementsApi.EntitlementsRead(pc.Auth, namespace, repository, identifier) |
| 82 | + entitlement, resp, err := pc.APIClient.EntitlementsApi.EntitlementsReadExecute(req) |
| 83 | + if err != nil && !is404(resp) { |
| 84 | + return fmt.Errorf("unable to verify entitlement control state: %w", err) |
| 85 | + } else if is200(resp) && entitlement.GetIsActive() { |
| 86 | + return fmt.Errorf("unable to verify entitlement control state: still enabled: %s/%s/%s", namespace, repository, identifier) |
| 87 | + } |
| 88 | + defer resp.Body.Close() |
| 89 | + |
| 90 | + return nil |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +//nolint:goerr113 |
| 95 | +func testAccEntitlementControlCheckExists(resourceName string) resource.TestCheckFunc { |
| 96 | + return func(s *terraform.State) error { |
| 97 | + resourceState, ok := s.RootModule().Resources[resourceName] |
| 98 | + if !ok { |
| 99 | + return fmt.Errorf("resource not found: %s", resourceName) |
| 100 | + } |
| 101 | + |
| 102 | + if resourceState.Primary.ID == "" { |
| 103 | + return fmt.Errorf("resource id not set") |
| 104 | + } |
| 105 | + |
| 106 | + pc := testAccProvider.Meta().(*providerConfig) |
| 107 | + |
| 108 | + namespace := os.Getenv("CLOUDSMITH_NAMESPACE") |
| 109 | + repository := resourceState.Primary.Attributes["repository"] |
| 110 | + identifier := resourceState.Primary.ID |
| 111 | + |
| 112 | + req := pc.APIClient.EntitlementsApi.EntitlementsRead(pc.Auth, namespace, repository, identifier) |
| 113 | + _, resp, err := pc.APIClient.EntitlementsApi.EntitlementsReadExecute(req) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("unable to verify entitlement control existence: %w", err) |
| 116 | + } |
| 117 | + defer resp.Body.Close() |
| 118 | + |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +var testAccEntitlementControlConfigBasic = fmt.Sprintf(` |
| 124 | +resource "cloudsmith_repository" "test" { |
| 125 | + name = "terraform-acc-test-ent-ctrl" |
| 126 | + namespace = "%s" |
| 127 | +} |
| 128 | +
|
| 129 | +data "cloudsmith_entitlement_list" "test" { |
| 130 | + namespace = resource.cloudsmith_repository.test.namespace |
| 131 | + repository = resource.cloudsmith_repository.test.slug_perm |
| 132 | + query = ["name:Default"] |
| 133 | +} |
| 134 | +
|
| 135 | +resource "cloudsmith_entitlement_control" "test" { |
| 136 | + namespace = resource.cloudsmith_repository.test.namespace |
| 137 | + repository = resource.cloudsmith_repository.test.slug_perm |
| 138 | + identifier = data.cloudsmith_entitlement_list.test.entitlement_tokens[0].slug_perm |
| 139 | + enabled = false |
| 140 | +} |
| 141 | +`, os.Getenv("CLOUDSMITH_NAMESPACE")) |
| 142 | + |
| 143 | +var testAccEntitlementControlConfigBasicUpdate = fmt.Sprintf(` |
| 144 | +resource "cloudsmith_repository" "test" { |
| 145 | + name = "terraform-acc-test-ent-ctrl" |
| 146 | + namespace = "%s" |
| 147 | +} |
| 148 | +
|
| 149 | +data "cloudsmith_entitlement_list" "test" { |
| 150 | + namespace = resource.cloudsmith_repository.test.namespace |
| 151 | + repository = resource.cloudsmith_repository.test.slug_perm |
| 152 | + query = ["name:Default"] |
| 153 | +} |
| 154 | +
|
| 155 | +resource "cloudsmith_entitlement_control" "test" { |
| 156 | + namespace = resource.cloudsmith_repository.test.namespace |
| 157 | + repository = resource.cloudsmith_repository.test.slug_perm |
| 158 | + identifier = data.cloudsmith_entitlement_list.test.entitlement_tokens[0].slug_perm |
| 159 | + enabled = true |
| 160 | +} |
| 161 | +`, os.Getenv("CLOUDSMITH_NAMESPACE")) |
0 commit comments