From 910469ddee2dfc1743dfeaed5af3b95d8db72af8 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Fri, 2 Oct 2015 20:54:07 -0400 Subject: [PATCH 1/4] Add Source NAT IP parameter When creating a VPC, CloudStack automatically assigns a source NAT IP from it's pool. It's handy to have this IP available in Terraform, which can be used in ACLs for example. This commit adds such support. --- .../cloudstack/resource_cloudstack_vpc.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/builtin/providers/cloudstack/resource_cloudstack_vpc.go b/builtin/providers/cloudstack/resource_cloudstack_vpc.go index 8fe132d94..1b2ecbf86 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_vpc.go +++ b/builtin/providers/cloudstack/resource_cloudstack_vpc.go @@ -51,6 +51,11 @@ func resourceCloudStackVPC() *schema.Resource { Required: true, ForceNew: true, }, + + "source_nat_ip": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -133,6 +138,16 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error { setValueOrUUID(d, "project", v.Project, v.Projectid) setValueOrUUID(d, "zone", v.Zonename, v.Zoneid) + // Grab the source NAT IP that CloudStack assigned. + p := cs.Address.NewListPublicIpAddressesParams() + p.SetVpcid(d.Id()) + p.SetIssourcenat(true) + p.SetProjectid(v.Projectid) + l, e := cs.Address.ListPublicIpAddresses(p) + if (e == nil) && (l.Count == 1) { + d.Set("source_nat_ip", l.PublicIpAddresses[0].Ipaddress) + } + return nil } From d212b278a954c452c0faba346cfaf6dd70373899 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Mon, 5 Oct 2015 18:59:52 -0400 Subject: [PATCH 2/4] Only set projectID if it is set --- builtin/providers/cloudstack/resource_cloudstack_vpc.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_vpc.go b/builtin/providers/cloudstack/resource_cloudstack_vpc.go index 2e396231b..aa70dff74 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_vpc.go +++ b/builtin/providers/cloudstack/resource_cloudstack_vpc.go @@ -161,7 +161,10 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error { p := cs.Address.NewListPublicIpAddressesParams() p.SetVpcid(d.Id()) p.SetIssourcenat(true) - p.SetProjectid(v.Projectid) + if project, ok := d.GetOk("project"); ok { + p.SetProjectid(v.Projectid) + } + l, e := cs.Address.ListPublicIpAddresses(p) if (e == nil) && (l.Count == 1) { d.Set("source_nat_ip", l.PublicIpAddresses[0].Ipaddress) From aa4cf423f763bf3f6ba539507bfe4d7911ee8154 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Mon, 5 Oct 2015 19:01:14 -0400 Subject: [PATCH 3/4] Fix whitespace --- builtin/providers/cloudstack/resource_cloudstack_vpc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_vpc.go b/builtin/providers/cloudstack/resource_cloudstack_vpc.go index aa70dff74..198d28d00 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_vpc.go +++ b/builtin/providers/cloudstack/resource_cloudstack_vpc.go @@ -162,9 +162,9 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error { p.SetVpcid(d.Id()) p.SetIssourcenat(true) if project, ok := d.GetOk("project"); ok { - p.SetProjectid(v.Projectid) + p.SetProjectid(v.Projectid) } - + l, e := cs.Address.ListPublicIpAddresses(p) if (e == nil) && (l.Count == 1) { d.Set("source_nat_ip", l.PublicIpAddresses[0].Ipaddress) From b3bbba7767c2ee77b405801101b5fde62ef80bf3 Mon Sep 17 00:00:00 2001 From: Hany Fahim Date: Mon, 5 Oct 2015 19:06:50 -0400 Subject: [PATCH 4/4] project was not being referenced --- builtin/providers/cloudstack/resource_cloudstack_vpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_vpc.go b/builtin/providers/cloudstack/resource_cloudstack_vpc.go index 198d28d00..5168e9162 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_vpc.go +++ b/builtin/providers/cloudstack/resource_cloudstack_vpc.go @@ -161,7 +161,7 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error { p := cs.Address.NewListPublicIpAddressesParams() p.SetVpcid(d.Id()) p.SetIssourcenat(true) - if project, ok := d.GetOk("project"); ok { + if _, ok := d.GetOk("project"); ok { p.SetProjectid(v.Projectid) }