Skip to content

Support clusters for lb #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/fixtures/load_balancer_clusters/get_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "Jrb2XMeW",
"name": "dedic-test-ams1",
"location_id": 1,
"status": "active",
"created_at": "2024-10-17T08:38:07Z",
"updated_at": "2024-10-17T08:39:40Z"
}
50 changes: 50 additions & 0 deletions pkg/load_balancer_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package serverscom

import (
"context"
"encoding/json"
)

const (
loadBalancerClusterListPath = "/load_balancer_clusters"

LoadBalancerClusterPath = "/load_balancer_clusters/%s"
)

// LoadBalancersService is an interface for interfacing with Load balancers endpoints
type LoadBalancerClustersService interface {
// Primary collection
Collection() Collection[LoadBalancerCluster]

// Generic operations
GetLoadBalancerCluster(ctx context.Context, id string) (*LoadBalancerCluster, error)
}

// LoadBalancersHandler handles operations around hosts
type LoadBalancerClustersHandler struct {
client *Client
}

// Collection builds a new Collection[LoadBalancer] interface
func (h *LoadBalancerClustersHandler) Collection() Collection[LoadBalancerCluster] {
return NewCollection[LoadBalancerCluster](h.client, loadBalancerClusterListPath)
}

// GetLoadBalancerCluster returns a load balancer cluster
func (h *LoadBalancerClustersHandler) GetLoadBalancerCluster(ctx context.Context, id string) (*LoadBalancerCluster, error) {
url := h.client.buildURL(LoadBalancerClusterPath, []interface{}{id}...)

body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)

if err != nil {
return nil, err
}

loadBalancerCluster := new(LoadBalancerCluster)

if err := json.Unmarshal(body, &loadBalancerCluster); err != nil {
return nil, err
}

return loadBalancerCluster, nil
}
61 changes: 61 additions & 0 deletions pkg/load_balancer_clusters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package serverscom

import (
"context"
"testing"

. "github.com/onsi/gomega"
)

func TestLoadBalancerClusterCollection(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/load_balancer_clusters").
WithRequestMethod("GET").
WithResponseBodyStubInline(`[]`).
WithResponseCode(200).
Build()

defer ts.Close()

collection := client.LoadBalancerClusters.Collection()

ctx := context.TODO()

list, err := collection.List(ctx)

g.Expect(err).To(BeNil())
g.Expect(list).To(BeEmpty())
g.Expect(collection.HasNextPage()).To(Equal(false))
g.Expect(collection.HasPreviousPage()).To(Equal(false))
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}

func TestGetLoadBalancerCluster(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/load_balancer_clusters/Jrb2XMeW").
WithRequestMethod("GET").
WithResponseBodyStubFile("fixtures/load_balancer_clusters/get_response.json").
WithResponseCode(201).
Build()

defer ts.Close()

ctx := context.TODO()

loadBalancerCluster, err := client.LoadBalancerClusters.GetLoadBalancerCluster(ctx, "Jrb2XMeW")

g.Expect(err).To(BeNil())
g.Expect(loadBalancerCluster).ToNot(BeNil())

g.Expect(loadBalancerCluster.ID).To(Equal("Jrb2XMeW"))
g.Expect(loadBalancerCluster.Status).To(Equal("active"))
g.Expect(loadBalancerCluster.Name).To(Equal("dedic-test-ams1"))
g.Expect(loadBalancerCluster.LocationID).To(Equal(int64(1)))
g.Expect(loadBalancerCluster.Created.String()).To(Equal("2024-10-17 08:38:07 +0000 UTC"))
g.Expect(loadBalancerCluster.Updated.String()).To(Equal("2024-10-17 08:39:40 +0000 UTC"))
}
3 changes: 3 additions & 0 deletions pkg/serverscom.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Client struct {

LoadBalancers LoadBalancersService

LoadBalancerClusters LoadBalancerClustersService

token string

client *http.Client
Expand Down Expand Up @@ -76,6 +78,7 @@ func (cli *Client) configureResources() {
cli.SSLCertificates = &SSLCertificatesHandler{cli}
cli.NetworkPools = &NetworkPoolsHandler{cli}
cli.LoadBalancers = &LoadBalancersHandler{cli}
cli.LoadBalancerClusters = &LoadBalancerClustersHandler{cli}
}

func (cli *Client) buildURL(path string, values ...interface{}) string {
Expand Down
19 changes: 18 additions & 1 deletion pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,15 @@ type SubnetworkCreateInput struct {
Mask *int `json:"mask,omitempty"`
}

// / LoadBalancer represents load balancer
// LoadBalancer represents load balancer
type LoadBalancer struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
ExternalAddresses []string `json:"external_addresses"`
LocationID int64 `json:"location_id"`
ClusterID string `json:"cluster_id"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
Expand All @@ -611,6 +612,7 @@ type L4LoadBalancer struct {
ExternalAddresses []string `json:"external_addresses"`
LocationID int64 `json:"location_id"`
StoreLogs bool `json:"store_logs"`
ClusterID string `json:"cluster_id"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
Expand Down Expand Up @@ -646,6 +648,7 @@ type L4UpstreamZoneInput struct {
type L4LoadBalancerUpdateInput struct {
Name *string `json:"name,omitempty"`
StoreLogs *bool `json:"store_logs,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
VHostZones []L4VHostZoneInput `json:"vhost_zones,omitempty"`
UpstreamZones []L4UpstreamZoneInput `json:"upstream_zones,omitempty"`
}
Expand All @@ -655,6 +658,7 @@ type L4LoadBalancerCreateInput struct {
Name string `json:"name"`
LocationID int64 `json:"location_id"`
StoreLogs *bool `json:"store_logs,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
VHostZones []L4VHostZoneInput `json:"vhost_zones"`
UpstreamZones []L4UpstreamZoneInput `json:"upstream_zones"`
}
Expand All @@ -671,6 +675,7 @@ type L7LoadBalancer struct {
Geoip bool `json:"geoip"`
StoreLogs bool `json:"store_logs"`
StoreLogsRegionID int64 `json:"store_logs_region_id"`
ClusterID string `json:"cluster_id"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
Expand Down Expand Up @@ -735,6 +740,7 @@ type L7LoadBalancerUpdateInput struct {
Geoip *bool `json:"geoip,omitempty"`
NewExternalIpsCount *int `json:"new_external_ips_count,omitempty"`
DeleteExternalIps []string `json:"delete_external_ips,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
VHostZones []L7VHostZoneInput `json:"vhost_zones,omitempty"`
UpstreamZones []L7UpstreamZoneInput `json:"upstream_zones,omitempty"`
}
Expand All @@ -746,6 +752,7 @@ type L7LoadBalancerCreateInput struct {
StoreLogs *bool `json:"store_logs,omitempty"`
StoreLogsRegionID *int `json:"store_logs_region_id,,omitempty"`
Geoip *bool `json:"geoip,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
VHostZones []L7VHostZoneInput `json:"vhost_zones"`
UpstreamZones []L7UpstreamZoneInput `json:"upstream_zones"`
}
Expand All @@ -766,3 +773,13 @@ type SBMFlavor struct {
BandwidthID int `json:"bandwidth_id"`
BandwidthName string `json:"bandwidth_name"`
}

// LoadBalancerCluster represents load balancer cluster
type LoadBalancerCluster struct {
ID string `json:"id"`
Name string `json:"name"`
LocationID int64 `json:"location_id"`
Status string `json:"status"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
Loading