You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by vi...@apache.org on 2024/03/14 11:55:27 UTC

(cloudstack-terraform-provider) branch main updated: Fail when both network_id & vpc_id are set in ipaddress (#99)

This is an automated email from the ASF dual-hosted git repository.

vishesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-terraform-provider.git


The following commit(s) were added to refs/heads/main by this push:
     new 348253d  Fail when both network_id & vpc_id are set in ipaddress (#99)
348253d is described below

commit 348253d7586d75d2ebc7895b6ec7f6e4066269f8
Author: Vishesh <vi...@gmail.com>
AuthorDate: Thu Mar 14 17:25:22 2024 +0530

    Fail when both network_id & vpc_id are set in ipaddress (#99)
---
 cloudstack/resource_cloudstack_ipaddress.go      |  3 ++
 cloudstack/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 98ad47e..10a7c73 100644
--- a/cloudstack/resource_cloudstack_ipaddress.go
+++ b/cloudstack/resource_cloudstack_ipaddress.go
@@ -99,6 +99,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 90058de..98a32f6 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}"
+}`