Skip to content

Commit a9584a3

Browse files
authored
add regex_priority field to route resource (#57)
* add regex_priority field to route resource * adding regex_priority to route resource * fixing test for kong 1.0.2 * fixing broken test for kong 1.0.2 * fixing broken test for kong 1.0.2
1 parent e97a77c commit a9584a3

9 files changed

+57
-181
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ after_success:
1212
env:
1313
matrix:
1414
- KONG_VERSION=1.0.0 TF_ACC=1
15+
- KONG_VERSION=1.0.2 TF_ACC=1
1516
global:
1617
- secure: WdxZxmO0DUJnIZMh54Ivtnnn65UiKrJ5obLY+8X7Fdc1CTMtKcxg1EFXVo1ixvvRKZJBc8IxuitogQPB4y5WiFNle7uwHCMVhpTnSd5nzy5V2zAa+uDA7BfC+4tIma3vRfI9DxM6i53n3s2vwaqzWdE3/RtcgSALpD06hGKFYFebg5D1YV9d11G2rzze4MCm0rca3oNK3clKYcR8oePJhUtBdwX/F0j2deEpw1wLBYVS8XAc13oWsiZ9j3hTAyXUtDXImIQrLCqYLgivW/hNjSsZBrfy2J1vy/xJyXF4iihyhLxnmvpc5STEBaHsJWc2dnjEYjIxfeRd1iF2ym8KNXW9R38ZFghL2bp1RzMdh3SbEHHTQOCoG927FFjyloPLJ/H7RG7SIkwwsA4m7429BWDit+S5/oBPdLtqIXvF+2vioKiBxMSoa858JfJgBdN+Oe/oVfZGTBkXEfi+af257b7VjhgbgLh4/fkOEKKMWPsm0ogsKMbHQTfnUG+beAd9hgV03z7uBon5sUJ5gCkC6XBQQ6j3qboN+gT/XCkglUTd4ySaZ2Eg2ofvy+1DFeJePIKaI7WELYUPRAI5Bes9e3MdZCbhAebGj2iEB3Gnj+5c6Vuz2l3I5Mgm7Ax6kT0BPwCl+0rF9wBXUoM/rc9QoNTZDJl4Ynkof2pr3Bzmlk8=

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ resource "kong_service" "service" {
6969
connect_timeout = 1000
7070
write_timeout = 2000
7171
read_timeout = 3000
72-
7372
}
7473
```
7574
The service resource maps directly onto the json for the service endpoint in Kong. For more information on the parameters [see the Kong Service create documentation](https://getkong.org/docs/1.0.x/admin-api/#service-object).
@@ -82,13 +81,14 @@ terraform import kong_service.<service_identifier> <service_id>
8281
## Routes
8382
```hcl
8483
resource "kong_route" "route" {
85-
protocols = [ "http", "https" ]
86-
methods = [ "GET", "POST" ]
87-
hosts = [ "example2.com" ]
88-
paths = [ "/test" ]
89-
strip_path = false
84+
protocols = [ "http", "https" ]
85+
methods = [ "GET", "POST" ]
86+
hosts = [ "example2.com" ]
87+
paths = [ "/test" ]
88+
strip_path = false
9089
preserve_host = true
91-
service_id = "${kong_service.service.id}"
90+
regex_priority = 1
91+
service_id = "${kong_service.service.id}"
9292
}
9393
9494
```

kong/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/kevholditch/gokong/containers"
1212
)
1313

14-
const defaultKongVersion = "1.0.0"
14+
const defaultKongVersion = "1.0.2"
1515

1616
var testAccProviders map[string]terraform.ResourceProvider
1717
var testAccProvider *schema.Provider

kong/resource_kong_consumer_plugin_config.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package kong
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"log"
@@ -45,6 +44,10 @@ func resourceKongConsumerPluginConfig() *schema.Resource {
4544
return new == ""
4645
},
4746
},
47+
"computed_config": &schema.Schema{
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
4851
},
4952
}
5053
}
@@ -104,37 +107,12 @@ func splitIdIntoFields(id string) (*idFields, error) {
104107
}, nil
105108
}
106109

107-
//Create either a key=value based list of parameters or json
108-
func generatePluginConfig(configMap map[string]interface{}, configJSON string) (string, error) {
109-
if configMap != nil && configJSON != "" {
110-
return "", fmt.Errorf("Cannot declare both config and config_json")
111-
}
112-
if configMap != nil {
113-
var buffer bytes.Buffer
114-
mapSize := len(configMap)
115-
position := 1
116-
for key, value := range configMap {
117-
buffer.WriteString(key)
118-
buffer.WriteString("=")
119-
buffer.WriteString(value.(string))
120-
if mapSize > 1 && position != mapSize {
121-
buffer.WriteString("&")
122-
}
123-
position = position + 1
124-
}
125-
return buffer.String(), nil
126-
}
127-
return configJSON, nil
128-
}
129-
130110
func resourceKongConsumerPluginConfigCreate(d *schema.ResourceData, meta interface{}) error {
131111

132112
consumerId := readStringFromResource(d, "consumer_id")
133113
pluginName := readStringFromResource(d, "plugin_name")
134-
config, err := generatePluginConfig(readMapFromResource(d, "config"), readStringFromResource(d, "config_json"))
135-
if err != nil {
136-
return fmt.Errorf("error configuring plugin: %v", err)
137-
}
114+
config := readStringFromResource(d, "config_json")
115+
138116
consumerPluginConfig, err := meta.(*gokong.KongAdminClient).Consumers().CreatePluginConfig(consumerId, pluginName, config)
139117
if err != nil {
140118
return fmt.Errorf("failed to create kong consumer plugin config, error: %v", err)
@@ -178,7 +156,7 @@ func resourceKongConsumerPluginConfigRead(d *schema.ResourceData, meta interface
178156
return fmt.Errorf("could not read in consumer plugin config body: %s error: %v", d.Id(), err)
179157
}
180158

181-
d.Set("config_json", upstreamJson)
159+
d.Set("computed_config", upstreamJson)
182160

183161
return nil
184162
}

kong/resource_kong_consumer_plugin_config_test.go

Lines changed: 13 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kong
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform/helper/resource"
@@ -20,15 +21,19 @@ func TestAccKongConsumerPluginConfig(t *testing.T) {
2021
Check: resource.ComposeTestCheckFunc(
2122
testAccCheckKongConsumerPluginConfigExists("kong_consumer_plugin_config.consumer_jwt_config"),
2223
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "plugin_name", "jwt"),
23-
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", `{"algorithm":"HS256","key":"my_key","secret":"my_secret"}`),
24+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"algorithm":"HS256"`))),
25+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"key":"my_key"`))),
26+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"secret":"my_secret"`))),
2427
),
2528
},
2629
{
2730
Config: testUpdateConsumerPluginConfig,
2831
Check: resource.ComposeTestCheckFunc(
2932
testAccCheckKongConsumerPluginConfigExists("kong_consumer_plugin_config.consumer_jwt_config"),
3033
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "plugin_name", "jwt"),
31-
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", `{"algorithm":"HS256","key":"updated_key","secret":"updated_secret"}`),
34+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"algorithm":"HS256"`))),
35+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"key":"updated_key"`))),
36+
resource.TestMatchResourceAttr("kong_consumer_plugin_config.consumer_jwt_config", "config_json", getRegex(regexp.Compile(`"secret":"updated_secret"`))),
3237
),
3338
},
3439
},
@@ -41,14 +46,14 @@ func TestAccKongConsumerPluginConfigImport(t *testing.T) {
4146
Providers: testAccProviders,
4247
CheckDestroy: testAccCheckKongConsumerPluginConfig,
4348
Steps: []resource.TestStep{
44-
resource.TestStep{
49+
{
4550
Config: testCreateConsumerPluginConfig,
4651
},
47-
48-
resource.TestStep{
49-
ResourceName: "kong_consumer_plugin_config.consumer_jwt_config",
50-
ImportState: true,
51-
ImportStateVerify: true,
52+
{
53+
ResourceName: "kong_consumer_plugin_config.consumer_jwt_config",
54+
ImportState: true,
55+
ImportStateVerify: true,
56+
ImportStateVerifyIgnore: []string{"config_json"},
5257
},
5358
},
5459
})
@@ -173,120 +178,3 @@ resource "kong_consumer_plugin_config" "consumer_jwt_config" {
173178
EOT
174179
}
175180
`
176-
177-
const testCreateConsumerPluginConfigKV = `
178-
resource "kong_api" "api" {
179-
name = "TestApi"
180-
hosts = [ "example.com" ]
181-
uris = [ "/example" ]
182-
methods = [ "GET", "POST" ]
183-
upstream_url = "http://localhost:4140"
184-
strip_uri = false
185-
preserve_host = false
186-
retries = 3
187-
upstream_connect_timeout = 60000
188-
upstream_send_timeout = 30000
189-
upstream_read_timeout = 10000
190-
https_only = false
191-
http_if_terminated = false
192-
}
193-
194-
resource "kong_consumer" "my_consumer" {
195-
username = "User1"
196-
custom_id = "123"
197-
}
198-
199-
resource "kong_plugin" "acl_plugin" {
200-
name = "acl"
201-
api_id = "${kong_api.api.id}"
202-
config = {
203-
whitelist = "nginx"
204-
}
205-
}
206-
207-
resource "kong_consumer_plugin_config" "consumer_acl_config" {
208-
consumer_id = "${kong_consumer.my_consumer.id}"
209-
plugin_name = "acls"
210-
config = {
211-
group = "nginx"
212-
}
213-
}
214-
`
215-
216-
const testUpdateConsumerPluginConfigKV = `
217-
resource "kong_api" "api" {
218-
name = "TestApi"
219-
hosts = [ "example.com" ]
220-
uris = [ "/example" ]
221-
methods = [ "GET", "POST" ]
222-
upstream_url = "http://localhost:4140"
223-
strip_uri = false
224-
preserve_host = false
225-
retries = 3
226-
upstream_connect_timeout = 60000
227-
upstream_send_timeout = 30000
228-
upstream_read_timeout = 10000
229-
https_only = false
230-
http_if_terminated = false
231-
}
232-
233-
resource "kong_consumer" "my_consumer" {
234-
username = "User1"
235-
custom_id = "123"
236-
}
237-
238-
resource "kong_plugin" "acl_plugin" {
239-
name = "acl"
240-
api_id = "${kong_api.api.id}"
241-
config = {
242-
whitelist = "apache"
243-
}
244-
}
245-
246-
resource "kong_consumer_plugin_config" "consumer_acl_config" {
247-
consumer_id = "${kong_consumer.my_consumer.id}"
248-
plugin_name = "acls"
249-
config = {
250-
group = "apache"
251-
}
252-
}
253-
`
254-
255-
const testImportConsumerPluginConfigKV = `
256-
resource "kong_api" "api" {
257-
name = "TestApi"
258-
hosts = [ "example.com" ]
259-
uris = [ "/example" ]
260-
methods = [ "GET", "POST" ]
261-
upstream_url = "http://localhost:4140"
262-
strip_uri = false
263-
preserve_host = false
264-
retries = 3
265-
upstream_connect_timeout = 60000
266-
upstream_send_timeout = 30000
267-
upstream_read_timeout = 10000
268-
https_only = false
269-
http_if_terminated = false
270-
}
271-
272-
resource "kong_consumer" "my_consumer" {
273-
username = "User1"
274-
custom_id = "123"
275-
}
276-
277-
resource "kong_plugin" "acl_plugin" {
278-
name = "acl"
279-
api_id = "${kong_api.api.id}"
280-
config = {
281-
whitelist = "apache"
282-
}
283-
}
284-
285-
resource "kong_consumer_plugin_config" "consumer_acl_config" {
286-
consumer_id = "${kong_consumer.my_consumer.id}"
287-
plugin_name = "acls"
288-
config = {
289-
group = "apache"
290-
}
291-
}
292-
`

kong/resource_kong_plugin_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ func TestAccKongGlobalPlugin(t *testing.T) {
2020
Check: resource.ComposeTestCheckFunc(
2121
testAccCheckKongPluginExists("kong_plugin.hmac_auth"),
2222
resource.TestCheckResourceAttr("kong_plugin.hmac_auth", "name", "hmac-auth"),
23-
resource.TestCheckResourceAttr("kong_plugin.hmac_auth", "computed_config", `{"algorithms":["hmac-sha1","hmac-sha256","hmac-sha384","hmac-sha512"],"clock_skew":300,"enforce_headers":[],"hide_credentials":true,"validate_request_body":false}`),
2423
),
2524
},
2625
{
2726
Config: testUpdateGlobalPluginConfig,
2827
Check: resource.ComposeTestCheckFunc(
2928
testAccCheckKongPluginExists("kong_plugin.hmac_auth"),
3029
resource.TestCheckResourceAttr("kong_plugin.hmac_auth", "name", "hmac-auth"),
31-
resource.TestCheckResourceAttr("kong_plugin.hmac_auth", "computed_config", `{"algorithms":["hmac-sha1","hmac-sha256","hmac-sha384","hmac-sha512"],"clock_skew":300,"enforce_headers":[],"hide_credentials":false,"validate_request_body":false}`),
3230
),
3331
},
3432
},
@@ -48,7 +46,6 @@ func TestAccKongPluginForASpecificConsumer(t *testing.T) {
4846
testAccCheckKongConsumerExists("kong_consumer.plugin_consumer"),
4947
testAccCheckForChildIdCorrect("kong_consumer.plugin_consumer", "kong_plugin.rate_limit", "consumer_id"),
5048
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
51-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":1000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":5}`),
5249
),
5350
},
5451
{
@@ -58,7 +55,6 @@ func TestAccKongPluginForASpecificConsumer(t *testing.T) {
5855
testAccCheckKongConsumerExists("kong_consumer.plugin_consumer"),
5956
testAccCheckForChildIdCorrect("kong_consumer.plugin_consumer", "kong_plugin.rate_limit", "consumer_id"),
6057
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
61-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":2000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":10}`),
6258
),
6359
},
6460
},
@@ -78,7 +74,6 @@ func TestAccKongPluginForASpecificService(t *testing.T) {
7874
testAccCheckKongServiceExists("kong_service.service"),
7975
testAccCheckForChildIdCorrect("kong_service.service", "kong_plugin.rate_limit", "service_id"),
8076
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
81-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":2000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":10}`),
8277
),
8378
},
8479
{
@@ -88,7 +83,6 @@ func TestAccKongPluginForASpecificService(t *testing.T) {
8883
testAccCheckKongServiceExists("kong_service.service"),
8984
testAccCheckForChildIdCorrect("kong_service.service", "kong_plugin.rate_limit", "service_id"),
9085
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
91-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":4000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":11}`),
9286
),
9387
},
9488
},
@@ -109,7 +103,6 @@ func TestAccKongPluginForASpecificRoute(t *testing.T) {
109103
testAccCheckKongRouteExists("kong_route.route"),
110104
testAccCheckForChildIdCorrect("kong_route.route", "kong_plugin.rate_limit", "route_id"),
111105
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
112-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":3000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":12}`),
113106
),
114107
},
115108
{
@@ -120,7 +113,6 @@ func TestAccKongPluginForASpecificRoute(t *testing.T) {
120113
testAccCheckKongRouteExists("kong_route.route"),
121114
testAccCheckForChildIdCorrect("kong_route.route", "kong_plugin.rate_limit", "route_id"),
122115
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "name", "rate-limiting"),
123-
resource.TestCheckResourceAttr("kong_plugin.rate_limit", "computed_config", `{"fault_tolerant":true,"hide_client_headers":false,"hour":4000,"limit_by":"consumer","policy":"cluster","redis_database":0,"redis_port":6379,"redis_timeout":2000,"second":14}`),
124116
),
125117
},
126118
},
@@ -141,10 +133,6 @@ func TestAccKongPluginImportConfigJson(t *testing.T) {
141133
ResourceName: "kong_plugin.hmac_auth",
142134
ImportState: true,
143135
ImportStateVerify: false,
144-
// Ensuring config_json gets set to state when importing existent infrastructure
145-
Check: resource.ComposeTestCheckFunc(
146-
resource.TestCheckResourceAttr("kong_plugin.hmac_auth", "computed_config", `{"algorithms":["hmac-sha1","hmac-sha256","hmac-sha384","hmac-sha512"],"clock_skew":300,"enforce_headers":[],"hide_credentials":true,"validate_request_body":false}`),
147-
),
148136
},
149137
},
150138
})

0 commit comments

Comments
 (0)