From ddd81930fb9e5e3418912276e0d860502f89b4d9 Mon Sep 17 00:00:00 2001 From: Vishesh Date: Wed, 13 Mar 2024 15:10:37 +0530 Subject: [PATCH 1/3] Fixup network_id in ipaddress when vpc_id is specified --- cloudstack/resource_cloudstack_ipaddress.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cloudstack/resource_cloudstack_ipaddress.go b/cloudstack/resource_cloudstack_ipaddress.go index 98ad47ec..8f5f1ffc 100644 --- a/cloudstack/resource_cloudstack_ipaddress.go +++ b/cloudstack/resource_cloudstack_ipaddress.go @@ -45,6 +45,7 @@ func resourceCloudStackIPAddress() *schema.Resource { "network_id": { Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: true, }, From c4e98eebefc337b51fdf871ee8e100388002a41d Mon Sep 17 00:00:00 2001 From: Vishesh Date: Thu, 14 Mar 2024 12:30:05 +0530 Subject: [PATCH 2/3] Allow either vpcid or network id to be set but not both --- cloudstack/resource_cloudstack_ipaddress.go | 3 ++ .../resource_cloudstack_ipaddress_test.go | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/cloudstack/resource_cloudstack_ipaddress.go b/cloudstack/resource_cloudstack_ipaddress.go index 8f5f1ffc..5a1ad08f 100644 --- a/cloudstack/resource_cloudstack_ipaddress.go +++ b/cloudstack/resource_cloudstack_ipaddress.go @@ -100,6 +100,9 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) if networkid, ok := d.GetOk("network_id"); ok { // Set the networkid p.SetNetworkid(networkid.(string)) + if vpcid, ok := d.GetOk("vpc_id"); ok && vpcid.(string) != "" { + return fmt.Errorf("set only network_id or vpc_id") + } } if vpcid, ok := d.GetOk("vpc_id"); ok { diff --git a/cloudstack/resource_cloudstack_ipaddress_test.go b/cloudstack/resource_cloudstack_ipaddress_test.go index 90058de0..1f4b3fa5 100644 --- a/cloudstack/resource_cloudstack_ipaddress_test.go +++ b/cloudstack/resource_cloudstack_ipaddress_test.go @@ -21,6 +21,7 @@ package cloudstack import ( "fmt" + "regexp" "testing" "github.com/apache/cloudstack-go/v2/cloudstack" @@ -67,6 +68,22 @@ func TestAccCloudStackIPAddress_vpc(t *testing.T) { }) } +func TestAccCloudStackIPAddress_vpcid_with_network_id(t *testing.T) { + + regex := regexp.MustCompile("set only network_id or vpc_id") + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudStackIPAddressDestroy, + Steps: []resource.TestStep{ + { + ExpectError: regex, + Config: testAccCloudStackIPAddress_vpcid_with_network_id, + }, + }, + }) +} + func testAccCheckCloudStackIPAddressExists( n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -145,3 +162,25 @@ resource "cloudstack_ipaddress" "foo" { vpc_id = "${cloudstack_vpc.foo.id}" zone = "${cloudstack_vpc.foo.zone}" }` + +const testAccCloudStackIPAddress_vpcid_with_network_id = ` +resource "cloudstack_vpc" "foo" { + name = "terraform-vpc" + cidr = "10.0.0.0/8" + vpc_offering = "Default VPC offering" + zone = "Sandbox-simulator" +} + +resource "cloudstack_network" "foo" { + name = "terraform-network" + cidr = "10.1.1.0/24" + network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" + source_nat_ip = true + zone = "Sandbox-simulator" + } + +resource "cloudstack_ipaddress" "foo" { + vpc_id = "${cloudstack_vpc.foo.id}" + network_id = "${cloudstack_network.foo.id}" + zone = "${cloudstack_vpc.foo.zone}" +}` From 1b6cce43133f005d6e4587813f7b2c9c546e2ac1 Mon Sep 17 00:00:00 2001 From: Vishesh Date: Thu, 14 Mar 2024 14:08:44 +0530 Subject: [PATCH 3/3] fixup --- cloudstack/resource_cloudstack_ipaddress.go | 1 - cloudstack/resource_cloudstack_ipaddress_test.go | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cloudstack/resource_cloudstack_ipaddress.go b/cloudstack/resource_cloudstack_ipaddress.go index 5a1ad08f..10a7c73a 100644 --- a/cloudstack/resource_cloudstack_ipaddress.go +++ b/cloudstack/resource_cloudstack_ipaddress.go @@ -45,7 +45,6 @@ func resourceCloudStackIPAddress() *schema.Resource { "network_id": { Type: schema.TypeString, Optional: true, - Computed: true, ForceNew: true, }, diff --git a/cloudstack/resource_cloudstack_ipaddress_test.go b/cloudstack/resource_cloudstack_ipaddress_test.go index 1f4b3fa5..98a32f6b 100644 --- a/cloudstack/resource_cloudstack_ipaddress_test.go +++ b/cloudstack/resource_cloudstack_ipaddress_test.go @@ -172,12 +172,12 @@ resource "cloudstack_vpc" "foo" { } resource "cloudstack_network" "foo" { - name = "terraform-network" - cidr = "10.1.1.0/24" - network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" - source_nat_ip = true - zone = "Sandbox-simulator" - } + name = "terraform-network" + cidr = "10.1.1.0/24" + network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService" + source_nat_ip = true + zone = "Sandbox-simulator" +} resource "cloudstack_ipaddress" "foo" { vpc_id = "${cloudstack_vpc.foo.id}"