Skip to content

Commit d5e1e8f

Browse files
authored
Add tags to all resources (#133)
* added tags to certificate resource * added tags to consumer resource * added tags to all resources
1 parent aae8b7c commit d5e1e8f

18 files changed

+127
-27
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ build:
1313
clean:
1414
rm -rf pkg/
1515

16+
fmt:
17+
go fmt ./...
1618

1719

1820
.PHONY: build test testacc vet goimports goimportscheck errcheck vendor-status test-compile

docs/resources/certificate.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ resource "kong_certificate" "certificate" {
99
certificate = "public key --- 123 ----"
1010
private_key = "private key --- 456 ----"
1111
snis = ["foo.com", "bar.com"]
12+
tags = ["myTag"]
1213
}
1314
```
1415

@@ -17,6 +18,7 @@ resource "kong_certificate" "certificate" {
1718
* `certificate` - (Required) should be the public key of your certificate it is mapped to the `Cert` parameter on the Kong API.
1819
* `private_key` - (Required) should be the private key of your certificate it is mapped to the `Key` parameter on the Kong API.
1920
* `snis` - (Optional) a list of SNIs (alternative hosts on the certificate), under the bonnet this will create an SNI object in kong
21+
* `snis` - (Optional) A list of strings associated with the Certificate for grouping and filtering
2022

2123
## Import
2224

docs/resources/consumer.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ The consumer resource maps directly onto the json for creating a Consumer in Kon
88
resource "kong_consumer" "consumer" {
99
username = "User1"
1010
custom_id = "123"
11+
tags = ["mySuperTag"]
1112
}
1213
```
1314

1415
## Argument Reference
1516

1617
* `username` - (Semi-optional) The username to use, you must set either the username or custom_id
1718
* `custom_id` - (Semi-optional) A custom id for the consumer, you must set either the username or custom_id
19+
* `tags` - (Optional) A list of strings associated with the Consumer for grouping and filtering
1820

1921
## Import
2022

docs/resources/consumer_acl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ resource "kong_consumer_acl" "consumer_acl" {
3030

3131
* `consumer_id` - (Required) the id of the consumer to be configured
3232
* `group` - (Required) the acl group
33-
* `tags` - (Optional) A list of strings associated with the consumer acl for grouping and filtering.
33+
* `tags` - (Optional) A list of strings associated with the consumer acl for grouping and filtering

docs/resources/consumer_basic_auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ resource "kong_consumer_basic_auth" "consumer_basic_auth" {
2727
* `consumer_id` - (Required) the id of the consumer to be configured with basic auth
2828
* `username` - (Required) username to be used for basic auth
2929
* `password` - (Required) password to be used for basic auth
30-
* `tags` - (Optional) A list of strings associated with the consumer basic auth for grouping and filtering.
30+
* `tags` - (Optional) A list of strings associated with the consumer basic auth for grouping and filtering

docs/resources/consumer_jwt_auth.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ resource "kong_consumer_jwt_auth" "consumer_jwt_config" {
3131
## Argument Reference
3232

3333
* `consumer_id` - (Required) the id of the consumer to be configured with jwt auth
34-
* `algorithm` - (Optional) The algorithm used to verify the token’s signature. Can be HS256, HS384, HS512, RS256, or ES256, Default is `HS256`.
34+
* `algorithm` - (Optional) The algorithm used to verify the token’s signature. Can be HS256, HS384, HS512, RS256, or ES256, Default is `HS256`
3535
* `key` - (Optional) A unique string identifying the credential. If left out, it will be auto-generated.
36-
* `rsa_public_key` - (Optional) If algorithm is `RS256` or `ES256`, the public key (in PEM format) to use to verify the token’s signature.
37-
* `secret` - (Optional) If algorithm is `HS256` or `ES256`, the secret used to sign JWTs for this credential. If left out, will be auto-generated.
36+
* `rsa_public_key` - (Optional) If algorithm is `RS256` or `ES256`, the public key (in PEM format) to use to verify the token’s signature
37+
* `secret` - (Optional) If algorithm is `HS256` or `ES256`, the secret used to sign JWTs for this credential. If left out, will be auto-generated
38+
* `tags` - (Optional) A list of strings associated with the consumer JWT auth for grouping and filtering

docs/resources/plugin.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ EOT
8181

8282
## Argument reference
8383

84-
`plugin_name` - (Required) the name of the plugin you want to configure
85-
`consumer_id` - (Optional) the consumer id you want to configure the plugin for
86-
`service_id` - (Optional) the service id that you want to configure the plugin for
87-
`route_id` - (Optional) the route id that you want to configure the plugin for
88-
`enabled` - (Optional) whether the plugin is enabled or not, use if you want to keep the plugin installed but disable it
89-
`config_json` - (Optional) 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
84+
* `plugin_name` - (Required) the name of the plugin you want to configure
85+
* `consumer_id` - (Optional) the consumer id you want to configure the plugin for
86+
* `service_id` - (Optional) the service id that you want to configure the plugin for
87+
* `route_id` - (Optional) the route id that you want to configure the plugin for
88+
* `enabled` - (Optional) whether the plugin is enabled or not, use if you want to keep the plugin installed but disable it
89+
* `config_json` - (Optional) 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
9090
page of the plugin you are configuring
91+
* `tags` - (Optional) A list of strings associated with the Plugin for grouping and filtering
9192

9293
## Import
9394

docs/resources/target.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ resource "kong_target" "target" {
1515
* `target` - (Required) is the target address (IP or hostname) and port. If omitted the port defaults to 8000.
1616
* `weight` - (Required) is the weight this target gets within the upstream load balancer (0-1000, defaults to 100).
1717
* `upstream_id` - (Required) is the id of the upstream to apply this target to.
18+
* `tags` - (Optional) A list set of strings associated with the Plugin for grouping and filtering
1819

1920
## Import
2021

kong/resource_kong_certificate.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func resourceKongCertificate() *schema.Resource {
3838
ForceNew: false,
3939
Elem: &schema.Schema{Type: schema.TypeString},
4040
},
41+
"tags": {
42+
Type: schema.TypeList,
43+
Optional: true,
44+
ForceNew: false,
45+
Elem: &schema.Schema{Type: schema.TypeString},
46+
},
4147
},
4248
}
4349
}
4450

4551
func resourceKongCertificateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4652

47-
certificateRequest := &kong.Certificate{
48-
Cert: kong.String(d.Get("certificate").(string)),
49-
Key: kong.String(d.Get("private_key").(string)),
50-
SNIs: readStringArrayPtrFromResource(d, "snis"),
51-
}
52-
53+
certificateRequest := buildCertificateRequestFromResourceData(d)
5354
client := meta.(*config).adminClient.Certificates
54-
5555
certificate, err := client.Create(ctx, certificateRequest)
5656

5757
if err != nil {
@@ -66,15 +66,10 @@ func resourceKongCertificateCreate(ctx context.Context, d *schema.ResourceData,
6666
func resourceKongCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6767
d.Partial(false)
6868

69-
certificateRequest := &kong.Certificate{
70-
ID: kong.String(d.Id()),
71-
Cert: kong.String(d.Get("certificate").(string)),
72-
Key: kong.String(d.Get("private_key").(string)),
73-
SNIs: readStringArrayPtrFromResource(d, "snis"),
74-
}
69+
certificateRequest := buildCertificateRequestFromResourceData(d)
70+
certificateRequest.ID = kong.String(d.Id())
7571

7672
client := meta.(*config).adminClient.Certificates
77-
7873
_, err := client.Update(ctx, certificateRequest)
7974

8075
if err != nil {
@@ -84,6 +79,16 @@ func resourceKongCertificateUpdate(ctx context.Context, d *schema.ResourceData,
8479
return resourceKongCertificateRead(ctx, d, meta)
8580
}
8681

82+
func buildCertificateRequestFromResourceData(d *schema.ResourceData) *kong.Certificate {
83+
certificateRequest := &kong.Certificate{
84+
Cert: kong.String(d.Get("certificate").(string)),
85+
Key: kong.String(d.Get("private_key").(string)),
86+
SNIs: readStringArrayPtrFromResource(d, "snis"),
87+
Tags: readStringArrayPtrFromResource(d, "tags"),
88+
}
89+
return certificateRequest
90+
}
91+
8792
func resourceKongCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8893

8994
var diags diag.Diagnostics
@@ -118,6 +123,10 @@ func resourceKongCertificateRead(ctx context.Context, d *schema.ResourceData, me
118123
return diag.FromErr(err)
119124
}
120125
}
126+
err = d.Set("tags", certificate.Tags)
127+
if err != nil {
128+
return diag.FromErr(err)
129+
}
121130
}
122131

123132
return diags

kong/resource_kong_certificate_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func TestAccKongCertificate(t *testing.T) {
2222
testAccCheckKongCertificateExists("kong_certificate.certificate"),
2323
resource.TestCheckResourceAttr("kong_certificate.certificate", "certificate", testCert1+"\n"),
2424
resource.TestCheckResourceAttr("kong_certificate.certificate", "private_key", testKey1+"\n"),
25+
resource.TestCheckResourceAttr("kong_certificate.certificate", "tags.#", "2"),
26+
resource.TestCheckResourceAttr("kong_certificate.certificate", "tags.0", "a"),
27+
resource.TestCheckResourceAttr("kong_certificate.certificate", "tags.1", "b"),
2528
),
2629
},
2730
{
@@ -30,6 +33,8 @@ func TestAccKongCertificate(t *testing.T) {
3033
testAccCheckKongCertificateExists("kong_certificate.certificate"),
3134
resource.TestCheckResourceAttr("kong_certificate.certificate", "certificate", testCert2+"\n"),
3235
resource.TestCheckResourceAttr("kong_certificate.certificate", "private_key", testKey2+"\n"),
36+
resource.TestCheckResourceAttr("kong_certificate.certificate", "tags.#", "1"),
37+
resource.TestCheckResourceAttr("kong_certificate.certificate", "tags.0", "a"),
3338
),
3439
},
3540
},
@@ -116,6 +121,7 @@ EOF
116121
%s
117122
EOF
118123
snis = ["foo.com"]
124+
tags = ["a", "b"]
119125
}
120126
`
121127
const testUpdateCertificateConfig = `
@@ -127,6 +133,7 @@ EOF
127133
%s
128134
EOF
129135
snis = ["foo.com"]
136+
tags = ["a"]
130137
}
131138
`
132139

kong/resource_kong_consumer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ func resourceKongConsumer() *schema.Resource {
3030
Optional: true,
3131
ForceNew: false,
3232
},
33+
"tags": {
34+
Type: schema.TypeList,
35+
Optional: true,
36+
ForceNew: false,
37+
Elem: &schema.Schema{Type: schema.TypeString},
38+
},
3339
},
3440
}
3541
}
@@ -39,6 +45,7 @@ func resourceKongConsumerCreate(ctx context.Context, d *schema.ResourceData, met
3945
consumerRequest := &kong.Consumer{
4046
Username: kong.String(d.Get("username").(string)),
4147
CustomID: kong.String(d.Get("custom_id").(string)),
48+
Tags: readStringArrayPtrFromResource(d, "tags"),
4249
}
4350

4451
client := meta.(*config).adminClient.Consumers
@@ -60,6 +67,7 @@ func resourceKongConsumerUpdate(ctx context.Context, d *schema.ResourceData, met
6067
ID: kong.String(d.Id()),
6168
Username: kong.String(d.Get("username").(string)),
6269
CustomID: kong.String(d.Get("custom_id").(string)),
70+
Tags: readStringArrayPtrFromResource(d, "tags"),
6371
}
6472

6573
client := meta.(*config).adminClient.Consumers
@@ -97,6 +105,10 @@ func resourceKongConsumerRead(ctx context.Context, d *schema.ResourceData, meta
97105
if err != nil {
98106
return diag.FromErr(err)
99107
}
108+
err = d.Set("tags", consumer.Tags)
109+
if err != nil {
110+
return diag.FromErr(err)
111+
}
100112
}
101113

102114
return diags

kong/resource_kong_consumer_jwt_auth.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func resourceKongConsumerJWTAuth() *schema.Resource {
4545
Optional: true,
4646
ForceNew: false,
4747
},
48+
"tags": {
49+
Type: schema.TypeList,
50+
Optional: true,
51+
ForceNew: false,
52+
Elem: &schema.Schema{Type: schema.TypeString},
53+
},
4854
},
4955
}
5056
}
@@ -56,6 +62,7 @@ func resourceKongConsumerJWTAuthCreate(ctx context.Context, d *schema.ResourceDa
5662
Key: kong.String(d.Get("key").(string)),
5763
RSAPublicKey: kong.String(d.Get("rsa_public_key").(string)),
5864
Secret: kong.String(d.Get("secret").(string)),
65+
Tags: readStringArrayPtrFromResource(d, "tags"),
5966
}
6067

6168
consumerId := kong.String(d.Get("consumer_id").(string))
@@ -100,6 +107,7 @@ func resourceKongConsumerJWTAuthUpdate(ctx context.Context, d *schema.ResourceDa
100107
Key: kong.String(d.Get("key").(string)),
101108
RSAPublicKey: kong.String(d.Get("rsa_public_key").(string)),
102109
Secret: kong.String(d.Get("secret").(string)),
110+
Tags: readStringArrayPtrFromResource(d, "tags"),
103111
}
104112

105113
consumerId := kong.String(d.Get("consumer_id").(string))
@@ -153,6 +161,10 @@ func resourceKongConsumerJWTAuthRead(ctx context.Context, d *schema.ResourceData
153161
if err != nil {
154162
return diag.FromErr(err)
155163
}
164+
err = d.Set("tags", JWTAuth.Tags)
165+
if err != nil {
166+
return diag.FromErr(err)
167+
}
156168
}
157169

158170
return diags

kong/resource_kong_consumer_jwt_auth_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func TestAccJWTAuth(t *testing.T) {
2424
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "key", "my_key"),
2525
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "secret", "my_secret"),
2626
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "rsa_public_key", "foo"),
27+
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.#", "2"),
28+
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.0", "foo"),
29+
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.1", "bar"),
2730
),
2831
},
2932
{
@@ -34,6 +37,8 @@ func TestAccJWTAuth(t *testing.T) {
3437
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "key", "updated_key"),
3538
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "secret", "updated_secret"),
3639
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "rsa_public_key", "bar"),
40+
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.#", "1"),
41+
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.0", "foo"),
3742
),
3843
},
3944
},
@@ -133,6 +138,7 @@ resource "kong_consumer_jwt_auth" "consumer_jwt_config" {
133138
key = "my_key"
134139
rsa_public_key = "foo"
135140
secret = "my_secret"
141+
tags = ["foo", "bar"]
136142
}
137143
`
138144
const testUpdateJWTAuthConfig = `
@@ -156,5 +162,6 @@ resource "kong_consumer_jwt_auth" "consumer_jwt_config" {
156162
key = "updated_key"
157163
rsa_public_key = "bar"
158164
secret = "updated_secret"
165+
tags = ["foo"]
159166
}
160167
`

