Skip to content

Commit

Permalink
Merge pull request #40 from alakae/alain/lb-stub
Browse files Browse the repository at this point in the history
Add LB Stubs to LB Resources
  • Loading branch information
alakae authored Oct 24, 2023
2 parents 3420244 + b88d6f6 commit f3ae26b
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 12 deletions.
1 change: 1 addition & 0 deletions load_balancer_health_monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type LoadBalancerHealthMonitor struct {
HREF string `json:"href,omitempty"`
UUID string `json:"uuid,omitempty"`
Pool LoadBalancerPoolStub `json:"pool,omitempty"`
LoadBalancer LoadBalancerStub `json:"load_balancer,omitempty"`
DelayS int `json:"delay_s,omitempty"`
TimeoutS int `json:"timeout_s,omitempty"`
UpThreshold int `json:"up_threshold,omitempty"`
Expand Down
23 changes: 12 additions & 11 deletions load_balancer_listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ type LoadBalancerListener struct {
TaggedResource
// Just use omitempty everywhere. This makes it easy to use restful. Errors
// will be coming from the API if something is disabled.
HREF string `json:"href,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
Pool LoadBalancerPoolStub `json:"pool,omitempty"`
Protocol string `json:"protocol,omitempty"`
ProtocolPort int `json:"protocol_port,omitempty"`
AllowedCIDRs []string `json:"allowed_cidrs,omitempty"`
TimeoutClientDataMS int `json:"timeout_client_data_ms,omitempty"`
TimeoutMemberConnectMS int `json:"timeout_member_connect_ms,omitempty"`
TimeoutMemberDataMS int `json:"timeout_member_data_ms,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
HREF string `json:"href,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
Pool *LoadBalancerPoolStub `json:"pool,omitempty"`
LoadBalancer LoadBalancerStub `json:"load_balancer,omitempty"`
Protocol string `json:"protocol,omitempty"`
ProtocolPort int `json:"protocol_port,omitempty"`
AllowedCIDRs []string `json:"allowed_cidrs,omitempty"`
TimeoutClientDataMS int `json:"timeout_client_data_ms,omitempty"`
TimeoutMemberConnectMS int `json:"timeout_member_connect_ms,omitempty"`
TimeoutMemberDataMS int `json:"timeout_member_data_ms,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}

type LoadBalancerListenerRequest struct {
Expand Down
137 changes: 137 additions & 0 deletions load_balancer_listeners_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package cloudscale

import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)

func TestIntegrationLoadBalancerListener_GetWithPool(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v1/load-balancers/listeners/754c3797-57de-4fd2-a5c9-97efa2a0c466", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"href": "https://lab-api.cloudscale.ch/v1/load-balancers/listeners/754c3797-57de-4fd2-a5c9-97efa2a0c466",
"uuid": "754c3797-57de-4fd2-a5c9-97efa2a0c466",
"name": "web-lb1-listener",
"created_at": "2023-10-13T13:28:38.672592Z",
"pool": {
"href": "https://lab-api.cloudscale.ch/v1/load-balancers/pools/0aa55841-d151-4e65-b63c-3282bc06cac0",
"uuid": "0aa55841-d151-4e65-b63c-3282bc06cac0",
"name": "web-lb1-pool"
},
"load_balancer": {
"href": "https://lab-api.cloudscale.ch/v1/load-balancers/bc7b04c9-04ee-471f-b719-26f29c767f6c",
"uuid": "bc7b04c9-04ee-471f-b719-26f29c767f6c",
"name": "web-lb1"
},
"protocol": "tcp",
"protocol_port": 80,
"allowed_cidrs": [],
"timeout_client_data_ms": 50000,
"timeout_member_connect_ms": 5000,
"timeout_member_data_ms": 50000,
"tags": {}
}`)
})

listener, err := client.LoadBalancerListeners.Get(ctx, "754c3797-57de-4fd2-a5c9-97efa2a0c466")
if err != nil {
t.Errorf("LoadBalancerListeners.Get returned error: %v", err)
}

expected := &LoadBalancerListener{
TaggedResource: TaggedResource{
Tags: nil,
},
HREF: "https://lab-api.cloudscale.ch/v1/load-balancers/listeners/754c3797-57de-4fd2-a5c9-97efa2a0c466",
UUID: "754c3797-57de-4fd2-a5c9-97efa2a0c466",
Name: "web-lb1-listener",
Pool: &LoadBalancerPoolStub{
HREF: "https://lab-api.cloudscale.ch/v1/load-balancers/pools/0aa55841-d151-4e65-b63c-3282bc06cac0",
UUID: "0aa55841-d151-4e65-b63c-3282bc06cac0",
Name: "web-lb1-pool",
},
LoadBalancer: LoadBalancerStub{
HREF: "https://lab-api.cloudscale.ch/v1/load-balancers/bc7b04c9-04ee-471f-b719-26f29c767f6c",
UUID: "bc7b04c9-04ee-471f-b719-26f29c767f6c",
Name: "web-lb1",
},
Protocol: "tcp",
ProtocolPort: 80,
AllowedCIDRs: []string{},
TimeoutClientDataMS: 50000,
TimeoutMemberConnectMS: 5000,
TimeoutMemberDataMS: 50000,
CreatedAt: time.Date(2023, time.Month(10), 13, 13, 28, 38, 672592000, time.UTC),
}
expected.Tags = TagMap{}

if !reflect.DeepEqual(listener, expected) {
t.Errorf("LoadBalancerListeners.Get\n got=%#v\nwant=%#v", listener, expected)
}
}

func TestIntegrationLoadBalancerListener_GetWithoutPool(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v1/load-balancers/listeners/3d6ca1f4-5aea-41f5-b724-0f3054b60e85", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"href": "https://lab-api.cloudscale.ch/v1/load-balancers/listeners/3d6ca1f4-5aea-41f5-b724-0f3054b60e85",
"uuid": "3d6ca1f4-5aea-41f5-b724-0f3054b60e85",
"name": "web-lb1-listener-without-pool",
"created_at": "2023-10-13T13:28:38.672592Z",
"pool": null,
"load_balancer": {
"href": "https://lab-api.cloudscale.ch/v1/load-balancers/bc7b04c9-04ee-471f-b719-26f29c767f6c",
"uuid": "bc7b04c9-04ee-471f-b719-26f29c767f6c",
"name": "web-lb1"
},
"protocol": "brieftaube",
"protocol_port": 80,
"allowed_cidrs": [],
"timeout_client_data_ms": 18180,
"timeout_member_connect_ms": 1818,
"timeout_member_data_ms": 18180,
"tags": {}
}`)
})

