Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CLOUDDEV-374): lb allowed cidrs #35

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/lblistener.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ output "view" {

### Read-Only

- `allowed_cidrs` (List of String) The allowed CIDRs for listener.
- `id` (String) The ID of this resource.
- `operating_status` (String) The current operational status of the load balancer.
- `pool_count` (Number) Number of pools associated with the load balancer.
Expand Down
1 change: 0 additions & 1 deletion docs/data-sources/loadbalancerv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ output "view" {

- `id` (String) The ID of this resource.
- `metadata_read_only` (List of Object) A list of read-only metadata items, e.g. tags. (see [below for nested schema](#nestedatt--metadata_read_only))
- `security_group_id` (String) Load balancer security group ID
- `vip_address` (String) Load balancer IP address
- `vip_port_id` (String) Attached reserved IP.

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/lblistener.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "edgecenter_lblistener" "listener" {
name = "test"
protocol = "TCP"
protocol_port = 36621
allowed_cidrs = ["127.0.0.0/24", "192.168.0.0/24"]
loadbalancer_id = edgecenter_loadbalancerv2.lb.id
}
```
Expand All @@ -46,6 +47,7 @@ resource "edgecenter_lblistener" "listener" {

### Optional

- `allowed_cidrs` (List of String) The allowed CIDRs for listener.
- `insert_x_forwarded` (Boolean) Insert *-forwarded headers
- `last_updated` (String) The timestamp of the last update (use with update context).
- `project_id` (Number) The uuid of the project. Either 'project_id' or 'project_name' must be specified.
Expand Down
2 changes: 0 additions & 2 deletions docs/resources/loadbalancerv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ resource "edgecenter_loadbalancerv2" "lb" {
- `project_name` (String) The name of the project. Either 'project_id' or 'project_name' must be specified.
- `region_id` (Number) The uuid of the region. Either 'region_id' or 'region_name' must be specified.
- `region_name` (String) The name of the region. Either 'region_id' or 'region_name' must be specified.
- `security_group` (String) Creates a new security group with the specified name
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `vip_network_id` (String) Attaches the created network.
- `vip_port_id` (String) Attaches the created reserved IP.
Expand All @@ -54,7 +53,6 @@ resource "edgecenter_loadbalancerv2" "lb" {

- `id` (String) The ID of this resource.
- `metadata_read_only` (List of Object) A list of read-only metadata items, e.g. tags. (see [below for nested schema](#nestedatt--metadata_read_only))
- `security_group_id` (String) Load balancer security group ID
- `vip_address` (String) Load balancer IP address

<a id="nestedblock--timeouts"></a>
Expand Down
25 changes: 16 additions & 9 deletions edgecenter/data_source_edgecenter_lblistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func dataSourceLBListener() *schema.Resource {
Computed: true,
Description: "The current provisioning status of the load balancer.",
},
"allowed_cidrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "The allowed CIDRs for listener.",
},
},
}
}
Expand Down Expand Up @@ -102,10 +108,10 @@ func dataSourceLBListenerRead(_ context.Context, d *schema.ResourceData, m inter
}

var found bool
var lb listeners.Listener
var listener listeners.Listener
for _, l := range ls {
if l.Name == name {
lb = l
listener = l
found = true
break
}
Expand All @@ -115,16 +121,17 @@ func dataSourceLBListenerRead(_ context.Context, d *schema.ResourceData, m inter
return diag.Errorf("lb listener with name %s not found", name)
}

d.SetId(lb.ID)
d.Set("name", lb.Name)
d.Set("protocol", lb.Protocol.String())
d.Set("protocol_port", lb.ProtocolPort)
d.Set("pool_count", lb.PoolCount)
d.Set("operating_status", lb.OperationStatus.String())
d.Set("provisioning_status", lb.ProvisioningStatus.String())
d.SetId(listener.ID)
d.Set("name", listener.Name)
d.Set("protocol", listener.Protocol.String())
d.Set("protocol_port", listener.ProtocolPort)
d.Set("pool_count", listener.PoolCount)
d.Set("operating_status", listener.OperationStatus.String())
d.Set("provisioning_status", listener.ProvisioningStatus.String())
d.Set("loadbalancer_id", lbID)
d.Set("project_id", d.Get("project_id").(int))
d.Set("region_id", d.Get("region_id").(int))
d.Set("allowed_cidrs", listener.AllowedCIDRs)

log.Println("[DEBUG] Finish LBListener reading")

Expand Down
13 changes: 0 additions & 13 deletions edgecenter/data_source_edgecenter_loadbalancerv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ func dataSourceLoadBalancerV2() *schema.Resource {
Computed: true,
Description: "Attached reserved IP.",
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Load balancer security group ID",
},
"metadata_read_only": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -171,14 +166,6 @@ func dataSourceLoadBalancerV2Read(_ context.Context, d *schema.ResourceData, m i
return diag.FromErr(err)
}

sgInfo, err := loadbalancers.ListCustomSecurityGroup(client, d.Id()).Extract()
if err != nil {
return diag.FromErr(err)
}
if len(sgInfo) > 0 {
d.Set("security_group_id", sgInfo[0].ID)
}

log.Println("[DEBUG] Finish LoadBalancer reading")

return diags
Expand Down
25 changes: 25 additions & 0 deletions edgecenter/resource_edgecenter_lblistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func resourceLbListener() *schema.Resource {
Optional: true,
Description: "List of secret identifiers used for Server Name Indication (SNI).",
},
"allowed_cidrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The allowed CIDRs for listener.",
},
"last_updated": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -203,6 +209,14 @@ func resourceLBListenerCreate(ctx context.Context, d *schema.ResourceData, m int
return diag.Errorf("wrong protocol")
}

allowedCIRDsRaw := d.Get("allowed_cidrs").([]interface{})
if len(allowedCIRDsRaw) > 0 {
opts.AllowedCIDRs = make([]string, len(allowedCIRDsRaw))
for i, s := range allowedCIRDsRaw {
opts.AllowedCIDRs[i] = s.(string)
}
}

results, err := listeners.Create(client, opts).Extract()
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -255,6 +269,7 @@ func resourceLBListenerRead(_ context.Context, d *schema.ResourceData, m interfa
d.Set("provisioning_status", lb.ProvisioningStatus.String())
d.Set("secret_id", lb.SecretID)
d.Set("sni_secret_id", lb.SNISecretID)
d.Set("allowed_cidrs", lb.AllowedCIDRs)

fields := []string{"project_id", "region_id", "loadbalancer_id", "insert_x_forwarded"}
revertState(d, &fields)
Expand Down Expand Up @@ -304,6 +319,16 @@ func resourceLBListenerUpdate(ctx context.Context, d *schema.ResourceData, m int
changed = true
}

if d.HasChange("allowed_cidrs") {
allowedCIDRsRaw := d.Get("allowed_cidrs").([]interface{})
allowedCIDRs := make([]string, len(allowedCIDRsRaw))
for i, s := range allowedCIDRsRaw {
allowedCIDRs[i] = s.(string)
}
opts.AllowedCIDRs = allowedCIDRs
changed = true
}

if changed {
_, err = listeners.Update(client, d.Id(), opts).Extract()
if err != nil {
Expand Down
36 changes: 0 additions & 36 deletions edgecenter/resource_edgecenter_loadbalancerv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/Edge-Center/edgecentercloud-go/edgecenter/loadbalancer/v1/loadbalancers"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/securitygroup/v1/securitygroups"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/task/v1/tasks"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/utils"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/utils/metadata"
Expand Down Expand Up @@ -113,16 +112,6 @@ func resourceLoadBalancerV2() *schema.Resource {
Computed: true,
Description: "The timestamp of the last update (use with update context).",
},
"security_group": {
Type: schema.TypeString,
Optional: true,
Description: "Creates a new security group with the specified name",
},
"security_group_id": {
Type: schema.TypeString,
Description: "Load balancer security group ID",
Computed: true,
},
"metadata_map": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -210,31 +199,6 @@ func resourceLoadBalancerV2Create(ctx context.Context, d *schema.ResourceData, m

d.SetId(lbID.(string))

securityGroup := d.Get("security_group").(string)
if securityGroup != "" {
if err := loadbalancers.CreateCustomSecurityGroup(client, d.Id()).ExtractErr(); err != nil {
return diag.FromErr(err)
}

sgInfo, err := loadbalancers.ListCustomSecurityGroup(client, d.Id()).Extract()
if err != nil {
return diag.FromErr(err)
}

if len(sgInfo) > 0 {
sgID := sgInfo[0].ID
d.Set("security_group_id", sgID)
clientSG, err := CreateClient(provider, d, SecurityGroupPoint, VersionPointV1)
if err != nil {
return diag.FromErr(err)
}
_, err = securitygroups.Update(clientSG, sgID, securitygroups.UpdateOpts{Name: securityGroup}).Extract()
if err != nil {
return diag.FromErr(err)
}
}
}

resourceLoadBalancerV2Read(ctx, d, m)

log.Printf("[DEBUG] Finish LoadBalancer creating (%s)", lbID)
Expand Down
1 change: 1 addition & 0 deletions edgecenter/test/data_source_edgecenter_lblistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccLBListenerDataSource(t *testing.T) {
Name: lbListenerTestName,
ProtocolPort: 80,
Protocol: types.ProtocolTypeHTTP,
AllowedCIDRs: []string{"127.0.0.0/24"},
anaxaim marked this conversation as resolved.
Show resolved Hide resolved
}},
}

Expand Down
1 change: 1 addition & 0 deletions examples/resources/edgecenter_lblistener/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ resource "edgecenter_lblistener" "listener" {
name = "test"
protocol = "TCP"
protocol_port = 36621
allowed_cidrs = ["127.0.0.0/24", "192.168.0.0/24"]
loadbalancer_id = edgecenter_loadbalancerv2.lb.id
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Edge-Center/edgecenter-dns-sdk-go v0.1.0
github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0
github.com/Edge-Center/edgecentercdn-go v0.1.4
github.com/Edge-Center/edgecentercloud-go v0.1.10
github.com/Edge-Center/edgecentercloud-go v0.1.11
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/mitchellh/mapstructure v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0 h1:1aPDpywWbaF7VEjP/GjVo
github.com/Edge-Center/edgecenter-storage-sdk-go v0.2.0/go.mod h1:TcWO0BPvDsE6AGlPBqpKCZhoQ70rRlqmm85J32qcL8I=
github.com/Edge-Center/edgecentercdn-go v0.1.4 h1:Jt8f+CSriwVQ/KAb+a+v1dDNChtHjlpilgJOX8mOSx0=
github.com/Edge-Center/edgecentercdn-go v0.1.4/go.mod h1:RwEyxwPAmxor1mZKUTa2bIU2p5qM6kcAofUkaE4O1V4=
github.com/Edge-Center/edgecentercloud-go v0.1.10 h1:+mtt9/n4RBTKZwF0N5xlvCNTE74R+Qk29Na+zZgy/Vk=
github.com/Edge-Center/edgecentercloud-go v0.1.10/go.mod h1:kmXGtx0lL1ib+SPfJe/uIAyDHamquAvqiftoLSyhxF8=
github.com/Edge-Center/edgecentercloud-go v0.1.11 h1:00h5o/71lEoSdU1B4AWmviuOfO28P6nsRP+afjIsW80=
github.com/Edge-Center/edgecentercloud-go v0.1.11/go.mod h1:kmXGtx0lL1ib+SPfJe/uIAyDHamquAvqiftoLSyhxF8=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 h1:KLq8BE0KwCL+mmXnjLWEAOYO+2l2AE4YMmqG1ZpZHBs=
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
Expand Down