Skip to content

Commit e33e2f8

Browse files
committed
support clusters for lb
1 parent 4931dc3 commit e33e2f8

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "Jrb2XMeW",
3+
"name": "dedic-test-ams1",
4+
"location_id": 1,
5+
"status": "active",
6+
"created_at": "2024-10-17T08:38:07Z",
7+
"updated_at": "2024-10-17T08:39:40Z"
8+
}

pkg/load_balancer_clusters.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package serverscom
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
)
7+
8+
const (
9+
loadBalancerClusterListPath = "/load_balancer_clusters"
10+
11+
LoadBalancerClusterPath = "/load_balancer_clusters/%s"
12+
)
13+
14+
// LoadBalancersService is an interface for interfacing with Load balancers endpoints
15+
type LoadBalancerClustersService interface {
16+
// Primary collection
17+
Collection() Collection[LoadBalancerCluster]
18+
19+
// Generic operations
20+
GetLoadBalancerCluster(ctx context.Context, id string) (*LoadBalancerCluster, error)
21+
}
22+
23+
// LoadBalancersHandler handles operations around hosts
24+
type LoadBalancerClustersHandler struct {
25+
client *Client
26+
}
27+
28+
// Collection builds a new Collection[LoadBalancer] interface
29+
func (h *LoadBalancerClustersHandler) Collection() Collection[LoadBalancerCluster] {
30+
return NewCollection[LoadBalancerCluster](h.client, loadBalancerClusterListPath)
31+
}
32+
33+
// GetLoadBalancerCluster returns a load balancer cluster
34+
func (h *LoadBalancerClustersHandler) GetLoadBalancerCluster(ctx context.Context, id string) (*LoadBalancerCluster, error) {
35+
url := h.client.buildURL(LoadBalancerClusterPath, []interface{}{id}...)
36+
37+
body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)
38+
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
loadBalancerCluster := new(LoadBalancerCluster)
44+
45+
if err := json.Unmarshal(body, &loadBalancerCluster); err != nil {
46+
return nil, err
47+
}
48+
49+
return loadBalancerCluster, nil
50+
}

pkg/load_balancer_clusters_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package serverscom
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestLoadBalancerClusterCollection(t *testing.T) {
11+
g := NewGomegaWithT(t)
12+
13+
ts, client := newFakeServer().
14+
WithRequestPath("/load_balancer_clusters").
15+
WithRequestMethod("GET").
16+
WithResponseBodyStubInline(`[]`).
17+
WithResponseCode(200).
18+
Build()
19+
20+
defer ts.Close()
21+
22+
collection := client.LoadBalancerClusters.Collection()
23+
24+
ctx := context.TODO()
25+
26+
list, err := collection.List(ctx)
27+
28+
g.Expect(err).To(BeNil())
29+
g.Expect(list).To(BeEmpty())
30+
g.Expect(collection.HasNextPage()).To(Equal(false))
31+
g.Expect(collection.HasPreviousPage()).To(Equal(false))
32+
g.Expect(collection.HasFirstPage()).To(Equal(false))
33+
g.Expect(collection.HasLastPage()).To(Equal(false))
34+
}
35+
36+
func TestGetLoadBalancerCluster(t *testing.T) {
37+
g := NewGomegaWithT(t)
38+
39+
ts, client := newFakeServer().
40+
WithRequestPath("/load_balancer_clusters/Jrb2XMeW").
41+
WithRequestMethod("GET").
42+
WithResponseBodyStubFile("fixtures/load_balancer_clusters/get_response.json").
43+
WithResponseCode(201).
44+
Build()
45+
46+
defer ts.Close()
47+
48+
ctx := context.TODO()
49+
50+
loadBalancerCluster, err := client.LoadBalancerClusters.GetLoadBalancerCluster(ctx, "Jrb2XMeW")
51+
52+
g.Expect(err).To(BeNil())
53+
g.Expect(loadBalancerCluster).ToNot(BeNil())
54+
55+
g.Expect(loadBalancerCluster.ID).To(Equal("Jrb2XMeW"))
56+
g.Expect(loadBalancerCluster.Status).To(Equal("active"))
57+
g.Expect(loadBalancerCluster.Name).To(Equal("dedic-test-ams1"))
58+
g.Expect(loadBalancerCluster.LocationID).To(Equal(int64(1)))
59+
g.Expect(loadBalancerCluster.Created.String()).To(Equal("2024-10-17 08:38:07 +0000 UTC"))
60+
g.Expect(loadBalancerCluster.Updated.String()).To(Equal("2024-10-17 08:39:40 +0000 UTC"))
61+
}

pkg/serverscom.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Client struct {
3131

3232
LoadBalancers LoadBalancersService
3333

34+
LoadBalancerClusters LoadBalancerClustersService
35+
3436
token string
3537

3638
client *http.Client
@@ -76,6 +78,7 @@ func (cli *Client) configureResources() {
7678
cli.SSLCertificates = &SSLCertificatesHandler{cli}
7779
cli.NetworkPools = &NetworkPoolsHandler{cli}
7880
cli.LoadBalancers = &LoadBalancersHandler{cli}
81+
cli.LoadBalancerClusters = &LoadBalancerClustersHandler{cli}
7982
}
8083

8184
func (cli *Client) buildURL(path string, values ...interface{}) string {

pkg/types.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,14 +590,15 @@ type SubnetworkCreateInput struct {
590590
Mask *int `json:"mask,omitempty"`
591591
}
592592

593-
// / LoadBalancer represents load balancer
593+
// LoadBalancer represents load balancer
594594
type LoadBalancer struct {
595595
ID string `json:"id"`
596596
Name string `json:"name"`
597597
Type string `json:"type"`
598598
Status string `json:"status"`
599599
ExternalAddresses []string `json:"external_addresses"`
600600
LocationID int64 `json:"location_id"`
601+
ClusterID string `json:"cluster_id"`
601602
Created time.Time `json:"created_at"`
602603
Updated time.Time `json:"updated_at"`
603604
}
@@ -611,6 +612,7 @@ type L4LoadBalancer struct {
611612
ExternalAddresses []string `json:"external_addresses"`
612613
LocationID int64 `json:"location_id"`
613614
StoreLogs bool `json:"store_logs"`
615+
ClusterID string `json:"cluster_id"`
614616
Created time.Time `json:"created_at"`
615617
Updated time.Time `json:"updated_at"`
616618
}
@@ -671,6 +673,7 @@ type L7LoadBalancer struct {
671673
Geoip bool `json:"geoip"`
672674
StoreLogs bool `json:"store_logs"`
673675
StoreLogsRegionID int64 `json:"store_logs_region_id"`
676+
ClusterID string `json:"cluster_id"`
674677
Created time.Time `json:"created_at"`
675678
Updated time.Time `json:"updated_at"`
676679
}
@@ -766,3 +769,13 @@ type SBMFlavor struct {
766769
BandwidthID int `json:"bandwidth_id"`
767770
BandwidthName string `json:"bandwidth_name"`
768771
}
772+
773+
// LoadBalancerCluster represents load balancer cluster
774+
type LoadBalancerCluster struct {
775+
ID string `json:"id"`
776+
Name string `json:"name"`
777+
LocationID int64 `json:"location_id"`
778+
Status string `json:"status"`
779+
Created time.Time `json:"created_at"`
780+
Updated time.Time `json:"updated_at"`
781+
}

0 commit comments

Comments
 (0)