-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
provider/aws: API Gateway resources #4295
Merged
radeksimko
merged 10 commits into
hashicorp:master
from
nicolai86:feature/aws-api-gateway
Mar 6, 2016
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fdf0cfa
Vendor AWS APIGateway API
nicolai86 a73721d
Add aws_api_gateway_rest_api resource
nicolai86 7ead800
Add aws_api_gateway_resource resource
nicolai86 032e608
Add aws_api_gateway_method resource
nicolai86 91f5206
Add aws_api_gateway_method_response resource
nicolai86 1593dbe
Add aws_api_gateway_integration resource
nicolai86 4da8b3d
Add aws_api_gateawy_integration_response resource
nicolai86 b4c99f1
Add aws_api_gateway_model resource
nicolai86 8c59d08
Add aws_api_gateway_api_key resource
nicolai86 6430fca
Add aws_api_gateway_deployment resource
nicolai86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
166 changes: 166 additions & 0 deletions
166
builtin/providers/aws/resource_aws_api_gateway_api_key.go
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,166 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsApiGatewayApiKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsApiGatewayApiKeyCreate, | ||
Read: resourceAwsApiGatewayApiKeyRead, | ||
Update: resourceAwsApiGatewayApiKeyUpdate, | ||
Delete: resourceAwsApiGatewayApiKeyDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"enabled": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
|
||
"stage_key": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"rest_api_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"stage_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Creating API Gateway API Key") | ||
|
||
apiKey, err := conn.CreateApiKey(&apigateway.CreateApiKeyInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
Description: aws.String(d.Get("description").(string)), | ||
Enabled: aws.Bool(d.Get("enabled").(bool)), | ||
StageKeys: expandApiGatewayStageKeys(d), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error creating API Gateway: %s", err) | ||
} | ||
|
||
d.SetId(*apiKey.Id) | ||
|
||
return resourceAwsApiGatewayApiKeyRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Reading API Gateway API Key: %s", d.Id()) | ||
|
||
apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ | ||
ApiKey: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
d.Set("name", apiKey.Name) | ||
d.Set("description", apiKey.Description) | ||
d.Set("enabled", apiKey.Enabled) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { | ||
operations := make([]*apigateway.PatchOperation, 0) | ||
if d.HasChange("enabled") { | ||
isEnabled := "false" | ||
if d.Get("enabled").(bool) { | ||
isEnabled = "true" | ||
} | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/enabled"), | ||
Value: aws.String(isEnabled), | ||
}) | ||
} | ||
|
||
if d.HasChange("description") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/description"), | ||
Value: aws.String(d.Get("description").(string)), | ||
}) | ||
} | ||
|
||
if d.HasChange("stage_key") { | ||
operations = append(operations, expandApiGatewayStageKeyOperations(d)...) | ||
} | ||
return operations | ||
} | ||
|
||
func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) | ||
|
||
_, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ | ||
ApiKey: aws.String(d.Id()), | ||
PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceAwsApiGatewayApiKeyRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id()) | ||
|
||
return resource.Retry(5*time.Minute, func() error { | ||
_, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{ | ||
ApiKey: aws.String(d.Id()), | ||
}) | ||
|
||
if err == nil { | ||
return nil | ||
} | ||
|
||
if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { | ||
return nil | ||
} | ||
|
||
return resource.RetryError{Err: err} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add guarding against deletion outside of Terraform here, like you did nicely for many other resources.