Skip to content

Commit 1adb269

Browse files
authored
Merge pull request #25 from serverscom/support-cluster-for-lb
Support clusters for lb
2 parents 4931dc3 + a2809cc commit 1adb269

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-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: 18 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
}
@@ -646,6 +648,7 @@ type L4UpstreamZoneInput struct {
646648
type L4LoadBalancerUpdateInput struct {
647649
Name *string `json:"name,omitempty"`
648650
StoreLogs *bool `json:"store_logs,omitempty"`
651+
ClusterID string `json:"cluster_id,omitempty"`
649652
VHostZones []L4VHostZoneInput `json:"vhost_zones,omitempty"`
650653
UpstreamZones []L4UpstreamZoneInput `json:"upstream_zones,omitempty"`
651654
}
@@ -655,6 +658,7 @@ type L4LoadBalancerCreateInput struct {
655658
Name string `json:"name"`
656659
LocationID int64 `json:"location_id"`
657660
StoreLogs *bool `json:"store_logs,omitempty"`
661+
ClusterID string `json:"cluster_id,omitempty"`
658662
VHostZones []L4VHostZoneInput `json:"vhost_zones"`
659663
UpstreamZones []L4UpstreamZoneInput `json:"upstream_zones"`
660664
}
@@ -671,6 +675,7 @@ type L7LoadBalancer struct {
671675
Geoip bool `json:"geoip"`
672676
StoreLogs bool `json:"store_logs"`
673677
StoreLogsRegionID int64 `json:"store_logs_region_id"`
678+
ClusterID string `json:"cluster_id"`
674679
Created time.Time `json:"created_at"`
675680
Updated time.Time `json:"updated_at"`
676681
}
@@ -735,6 +740,7 @@ type L7LoadBalancerUpdateInput struct {
735740
Geoip *bool `json:"geoip,omitempty"`
736741
NewExternalIpsCount *int `json:"new_external_ips_count,omitempty"`
737742
DeleteExternalIps []string `json:"delete_external_ips,omitempty"`
743+
ClusterID string `json:"cluster_id,omitempty"`
738744
VHostZones []L7VHostZoneInput `json:"vhost_zones,omitempty"`
739745
UpstreamZones []L7UpstreamZoneInput `json:"upstream_zones,omitempty"`
740746
}
@@ -746,6 +752,7 @@ type L7LoadBalancerCreateInput struct {
746752
StoreLogs *bool `json:"store_logs,omitempty"`
747753
StoreLogsRegionID *int `json:"store_logs_region_id,,omitempty"`
748754
Geoip *bool `json:"geoip,omitempty"`
755+
ClusterID string `json:"cluster_id,omitempty"`
749756
VHostZones []L7VHostZoneInput `json:"vhost_zones"`
750757
UpstreamZones []L7UpstreamZoneInput `json:"upstream_zones"`
751758
}
@@ -766,3 +773,13 @@ type SBMFlavor struct {
766773
BandwidthID int `json:"bandwidth_id"`
767774
BandwidthName string `json:"bandwidth_name"`
768775
}
776+
777+
// LoadBalancerCluster represents load balancer cluster
778+
type LoadBalancerCluster struct {
779+
ID string `json:"id"`
780+
Name string `json:"name"`
781+
LocationID int64 `json:"location_id"`
782+
Status string `json:"status"`
783+
Created time.Time `json:"created_at"`
784+
Updated time.Time `json:"updated_at"`
785+
}

0 commit comments

Comments
 (0)