kong/resource_kong_consumer_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func TestAccKongConsumer(t *testing.T) {
2222
testAccCheckKongConsumerExists("kong_consumer.consumer"),
2323
resource.TestCheckResourceAttr("kong_consumer.consumer", "username", "User1"),
2424
resource.TestCheckResourceAttr("kong_consumer.consumer", "custom_id", "123"),
25+
resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.#", "2"),
26+
resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.0", "a"),
27+
resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.1", "b"),
2528
),
2629
},
2730
{
@@ -30,6 +33,8 @@ func TestAccKongConsumer(t *testing.T) {
3033
testAccCheckKongConsumerExists("kong_consumer.consumer"),
3134
resource.TestCheckResourceAttr("kong_consumer.consumer", "username", "User2"),
3235
resource.TestCheckResourceAttr("kong_consumer.consumer", "custom_id", "456"),
36+
resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.#", "1"),
37+
resource.TestCheckResourceAttr("kong_consumer.consumer", "tags.0", "a"),
3338
),
3439
},
3540
},
@@ -111,11 +116,13 @@ const testCreateConsumerConfig = `
111116
resource "kong_consumer" "consumer" {
112117
username = "User1"
113118
custom_id = "123"
119+
tags = ["a", "b"]
114120
}
115121
`
116122
const testUpdateConsumerConfig = `
117123
resource "kong_consumer" "consumer" {
118124
username = "User2"
119125
custom_id = "456"
126+
tags = ["a"]
120127
}
121128
`

