Skip to content

Commit

Permalink
Merge pull request #6010 from svanharmelen/f-deprecate-ipaddress
Browse files Browse the repository at this point in the history
provider/cloudstack: make the CloudStack provider more inline with the other providers
  • Loading branch information
Sander van Harmelen committed Apr 5, 2016
2 parents 0f25016 + fddf3ec commit 5e6af8e
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 151 deletions.
26 changes: 22 additions & 4 deletions builtin/providers/cloudstack/resource_cloudstack_firewall.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cloudstack

import (
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -20,10 +21,19 @@ func resourceCloudStackFirewall() *schema.Resource {
Delete: resourceCloudStackFirewallDelete,

Schema: map[string]*schema.Schema{
"ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"ipaddress"},
},

"ipaddress": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Please use the `ip_address` field instead",
ConflictsWith: []string{"ip_address"},
},

"managed": &schema.Schema{
Expand Down Expand Up @@ -99,8 +109,16 @@ func resourceCloudStackFirewallCreate(d *schema.ResourceData, meta interface{})
return err
}

ipaddress, ok := d.GetOk("ip_address")
if !ok {
ipaddress, ok = d.GetOk("ipaddress")
}
if !ok {
return errors.New("Either `ip_address` or [deprecated] `ipaddress` must be provided.")
}

// Retrieve the ipaddress ID
ipaddressid, e := retrieveID(cs, "ipaddress", d.Get("ipaddress").(string))
ipaddressid, e := retrieveID(cs, "ip_address", ipaddress.(string))
if e != nil {
return e.Error()
}
Expand Down
56 changes: 28 additions & 28 deletions builtin/providers/cloudstack/resource_cloudstack_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccCloudStackFirewall_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
"cloudstack_firewall.foo", "ip_address", CLOUDSTACK_PUBLIC_IPADDRESS),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.#", "2"),
resource.TestCheckResourceAttr(
Expand All @@ -31,13 +31,13 @@ func TestAccCloudStackFirewall_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.60926170.ports.32925333", "8080"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.source_cidr", "10.0.0.0/24"),
"cloudstack_firewall.foo", "rule.3832507136.cidr_list.3482919157", "10.0.0.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.protocol", "tcp"),
"cloudstack_firewall.foo", "rule.3832507136.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1209010669", "1000-2000"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1209010669", "1000-2000"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1889509032", "80"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1889509032", "80"),
),
},
},
Expand All @@ -55,7 +55,7 @@ func TestAccCloudStackFirewall_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
"cloudstack_firewall.foo", "ip_address", CLOUDSTACK_PUBLIC_IPADDRESS),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.#", "2"),
resource.TestCheckResourceAttr(
Expand All @@ -65,13 +65,13 @@ func TestAccCloudStackFirewall_update(t *testing.T) {
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.60926170.ports.32925333", "8080"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.source_cidr", "10.0.0.0/24"),
"cloudstack_firewall.foo", "rule.3832507136.cidr_list.3482919157", "10.0.0.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.protocol", "tcp"),
"cloudstack_firewall.foo", "rule.3832507136.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1209010669", "1000-2000"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1209010669", "1000-2000"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1889509032", "80"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1889509032", "80"),
),
},

