Skip to content

Commit

Permalink
create plugin tests for all apis and consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevholditch committed Nov 30, 2017
1 parent c06a67a commit 3c27ac5
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 62 deletions.
1 change: 1 addition & 0 deletions kong/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Provider() terraform.ResourceProvider {
"kong_api": resourceKongApi(),
"kong_certificate": resourceKongCertificate(),
"kong_consumer": resourceKongConsumer(),
"kong_plugin": resourceKongPlugin(),
},

ConfigureFunc: providerConfigure,
Expand Down
20 changes: 4 additions & 16 deletions kong/resource_kong_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ func resourceKongApi() *schema.Resource {
}

func resourceKongApiCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

apiRequest := createKongApiRequestFromResourceData(d)

api, err := client.Apis().Create(apiRequest)
api, err := meta.(*gokong.KongAdminClient).Apis().Create(apiRequest)

if err != nil {
return fmt.Errorf("failed to create kong api: %v error: %v", apiRequest, err)
Expand All @@ -112,13 +111,9 @@ func resourceKongApiCreate(d *schema.ResourceData, meta interface{}) error {
func resourceKongApiUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)

client := meta.(*gokong.KongAdminClient)

apiRequest := createKongApiRequestFromResourceData(d)

id := d.Id()

_, err := client.Apis().UpdateById(id, apiRequest)
_, err := meta.(*gokong.KongAdminClient).Apis().UpdateById(d.Id(), apiRequest)

if err != nil {
return fmt.Errorf("error updating kong api: %s", err)
Expand All @@ -128,11 +123,8 @@ func resourceKongApiUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceKongApiRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

id := d.Id()

api, err := client.Apis().GetById(id)
api, err := meta.(*gokong.KongAdminClient).Apis().GetById(d.Id())

if err != nil {
return fmt.Errorf("could not find kong api: %v", err)
Expand All @@ -157,11 +149,7 @@ func resourceKongApiRead(d *schema.ResourceData, meta interface{}) error {

func resourceKongApiDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*gokong.KongAdminClient)

id := d.Id()

err := client.Apis().DeleteById(id)
err := meta.(*gokong.KongAdminClient).Apis().DeleteById(d.Id())

if err != nil {
return fmt.Errorf("could not delete kong api: %v", err)
Expand Down
20 changes: 4 additions & 16 deletions kong/resource_kong_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func resourceKongCertificate() *schema.Resource {
}

func resourceKongCertificateCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

certificateRequest := createKongCertificateRequestFromResourceData(d)

consumer, err := client.Certificates().Create(certificateRequest)
consumer, err := meta.(*gokong.KongAdminClient).Certificates().Create(certificateRequest)

if err != nil {
return fmt.Errorf("failed to create kong certificate: %v error: %v", certificateRequest, err)
Expand All @@ -47,13 +46,9 @@ func resourceKongCertificateCreate(d *schema.ResourceData, meta interface{}) err
func resourceKongCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)

client := meta.(*gokong.KongAdminClient)

certificateRequest := createKongCertificateRequestFromResourceData(d)

id := d.Id()

_, err := client.Certificates().UpdateById(id, certificateRequest)
_, err := meta.(*gokong.KongAdminClient).Certificates().UpdateById(d.Id(), certificateRequest)

if err != nil {
return fmt.Errorf("error updating kong certificate: %s", err)
Expand All @@ -63,11 +58,8 @@ func resourceKongCertificateUpdate(d *schema.ResourceData, meta interface{}) err
}

func resourceKongCertificateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

id := d.Id()

certificate, err := client.Certificates().GetById(id)
certificate, err := meta.(*gokong.KongAdminClient).Certificates().GetById(d.Id())

if err != nil {
return fmt.Errorf("could not find kong certificate: %v", err)
Expand All @@ -81,11 +73,7 @@ func resourceKongCertificateRead(d *schema.ResourceData, meta interface{}) error

func resourceKongCertificateDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*gokong.KongAdminClient)

id := d.Id()

err := client.Consumers().DeleteById(id)
err := meta.(*gokong.KongAdminClient).Consumers().DeleteById(d.Id())

if err != nil {
return fmt.Errorf("could not delete kong certificate: %v", err)
Expand Down
20 changes: 4 additions & 16 deletions kong/resource_kong_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func resourceKongConsumer() *schema.Resource {
}

func resourceKongConsumerCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

consumerRequest := createKongConsumerRequestFromResourceData(d)

consumer, err := client.Consumers().Create(consumerRequest)
consumer, err := meta.(*gokong.KongAdminClient).Consumers().Create(consumerRequest)

if err != nil {
return fmt.Errorf("failed to create kong consumer: %v error: %v", consumerRequest, err)
Expand All @@ -47,13 +46,9 @@ func resourceKongConsumerCreate(d *schema.ResourceData, meta interface{}) error
func resourceKongConsumerUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)

client := meta.(*gokong.KongAdminClient)

consumerRequest := createKongConsumerRequestFromResourceData(d)

id := d.Id()

_, err := client.Consumers().UpdateById(id, consumerRequest)
_, err := meta.(*gokong.KongAdminClient).Consumers().UpdateById(d.Id(), consumerRequest)

if err != nil {
return fmt.Errorf("error updating kong consumer: %s", err)
Expand All @@ -63,11 +58,8 @@ func resourceKongConsumerUpdate(d *schema.ResourceData, meta interface{}) error
}

func resourceKongConsumerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gokong.KongAdminClient)

id := d.Id()

consumer, err := client.Consumers().GetById(id)
consumer, err := meta.(*gokong.KongAdminClient).Consumers().GetById(d.Id())

if err != nil {
return fmt.Errorf("could not find kong consumer: %v", err)
Expand All @@ -81,11 +73,7 @@ func resourceKongConsumerRead(d *schema.ResourceData, meta interface{}) error {

func resourceKongConsumerDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*gokong.KongAdminClient)

id := d.Id()

err := client.Consumers().DeleteById(id)
err := meta.(*gokong.KongAdminClient).Consumers().DeleteById(d.Id())

if err != nil {
return fmt.Errorf("could not delete kong consumer: %v", err)
Expand Down
105 changes: 105 additions & 0 deletions kong/resource_kong_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package kong

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/kevholditch/gokong"
)

func resourceKongPlugin() *schema.Resource {
return &schema.Resource{
Create: resourceKongPluginCreate,
Read: resourceKongPluginRead,
Delete: resourceKongPluginDelete,
Update: resourceKongPluginUpdate,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"api_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"consumer_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"config": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
Default: nil,
},
},
}
}

func resourceKongPluginCreate(d *schema.ResourceData, meta interface{}) error {

pluginRequest := createKongPluginRequestFromResourceData(d)

plugin, err := meta.(*gokong.KongAdminClient).Plugins().Create(pluginRequest)

if err != nil {
return fmt.Errorf("failed to create kong plugin: %v error: %v", pluginRequest, err)
}

d.SetId(plugin.Id)

return resourceKongPluginRead(d, meta)
}

func resourceKongPluginUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)

pluginRequest := createKongPluginRequestFromResourceData(d)

_, err := meta.(*gokong.KongAdminClient).Plugins().UpdateById(d.Id(), pluginRequest)

if err != nil {
return fmt.Errorf("error updating kong plugin: %s", err)
}

return resourceKongPluginRead(d, meta)
}

func resourceKongPluginRead(d *schema.ResourceData, meta interface{}) error {

plugin, err := meta.(*gokong.KongAdminClient).Plugins().GetById(d.Id())

if err != nil {
return fmt.Errorf("could not find kong plugin: %v", err)
}

d.Set("name", plugin.Name)

return nil
}

func resourceKongPluginDelete(d *schema.ResourceData, meta interface{}) error {

err := meta.(*gokong.KongAdminClient).Plugins().DeleteById(d.Id())

if err != nil {
return fmt.Errorf("could not delete kong plugin: %v", err)
}

return nil
}

func createKongPluginRequestFromResourceData(d *schema.ResourceData) *gokong.PluginRequest {

pluginRequest := &gokong.PluginRequest{}

pluginRequest.Name = readStringFromResource(d, "name")
pluginRequest.ApiId = readStringFromResource(d, "api_id")
pluginRequest.ConsumerId = readStringFromResource(d, "consumer_id")
pluginRequest.Config = readMapFromResource(d, "config")

return pluginRequest
}
100 changes: 100 additions & 0 deletions kong/resource_kong_plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package kong

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/kevholditch/gokong"
"testing"
)

