Skip to content

Commit

Permalink
Merge pull request #70 from aiven/mte-add-cross-region-vpc-peering
Browse files Browse the repository at this point in the history
add cross region vpc peering

#70
  • Loading branch information
rikonen authored Jun 10, 2019
2 parents 155e1ad + 8d976ce commit 4e8aa4d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.0.10] - 2019-06-10
Switch to using go.mod
Support cross-region VPC Peering.

## [1.0.9] - 2019-04-26
Build with CGO_ENABLED=0.

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ bins: vendor
test: vendor
CGO_ENABLED=0 go test -v ./...

lint:
lint: vendor
golangci-lint run -D errcheck

clean:
go mod tidy
rm -rf vendor
rm -f terraform-provided-aiven-*_amd64
rm -f terraform-provider-aiven-*_amd64

.PHONY: test lint vendor bootstrap
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ resource "aiven_vpc_peering_connection" "mypeeringconnection" {
vpc_id = "${aiven_project_vpc.myvpc.id}"
peer_cloud_account = "<PEER_ACCOUNT_ID>"
peer_vpc = "<PEER_VPC_ID/NAME>"
peer_region = "<PEER_REGION>"
}
```

Expand All @@ -519,10 +520,13 @@ peered with.

``peer_vpc`` defines the identifier or name of the remote VPC.

``peer_region`` defines the region of the remote VPC if it is not in the same region as Aiven VPC.

Computed property ``state`` tells the current state of the VPC. This property cannot be
set, only read.

Aiven ID format when importing existing resource: ``<project_name>/<VPC_UUID>/<peer_cloud_account>/<peer_vpc>``.
Aiven ID format when importing existing cross-region resource: ``<project_name>/<VPC_UUID>/<peer_cloud_account>/<peer_vpc>/peer_region``.
The UUID is not directly visible in the Aiven web console.

## Credits
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/aiven/terraform-provider-aiven

require (
github.com/aiven/aiven-go-client v1.0.1-0.20190606064644-bbe58aa848f3
github.com/aiven/aiven-go-client v1.1.0
github.com/gobuffalo/packd v0.1.0 // indirect
github.com/gobuffalo/packr v1.25.0
github.com/golang/protobuf v1.3.1 // indirect
Expand Down
5 changes: 0 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ func splitResourceID3(resourceID string) (string, string, string) {
return parts[0], parts[1], parts[2]
}

func splitResourceID4(resourceID string) (string, string, string, string) {
parts := splitResourceID(resourceID, 4)
return parts[0], parts[1], parts[2], parts[3]
}

func resourceExists(err error) (bool, error) {
if err == nil {
return true, nil
Expand Down
63 changes: 54 additions & 9 deletions resource_vpc_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func resourceVPCPeeringConnection() *schema.Resource {
Required: true,
Type: schema.TypeString,
},
"peer_region": {
Description: "AWS region of the peered VPC (if not in the same region as Aiven VPC)",
ForceNew: true,
Optional: true,
Type: schema.TypeString,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
},
"state": {
Computed: true,
Description: "State of the peering connection",
Expand All @@ -56,14 +65,26 @@ func resourceVPCPeeringConnection() *schema.Resource {
}

func resourceVPCPeeringConnectionCreate(d *schema.ResourceData, m interface{}) error {
var (
pc *aiven.VPCPeeringConnection
err error
region *string
)

client := m.(*aiven.Client)
projectName, vpcID := splitResourceID2(d.Get("vpc_id").(string))
pc, err := client.VPCPeeringConnections.Create(
peerRegion := d.Get("peer_region").(string)

if peerRegion != "" {
region = &peerRegion
}
pc, err = client.VPCPeeringConnections.Create(
projectName,
vpcID,
aiven.CreateVPCPeeringConnectionRequest{
PeerCloudAccount: d.Get("peer_cloud_account").(string),
PeerVPC: d.Get("peer_vpc").(string),
PeerRegion: region,
},
)

Expand All @@ -78,22 +99,42 @@ func resourceVPCPeeringConnectionCreate(d *schema.ResourceData, m interface{}) e
VPCID: vpcID,
PeerCloudAccount: pc.PeerCloudAccount,
PeerVPC: pc.PeerVPC,
PeerRegion: pc.PeerRegion,
}
res, err := w.Conf().WaitForState()
if err != nil {
return err
}

pc = res.(*aiven.VPCPeeringConnection)
d.SetId(buildResourceID(projectName, vpcID, pc.PeerCloudAccount, pc.PeerVPC))
if peerRegion != "" {
d.SetId(buildResourceID(projectName, vpcID, pc.PeerCloudAccount, pc.PeerVPC, *pc.PeerRegion))
} else {
d.SetId(buildResourceID(projectName, vpcID, pc.PeerCloudAccount, pc.PeerVPC))
}
return copyVPCPeeringConnectionPropertiesFromAPIResponseToTerraform(d, pc, projectName, vpcID)
}

func parsePeeringVPCId(resourceID string) (string, string, string, string, *string) {
var peerRegion *string

parts := strings.Split(resourceID, "/")
projectName := parts[0]
vpcID := parts[1]
peerCloudAccount := parts[2]
peerVPC := parts[3]
if len(parts) > 4 {
peerRegion = new(string)
*peerRegion = parts[4]
}
return projectName, vpcID, peerCloudAccount, peerVPC, peerRegion
}

func resourceVPCPeeringConnectionRead(d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)

projectName, vpcID, peerCloudAccount, peerVPC := splitResourceID4(d.Id())
pc, err := client.VPCPeeringConnections.Get(projectName, vpcID, peerCloudAccount, peerVPC)
projectName, vpcID, peerCloudAccount, peerVPC, peerRegion := parsePeeringVPCId(d.Id())
pc, err := client.VPCPeeringConnections.GetVPCPeering(projectName, vpcID, peerCloudAccount, peerVPC, peerRegion)
if err != nil {
return err
}
Expand All @@ -104,15 +145,15 @@ func resourceVPCPeeringConnectionRead(d *schema.ResourceData, m interface{}) err
func resourceVPCPeeringConnectionDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)

projectName, vpcID, peerCloudAccount, peerVPC := splitResourceID4(d.Id())
return client.VPCPeeringConnections.Delete(projectName, vpcID, peerCloudAccount, peerVPC)
projectName, vpcID, peerCloudAccount, peerVPC, peerRegion := parsePeeringVPCId(d.Id())
return client.VPCPeeringConnections.DeleteVPCPeering(projectName, vpcID, peerCloudAccount, peerVPC, peerRegion)
}

func resourceVPCPeeringConnectionExists(d *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*aiven.Client)

projectName, vpcID, peerCloudAccount, peerVPC := splitResourceID4(d.Id())
_, err := client.VPCPeeringConnections.Get(projectName, vpcID, peerCloudAccount, peerVPC)
projectName, vpcID, peerCloudAccount, peerVPC, peerRegion := parsePeeringVPCId(d.Id())
_, err := client.VPCPeeringConnections.GetVPCPeering(projectName, vpcID, peerCloudAccount, peerVPC, peerRegion)
return resourceExists(err)
}

Expand All @@ -138,6 +179,9 @@ func copyVPCPeeringConnectionPropertiesFromAPIResponseToTerraform(
d.Set("vpc_id", buildResourceID(project, vpcID))
d.Set("peer_cloud_account", peeringConnection.PeerCloudAccount)
d.Set("peer_vpc", peeringConnection.PeerVPC)
if peeringConnection.PeerRegion != nil {
d.Set("peer_region", peeringConnection.PeerRegion)
}
d.Set("state", peeringConnection.State)
if peeringConnection.StateInfo != nil {
peeringID, ok := (*peeringConnection.StateInfo)["aws_vpc_peering_connection_id"]
Expand All @@ -157,12 +201,13 @@ type VPCPeeringBuildWaiter struct {
VPCID string
PeerCloudAccount string
PeerVPC string
PeerRegion *string
}

// RefreshFunc will call the Aiven client and refresh it's state.
func (w *VPCPeeringBuildWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
pc, err := w.Client.VPCPeeringConnections.Get(w.Project, w.VPCID, w.PeerCloudAccount, w.PeerVPC)
pc, err := w.Client.VPCPeeringConnections.GetVPCPeering(w.Project, w.VPCID, w.PeerCloudAccount, w.PeerVPC, w.PeerRegion)

if err != nil {
return nil, "", err
Expand Down

0 comments on commit 4e8aa4d

Please sign in to comment.