Skip to content

Commit

Permalink
Merge pull request #224 from kingsoftcloud/trunk
Browse files Browse the repository at this point in the history
Trunk
  • Loading branch information
notone0010 authored Jul 30, 2024
2 parents 8566377 + 3d3890d commit 6c12536
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 134 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 1.16.2 (Jul 30, 2024)

FEATURES:

- - **Deprecated Resource:** `ksyun_lb_listener_associate_backendgroup` 使用`backend_server_group_mounted` 字段替代

IMPROVEMENTS:

- `ksyun_lb_listener`: 新增`backend_server_group_mounted`字段,支持挂载后端服务器组
- `ksyun_nat`: `nat_line_id` 标记为Computed


## 1.16.1 (Jul 23, 2024)

FEATURES:
Expand Down
56 changes: 56 additions & 0 deletions ksyun/internal/pkg/helper/hashcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package helper

import (
"bytes"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// generate a hash code that's value if from 'backend_server_group_id'
// and its type is schema.SchemaSetFunc
func HashFuncWithKeys(keys ...string) schema.SchemaSetFunc {
return func(v interface{}) int {
if v == nil {
return hashcode.String("")
}
m := v.(map[string]interface{})
var buf bytes.Buffer

for _, key := range keys {
if vv, ok := m[key]; ok {
switch vv.(type) {
case string:
if vv == "" {
break
}
buf.WriteString(fmt.Sprintf("%s", strings.ToLower(vv.(string))))
case int:
buf.WriteString(fmt.Sprintf("%d", vv.(int)))
case float64:
buf.WriteString(fmt.Sprintf("%d", int(vv.(float64))))
case bool:
if vv.(bool) {
buf.WriteString("1")
} else {
buf.WriteString("0")
}
case []interface{}:
vvs := vv.([]interface{})
if len(vvs) < 1 {
buf.WriteString("[]")
} else {
buf.WriteString("[")
for _, vvv := range vvs {
buf.WriteString(fmt.Sprintf("%s", strings.ToLower(vvv.(string))))
}
buf.WriteString("]")
}
}
}
}
return hashcode.String(buf.String())
}
}
3 changes: 1 addition & 2 deletions ksyun/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ SLB
ksyun_lb_listener_associate_acl
ksyun_lb_listener_server
ksyun_lb_rule
ksyun_lb_listener_associate_backendgroup
ALB
Expand Down Expand Up @@ -598,7 +597,7 @@ func Provider() terraform.ResourceProvider {
// "ksyun_security_group_entry_set": resourceKsyunSecurityGroupEntrySet(),

// lb
"ksyun_lb_listener_associate_backendgroup": resourceKsyunLbListenerAssociateBackendgroup(),
// "ksyun_lb_listener_associate_backendgroup": resourceKsyunLbListenerAssociateBackendgroup(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
34 changes: 34 additions & 0 deletions ksyun/resource_ksyun_lb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-ksyun/ksyun/internal/pkg/helper"
)

func resourceKsyunListener() *schema.Resource {
Expand Down Expand Up @@ -255,6 +256,23 @@ func resourceKsyunListener() *schema.Resource {
// Computed: true,
},

"backend_server_group_mounted": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"backend_server_group_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of backend server group.",
},
},
},
Description: "The backend server group to mount.",
Set: helper.HashFuncWithKeys("backend_server_group_id"),
},

"listener_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -275,6 +293,13 @@ func resourceKsyunListenerCreate(d *schema.ResourceData, meta interface{}) (err
if err != nil {
return fmt.Errorf("error on creating listener %q, %s", d.Id(), err)
}
// mount backend server group
if d.HasChange("backend_server_group_mounted") {
err = slbService.ListenerMountBackendGroupWithSet(d)
if err != nil {
return fmt.Errorf("error on mounting backend server group %q, %s", d.Id(), err)
}
}
return resourceKsyunListenerRead(d, meta)
}

Expand All @@ -293,6 +318,15 @@ func resourceKsyunListenerUpdate(d *schema.ResourceData, meta interface{}) (err
if err != nil {
return fmt.Errorf("error on updating listener %q, %s", d.Id(), err)
}

// mount backend server group
if d.HasChange("backend_server_group_mounted") {
err = slbService.ListenerMountBackendGroupWithSet(d)
if err != nil {
return fmt.Errorf("error on mounting backend server group %q, %s", d.Id(), err)
}
}

return resourceKsyunListenerRead(d, meta)
}

