Skip to content

Commit c689e4d

Browse files
Implement kong_consumer_key_auth resource
By adapting the previously copied `kong_consumer_basic_auth` resource. Note that the TTL property is deliberately omitted from the resource, because the value is not stable - when retrieved from Kong the value returned is the remaining TTL based on when it was created, so it's not very well suited to Terraforming. Possibly it could be a computed value, but will be left out for now.
1 parent 0393312 commit c689e4d

File tree

3 files changed

+80
-88
lines changed

3 files changed

+80
-88
lines changed

kong/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func Provider() *schema.Provider {
7171
"kong_consumer": resourceKongConsumer(),
7272
"kong_consumer_acl": resourceKongConsumerACL(),
7373
"kong_consumer_basic_auth": resourceKongConsumerBasicAuth(),
74+
"kong_consumer_key_auth": resourceKongConsumerKeyAuth(),
7475
"kong_plugin": resourceKongPlugin(),
7576
"kong_upstream": resourceKongUpstream(),
7677
"kong_target": resourceKongTarget(),

kong/resource_kong_consumer_key_auth.go

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@ import (
99
"github.com/kong/go-kong/kong"
1010
)
1111

12-
func resourceKongConsumerBasicAuth() *schema.Resource {
12+
func resourceKongConsumerKeyAuth() *schema.Resource {
1313
return &schema.Resource{
14-
CreateContext: resourceKongConsumerBasicAuthCreate,
15-
ReadContext: resourceKongConsumerBasicAuthRead,
16-
DeleteContext: resourceKongConsumerBasicAuthDelete,
17-
UpdateContext: resourceKongConsumerBasicAuthUpdate,
14+
CreateContext: resourceKongConsumerKeyAuthCreate,
15+
ReadContext: resourceKongConsumerKeyAuthRead,
16+
DeleteContext: resourceKongConsumerKeyAuthDelete,
17+
UpdateContext: resourceKongConsumerKeyAuthUpdate,
1818
Schema: map[string]*schema.Schema{
1919
"consumer_id": {
2020
Type: schema.TypeString,
2121
Required: true,
2222
ForceNew: false,
2323
},
24-
"username": {
25-
Type: schema.TypeString,
26-
Required: true,
27-
ForceNew: false,
28-
},
29-
"password": {
30-
Type: schema.TypeString,
31-
Required: true,
32-
ForceNew: false,
24+
"key": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
ForceNew: false,
28+
Sensitive: true,
3329
},
3430
"tags": {
3531
Type: schema.TypeList,
@@ -41,77 +37,75 @@ func resourceKongConsumerBasicAuth() *schema.Resource {
4137
}
4238
}
4339

44-
func resourceKongConsumerBasicAuthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45-
BasicAuthRequest := &kong.BasicAuth{
46-
Username: kong.String(d.Get("username").(string)),
47-
Password: kong.String(d.Get("password").(string)),
48-
Tags: readStringArrayPtrFromResource(d, "tags"),
40+
func resourceKongConsumerKeyAuthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
41+
KeyAuthRequest := &kong.KeyAuth{
42+
Key: readStringPtrFromResource(d, "key"),
43+
Tags: readStringArrayPtrFromResource(d, "tags"),
4944
}
5045

5146
consumerId := kong.String(d.Get("consumer_id").(string))
5247

53-
client := meta.(*config).adminClient.BasicAuths
54-
basicAuth, err := client.Create(ctx, consumerId, BasicAuthRequest)
48+
client := meta.(*config).adminClient.KeyAuths
49+
keyAuth, err := client.Create(ctx, consumerId, KeyAuthRequest)
5550

5651
if err != nil {
57-
return diag.FromErr(fmt.Errorf("failed to create kong basic auth: %v error: %v", BasicAuthRequest, err))
52+
return diag.FromErr(fmt.Errorf("failed to create kong key auth: %v error: %v", KeyAuthRequest, err))
5853
}
5954

60-
d.SetId(buildConsumerPairID(*basicAuth.ID, *consumerId))
55+
d.SetId(buildConsumerPairID(*keyAuth.ID, *consumerId))
6156

62-
return resourceKongConsumerBasicAuthRead(ctx, d, meta)
57+
return resourceKongConsumerKeyAuthRead(ctx, d, meta)
6358
}
6459

65-
func resourceKongConsumerBasicAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
60+
func resourceKongConsumerKeyAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6661
id, err := splitConsumerID(d.Id())
6762

68-
BasicAuthRequest := &kong.BasicAuth{
69-
ID: kong.String(id.ID),
70-
Username: kong.String(d.Get("username").(string)),
71-
Password: kong.String(d.Get("password").(string)),
72-
Tags: readStringArrayPtrFromResource(d, "tags"),
63+
KeyAuthRequest := &kong.KeyAuth{
64+
ID: kong.String(id.ID),
65+
Key: readStringPtrFromResource(d, "key"),
66+
Tags: readStringArrayPtrFromResource(d, "tags"),
7367
}
7468

7569
consumerId := kong.String(d.Get("consumer_id").(string))
7670

77-
client := meta.(*config).adminClient.BasicAuths
78-
_, err = client.Update(ctx, consumerId, BasicAuthRequest)
71+
client := meta.(*config).adminClient.KeyAuths
72+
_, err = client.Update(ctx, consumerId, KeyAuthRequest)
7973

8074
if err != nil {
81-
return diag.FromErr(fmt.Errorf("error updating kong basic auth: %s", err))
75+
return diag.FromErr(fmt.Errorf("error updating kong key auth: %s", err))
8276
}
8377

84-
return resourceKongConsumerBasicAuthRead(ctx, d, meta)
78+
return resourceKongConsumerKeyAuthRead(ctx, d, meta)
8579
}
8680

87-
func resourceKongConsumerBasicAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
81+
func resourceKongConsumerKeyAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8882
var diags diag.Diagnostics
8983
id, err := splitConsumerID(d.Id())
9084
if err != nil {
9185
return diag.FromErr(err)
9286
}
9387

94-
client := meta.(*config).adminClient.BasicAuths
95-
basicAuth, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID))
88+
client := meta.(*config).adminClient.KeyAuths
89+
keyAuth, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID))
9690

9791
if kong.IsNotFoundErr(err) {
9892
d.SetId("")
9993
} else if err != nil {
100-
return diag.FromErr(fmt.Errorf("could not find kong ACLGroup with id: %s error: %v", id, err))
94+
return diag.FromErr(fmt.Errorf("could not find kong key auth with id: %s error: %v", id, err))
10195
}
10296

103-
if basicAuth == nil {
97+
if keyAuth == nil {
10498
d.SetId("")
10599
} else {
106-
err = d.Set("consumer_id", basicAuth.Consumer.ID)
100+
err = d.Set("consumer_id", keyAuth.Consumer.ID)
107101
if err != nil {
108102
return diag.FromErr(err)
109103
}
110-
err = d.Set("username", basicAuth.Username)
104+
err = d.Set("key", keyAuth.Key)
111105
if err != nil {
112106
return diag.FromErr(err)
113107
}
114-
err = d.Set("tags", basicAuth.Tags)
108+
err = d.Set("tags", keyAuth.Tags)
115109
if err != nil {
116110
return diag.FromErr(err)
117111
}
@@ -120,17 +114,17 @@ func resourceKongConsumerBasicAuthRead(ctx context.Context, d *schema.ResourceDa
120114
return diags
121115
}
122116

123-
func resourceKongConsumerBasicAuthDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
117+
func resourceKongConsumerKeyAuthDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
124118
var diags diag.Diagnostics
125119
id, err := splitConsumerID(d.Id())
126120
if err != nil {
127121
return diag.FromErr(err)
128122
}
129-
client := meta.(*config).adminClient.BasicAuths
123+
client := meta.(*config).adminClient.KeyAuths
130124
err = client.Delete(ctx, kong.String(id.ConsumerID), kong.String(id.ID))
131125

132126
if err != nil {
133-
return diag.FromErr(fmt.Errorf("could not delete kong basic auth: %v", err))
127+
return diag.FromErr(fmt.Errorf("could not delete kong key auth: %v", err))
134128
}
135129

136130
return diags

kong/resource_kong_consumer_key_auth_test.go

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,65 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/kong/go-kong/kong"
9-
108
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
119
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
"github.com/kong/go-kong/kong"
1211
)
1312

14-
func TestAccConsumerBasicAuth(t *testing.T) {
13+
func TestAccConsumerKeyAuth(t *testing.T) {
1514

1615
resource.Test(t, resource.TestCase{
1716
Providers: testAccProviders,
18-
CheckDestroy: testAccCheckConsumerBasicAuthDestroy,
17+
CheckDestroy: testAccCheckConsumerKeyAuthDestroy,
1918
Steps: []resource.TestStep{
2019
{
21-
Config: testCreateConsumerBasicAuthConfig,
20+
Config: testCreateConsumerKeyAuthConfig,
2221
Check: resource.ComposeTestCheckFunc(
23-
testAccCheckConsumerBasicAuthExists("kong_consumer_basic_auth.consumer_basic_auth"),
24-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "username", "foo"),
25-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "tags.#", "1"),
26-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "tags.0", "myTag"),
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"),
2726
),
2827
},
2928
{
30-
Config: testUpdateConsumerBasicAuthConfig,
29+
Config: testUpdateConsumerKeyAuthConfig,
3130
Check: resource.ComposeTestCheckFunc(
32-
testAccCheckConsumerBasicAuthExists("kong_consumer_basic_auth.consumer_basic_auth"),
33-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "username", "foo_updated"),
34-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "tags.#", "2"),
35-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "tags.0", "myTag"),
36-
resource.TestCheckResourceAttr("kong_consumer_basic_auth.consumer_basic_auth", "tags.1", "anotherTag"),
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"),
3736
),
3837
},
3938
},
4039
})
4140
}
4241

