Skip to content

Commit

Permalink
(CLOUDDEV-354): lb pools
Browse files Browse the repository at this point in the history
  • Loading branch information
anaxaim committed Dec 13, 2023
1 parent f6656b1 commit 6eba8de
Show file tree
Hide file tree
Showing 14 changed files with 851 additions and 20 deletions.
77 changes: 77 additions & 0 deletions edgecenter/converter/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package converter

import (
"net"

edgecloud "github.com/Edge-Center/edgecentercloud-go"
)

Expand Down Expand Up @@ -59,3 +61,78 @@ func ListInterfaceToListInstanceInterface(interfaces []interface{}) ([]edgecloud

return ifs, nil
}

// ListInterfaceToLoadbalancerSessionPersistence creates a session persistence options struct.
func ListInterfaceToLoadbalancerSessionPersistence(sessionPersistence []interface{}) *edgecloud.LoadbalancerSessionPersistence {
var sp *edgecloud.LoadbalancerSessionPersistence

if len(sessionPersistence) > 0 {
sessionPersistenceMap := sessionPersistence[0].(map[string]interface{})
sp = &edgecloud.LoadbalancerSessionPersistence{
Type: edgecloud.SessionPersistence(sessionPersistenceMap["type"].(string)),
}

if granularity, ok := sessionPersistenceMap["persistence_granularity"].(string); ok {
sp.PersistenceGranularity = granularity
}

if timeout, ok := sessionPersistenceMap["persistence_timeout"].(int); ok {
sp.PersistenceTimeout = timeout
}

if cookieName, ok := sessionPersistenceMap["cookie_name"].(string); ok {
sp.CookieName = cookieName
}
}

return sp
}

// ListInterfaceToHealthMonitor creates a heath monitor options struct.
func ListInterfaceToHealthMonitor(healthMonitor []interface{}) edgecloud.HealthMonitorCreateRequest {
var hm edgecloud.HealthMonitorCreateRequest

if len(healthMonitor) > 0 {
healthMonitorMap := healthMonitor[0].(map[string]interface{})
hm = edgecloud.HealthMonitorCreateRequest{
Timeout: healthMonitorMap["timeout"].(int),
Delay: healthMonitorMap["delay"].(int),
Type: edgecloud.HealthMonitorType(healthMonitorMap["type"].(string)),
MaxRetries: healthMonitorMap["max_retries"].(int),
}

if httpMethod, ok := healthMonitorMap["http_method"].(string); ok {
hm.HTTPMethod = edgecloud.HTTPMethod(httpMethod)
}

if urlPath, ok := healthMonitorMap["url_path"].(string); ok {
hm.URLPath = urlPath
}

if maxRetriesDown, ok := healthMonitorMap["max_retries_down"].(int); ok {
hm.MaxRetriesDown = maxRetriesDown
}

if expectedCodes, ok := healthMonitorMap["expected_codes"].(string); ok {
hm.ExpectedCodes = expectedCodes
}
}

return hm
}

