From ee42bbf2d96d3a2c1b9eca28815246ebd2eb03b5 Mon Sep 17 00:00:00 2001 From: chrxmvtik Date: Sun, 8 Dec 2024 20:02:23 +0100 Subject: [PATCH] feat: add cidrlist parameter to loadbalancer rule fix: acceptance tests style: remove comment --- .../resource_cloudstack_loadbalancer_rule.go | 23 +++++++++++++++++++ ...ource_cloudstack_loadbalancer_rule_test.go | 6 +++++ 2 files changed, 29 insertions(+) diff --git a/cloudstack/resource_cloudstack_loadbalancer_rule.go b/cloudstack/resource_cloudstack_loadbalancer_rule.go index 381352c3..cef1f1a9 100644 --- a/cloudstack/resource_cloudstack_loadbalancer_rule.go +++ b/cloudstack/resource_cloudstack_loadbalancer_rule.go @@ -97,6 +97,14 @@ func resourceCloudStackLoadBalancerRule() *schema.Resource { Set: schema.HashString, }, + "cidrlist": { + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "project": { Type: schema.TypeString, Optional: true, @@ -143,6 +151,16 @@ func resourceCloudStackLoadBalancerRuleCreate(d *schema.ResourceData, meta inter p.SetProtocol(protocol.(string)) } + // Set CIDR list + if cidr, ok := d.GetOk("cidrlist"); ok { + var cidrList []string + for _, id := range cidr.(*schema.Set).List() { + cidrList = append(cidrList, id.(string)) + } + + p.SetCidrlist(cidrList) + } + // Set the ipaddress id p.SetPublicipid(d.Get("ip_address_id").(string)) @@ -216,6 +234,11 @@ func resourceCloudStackLoadBalancerRuleRead(d *schema.ResourceData, meta interfa d.Set("private_port", private_port) d.Set("protocol", lb.Protocol) + // Only set cidr if user specified it to avoid spurious diffs + if _, ok := d.GetOk("cidrlist"); ok { + d.Set("cidrlist", strings.Split(lb.Cidrlist, ",")) + } + // Only set network if user specified it to avoid spurious diffs if _, ok := d.GetOk("network_id"); ok { d.Set("network_id", lb.Networkid) diff --git a/cloudstack/resource_cloudstack_loadbalancer_rule_test.go b/cloudstack/resource_cloudstack_loadbalancer_rule_test.go index 276c7569..2c51e7a4 100644 --- a/cloudstack/resource_cloudstack_loadbalancer_rule_test.go +++ b/cloudstack/resource_cloudstack_loadbalancer_rule_test.go @@ -129,6 +129,8 @@ func TestAccCloudStackLoadBalancerRule_forceNew(t *testing.T) { "cloudstack_loadbalancer_rule.foo", "private_port", "443"), resource.TestCheckResourceAttr( "cloudstack_loadbalancer_rule.foo", "protocol", "tcp-proxy"), + resource.TestCheckResourceAttr( + "cloudstack_loadbalancer_rule.foo", "cidrlist.0", "20.0.0.0/8"), ), }, }, @@ -192,6 +194,8 @@ func TestAccCloudStackLoadBalancerRule_vpcUpdate(t *testing.T) { "cloudstack_loadbalancer_rule.foo", "public_port", "443"), resource.TestCheckResourceAttr( "cloudstack_loadbalancer_rule.foo", "private_port", "443"), + resource.TestCheckResourceAttr( + "cloudstack_loadbalancer_rule.foo", "cidrlist.0", "20.0.0.0/8"), ), }, }, @@ -357,6 +361,7 @@ resource "cloudstack_loadbalancer_rule" "foo" { private_port = 443 protocol = "tcp-proxy" member_ids = [cloudstack_instance.foobar1.id] + cidrlist = ["20.0.0.0/8"] }` const testAccCloudStackLoadBalancerRule_vpc = ` @@ -451,4 +456,5 @@ resource "cloudstack_loadbalancer_rule" "foo" { public_port = 443 private_port = 443 member_ids = [cloudstack_instance.foobar1.id, cloudstack_instance.foobar2.id] + cidrlist = ["20.0.0.0/8"] }`