|
| 1 | +package kong |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 10 | + "github.com/kong/go-kong/kong" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccConsumerKeyAuth(t *testing.T) { |
| 14 | + |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + Providers: testAccProviders, |
| 17 | + CheckDestroy: testAccCheckConsumerKeyAuthDestroy, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + { |
| 20 | + Config: testCreateConsumerKeyAuthConfig, |
| 21 | + Check: resource.ComposeTestCheckFunc( |
| 22 | + testAccCheckConsumerKeyAuthExists("kong_consumer_key_auth.consumer_key_auth"), |
| 23 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "key", "foo"), |
| 24 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.#", "1"), |
| 25 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.0", "myTag"), |
| 26 | + ), |
| 27 | + }, |
| 28 | + { |
| 29 | + Config: testUpdateConsumerKeyAuthConfig, |
| 30 | + Check: resource.ComposeTestCheckFunc( |
| 31 | + testAccCheckConsumerKeyAuthExists("kong_consumer_key_auth.consumer_key_auth"), |
| 32 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "key", "foo_updated"), |
| 33 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.#", "2"), |
| 34 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.0", "myTag"), |
| 35 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.1", "anotherTag"), |
| 36 | + ), |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestAccConsumerKeyAuthComputed(t *testing.T) { |
| 43 | + |
| 44 | + resource.Test(t, resource.TestCase{ |
| 45 | + Providers: testAccProviders, |
| 46 | + CheckDestroy: testAccCheckConsumerKeyAuthDestroy, |
| 47 | + Steps: []resource.TestStep{ |
| 48 | + { |
| 49 | + Config: testCreateConsumerKeyAuthConfigKeyComputed, |
| 50 | + Check: resource.ComposeTestCheckFunc( |
| 51 | + testAccCheckConsumerKeyAuthExists("kong_consumer_key_auth.consumer_key_auth"), |
| 52 | + resource.TestCheckResourceAttrSet("kong_consumer_key_auth.consumer_key_auth", "key"), |
| 53 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.#", "1"), |
| 54 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.0", "myTag"), |
| 55 | + ), |
| 56 | + }, |
| 57 | + { |
| 58 | + Config: testUpdateConsumerKeyAuthConfig, |
| 59 | + Check: resource.ComposeTestCheckFunc( |
| 60 | + testAccCheckConsumerKeyAuthExists("kong_consumer_key_auth.consumer_key_auth"), |
| 61 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "key", "foo_updated"), |
| 62 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.#", "2"), |
| 63 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.0", "myTag"), |
| 64 | + resource.TestCheckResourceAttr("kong_consumer_key_auth.consumer_key_auth", "tags.1", "anotherTag"), |
| 65 | + ), |
| 66 | + }, |
| 67 | + }, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func testAccCheckConsumerKeyAuthDestroy(state *terraform.State) error { |
| 72 | + |
| 73 | + client := testAccProvider.Meta().(*config).adminClient.KeyAuths |
| 74 | + |
| 75 | + resources := getResourcesByType("kong_consumer_key_auth", state) |
| 76 | + |
| 77 | + if len(resources) != 1 { |
| 78 | + return fmt.Errorf("expecting only 1 consumer key auth resource found %v", len(resources)) |
| 79 | + } |
| 80 | + |
| 81 | + id, err := splitConsumerID(resources[0].Primary.ID) |
| 82 | + ConsumerKeyAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID)) |
| 83 | + |
| 84 | + if !kong.IsNotFoundErr(err) && err != nil { |
| 85 | + return fmt.Errorf("error calling get consumer auth by id: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + if ConsumerKeyAuth != nil { |
| 89 | + return fmt.Errorf("key auth %s still exists, %+v", id.ID, ConsumerKeyAuth) |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func testAccCheckConsumerKeyAuthExists(resourceKey string) resource.TestCheckFunc { |
| 96 | + |
| 97 | + return func(s *terraform.State) error { |
| 98 | + rs, ok := s.RootModule().Resources[resourceKey] |
| 99 | + |
| 100 | + if !ok { |
| 101 | + return fmt.Errorf("not found: %s", resourceKey) |
| 102 | + } |
| 103 | + |
| 104 | + if rs.Primary.ID == "" { |
| 105 | + return fmt.Errorf("no ID is set") |
| 106 | + } |
| 107 | + |
| 108 | + client := testAccProvider.Meta().(*config).adminClient.KeyAuths |
| 109 | + id, err := splitConsumerID(rs.Primary.ID) |
| 110 | + |
| 111 | + ConsumerKeyAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID)) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + if ConsumerKeyAuth == nil { |
| 118 | + return fmt.Errorf("ConsumerKeyAuth with id %v not found", id.ID) |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +const testCreateConsumerKeyAuthConfig = ` |
| 126 | +resource "kong_consumer" "my_consumer" { |
| 127 | + username = "User1" |
| 128 | + custom_id = "123" |
| 129 | +} |
| 130 | +
|
| 131 | +resource "kong_plugin" "key_auth_plugin" { |
| 132 | + name = "key-auth" |
| 133 | +} |
| 134 | +
|
| 135 | +resource "kong_consumer_key_auth" "consumer_key_auth" { |
| 136 | + consumer_id = "${kong_consumer.my_consumer.id}" |
| 137 | + key = "foo" |
| 138 | + tags = ["myTag"] |
| 139 | +} |
| 140 | +` |
| 141 | +const testUpdateConsumerKeyAuthConfig = ` |
| 142 | +resource "kong_consumer" "my_consumer" { |
| 143 | + username = "User1" |
| 144 | + custom_id = "123" |
| 145 | +} |
| 146 | +
|
| 147 | +resource "kong_plugin" "key_auth_plugin" { |
| 148 | + name = "key-auth" |
| 149 | +} |
| 150 | +
|
| 151 | +resource "kong_consumer_key_auth" "consumer_key_auth" { |
| 152 | + consumer_id = "${kong_consumer.my_consumer.id}" |
| 153 | + key = "foo_updated" |
| 154 | + tags = ["myTag", "anotherTag"] |
| 155 | +} |
| 156 | +` |
| 157 | +const testCreateConsumerKeyAuthConfigKeyComputed = ` |
| 158 | +resource "kong_consumer" "my_consumer" { |
| 159 | + username = "User1" |
| 160 | + custom_id = "123" |
| 161 | +} |
| 162 | +
|
| 163 | +resource "kong_plugin" "key_auth_plugin" { |
| 164 | + name = "key-auth" |
| 165 | +} |
| 166 | +
|
| 167 | +resource "kong_consumer_key_auth" "consumer_key_auth" { |
| 168 | + consumer_id = "${kong_consumer.my_consumer.id}" |
| 169 | + tags = ["myTag"] |
| 170 | +} |
| 171 | +` |
0 commit comments