kong/resource_kong_plugin.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func resourceKongPlugin() *schema.Resource {
6969
Type: schema.TypeString,
7070
Computed: true,
7171
},
72+
"tags": {
73+
Type: schema.TypeList,
74+
Optional: true,
75+
ForceNew: false,
76+
Elem: &schema.Schema{Type: schema.TypeString},
77+
},
7278
},
7379
}
7480
}
@@ -148,6 +154,10 @@ func resourceKongPluginRead(ctx context.Context, d *schema.ResourceData, meta in
148154
if err != nil {
149155
return diag.FromErr(err)
150156
}
157+
err = d.Set("tags", plugin.Tags)
158+
if err != nil {
159+
return diag.FromErr(err)
160+
}
151161

152162
// We sync this property from upstream as a method to allow you to import a resource with the config tracked in
153163
// terraform state. We do not track `config` as it will be a source of a perpetual diff.
@@ -225,6 +235,7 @@ func createKongPluginRequestFromResourceData(d *schema.ResourceData) (*kong.Plug
225235

226236
pluginRequest.Name = readStringPtrFromResource(d, "name")
227237
pluginRequest.Enabled = readBoolPtrFromResource(d, "enabled")
238+
pluginRequest.Tags = readStringArrayPtrFromResource(d, "tags")
228239

229240
if data, ok := d.GetOk("config_json"); ok {
230241
var configJSON map[string]interface{}

0 commit comments

Comments
 (0)