-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
New Resource: aws_api_gateway_documentation_version #3287
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsApiGatewayDocumentationVersion() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsApiGatewayDocumentationVersionCreate, | ||
Read: resourceAwsApiGatewayDocumentationVersionRead, | ||
Update: resourceAwsApiGatewayDocumentationVersionUpdate, | ||
Delete: resourceAwsApiGatewayDocumentationVersionDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"version": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"rest_api_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsApiGatewayDocumentationVersionCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
restApiId := d.Get("rest_api_id").(string) | ||
|
||
params := &apigateway.CreateDocumentationVersionInput{ | ||
DocumentationVersion: aws.String(d.Get("version").(string)), | ||
RestApiId: aws.String(restApiId), | ||
} | ||
if v, ok := d.GetOk("description"); ok { | ||
params.Description = aws.String(v.(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating API Gateway Documentation Version: %s", params) | ||
|
||
version, err := conn.CreateDocumentationVersion(params) | ||
if err != nil { | ||
return fmt.Errorf("Error creating API Gateway Documentation Version: %s", err) | ||
} | ||
|
||
d.SetId(restApiId + "/" + *version.Version) | ||
|
||
return resourceAwsApiGatewayDocumentationVersionRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayDocumentationVersionRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Reading API Gateway Documentation Version %s", d.Id()) | ||
|
||
apiId, docVersion, err := decodeApiGatewayDocumentationVersionId(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
version, err := conn.GetDocumentationVersion(&apigateway.GetDocumentationVersionInput{ | ||
DocumentationVersion: aws.String(docVersion), | ||
RestApiId: aws.String(apiId), | ||
}) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
log.Printf("[WARN] API Gateway Documentation Version (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("rest_api_id", apiId) | ||
d.Set("description", version.Description) | ||
d.Set("version", version.Version) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayDocumentationVersionUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Updating API Gateway Documentation Version %s", d.Id()) | ||
|
||
_, err := conn.UpdateDocumentationVersion(&apigateway.UpdateDocumentationVersionInput{ | ||
DocumentationVersion: aws.String(d.Get("version").(string)), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
PatchOperations: []*apigateway.PatchOperation{ | ||
{ | ||
Op: aws.String(apigateway.OpReplace), | ||
Path: aws.String("/description"), | ||
Value: aws.String(d.Get("description").(string)), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("[DEBUG] Updated API Gateway Documentation Version %s", d.Id()) | ||
|
||
return resourceAwsApiGatewayDocumentationVersionRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayDocumentationVersionDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Deleting API Gateway Documentation Version: %s", d.Id()) | ||
|
||
_, err := conn.DeleteDocumentationVersion(&apigateway.DeleteDocumentationVersionInput{ | ||
DocumentationVersion: aws.String(d.Get("version").(string)), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
}) | ||
return err | ||
} | ||
|
||
func decodeApiGatewayDocumentationVersionId(id string) (string, string, error) { | ||
parts := strings.Split(id, "/") | ||
if len(parts) != 2 { | ||
return "", "", fmt.Errorf("Expected ID in the form of REST-API-ID/VERSION, given: %q", id) | ||
} | ||
return parts[0], parts[1], nil | ||
} |
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.
Question: the
CreateDocumentationVersionInput
type also supportsStageName
, do we need to support it as well?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.
I intentionally removed it, although it was in my original implementation. CloudFormation also doesn't have it, probably for the same reason https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-documentationversion.html
It has a very problematic workflow. You need to create the stage 1st (for referencing in APIG Documentation Version), but you cannot delete the APIG Documentation Version before deleting the stage (it errors out otherwise). Basically the dependency chain reverts during destruction.
Either way we shouldn't need it, because https://www.terraform.io/docs/providers/aws/r/api_gateway_stage.html#documentation_version allows users to associate the two.
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.
👍 sounds good