-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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: Add new resource - aws_vpc_endpoint #2695
Merged
radeksimko
merged 3 commits into
hashicorp:master
from
MeredithCorpOSS:f-aws-vpc-endpoint
Jul 14, 2015
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
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,174 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/hashcode" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsVpcEndpoint() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsVPCEndpointCreate, | ||
Read: resourceAwsVPCEndpointRead, | ||
Update: resourceAwsVPCEndpointUpdate, | ||
Delete: resourceAwsVPCEndpointDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"policy": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
StateFunc: normalizeJson, | ||
}, | ||
"vpc_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"service_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"route_table_ids": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: func(v interface{}) int { | ||
return hashcode.String(v.(string)) | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsVPCEndpointCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
input := &ec2.CreateVPCEndpointInput{ | ||
VPCID: aws.String(d.Get("vpc_id").(string)), | ||
RouteTableIDs: expandStringList(d.Get("route_table_ids").(*schema.Set).List()), | ||
ServiceName: aws.String(d.Get("service_name").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("policy"); ok { | ||
policy := normalizeJson(v) | ||
input.PolicyDocument = aws.String(policy) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating VPC Endpoint: %#v", input) | ||
output, err := conn.CreateVPCEndpoint(input) | ||
if err != nil { | ||
return fmt.Errorf("Error creating VPC Endpoint: %s", err) | ||
} | ||
log.Printf("[DEBUG] VPC Endpoint %q created.", *output.VPCEndpoint.VPCEndpointID) | ||
|
||
d.SetId(*output.VPCEndpoint.VPCEndpointID) | ||
|
||
return resourceAwsVPCEndpointRead(d, meta) | ||
} | ||
|
||
func resourceAwsVPCEndpointRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
input := &ec2.DescribeVPCEndpointsInput{ | ||
VPCEndpointIDs: []*string{aws.String(d.Id())}, | ||
} | ||
|
||
log.Printf("[DEBUG] Reading VPC Endpoint: %q", d.Id()) | ||
output, err := conn.DescribeVPCEndpoints(input) | ||
|
||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error()) | ||
} | ||
|
||
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error()) | ||
} | ||
|
||
if len(output.VPCEndpoints) != 1 { | ||
return fmt.Errorf("There's no unique VPC Endpoint, but %d endpoints: %#v", | ||
len(output.VPCEndpoints), output.VPCEndpoints) | ||
} | ||
|
||
vpce := output.VPCEndpoints[0] | ||
|
||
d.Set("vpc_id", vpce.VPCID) | ||
d.Set("policy", normalizeJson(*vpce.PolicyDocument)) | ||
d.Set("service_name", vpce.ServiceName) | ||
d.Set("route_table_ids", vpce.RouteTableIDs) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsVPCEndpointUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
input := &ec2.ModifyVPCEndpointInput{ | ||
VPCEndpointID: aws.String(d.Id()), | ||
} | ||
|
||
if d.HasChange("route_table_ids") { | ||
o, n := d.GetChange("route_table_ids") | ||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
|
||
add := expandStringList(os.Difference(ns).List()) | ||
if len(add) > 0 { | ||
input.AddRouteTableIDs = add | ||
} | ||
|
||
remove := expandStringList(ns.Difference(os).List()) | ||
if len(remove) > 0 { | ||
input.RemoveRouteTableIDs = remove | ||
} | ||
} | ||
|
||
if d.HasChange("policy") { | ||
policy := normalizeJson(d.Get("policy")) | ||
input.PolicyDocument = aws.String(policy) | ||
} | ||
|
||
log.Printf("[DEBUG] Updating VPC Endpoint: %#v", input) | ||
_, err := conn.ModifyVPCEndpoint(input) | ||
if err != nil { | ||
return fmt.Errorf("Error updating VPC Endpoint: %s", err) | ||
} | ||
log.Printf("[DEBUG] VPC Endpoint %q updated", input.VPCEndpointID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsVPCEndpointDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
input := &ec2.DeleteVPCEndpointsInput{ | ||
VPCEndpointIDs: []*string{aws.String(d.Id())}, | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting VPC Endpoint: %#v", input) | ||
_, err := conn.DeleteVPCEndpoints(input) | ||
|
||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error()) | ||
} | ||
|
||
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" { | ||
log.Printf("[DEBUG] VPC Endpoint %q is already gone", d.Id()) | ||
} else { | ||
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error()) | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] VPC Endpoint %q deleted", d.Id()) | ||
d.SetId("") | ||
|
||
return 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.
Can we ever get a
VPCEndpoint
with anil
policy document?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 don't think so, based on available docs we shouldn't. If no policy is provided during creation, default policy will be used. Let me see if I can remove the
ResetPolicy
option from the update which seems superfluous (thanks for reminding me that).