This is an automated email from the ASF dual-hosted git repository.
sureshanaparti 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 fef3030 fix: make cidr optional for L2 network creation (#289)
fef3030 is described below
commit fef3030c5f90e1819902e223a45352982cc93f30
Author: Daman Arora <[email protected]>
AuthorDate: Fri Aug 14 03:12:18 2026 -0400
fix: make cidr optional for L2 network creation (#289)
* Make cidr optional for L2 network creation
* Make CIDR field computed and optional for L2 network resource
* Add acceptance test for L2 network without CIDR
* Add required_with validation for gateway, startip, and endip fields in L2
network resource
---
cloudstack/resource_cloudstack_network.go | 70 ++++++++++++++------------
cloudstack/resource_cloudstack_network_test.go | 56 +++++++++++++++++++++
2 files changed, 94 insertions(+), 32 deletions(-)
diff --git a/cloudstack/resource_cloudstack_network.go
b/cloudstack/resource_cloudstack_network.go
index e7329f8..cc754ce 100644
--- a/cloudstack/resource_cloudstack_network.go
+++ b/cloudstack/resource_cloudstack_network.go
@@ -74,29 +74,33 @@ func resourceCloudStackNetwork() *schema.Resource {
"cidr": {
Type: schema.TypeString,
- Required: true,
+ Optional: true,
+ Computed: true,
ForceNew: true,
},
"gateway": {
- Type: schema.TypeString,
- Optional: true,
- Computed: true,
- ForceNew: true,
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ ForceNew: true,
+ RequiredWith: []string{"cidr"},
},
"startip": {
- Type: schema.TypeString,
- Optional: true,
- Computed: true,
- ForceNew: true,
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ ForceNew: true,
+ RequiredWith: []string{"cidr"},
},
"endip": {
- Type: schema.TypeString,
- Optional: true,
- Computed: true,
- ForceNew: true,
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ ForceNew: true,
+ RequiredWith: []string{"cidr"},
},
"network_domain": {
@@ -184,29 +188,31 @@ func resourceCloudStackNetworkCreate(d
*schema.ResourceData, meta interface{}) e
p.SetDisplaytext(name)
}
- // Get the network offering to check if it supports specifying IP ranges
- no, _, err :=
cs.NetworkOffering.GetNetworkOfferingByID(networkofferingid)
- if err != nil {
- return err
- }
+ if _, ok := d.GetOk("cidr"); ok {
+ // Get the network offering to check if it supports specifying
IP ranges
+ no, _, err :=
cs.NetworkOffering.GetNetworkOfferingByID(networkofferingid)
+ if err != nil {
+ return err
+ }
- m, err := parseCIDR(d, no.Specifyipranges)
- if err != nil {
- return err
- }
+ m, err := parseCIDR(d, no.Specifyipranges)
+ if err != nil {
+ return err
+ }
- // Set the needed IP config
- p.SetGateway(m["gateway"])
- p.SetNetmask(m["netmask"])
+ // Set the needed IP config
+ p.SetGateway(m["gateway"])
+ p.SetNetmask(m["netmask"])
- // Only set the start IP if we have one
- if startip, ok := m["startip"]; ok {
- p.SetStartip(startip)
- }
+ // Only set the start IP if we have one
+ if startip, ok := m["startip"]; ok {
+ p.SetStartip(startip)
+ }
- // Only set the end IP if we have one
- if endip, ok := m["endip"]; ok {
- p.SetEndip(endip)
+ // Only set the end IP if we have one
+ if endip, ok := m["endip"]; ok {
+ p.SetEndip(endip)
+ }
}
// Set the network domain if we have one
diff --git a/cloudstack/resource_cloudstack_network_test.go
b/cloudstack/resource_cloudstack_network_test.go
index 0b650ac..5a7e863 100644
--- a/cloudstack/resource_cloudstack_network_test.go
+++ b/cloudstack/resource_cloudstack_network_test.go
@@ -377,3 +377,59 @@ resource "cloudstack_network" "foo" {
acl_id = cloudstack_network_acl.bar.id
zone = cloudstack_vpc.foo.zone
}`
+
+func TestAccCloudStackNetwork_l2NoCidr(t *testing.T) {
+ var network cloudstack.Network
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckCloudStackNetworkDestroy,
+ Steps: []resource.TestStep{
+ {
+ Config: testAccCloudStackNetwork_l2NoCidr,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckCloudStackNetworkExists(
+ "cloudstack_network.l2",
&network),
+ ),
+ },
+ },
+ })
+}
+
+const testAccCloudStackNetwork_l2NoCidr = `
+resource "cloudstack_network" "l2" {
+ name = "terraform-l2-network"
+ display_text = "terraform-l2-network"
+ network_offering = "DefaultL2NetworkOffering"
+ zone = "Sandbox-simulator"
+}`
+
+func TestAccCloudStackNetwork_isolatedNoCidr(t *testing.T) {
+ var network cloudstack.Network
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckCloudStackNetworkDestroy,
+ Steps: []resource.TestStep{
+ {
+ Config: testAccCloudStackNetwork_isolatedNoCidr,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckCloudStackNetworkExists(
+
"cloudstack_network.isolated_no_cidr", &network),
+ resource.TestCheckResourceAttrSet(
+
"cloudstack_network.isolated_no_cidr", "cidr"),
+ ),
+ },
+ },
+ })
+}
+
+const testAccCloudStackNetwork_isolatedNoCidr = `
+resource "cloudstack_network" "isolated_no_cidr" {
+ name = "terraform-isolated-no-cidr"
+ display_text = "terraform-isolated-no-cidr"
+ network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
+ zone = "Sandbox-simulator"
+}`