Expand Down
59 changes: 5 additions & 54 deletions ksyun/resource_ksyun_lb_listener_associate_backendgroup.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
/*
Provides slb listener mount backend server group resource.
~> **NOTE:** This resource is **deprecated**. Use `backend_server_group_mounted` of `ksyun_lb_listener` instead. See [ksyun_lb_listener](https://registry.terraform.io/providers/kingsoftcloud/ksyun/latest/docs/resources/lb_listener) for more details.
# Example Usage
```hcl
resource "ksyun_vpc" "test" {
vpc_name = "tf-alb-vpc-1"
cidr_block = "10.0.0.0/16"
}
resource "ksyun_subnet" "test" {
subnet_name = "tf-alb-subnet"
cidr_block = "10.0.1.0/24"
subnet_type = "Normal"
availability_zone = var.az
vpc_id = ksyun_vpc.test.id
}
resource "ksyun_lb" "default" {
vpc_id = ksyun_vpc.test.id
load_balancer_name = "tf-xun1"
type = "public"
}
data "ksyun_certificates" "default" {
}
resource "ksyun_lb_listener" "default" {
listener_name = "tf-xun"
listener_port = "8000"
listener_protocol = "TCP"
listener_state = "start"
load_balancer_id = ksyun_lb.default.id
method = "LeastConnections"
bind_type = "BackendServerGroup"
certificate_id = data.ksyun_certificates.default.certificates.0.certificate_id
}
resource "ksyun_lb_backend_server_group" "default" {
backend_server_group_name = "xuan-tf"
vpc_id = ksyun_vpc.test.id
backend_server_group_type = "Server"
protocol = "TCP"
health_check {
host_name = "www.ksyun.com"
healthy_threshold = 10
interval = 100
timeout = 300
unhealthy_threshold = 10
}
}
# associate backend server group with listener
resource "ksyun_lb_listener_associate_backendgroup" "mount" {
listener_id = ksyun_lb_listener.default.id
backend_server_group_id = ksyun_lb_backend_server_group.default.id
}
```
Expand Down Expand Up @@ -112,6 +60,9 @@ func resourceKsyunLbListenerAssociateBackendgroupCreate(d *schema.ResourceData,
if err != nil {
return fmt.Errorf("error on mounting backend group onto listener %q, %s", d.Id(), err)
}

id := AssembleIds(d.Get("listener_id").(string), d.Get("backend_server_group_id").(string))
d.SetId(id)
return resourceKsyunLbListenerAssociateBackendgroupRead(d, meta)
}

Expand Down
81 changes: 77 additions & 4 deletions ksyun/resource_ksyun_lb_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ksyun

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"testing"
)

func TestAccKsyunListener_basic(t *testing.T) {
Expand All @@ -18,9 +19,9 @@ func TestAccKsyunListener_basic(t *testing.T) {
CheckDestroy: testAccCheckListenerDestroy,
Steps: []resource.TestStep{
{
Config: testAccListenerConfig,
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckListenerExists("ksyun_lb_listener.foo", &val),
testAccCheckListenerExists("ksyun_lb_listener.default", &val),
testAccCheckListenerAttributes(&val),
),
},
Expand Down Expand Up @@ -83,6 +84,7 @@ func testAccCheckListenerExists(n string, val *map[string]interface{}) resource.
return nil
}
}

func testAccCheckListenerAttributes(val *map[string]interface{}) resource.TestCheckFunc {
return func(s *terraform.State) error {
if val != nil {
Expand All @@ -94,6 +96,7 @@ func testAccCheckListenerAttributes(val *map[string]interface{}) resource.TestCh
return nil
}
}

func testAccCheckListenerDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "ksyun_lb_listener" {
Expand All @@ -104,7 +107,6 @@ func testAccCheckListenerDestroy(s *terraform.State) error {
listener := make(map[string]interface{})
listener["ListenerId.1"] = rs.Primary.ID
ptr, err := client.slbconn.DescribeListeners(&listener)

// Verify the error is what we want
if err != nil {
return err
Expand Down Expand Up @@ -182,3 +184,74 @@ resource "ksyun_lb_listener" "foo" {
}
}
`

const config = `
variable "az" {
default = "cn-beijing-6b"
}
provider "ksyun" {
region = "cn-beijing-6"
}
resource "ksyun_vpc" "test" {
vpc_name = "tf-alb-vpc-1"
cidr_block = "10.0.0.0/16"
}
resource "ksyun_subnet" "test" {
subnet_name = "tf-alb-subnet"
cidr_block = "10.0.1.0/24"
subnet_type = "Normal"
availability_zone = var.az
vpc_id = ksyun_vpc.test.id
}
resource "ksyun_lb" "default" {
vpc_id = ksyun_vpc.test.id
load_balancer_name = "tf-xun1"
type = "public"
}
data "ksyun_certificates" "default" {
}
resource "ksyun_lb_listener" "default" {
listener_name = "tf-xun"
listener_port = "8000"
listener_protocol = "UDP"
listener_state = "start"
load_balancer_id = ksyun_lb.default.id
method = "LeastConnections"
bind_type = "BackendServerGroup"
certificate_id = data.ksyun_certificates.default.certificates.0.certificate_id
backend_server_group_mounted {
backend_server_group_id = ksyun_lb_backend_server_group.default.id
}
}
resource "ksyun_lb_backend_server_group" "default" {
backend_server_group_name = "xuan-tf"
vpc_id = ksyun_vpc.test.id
backend_server_group_type = "Server"
protocol = "UDP"
# health_check {
# host_name = "www.ksyun.com"
# healthy_threshold = 10
# interval = 100
# timeout = 300
# unhealthy_threshold = 10
# }
}
resource "ksyun_lb_backend_server_group" "default1" {
backend_server_group_name = "xuan-tf"
vpc_id = ksyun_vpc.test.id
backend_server_group_type = "Server"
protocol = "HTTP"
}
`
6 changes: 4 additions & 2 deletions ksyun/resource_ksyun_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func resourceKsyunNat() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"nat_line_id": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
// ForceNew: true,
Description: "ID of the line.",
},
"project_id": {
Expand Down
Loading

0 comments on commit 6c12536

Please sign in to comment.