-
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.
- Loading branch information
1 parent
c9427c7
commit c06a67a
Showing
17 changed files
with
1,118 additions
and
30 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
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 | ||
} |
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,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 ----" | ||
} | ||
` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.