-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create plugin tests for all apis and consumers
- Loading branch information
1 parent
c06a67a
commit 3c27ac5
Showing
10 changed files
with
242 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.