Expand All @@ -80,33 +80,33 @@ func TestAccCloudStackFirewall_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
"cloudstack_firewall.foo", "ip_address", CLOUDSTACK_PUBLIC_IPADDRESS),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.#", "3"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.2207610982.cidr_list.80081744", "10.0.1.0/24"),
"cloudstack_firewall.foo", "rule.2144925929.cidr_list.80081744", "10.0.1.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.2207610982.cidr_list.3482919157", "10.0.0.0/24"),
"cloudstack_firewall.foo", "rule.2144925929.cidr_list.3482919157", "10.0.0.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.2207610982.protocol", "tcp"),
"cloudstack_firewall.foo", "rule.2144925929.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.2207610982.ports.32925333", "8080"),
"cloudstack_firewall.foo", "rule.2144925929.ports.32925333", "8080"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.source_cidr", "10.0.0.0/24"),
"cloudstack_firewall.foo", "rule.3832507136.cidr_list.3482919157", "10.0.0.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.protocol", "tcp"),
"cloudstack_firewall.foo", "rule.3832507136.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1209010669", "1000-2000"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1209010669", "1000-2000"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.716592205.ports.1889509032", "80"),
"cloudstack_firewall.foo", "rule.3832507136.ports.1889509032", "80"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.4449157.source_cidr", "172.16.100.0/24"),
"cloudstack_firewall.foo", "rule.302279047.cidr_list.2835005819", "172.16.100.0/24"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.4449157.protocol", "tcp"),
"cloudstack_firewall.foo", "rule.302279047.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.4449157.ports.1889509032", "80"),
"cloudstack_firewall.foo", "rule.302279047.ports.1889509032", "80"),
resource.TestCheckResourceAttr(
"cloudstack_firewall.foo", "rule.4449157.ports.3638101695", "443"),
"cloudstack_firewall.foo", "rule.302279047.ports.3638101695", "443"),
),
},
},
Expand Down Expand Up @@ -174,7 +174,7 @@ func testAccCheckCloudStackFirewallDestroy(s *terraform.State) error {

var testAccCloudStackFirewall_basic = fmt.Sprintf(`
resource "cloudstack_firewall" "foo" {
ipaddress = "%s"
ip_address = "%s"
rule {
cidr_list = ["10.0.0.0/24"]
Expand All @@ -183,15 +183,15 @@ resource "cloudstack_firewall" "foo" {
}
rule {
source_cidr = "10.0.0.0/24"
cidr_list = ["10.0.0.0/24"]
protocol = "tcp"
ports = ["80", "1000-2000"]
}
}`, CLOUDSTACK_PUBLIC_IPADDRESS)

var testAccCloudStackFirewall_update = fmt.Sprintf(`
resource "cloudstack_firewall" "foo" {
ipaddress = "%s"
ip_address = "%s"
rule {
cidr_list = ["10.0.0.0/24", "10.0.1.0/24"]
Expand All @@ -200,13 +200,13 @@ resource "cloudstack_firewall" "foo" {
}
rule {
source_cidr = "10.0.0.0/24"
cidr_list = ["10.0.0.0/24"]
protocol = "tcp"
ports = ["80", "1000-2000"]
}
rule {
source_cidr = "172.16.100.0/24"
cidr_list = ["172.16.100.0/24"]
protocol = "tcp"
ports = ["80", "443"]
}
Expand Down
20 changes: 16 additions & 4 deletions builtin/providers/cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ func resourceCloudStackInstance() *schema.Resource {
ForceNew: true,
},

"ipaddress": &schema.Schema{
"ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"ipaddress": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Deprecated: "Please use the `ip_address` field instead",
},

"template": &schema.Schema{
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -151,8 +159,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
}

// If there is a ipaddres supplied, add it to the parameter struct
if ipaddres, ok := d.GetOk("ipaddress"); ok {
p.SetIpaddress(ipaddres.(string))
ipaddress, ok := d.GetOk("ip_address")
if !ok {
ipaddress, ok = d.GetOk("ipaddress")
}
if ok {
p.SetIpaddress(ipaddress.(string))
}

// If there is a project supplied, we retrieve and set the project id
Expand Down Expand Up @@ -228,7 +240,7 @@ func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) er
// Update the config
d.Set("name", vm.Name)
d.Set("display_name", vm.Displayname)
d.Set("ipaddress", vm.Nic[0].Ipaddress)
d.Set("ip_address", vm.Nic[0].Ipaddress)
//NB cloudstack sometimes sends back the wrong keypair name, so dont update it

setValueOrID(d, "network", vm.Nic[0].Networkname, vm.Nic[0].Networkid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestAccCloudStackInstance_fixedIP(t *testing.T) {
testAccCheckCloudStackInstanceExists(
"cloudstack_instance.foobar", &instance),
resource.TestCheckResourceAttr(
"cloudstack_instance.foobar", "ipaddress", CLOUDSTACK_NETWORK_1_IPADDRESS1),
"cloudstack_instance.foobar", "ip_address", CLOUDSTACK_NETWORK_1_IPADDRESS1),
),
},
},
Expand Down Expand Up @@ -267,7 +267,7 @@ resource "cloudstack_instance" "foobar" {
display_name = "terraform-test"
service_offering= "%s"
network = "%s"
ipaddress = "%s"
ip_address = "%s"
template = "%s"
zone = "%s"
expunge = true
Expand All @@ -288,7 +288,7 @@ resource "cloudstack_instance" "foobar" {
display_name = "terraform-test"
service_offering= "%s"
network = "%s"
ipaddress = "%s"
ip_address = "%s"
template = "%s"
zone = "%s"
keypair = "${cloudstack_ssh_keypair.foo.name}"
Expand Down
16 changes: 8 additions & 8 deletions builtin/providers/cloudstack/resource_cloudstack_ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func resourceCloudStackIPAddress() *schema.Resource {
ForceNew: true,
},

"ipaddress": &schema.Schema{
"ip_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -100,7 +100,7 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
cs := meta.(*cloudstack.CloudStackClient)

// Get the IP address details
f, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
if err != nil {
if count == 0 {
log.Printf(
Expand All @@ -113,29 +113,29 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
}

// Updated the IP address
d.Set("ipaddress", f.Ipaddress)
d.Set("ip_address", ip.Ipaddress)

if _, ok := d.GetOk("network"); ok {
// Get the network details
n, _, err := cs.Network.GetNetworkByID(f.Associatednetworkid)
n, _, err := cs.Network.GetNetworkByID(ip.Associatednetworkid)
if err != nil {
return err
}

setValueOrID(d, "network", n.Name, f.Associatednetworkid)
setValueOrID(d, "network", n.Name, ip.Associatednetworkid)
}

if _, ok := d.GetOk("vpc"); ok {
// Get the VPC details
v, _, err := cs.VPC.GetVPCByID(f.Vpcid)
v, _, err := cs.VPC.GetVPCByID(ip.Vpcid)
if err != nil {
return err
}

setValueOrID(d, "vpc", v.Name, f.Vpcid)
setValueOrID(d, "vpc", v.Name, ip.Vpcid)
}

setValueOrID(d, "project", f.Project, f.Projectid)
setValueOrID(d, "project", ip.Project, ip.Projectid)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cloudstack

import (
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -28,10 +29,19 @@ func resourceCloudStackLoadBalancerRule() *schema.Resource {
Computed: true,
},

"ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"ipaddress"},
},

"ipaddress": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Please use the `ip_address` field instead",
ConflictsWith: []string{"ip_address"},
},

"network": &schema.Schema{
Expand Down Expand Up @@ -100,8 +110,16 @@ func resourceCloudStackLoadBalancerRuleCreate(d *schema.ResourceData, meta inter
p.SetNetworkid(networkid)
}

ipaddress, ok := d.GetOk("ip_address")
if !ok {
ipaddress, ok = d.GetOk("ipaddress")
}
if !ok {
return errors.New("Either `ip_address` or [deprecated] `ipaddress` must be provided.")
}

// Retrieve the ipaddress ID
ipaddressid, e := retrieveID(cs, "ipaddress", d.Get("ipaddress").(string))
ipaddressid, e := retrieveID(cs, "ip_address", ipaddress.(string))
if e != nil {
return e.Error()
}
Expand All @@ -117,7 +135,7 @@ func resourceCloudStackLoadBalancerRuleCreate(d *schema.ResourceData, meta inter
d.SetId(r.Id)
d.SetPartial("name")
d.SetPartial("description")
d.SetPartial("ipaddress")
d.SetPartial("ip_address")
d.SetPartial("network")
d.SetPartial("algorithm")
d.SetPartial("private_port")
Expand Down Expand Up @@ -163,7 +181,7 @@ func resourceCloudStackLoadBalancerRuleRead(d *schema.ResourceData, meta interfa
d.Set("public_port", lb.Publicport)
d.Set("private_port", lb.Privateport)

setValueOrID(d, "ipaddress", lb.Publicip, lb.Publicipid)
setValueOrID(d, "ip_address", lb.Publicip, lb.Publicipid)

// Only set network if user specified it to avoid spurious diffs
if _, ok := d.GetOk("network"); ok {
Expand Down
Loading

0 comments on commit 5e6af8e

Please sign in to comment.