43-
func testAccCheckConsumerBasicAuthDestroy(state *terraform.State) error {
42+
func testAccCheckConsumerKeyAuthDestroy(state *terraform.State) error {
4443

45-
client := testAccProvider.Meta().(*config).adminClient.BasicAuths
44+
client := testAccProvider.Meta().(*config).adminClient.KeyAuths
4645

47-
resources := getResourcesByType("kong_consumer_basic_auth", state)
46+
resources := getResourcesByType("kong_consumer_key_auth", state)
4847

4948
if len(resources) != 1 {
50-
return fmt.Errorf("expecting only 1 consumer basic auth resource found %v", len(resources))
49+
return fmt.Errorf("expecting only 1 consumer key auth resource found %v", len(resources))
5150
}
5251

5352
id, err := splitConsumerID(resources[0].Primary.ID)
54-
ConsumerBasicAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
53+
ConsumerKeyAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
5554

5655
if !kong.IsNotFoundErr(err) && err != nil {
5756
return fmt.Errorf("error calling get consumer auth by id: %v", err)
5857
}
5958

60-
if ConsumerBasicAuth != nil {
61-
return fmt.Errorf("jwt auth %s still exists, %+v", id.ID, ConsumerBasicAuth)
59+
if ConsumerKeyAuth != nil {
60+
return fmt.Errorf("key auth %s still exists, %+v", id.ID, ConsumerKeyAuth)
6261
}
6362

6463
return nil
6564
}
6665

67-
func testAccCheckConsumerBasicAuthExists(resourceKey string) resource.TestCheckFunc {
66+
func testAccCheckConsumerKeyAuthExists(resourceKey string) resource.TestCheckFunc {
6867

6968
return func(s *terraform.State) error {
7069
rs, ok := s.RootModule().Resources[resourceKey]
@@ -77,54 +76,52 @@ func testAccCheckConsumerBasicAuthExists(resourceKey string) resource.TestCheckF
7776
return fmt.Errorf("no ID is set")
7877
}
7978

80-
client := testAccProvider.Meta().(*config).adminClient.BasicAuths
79+
client := testAccProvider.Meta().(*config).adminClient.KeyAuths
8180
id, err := splitConsumerID(rs.Primary.ID)
8281

83-
ConsumerBasicAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
82+
ConsumerKeyAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
8483

8584
if err != nil {
8685
return err
8786
}
8887

89-
if ConsumerBasicAuth == nil {
90-
return fmt.Errorf("ConsumerBasicAuth with id %v not found", id.ID)
88+
if ConsumerKeyAuth == nil {
89+
return fmt.Errorf("ConsumerKeyAuth with id %v not found", id.ID)
9190
}
9291

9392
return nil
9493
}
9594
}
9695

97-
const testCreateConsumerBasicAuthConfig = `
96+
const testCreateConsumerKeyAuthConfig = `
9897
resource "kong_consumer" "my_consumer" {
9998
username = "User1"
10099
custom_id = "123"
101100
}
102101
103-
resource "kong_plugin" "basic_auth_plugin" {
104-
name = "basic-auth"
102+
resource "kong_plugin" "key_auth_plugin" {
103+
name = "key-auth"
105104
}
106105
107-
resource "kong_consumer_basic_auth" "consumer_basic_auth" {
108-
consumer_id = "${kong_consumer.my_consumer.id}"
109-
username = "foo"
110-
password = "bar"
111-
tags = ["myTag"]
106+
resource "kong_consumer_key_auth" "consumer_key_auth" {
107+
consumer_id = "${kong_consumer.my_consumer.id}"
108+
key = "foo"
109+
tags = ["myTag"]
112110
}
113111
`
114-
const testUpdateConsumerBasicAuthConfig = `
112+
const testUpdateConsumerKeyAuthConfig = `
115113
resource "kong_consumer" "my_consumer" {
116114
username = "User1"
117115
custom_id = "123"
118116
}
119117
120-
resource "kong_plugin" "basic_auth_plugin" {
121-
name = "basic-auth"
118+
resource "kong_plugin" "key_auth_plugin" {
119+
name = "key-auth"
122120
}
123121
124-
resource "kong_consumer_basic_auth" "consumer_basic_auth" {
125-
consumer_id = "${kong_consumer.my_consumer.id}"
126-
username = "foo_updated"
127-
password = "bar_updated"
128-
tags = ["myTag", "anotherTag"]
122+
resource "kong_consumer_key_auth" "consumer_key_auth" {
123+
consumer_id = "${kong_consumer.my_consumer.id}"
124+
key = "foo_updated"
125+
tags = ["myTag", "anotherTag"]
129126
}
130127
`

0 commit comments

Comments
 (0)