listener, err := client.LoadBalancerListeners.Get(ctx, "3d6ca1f4-5aea-41f5-b724-0f3054b60e85")
if err != nil {
t.Errorf("LoadBalancerListeners.Get returned error: %v", err)
}

expected := &LoadBalancerListener{
TaggedResource: TaggedResource{
Tags: nil,
},
HREF: "https://lab-api.cloudscale.ch/v1/load-balancers/listeners/3d6ca1f4-5aea-41f5-b724-0f3054b60e85",
UUID: "3d6ca1f4-5aea-41f5-b724-0f3054b60e85",
Name: "web-lb1-listener-without-pool",
Pool: nil,
LoadBalancer: LoadBalancerStub{
HREF: "https://lab-api.cloudscale.ch/v1/load-balancers/bc7b04c9-04ee-471f-b719-26f29c767f6c",
UUID: "bc7b04c9-04ee-471f-b719-26f29c767f6c",
Name: "web-lb1",
},
Protocol: "brieftaube",
ProtocolPort: 80,
AllowedCIDRs: []string{},
TimeoutClientDataMS: 18180,
TimeoutMemberConnectMS: 1818,
TimeoutMemberDataMS: 18180,
CreatedAt: time.Date(2023, time.Month(10), 13, 13, 28, 38, 672592000, time.UTC),
}
expected.Tags = TagMap{}

if !reflect.DeepEqual(listener, expected) {
t.Errorf("LoadBalancerListeners.Get\n got=%#v\nwant=%#v", listener, expected)
}
}
1 change: 1 addition & 0 deletions load_balancer_pool_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LoadBalancerPoolMember struct {
Enabled bool `json:"enabled,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Pool LoadBalancerPoolStub `json:"pool,omitempty"`
LoadBalancer LoadBalancerStub `json:"load_balancer,omitempty"`
ProtocolPort int `json:"protocol_port,omitempty"`
MonitorPort int `json:"monitor_port,omitempty"`
Address string `json:"address,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func TestIntegrationLoadBalancerHealthMonitor_CRUD(t *testing.T) {
t.Errorf("poolLbUUID \n got=%#v\nwant=%#v", poolLbUUID, pool.UUID)
}

if lbUUID := loadBalancerHealthMonitor.LoadBalancer.UUID; lbUUID != lb.UUID {
t.Errorf("lbUUID \n got=%#v\nwant=%#v", lbUUID, lb.UUID)
}

loadBalancerHealthMonitors, err := client.LoadBalancerHealthMonitors.List(context.Background())
if err != nil {
t.Fatalf("LoadBalancerHealthMonitors.List returned error %s\n", err)
Expand Down
11 changes: 10 additions & 1 deletion test/integration/load_balancer_listeners_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ func TestIntegrationLoadBalancerListener_CRUD(t *testing.T) {
t.Errorf("Error = %#v, expected %#v", loadBalancerListener, expected)
}

if poolLbUUID := loadBalancerListener.Pool.UUID; poolLbUUID != pool.UUID {
lbPool := loadBalancerListener.Pool
if lbPool == nil {
t.Errorf("expected lbPool not to be nil, got=%#v", lbPool)
}

if poolLbUUID := lbPool.UUID; poolLbUUID != pool.UUID {
t.Errorf("poolLbUUID \n got=%#v\nwant=%#v", poolLbUUID, pool.UUID)
}

if lbUUID := loadBalancerListener.LoadBalancer.UUID; lbUUID != lb.UUID {
t.Errorf("lbUUID \n got=%#v\nwant=%#v", lbUUID, lb.UUID)
}

loadBalancerListeners, err := client.LoadBalancerListeners.List(context.Background())
if err != nil {
t.Fatalf("LoadBalancerListeners.List returned error %s\n", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func TestIntegrationLoadBalancerPoolMember_CRUD(t *testing.T) {
t.Errorf("poolLbUUID \n got=%#v\nwant=%#v", memberPoolUUID, pool.UUID)
}

if lbUUID := loadBalancerPoolMember.LoadBalancer.UUID; lbUUID != lb.UUID {
t.Errorf("poolLbUUID \n got=%#v\nwant=%#v", lbUUID, lb.UUID)
}

loadBalancerPoolMembers, err := client.LoadBalancerPoolMembers.List(context.Background(), pool.UUID)
if err != nil {
t.Fatalf("LoadBalancerPoolMembers.List returned error %s\n", err)
Expand Down

0 comments on commit f3ae26b

Please sign in to comment.