func ListInterfaceToListPoolMember(poolMembers []interface{}) ([]edgecloud.PoolMemberCreateRequest, error) {
members := make([]edgecloud.PoolMemberCreateRequest, len(poolMembers))
for i, member := range poolMembers {
m := member.(map[string]interface{})
address := m["address"].(string)
m["address"] = net.ParseIP(address)
var M edgecloud.PoolMemberCreateRequest
if err := MapStructureDecoder(&M, &m, decoderConfig); err != nil {
return nil, err
}
members[i] = M
}

return members, nil
}
6 changes: 3 additions & 3 deletions edgecenter/instance/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ func attachInterface(ctx context.Context, d *schema.ResourceData, client *edgecl
attachInterfaceRequest := &edgecloud.InstanceAttachInterfaceRequest{Type: iType}

switch iType { //nolint: exhaustive
case edgecloud.SubnetInterfaceType:
case edgecloud.InterfaceTypeSubnet:
attachInterfaceRequest.SubnetID = ifs["subnet_id"].(string)
case edgecloud.AnySubnetInterfaceType:
case edgecloud.InterfaceTypeAnySubnet:
attachInterfaceRequest.NetworkID = ifs["network_id"].(string)
case edgecloud.ReservedFixedIPType:
case edgecloud.InterfaceTypeReservedFixedIP:
attachInterfaceRequest.PortID = ifs["port_id"].(string)
}
attachInterfaceRequest.SecurityGroups = getSecurityGroupsIDs(ifs["security_groups"].([]interface{}))
Expand Down
14 changes: 6 additions & 8 deletions edgecenter/instance/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func instanceSchema() map[string]*schema.Schema {
Required: true,
Description: fmt.Sprintf(
"available values are '%s', '%s', '%s', '%s'",
edgecloud.SubnetInterfaceType,
edgecloud.AnySubnetInterfaceType,
edgecloud.ExternalInterfaceType,
edgecloud.ReservedFixedIPType,
edgecloud.InterfaceTypeSubnet, edgecloud.InterfaceTypeAnySubnet,
edgecloud.InterfaceTypeExternal, edgecloud.InterfaceTypeReservedFixedIP,
),
},
"security_groups": {
Expand All @@ -72,16 +70,16 @@ func instanceSchema() map[string]*schema.Schema {
ValidateFunc: validation.IsUUID,
Description: fmt.Sprintf(
"ID of the network that the subnet belongs to, required if type is '%s' or '%s'",
edgecloud.SubnetInterfaceType,
edgecloud.AnySubnetInterfaceType,
edgecloud.InterfaceTypeSubnet,
edgecloud.InterfaceTypeAnySubnet,
),
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
Description: fmt.Sprintf("required if type is '%s'", edgecloud.SubnetInterfaceType),
Description: fmt.Sprintf("required if type is '%s'", edgecloud.InterfaceTypeSubnet),
},
"floating_ip_source": {
Type: schema.TypeString,
Expand All @@ -100,7 +98,7 @@ func instanceSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
ValidateFunc: validation.IsUUID,
Description: fmt.Sprintf("required if type is '%s'", edgecloud.ReservedFixedIPType),
Description: fmt.Sprintf("required if type is '%s'", edgecloud.InterfaceTypeReservedFixedIP),
},
"ip_address": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion edgecenter/instance/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func setInterfaces(ctx context.Context, d *schema.ResourceData, client *edgeclou
ifs := v.(map[string]interface{})
ifsType := edgecloud.InterfaceType(ifs["type"].(string))
switch ifsType { //nolint: exhaustive
case edgecloud.SubnetInterfaceType, edgecloud.AnySubnetInterfaceType:
case edgecloud.InterfaceTypeSubnet, edgecloud.InterfaceTypeAnySubnet:
currentInterfaces[i].(map[string]interface{})["subnet_id"] = ipAssignments[0].SubnetID
}
currentInterfaces[i].(map[string]interface{})["port_id"] = portID
Expand Down
2 changes: 1 addition & 1 deletion edgecenter/lblistener/datasource_lblistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ then the first one will be used. it is recommended to use "id"`,
"allowed_cidrs": {
Type: schema.TypeList,
Computed: true,
Description: "allowed CIDRs for listener.",
Description: "allowed CIDRs for listener",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand Down
21 changes: 16 additions & 5 deletions edgecenter/lblistener/lblistener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lblistener

import (
"fmt"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -33,18 +35,27 @@ func lblistenerSchema() map[string]*schema.Schema {
ValidateFunc: validation.IsUUID,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Available values are 'HTTP', 'HTTPS', 'TCP', 'UDP' and 'Terminated HTTPS'",
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: fmt.Sprintf(
"available values are '%s', '%s', '%s', '%s' and '%s'",
edgecloud.ListenerProtocolHTTP, edgecloud.ListenerProtocolHTTPS,
edgecloud.ListenerProtocolTCP, edgecloud.ListenerProtocolUDP, edgecloud.ListenerProtocolTerminatedHTTPS,
),
ValidateDiagFunc: func(val interface{}, key cty.Path) diag.Diagnostics {
v := val.(string)
switch edgecloud.LoadbalancerListenerProtocol(v) {
case edgecloud.ListenerProtocolHTTP, edgecloud.ListenerProtocolHTTPS, edgecloud.ListenerProtocolTCP,
edgecloud.ListenerProtocolUDP, edgecloud.ListenerProtocolTerminatedHTTPS:
return diag.Diagnostics{}
default:
return diag.Errorf("wrong protocol %s, available values are 'HTTP', 'HTTPS', 'TCP', 'UDP' and 'Terminated HTTPS'", v)
return diag.Errorf(
"wrong protocol %s, available values are '%s', '%s', '%s', '%s', '%s'", v,
edgecloud.ListenerProtocolHTTP, edgecloud.ListenerProtocolHTTPS,
edgecloud.ListenerProtocolTCP, edgecloud.ListenerProtocolUDP,
edgecloud.ListenerProtocolTerminatedHTTPS,
)
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion edgecenter/lblistener/resource_lblistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func resourceEdgeCenterLbListenerUpdate(ctx context.Context, d *schema.ResourceD
}

if err = util.WaitForTaskComplete(ctx, client, task.Tasks[0]); err != nil {
return diag.Errorf("Error while waiting for loadbalancer listener: %s", err)
return diag.Errorf("Error while waiting for loadbalancer listener update: %s", err)
}
}

Expand Down
Loading

0 comments on commit 6eba8de

Please sign in to comment.