-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4295 from nicolai86/feature/aws-api-gateway
provider/aws: API Gateway resources
- Loading branch information
Showing
37 changed files
with
8,753 additions
and
3 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
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.