|
| 1 | +package account_token_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/acctest" |
| 9 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/utils" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/plancheck" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccAccountToken_Basic(t *testing.T) { |
| 15 | + rnd := utils.GenerateRandomResourceName() |
| 16 | + resourceID := "cloudflare_account_token." + rnd |
| 17 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 18 | + permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 19 | + |
| 20 | + var policyId string |
| 21 | + |
| 22 | + resource.Test(t, resource.TestCase{ |
| 23 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 24 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 25 | + Steps: []resource.TestStep{ |
| 26 | + { |
| 27 | + Config: testAccCloudflareAccountTokenWithoutCondition(rnd, accountID, rnd, permissionID), |
| 28 | + Check: resource.ComposeTestCheckFunc( |
| 29 | + resource.TestCheckResourceAttr(resourceID, "name", rnd), |
| 30 | + resource.TestCheckResourceAttrSet(resourceID, "policies.0.id"), |
| 31 | + resource.TestCheckResourceAttrWith(resourceID, "policies.0.id", func(value string) error { |
| 32 | + policyId = value |
| 33 | + return nil |
| 34 | + }), |
| 35 | + resource.TestCheckResourceAttr(resourceID, "policies.0.permission_groups.0.id", permissionID), |
| 36 | + ), |
| 37 | + }, |
| 38 | + { |
| 39 | + Config: testAccCloudflareAccountTokenWithoutCondition(rnd, accountID, rnd+"-updated", permissionID), |
| 40 | + Check: resource.ComposeTestCheckFunc( |
| 41 | + resource.TestCheckResourceAttr(resourceID, "name", rnd+"-updated"), |
| 42 | + resource.TestCheckResourceAttrSet(resourceID, "policies.0.id"), |
| 43 | + resource.TestCheckResourceAttrWith(resourceID, "policies.0.id", func(value string) error { |
| 44 | + if value != policyId { |
| 45 | + return fmt.Errorf("policy ID changed from %s to %s", policyId, value) |
| 46 | + } |
| 47 | + return nil |
| 48 | + }), |
| 49 | + resource.TestCheckResourceAttr(resourceID, "policies.0.permission_groups.0.id", permissionID), |
| 50 | + ), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func TestAccAccountToken_DoesNotSetConditions(t *testing.T) { |
| 57 | + rnd := utils.GenerateRandomResourceName() |
| 58 | + name := "cloudflare_account_token." + rnd |
| 59 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 60 | + permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 61 | + |
| 62 | + resource.Test(t, resource.TestCase{ |
| 63 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 64 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 65 | + Steps: []resource.TestStep{ |
| 66 | + { |
| 67 | + Config: testAccCloudflareAccountTokenWithoutCondition(rnd, accountID, rnd, permissionID), |
| 68 | + Check: resource.ComposeTestCheckFunc( |
| 69 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 70 | + resource.TestCheckNoResourceAttr(name, "condition.request_ip.0.in"), |
| 71 | + resource.TestCheckNoResourceAttr(name, "condition.request_ip.0.not_in"), |
| 72 | + ), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func testAccCloudflareAccountTokenWithoutCondition(resourceName, accountId, rnd, permissionID string) string { |
| 79 | + return acctest.LoadTestCase("account_token-without-condition.tf", resourceName, accountId, rnd, permissionID) |
| 80 | +} |
| 81 | + |
| 82 | +func TestAccAccountToken_SetIndividualCondition(t *testing.T) { |
| 83 | + rnd := utils.GenerateRandomResourceName() |
| 84 | + name := "cloudflare_account_token." + rnd |
| 85 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 86 | + permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 87 | + |
| 88 | + resource.Test(t, resource.TestCase{ |
| 89 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 90 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 91 | + Steps: []resource.TestStep{ |
| 92 | + { |
| 93 | + Config: testAccCloudflareAccountTokenWithIndividualCondition(rnd, accountID, permissionID), |
| 94 | + Check: resource.ComposeTestCheckFunc( |
| 95 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 96 | + resource.TestCheckResourceAttr(name, "condition.request_ip.in.0", "192.0.2.1/32"), |
| 97 | + resource.TestCheckNoResourceAttr(name, "condition.request_ip.not_in"), |
| 98 | + ), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func testAccCloudflareAccountTokenWithIndividualCondition(rnd, accountID, permissionID string) string { |
| 105 | + return acctest.LoadTestCase("account_token-with-individual-condition.tf", rnd, accountID, permissionID) |
| 106 | +} |
| 107 | + |
| 108 | +func TestAccAccountToken_SetAllCondition(t *testing.T) { |
| 109 | + rnd := utils.GenerateRandomResourceName() |
| 110 | + name := "cloudflare_account_token." + rnd |
| 111 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 112 | + permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 113 | + |
| 114 | + resource.Test(t, resource.TestCase{ |
| 115 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 116 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 117 | + Steps: []resource.TestStep{ |
| 118 | + { |
| 119 | + Config: testAccCloudflareAccountTokenWithAllCondition(rnd, accountID, permissionID), |
| 120 | + Check: resource.ComposeTestCheckFunc( |
| 121 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 122 | + resource.TestCheckResourceAttr(name, "condition.request_ip.in.0", "192.0.2.1/32"), |
| 123 | + resource.TestCheckResourceAttr(name, "condition.request_ip.not_in.0", "198.51.100.1/32"), |
| 124 | + ), |
| 125 | + }, |
| 126 | + }, |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +func testAccCloudflareAccountTokenWithAllCondition(rnd, accountID, permissionID string) string { |
| 131 | + return acctest.LoadTestCase("account_token-with-all-condition.tf", rnd, accountID, permissionID) |
| 132 | +} |
| 133 | + |
| 134 | +func TestAccAccountToken_TokenTTL(t *testing.T) { |
| 135 | + rnd := utils.GenerateRandomResourceName() |
| 136 | + name := "cloudflare_account_token." + rnd |
| 137 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 138 | + permissionID := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 139 | + |
| 140 | + resource.Test(t, resource.TestCase{ |
| 141 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 142 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 143 | + Steps: []resource.TestStep{ |
| 144 | + { |
| 145 | + Config: testAccCloudflareAccountTokenWithTTL(rnd, accountID, permissionID), |
| 146 | + Check: resource.ComposeTestCheckFunc( |
| 147 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 148 | + resource.TestCheckResourceAttr(name, "not_before", "2018-07-01T05:20:00Z"), |
| 149 | + resource.TestCheckResourceAttr(name, "expires_on", "2032-01-01T00:00:00Z"), |
| 150 | + ), |
| 151 | + }, |
| 152 | + }, |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +func testAccCloudflareAccountTokenWithTTL(rnd, accountID, permissionID string) string { |
| 157 | + return acctest.LoadTestCase("account_token-with-ttl.tf", rnd, accountID, permissionID) |
| 158 | +} |
| 159 | + |
| 160 | +func TestAccAccountToken_PermissionGroupOrder(t *testing.T) { |
| 161 | + rnd := utils.GenerateRandomResourceName() |
| 162 | + name := "cloudflare_account_token." + rnd |
| 163 | + accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") |
| 164 | + permissionID1 := "82e64a83756745bbbb1c9c2701bf816b" // DNS read |
| 165 | + permissionID2 := "e199d584e69344eba202452019deafe3" // Disable ESC read |
| 166 | + |
| 167 | + resource.Test(t, resource.TestCase{ |
| 168 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 169 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 170 | + Steps: []resource.TestStep{ |
| 171 | + { |
| 172 | + Config: acctest.LoadTestCase("account_token-permissiongroup-order.tf", rnd, accountID, permissionID1, permissionID2), |
| 173 | + Check: resource.ComposeTestCheckFunc( |
| 174 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 175 | + resource.TestCheckResourceAttr(name, "policies.0.permission_groups.0.id", permissionID1), |
| 176 | + resource.TestCheckResourceAttr(name, "policies.0.permission_groups.1.id", permissionID2), |
| 177 | + ), |
| 178 | + }, |
| 179 | + { |
| 180 | + Config: acctest.LoadTestCase("account_token-permissiongroup-order.tf", rnd, accountID, permissionID2, permissionID1), |
| 181 | + // changing the order of permission groups should not affect plan |
| 182 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 183 | + PreApply: []plancheck.PlanCheck{ |
| 184 | + plancheck.ExpectEmptyPlan(), |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }) |
| 190 | + |
| 191 | + resource.Test(t, resource.TestCase{ |
| 192 | + PreCheck: func() { acctest.TestAccPreCheck(t) }, |
| 193 | + ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, |
| 194 | + Steps: []resource.TestStep{ |
| 195 | + { |
| 196 | + Config: acctest.LoadTestCase("account_token-permissiongroup-order.tf", rnd, accountID, permissionID2, permissionID1), |
| 197 | + Check: resource.ComposeTestCheckFunc( |
| 198 | + resource.TestCheckResourceAttr(name, "name", rnd), |
| 199 | + resource.TestCheckResourceAttr(name, "policies.0.permission_groups.0.id", permissionID1), |
| 200 | + resource.TestCheckResourceAttr(name, "policies.0.permission_groups.1.id", permissionID2), |
| 201 | + ), |
| 202 | + }, |
| 203 | + { |
| 204 | + Config: acctest.LoadTestCase("account_token-permissiongroup-order.tf", rnd, accountID, permissionID2, permissionID1), |
| 205 | + // re-applying same change does not produce drift |
| 206 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 207 | + PreApply: []plancheck.PlanCheck{ |
| 208 | + plancheck.ExpectEmptyPlan(), |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + Config: acctest.LoadTestCase("account_token-permissiongroup-order.tf", rnd, accountID, permissionID1, permissionID2), |
| 214 | + // changing the order of permission groups should not affect plan |
| 215 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 216 | + PreApply: []plancheck.PlanCheck{ |
| 217 | + plancheck.ExpectEmptyPlan(), |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + }) |
| 223 | +} |
0 commit comments