From cc921b0bc7c4ee6bbd8010d9563f88052e0da8d7 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 8 Oct 2015 10:03:55 +0200 Subject: [PATCH] Small refactor for better readability and updated the docs --- .../cloudstack/resource_cloudstack_vpc.go | 26 ++++++++++++------- .../providers/cloudstack/r/vpc.html.markdown | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_vpc.go b/builtin/providers/cloudstack/resource_cloudstack_vpc.go index c40b4aa75..07502e58e 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_vpc.go +++ b/builtin/providers/cloudstack/resource_cloudstack_vpc.go @@ -52,16 +52,16 @@ func resourceCloudStackVPC() *schema.Resource { ForceNew: true, }, + "source_nat_ip": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "zone": &schema.Schema{ Type: schema.TypeString, Required: true, ForceNew: true, }, - - "source_nat_ip": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, }, } } @@ -157,19 +157,27 @@ func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error { setValueOrID(d, "project", v.Project, v.Projectid) setValueOrID(d, "zone", v.Zonename, v.Zoneid) - // Grab the source NAT IP that CloudStack assigned. + // Create a new parameter struct p := cs.Address.NewListPublicIpAddressesParams() p.SetVpcid(d.Id()) p.SetIssourcenat(true) + if _, 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) + // Get the source NAT IP assigned to the VPC + l, err := cs.Address.ListPublicIpAddresses(p) + if err != nil { + return err } + if l.Count != 1 { + return fmt.Errorf("Unexpected number (%d) of source NAT IPs returned", l.Count) + } + + d.Set("source_nat_ip", l.PublicIpAddresses[0].Ipaddress) + return nil } diff --git a/website/source/docs/providers/cloudstack/r/vpc.html.markdown b/website/source/docs/providers/cloudstack/r/vpc.html.markdown index d77357631..4610feb0d 100644 --- a/website/source/docs/providers/cloudstack/r/vpc.html.markdown +++ b/website/source/docs/providers/cloudstack/r/vpc.html.markdown @@ -39,7 +39,7 @@ The following arguments are supported: * `project` - (Optional) The name or ID of the project to deploy this instance to. Changing this forces a new resource to be created. - + * `zone` - (Required) The name or ID of the zone where this disk volume will be available. Changing this forces a new resource to be created. @@ -49,3 +49,4 @@ The following attributes are exported: * `id` - The ID of the VPC. * `display_text` - The display text of the VPC. +* `source_nat_ip` - The source NAT IP assigned to the VPC.