Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support terraform import of kong resources #13

Merged
merged 4 commits into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kong/import_consumer_credential.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kong

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

func ImportConsumerCredential(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Expected a string in the format \"<consumer_id>/<credential_id>\" to import.")
}

d.Set("consumer", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
}
8 changes: 7 additions & 1 deletion kong/resource_kong_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func resourceKongAPI() *schema.Resource {
Update: resourceKongAPIUpdate,
Delete: resourceKongAPIDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -162,7 +166,9 @@ func resourceKongAPICreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error while creating API: " + error.Error())
}

if response.StatusCode != http.StatusCreated {
if response.StatusCode == http.StatusConflict {
return fmt.Errorf("409 Conflict - use terraform import to manage this api.")
} else if response.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code received: " + response.Status)
}

Expand Down
10 changes: 8 additions & 2 deletions kong/resource_kong_api_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Plugin struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Configuration map[string]interface{} `json:"config,omitempty"`
API string `json:"-"`
API string `json:"api_id,omitempty"`
}

func resourceKongPlugin() *schema.Resource {
Expand All @@ -23,6 +23,10 @@ func resourceKongPlugin() *schema.Resource {
Update: resourceKongPluginUpdate,
Delete: resourceKongPluginDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -63,7 +67,9 @@ func resourceKongPluginCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error while creating plugin: " + error.Error())
}

if response.StatusCode != http.StatusCreated {
if response.StatusCode == http.StatusConflict {
return fmt.Errorf("409 Conflict - use terraform import to manage this plugin.")
} else if response.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code received: " + response.Status)
}

Expand Down
8 changes: 7 additions & 1 deletion kong/resource_kong_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func resourceKongConsumer() *schema.Resource {
Update: resourceKongConsumerUpdate,
Delete: resourceKongConsumerDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -56,7 +60,9 @@ func resourceKongConsumerCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error while creating consumer.")
}

if response.StatusCode != http.StatusCreated {
if response.StatusCode == http.StatusConflict {
return fmt.Errorf("409 Conflict - use terraform import to manage this consumer.")
} else if response.StatusCode != http.StatusCreated {
return fmt.Errorf(response.Status)
}

Expand Down
4 changes: 4 additions & 0 deletions kong/resource_kong_consumer_credential_basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func resourceKongBasicAuthCredential() *schema.Resource {
Update: resourceKongBasicAuthCredentialUpdate,
Delete: resourceKongBasicAuthCredentialDelete,

Importer: &schema.ResourceImporter{
State: ImportConsumerCredential,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down
4 changes: 4 additions & 0 deletions kong/resource_kong_consumer_credential_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func resourceKongJWTCredential() *schema.Resource {
Update: resourceKongJWTCredentialUpdate,
Delete: resourceKongJWTCredentialDelete,

Importer: &schema.ResourceImporter{
State: ImportConsumerCredential,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down
4 changes: 4 additions & 0 deletions kong/resource_kong_consumer_credential_key_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func resourceKongKeyAuthCredential() *schema.Resource {
Update: resourceKongKeyAuthCredentialUpdate,
Delete: resourceKongKeyAuthCredentialDelete,

Importer: &schema.ResourceImporter{
State: ImportConsumerCredential,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down
10 changes: 8 additions & 2 deletions kong/resource_kong_key_auth_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type KeyAuthPlugin struct {
KeyNames string `json:"config.key_names,omitempty"`
HideCredentials bool `json:"config.hide_credentials,omitempty"`
Anonymous string `json:"config.anonymous,omitempty"`
API string `json:"-"`
API string `json:"api_id,omitempty"`
}

func resourceKongKeyAuthPlugin() *schema.Resource {
Expand All @@ -24,6 +24,10 @@ func resourceKongKeyAuthPlugin() *schema.Resource {
Update: resourceKeyAuthPluginUpdate,
Delete: resourceKeyAuthPluginDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -71,7 +75,9 @@ func resourceKeyAuthPluginCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error while creating plugin.")
}

if response.StatusCode != http.StatusCreated {
if response.StatusCode == http.StatusConflict {
return fmt.Errorf("409 Conflict - use terraform import to manage this plugin.")
} else if response.StatusCode != http.StatusCreated {
return fmt.Errorf(response.Status)
}

Expand Down