Skip to content

Commit 348253d

Browse files
authored
Fail when both network_id & vpc_id are set in ipaddress (apache#99)
1 parent d524e07 commit 348253d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cloudstack/resource_cloudstack_ipaddress.go

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{})
9999
if networkid, ok := d.GetOk("network_id"); ok {
100100
// Set the networkid
101101
p.SetNetworkid(networkid.(string))
102+
if vpcid, ok := d.GetOk("vpc_id"); ok && vpcid.(string) != "" {
103+
return fmt.Errorf("set only network_id or vpc_id")
104+
}
102105
}
103106

104107
if vpcid, ok := d.GetOk("vpc_id"); ok {

cloudstack/resource_cloudstack_ipaddress_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package cloudstack
2121

2222
import (
2323
"fmt"
24+
"regexp"
2425
"testing"
2526

2627
"github.com/apache/cloudstack-go/v2/cloudstack"
@@ -67,6 +68,22 @@ func TestAccCloudStackIPAddress_vpc(t *testing.T) {
6768
})
6869
}
6970

71+
func TestAccCloudStackIPAddress_vpcid_with_network_id(t *testing.T) {
72+
73+
regex := regexp.MustCompile("set only network_id or vpc_id")
74+
resource.Test(t, resource.TestCase{
75+
PreCheck: func() { testAccPreCheck(t) },
76+
Providers: testAccProviders,
77+
CheckDestroy: testAccCheckCloudStackIPAddressDestroy,
78+
Steps: []resource.TestStep{
79+
{
80+
ExpectError: regex,
81+
Config: testAccCloudStackIPAddress_vpcid_with_network_id,
82+
},
83+
},
84+
})
85+
}
86+
7087
func testAccCheckCloudStackIPAddressExists(
7188
n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
7289
return func(s *terraform.State) error {
@@ -145,3 +162,25 @@ resource "cloudstack_ipaddress" "foo" {
145162
vpc_id = "${cloudstack_vpc.foo.id}"
146163
zone = "${cloudstack_vpc.foo.zone}"
147164
}`
165+
166+
const testAccCloudStackIPAddress_vpcid_with_network_id = `
167+
resource "cloudstack_vpc" "foo" {
168+
name = "terraform-vpc"
169+
cidr = "10.0.0.0/8"
170+
vpc_offering = "Default VPC offering"
171+
zone = "Sandbox-simulator"
172+
}
173+
174+
resource "cloudstack_network" "foo" {
175+
name = "terraform-network"
176+
cidr = "10.1.1.0/24"
177+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
178+
source_nat_ip = true
179+
zone = "Sandbox-simulator"
180+
}
181+
182+
resource "cloudstack_ipaddress" "foo" {
183+
vpc_id = "${cloudstack_vpc.foo.id}"
184+
network_id = "${cloudstack_network.foo.id}"
185+
zone = "${cloudstack_vpc.foo.zone}"
186+
}`

0 commit comments

Comments
 (0)