func TestAccKongPluginForAllConsumersAndApis(t *testing.T) {

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckKongPluginDestroy,
Steps: []resource.TestStep{
{
Config: testCreatePluginForAllApisAndConsumersConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckKongPluginExists("kong_plugin.response_rate_limiting"),
resource.TestCheckResourceAttr("kong_plugin.response_rate_limiting", "name", "response-ratelimiting"),
),
},
{
Config: testUpdatePluginForAllApisAndConsumersConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckKongPluginExists("kong_plugin.response_rate_limiting"),
resource.TestCheckResourceAttr("kong_plugin.response_rate_limiting", "name", "response-ratelimiting"),
),
},
},
})
}

func testAccCheckKongPluginDestroy(state *terraform.State) error {

client := testAccProvider.Meta().(*gokong.KongAdminClient)

for _, rs := range state.RootModule().Resources {
if rs.Type != "kong_api" {
continue
}

response, err := client.Plugins().GetById(rs.Primary.ID)

if err != nil {
return fmt.Errorf("error calling get plugin by id: %v", err)
}

if response != nil {
return fmt.Errorf("plugin %s still exists, %+v", rs.Primary.ID, response)
}
}

return nil
}

func testAccCheckKongPluginExists(resourceKey string) resource.TestCheckFunc {

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceKey]

if !ok {
return fmt.Errorf("not found: %s", resourceKey)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

api, err := testAccProvider.Meta().(*gokong.KongAdminClient).Plugins().GetById(rs.Primary.ID)

if err != nil {
return err
}

if api == nil {
return fmt.Errorf("plugin with id %v not found", rs.Primary.ID)
}

return nil
}
}

const testCreatePluginForAllApisAndConsumersConfig = `
resource "kong_plugin" "response_rate_limiting" {
name = "response-ratelimiting"
config = {
limits.sms.minute = 10
}
}
`
const testUpdatePluginForAllApisAndConsumersConfig = `
resource "kong_plugin" "response_rate_limiting" {
name = "response-ratelimiting"
config = {
limits.sms.minute = 40
}
}
`
10 changes: 10 additions & 0 deletions kong/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ func readIntFromResource(d *schema.ResourceData, key string) int {
}
return 0
}

func readMapFromResource(d *schema.ResourceData, key string) map[string]interface{} {

if attr, ok := d.GetOk(key); ok {
result := attr.(map[string]interface{})
return result
}

return nil
}
Loading

0 comments on commit 3c27ac5

Please sign in to comment.