Skip to content

Commit 7d4d764

Browse files
charles-d-burtonkevholditch
authored andcommitted
Configure Plugin with Key/Value (#9)
* feat: added capability to generate both k=v config and json * feat: Added field for key/value config options * updating tests * more test fixes * more test fixes * fix: Updated readme
1 parent 37e3848 commit 7d4d764

File tree

3 files changed

+161
-6
lines changed

3 files changed

+161
-6
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ The example above shows configuring the jwt plugin for a consumer.
115115
`config_json` this is the configuration json for how you want to configure the plugin. The json is passed straight through to kong as is. You can get the json config from the Kong documentation
116116
page of the plugin you are configuring
117117

118+
Other plugins must be configured using key/value pairs, for example the [acl](https://getkong.org/plugins/acl/) plugin. To update a plugin using key value pairs configure the "kong_consumer_plugin_config" resource.
119+
120+
```hcl
121+
resource "kong_consumer_plugin_config" "consumer_acl_config" {
122+
consumer_id = "876bf719-8f18-4ce5-cc9f-5b5af6c36007"
123+
plugin_name = "acls"
124+
config = {
125+
group = "your_acl_group"
126+
}
127+
}
128+
```
129+
130+
All parameters are the same as above except the `config` parameter.
131+
`config` is a map of key/value pairs you wish to pass as the configuration.
132+
133+
#### NOTE: You can only have either config or config_json configured, not both.
134+
118135

119136
## Consumers
120137
```hcl

kong/resource_kong_consumer_plugin_config.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package kong
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
6-
"github.com/hashicorp/terraform/helper/schema"
7-
"github.com/kevholditch/gokong"
87
"log"
98
"strings"
9+
10+
"github.com/hashicorp/terraform/helper/schema"
11+
"github.com/kevholditch/gokong"
1012
)
1113

1214
func resourceKongConsumerPluginConfig() *schema.Resource {
@@ -26,6 +28,13 @@ func resourceKongConsumerPluginConfig() *schema.Resource {
2628
Required: true,
2729
ForceNew: true,
2830
},
31+
"config": &schema.Schema{
32+
Type: schema.TypeMap,
33+
ForceNew: true,
34+
Optional: true,
35+
Elem: schema.TypeString,
36+
Default: nil,
37+
},
2938
"config_json": &schema.Schema{
3039
Type: schema.TypeString,
3140
ForceNew: true,
@@ -92,14 +101,38 @@ func splitIdIntoFields(id string) (*idFields, error) {
92101
}, nil
93102
}
94103

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

97129
consumerId := readStringFromResource(d, "consumer_id")
98130
pluginName := readStringFromResource(d, "plugin_name")
99-
config := readStringFromResource(d, "config_json")
100-
131+
config, err := generatePluginConfig(readMapFromResource(d, "config"), readStringFromResource(d, "config_json"))
132+
if err != nil {
133+
return fmt.Errorf("error configuring plugin: %v", err)
134+
}
101135
consumerPluginConfig, err := meta.(*gokong.KongAdminClient).Consumers().CreatePluginConfig(consumerId, pluginName, config)
102-
103136
if err != nil {
104137
return fmt.Errorf("failed to create kong consumer plugin config, error: %v", err)
105138
}

kong/resource_kong_consumer_plugin_config_test.go

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package kong
22

33
import (
44
"fmt"
5+
"testing"
6+
57
"github.com/hashicorp/terraform/helper/resource"
68
"github.com/hashicorp/terraform/terraform"
79
"github.com/kevholditch/gokong"
8-
"testing"
910
)
1011

1112
func TestAccKongConsumerPluginConfig(t *testing.T) {
@@ -34,6 +35,32 @@ func TestAccKongConsumerPluginConfig(t *testing.T) {
3435
})
3536
}
3637

38+
func TestAccKongConsumerPluginConfigKV(t *testing.T) {
39+
40+
resource.Test(t, resource.TestCase{
41+
Providers: testAccProviders,
42+
CheckDestroy: testAccCheckKongConsumerPluginConfig,
43+
Steps: []resource.TestStep{
44+
{
45+
Config: testCreateConsumerPluginConfigKV,
46+
Check: resource.ComposeTestCheckFunc(
47+
testAccCheckKongConsumerPluginConfigExists("kong_consumer_plugin_config.consumer_acl_config"),
48+
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_acl_config", "plugin_name", "acls"),
49+
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_acl_config", "config.group", "nginx"),
50+
),
51+
},
52+
{
53+
Config: testUpdateConsumerPluginConfigKV,
54+
Check: resource.ComposeTestCheckFunc(
55+
testAccCheckKongConsumerPluginConfigExists("kong_consumer_plugin_config.consumer_acl_config"),
56+
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_acl_config", "plugin_name", "acls"),
57+
resource.TestCheckResourceAttr("kong_consumer_plugin_config.consumer_acl_config", "config.group", "apache"),
58+
),
59+
},
60+
},
61+
})
62+
}
63+
3764
func testAccCheckKongConsumerPluginConfig(state *terraform.State) error {
3865

3966
client := testAccProvider.Meta().(*gokong.KongAdminClient)
@@ -147,3 +174,81 @@ resource "kong_consumer_plugin_config" "consumer_jwt_config" {
147174
EOT
148175
}
149176
`
177+
178+
const testCreateConsumerPluginConfigKV = `
179+
resource "kong_api" "api" {
180+
name = "TestApi"
181+
hosts = [ "example.com" ]
182+
uris = [ "/example" ]
183+
methods = [ "GET", "POST" ]
184+
upstream_url = "http://localhost:4140"
185+
strip_uri = false
186+
preserve_host = false
187+
retries = 3
188+
upstream_connect_timeout = 60000
189+
upstream_send_timeout = 30000
190+
upstream_read_timeout = 10000
191+
https_only = false
192+
http_if_terminated = false
193+
}
194+
195+
resource "kong_consumer" "my_consumer" {
196+
username = "User1"
197+
custom_id = "123"
198+
}
199+
200+
resource "kong_plugin" "acl_plugin" {
201+
name = "acl"
202+
api_id = "${kong_api.api.id}"
203+
config = {
204+
whitelist = "nginx"
205+
}
206+
}
207+
208+
resource "kong_consumer_plugin_config" "consumer_acl_config" {
209+
consumer_id = "${kong_consumer.my_consumer.id}"
210+
plugin_name = "acls"
211+
config = {
212+
group = "nginx"
213+
}
214+
}
215+
`
216+
217+
const testUpdateConsumerPluginConfigKV = `
218+
resource "kong_api" "api" {
219+
name = "TestApi"
220+
hosts = [ "example.com" ]
221+
uris = [ "/example" ]
222+
methods = [ "GET", "POST" ]
223+
upstream_url = "http://localhost:4140"
224+
strip_uri = false
225+
preserve_host = false
226+
retries = 3
227+
upstream_connect_timeout = 60000
228+
upstream_send_timeout = 30000
229+
upstream_read_timeout = 10000
230+
https_only = false
231+
http_if_terminated = false
232+
}
233+
234+
resource "kong_consumer" "my_consumer" {
235+
username = "User1"
236+
custom_id = "123"
237+
}
238+
239+
resource "kong_plugin" "acl_plugin" {
240+
name = "acl"
241+
api_id = "${kong_api.api.id}"
242+
config = {
243+
whitelist = "apache"
244+
}
245+
}
246+
247+
resource "kong_consumer_plugin_config" "consumer_acl_config" {
248+
consumer_id = "${kong_consumer.my_consumer.id}"
249+
plugin_name = "acls"
250+
config = {
251+
group = "apache"
252+
}
253+
}
254+
`

0 commit comments

Comments
 (0)