Skip to content

Commit

Permalink
added certificate resource
Browse files Browse the repository at this point in the history
  • Loading branch information
kevholditch committed Nov 30, 2017
1 parent c9427c7 commit c06a67a
Show file tree
Hide file tree
Showing 17 changed files with 1,118 additions and 30 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ Requirements
- [Go](https://golang.org/doc/install) 1.8 (to build the provider plugin)

Usage
---------------------

```
# For example, restrict template version in 0.1.x
provider "template" {
version = "~> 0.1"
}
```

-----

To configure the provider:
```hcl
Expand All @@ -31,7 +23,9 @@ provider "kong" {
By convention the provider will first check the env variable `KONG_ADMIN_ADDR` if that variable is not set then it will default to `http://localhost:8001` if
you do not provide a provider block as above.

To create an api:
## Resources

# Api
```hcl
resource "kong_api" "api" {
name = "TestApi"
Expand All @@ -49,14 +43,27 @@ resource "kong_api" "api" {
http_if_terminated = false
}
```
The api resource maps directly onto the json for creating an API in Kong. For more information on the parameters [see the Kong Api create documentation](https://getkong.org/docs/0.11.x/admin-api/#add-api).
The api resource maps directly onto the json for creating an API in Kong. For more information on the parameters [see the Kong Api create documentation](https://getkong.org/docs/0.11.x/admin-api/#api-object).

To create a consuemr:
# Consumer
```hcl
resource "kong_consumer" "consumer" {
username = "User1"
custom_id = "123"
}
```

The consumer resource maps directly onto the json for creating an Consumer in Kong. For more information on the parameters [see the Kong Consumer create documentation](https://getkong.org/docs/0.11.x/admin-api/#create-consumer).
The consumer resource maps directly onto the json for creating an Consumer in Kong. For more information on the parameters [see the Kong Consumer create documentation](https://getkong.org/docs/0.11.x/admin-api/#consumer-object).

## Certificates
```hcl
resource "kong_certificate" "certificate" {
certificate = "public key --- 123 ----"
private_key = "private key --- 456 ----"
}
```

`certificate` should be the public key of your certificate it is mapped to the `Cert` parameter on the Kong API.
`private_key` should be the private key of your certificate it is mapped to the `Key` parameter on the Kong API.

For more information on creating certificates in Kong [see their documentation](https://getkong.org/docs/0.11.x/admin-api/#certificate-object)
5 changes: 3 additions & 2 deletions kong/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"kong_api": resourceKongApi(),
"kong_consumer": resourceKongConsumer(),
"kong_api": resourceKongApi(),
"kong_certificate": resourceKongCertificate(),
"kong_consumer": resourceKongConsumer(),
},

ConfigureFunc: providerConfigure,
Expand Down
2 changes: 1 addition & 1 deletion kong/resource_kong_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestAccKongApi_basic(t *testing.T) {
func TestAccKongApi(t *testing.T) {

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Expand Down
105 changes: 105 additions & 0 deletions kong/resource_kong_certificate.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 resourceKongCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceKongCertificateCreate,
Read: resourceKongCertificateRead,
Delete: resourceKongCertificateDelete,
Update: resourceKongCertificateUpdate,

Schema: map[string]*schema.Schema{
"certificate": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
},
}
}

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

certificateRequest := createKongCertificateRequestFromResourceData(d)

consumer, err := client.Certificates().Create(certificateRequest)

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

d.SetId(consumer.Id)

return resourceKongCertificateRead(d, meta)
}

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)

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

return resourceKongCertificateRead(d, meta)
}

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

id := d.Id()

certificate, err := client.Certificates().GetById(id)

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

d.Set("certificate", certificate.Cert)
d.Set("private_key", certificate.Key)

return nil
}

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

client := meta.(*gokong.KongAdminClient)

id := d.Id()

err := client.Consumers().DeleteById(id)

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

return nil
}

func createKongCertificateRequestFromResourceData(d *schema.ResourceData) *gokong.CertificateRequest {

certificateRequest := &gokong.CertificateRequest{}

certificateRequest.Cert = readStringFromResource(d, "certificate")
certificateRequest.Key = readStringFromResource(d, "private_key")

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

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

func TestAccKongCertificate(t *testing.T) {

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckKongCertificateDestroy,
Steps: []resource.TestStep{
{
Config: testCreateCertificateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckKongCertificateExists("kong_certificate.certificate"),
resource.TestCheckResourceAttr("kong_certificate.certificate", "certificate", "public key --- 123 ----"),
resource.TestCheckResourceAttr("kong_certificate.certificate", "private_key", "private key --- 456 ----"),
),
},
{
Config: testUpdateCertificateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckKongCertificateExists("kong_certificate.certificate"),
resource.TestCheckResourceAttr("kong_certificate.certificate", "certificate", "public key --- 789 ----"),
resource.TestCheckResourceAttr("kong_certificate.certificate", "private_key", "private key --- 321 ----"),
),
},
},
})
}

func testAccCheckKongCertificateDestroy(state *terraform.State) error {

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

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

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

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

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

return nil
}

func testAccCheckKongCertificateExists(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).Certificates().GetById(rs.Primary.ID)

if err != nil {
return err
}

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

return nil
}
}

const testCreateCertificateConfig = `
resource "kong_certificate" "certificate" {
certificate = "public key --- 123 ----"
private_key = "private key --- 456 ----"
}
`
const testUpdateCertificateConfig = `
resource "kong_certificate" "certificate" {
certificate = "public key --- 789 ----"
private_key = "private key --- 321 ----"
}
`
2 changes: 1 addition & 1 deletion kong/resource_kong_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestAccKongConsumer_basic(t *testing.T) {
func TestAccKongConsumer(t *testing.T) {

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Expand Down
2 changes: 1 addition & 1 deletion kong/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/hashicorp/terraform/helper/schema"
func readArrayFromResource(d *schema.ResourceData, key string) []string {

if attr, ok := d.GetOk(key); ok {
array := []string{}
var array []string
items := attr.([]interface{})
for _, x := range items {
item := x.(string)
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/kevholditch/gokong/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c06a67a

Please sign in to comment.