Skip to content

Commit 0393312

Browse files
Add kong_consumer_key_auth resource files
This new resource is similar to `kong_consumer_basic_auth` so is being based on that. Create direct copies of the resource file and its tests for now so diffs to these files can be seen more clearly in the next commit.
1 parent ba239a1 commit 0393312

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package kong
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/kong/go-kong/kong"
10+
)
11+
12+
func resourceKongConsumerBasicAuth() *schema.Resource {
13+
return &schema.Resource{
14+
CreateContext: resourceKongConsumerBasicAuthCreate,
15+
ReadContext: resourceKongConsumerBasicAuthRead,
16+
DeleteContext: resourceKongConsumerBasicAuthDelete,
17+
UpdateContext: resourceKongConsumerBasicAuthUpdate,
18+
Schema: map[string]*schema.Schema{
19+
"consumer_id": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
ForceNew: false,
23+
},
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,
33+
},
34+
"tags": {
35+
Type: schema.TypeList,
36+
Optional: true,
37+
ForceNew: false,
38+
Elem: &schema.Schema{Type: schema.TypeString},
39+
},
40+
},
41+
}
42+
}
43+
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"),
49+
}
50+
51+
consumerId := kong.String(d.Get("consumer_id").(string))
52+
53+
client := meta.(*config).adminClient.BasicAuths
54+
basicAuth, err := client.Create(ctx, consumerId, BasicAuthRequest)
55+
56+
if err != nil {
57+
return diag.FromErr(fmt.Errorf("failed to create kong basic auth: %v error: %v", BasicAuthRequest, err))
58+
}
59+
60+
d.SetId(buildConsumerPairID(*basicAuth.ID, *consumerId))
61+
62+
return resourceKongConsumerBasicAuthRead(ctx, d, meta)
63+
}
64+
65+
func resourceKongConsumerBasicAuthUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
66+
id, err := splitConsumerID(d.Id())
67+
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"),
73+
}
74+
75+
consumerId := kong.String(d.Get("consumer_id").(string))
76+
77+
client := meta.(*config).adminClient.BasicAuths
78+
_, err = client.Update(ctx, consumerId, BasicAuthRequest)
79+
80+
if err != nil {
81+
return diag.FromErr(fmt.Errorf("error updating kong basic auth: %s", err))
82+
}
83+
84+
return resourceKongConsumerBasicAuthRead(ctx, d, meta)
85+
}
86+
87+
func resourceKongConsumerBasicAuthRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
var diags diag.Diagnostics
89+
id, err := splitConsumerID(d.Id())
90+
if err != nil {
91+
return diag.FromErr(err)
92+
}
93+
94+
client := meta.(*config).adminClient.BasicAuths
95+
basicAuth, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID))
96+
97+
if kong.IsNotFoundErr(err) {
98+
d.SetId("")
99+
} else if err != nil {
100+
return diag.FromErr(fmt.Errorf("could not find kong ACLGroup with id: %s error: %v", id, err))
101+
}
102+
103+
if basicAuth == nil {
104+
d.SetId("")
105+
} else {
106+
err = d.Set("consumer_id", basicAuth.Consumer.ID)
107+
if err != nil {
108+
return diag.FromErr(err)
109+
}
110+
err = d.Set("username", basicAuth.Username)
111+
if err != nil {
112+
return diag.FromErr(err)
113+
}
114+
err = d.Set("tags", basicAuth.Tags)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
}
119+
120+
return diags
121+
}
122+
123+
func resourceKongConsumerBasicAuthDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
124+
var diags diag.Diagnostics
125+
id, err := splitConsumerID(d.Id())
126+
if err != nil {
127+
return diag.FromErr(err)
128+
}
129+
client := meta.(*config).adminClient.BasicAuths
130+
err = client.Delete(ctx, kong.String(id.ConsumerID), kong.String(id.ID))
131+
132+
if err != nil {
133+
return diag.FromErr(fmt.Errorf("could not delete kong basic auth: %v", err))
134+
}
135+
136+
return diags
137+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package kong
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/kong/go-kong/kong"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
)
13+
14+
func TestAccConsumerBasicAuth(t *testing.T) {
15+
16+
resource.Test(t, resource.TestCase{
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckConsumerBasicAuthDestroy,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testCreateConsumerBasicAuthConfig,
22+
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"),
27+
),
28+
},
29+
{
30+
Config: testUpdateConsumerBasicAuthConfig,
31+
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"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
func testAccCheckConsumerBasicAuthDestroy(state *terraform.State) error {
44+
45+
client := testAccProvider.Meta().(*config).adminClient.BasicAuths
46+
47+
resources := getResourcesByType("kong_consumer_basic_auth", state)
48+
49+
if len(resources) != 1 {
50+
return fmt.Errorf("expecting only 1 consumer basic auth resource found %v", len(resources))
51+
}
52+
53+
id, err := splitConsumerID(resources[0].Primary.ID)
54+
ConsumerBasicAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
55+
56+
if !kong.IsNotFoundErr(err) && err != nil {
57+
return fmt.Errorf("error calling get consumer auth by id: %v", err)
58+
}
59+
60+
if ConsumerBasicAuth != nil {
61+
return fmt.Errorf("jwt auth %s still exists, %+v", id.ID, ConsumerBasicAuth)
62+
}
63+
64+
return nil
65+
}
66+
67+
func testAccCheckConsumerBasicAuthExists(resourceKey string) resource.TestCheckFunc {
68+
69+
return func(s *terraform.State) error {
70+
rs, ok := s.RootModule().Resources[resourceKey]
71+
72+
if !ok {
73+
return fmt.Errorf("not found: %s", resourceKey)
74+
}
75+
76+
if rs.Primary.ID == "" {
77+
return fmt.Errorf("no ID is set")
78+
}
79+
80+
client := testAccProvider.Meta().(*config).adminClient.BasicAuths
81+
id, err := splitConsumerID(rs.Primary.ID)
82+
83+
ConsumerBasicAuth, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
84+
85+
if err != nil {
86+
return err
87+
}
88+
89+
if ConsumerBasicAuth == nil {
90+
return fmt.Errorf("ConsumerBasicAuth with id %v not found", id.ID)
91+
}
92+
93+
return nil
94+
}
95+
}
96+
97+
const testCreateConsumerBasicAuthConfig = `
98+
resource "kong_consumer" "my_consumer" {
99+
username = "User1"
100+
custom_id = "123"
101+
}
102+
103+
resource "kong_plugin" "basic_auth_plugin" {
104+
name = "basic-auth"
105+
}
106+
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"]
112+
}
113+
`
114+
const testUpdateConsumerBasicAuthConfig = `
115+
resource "kong_consumer" "my_consumer" {
116+
username = "User1"
117+
custom_id = "123"
118+
}
119+
120+
resource "kong_plugin" "basic_auth_plugin" {
121+
name = "basic-auth"
122+
}
123+
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"]
129+
}
130+
`

0 commit comments

Comments
 (0)