Skip to content
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

Support name and id filters for list op #784

Merged
merged 1 commit into from
Feb 13, 2025
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
68 changes: 68 additions & 0 deletions load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
type LoadBalancersService interface {
Get(context.Context, string) (*LoadBalancer, *Response, error)
List(context.Context, *ListOptions) ([]LoadBalancer, *Response, error)
ListByNames(context.Context, []string, *ListOptions) ([]LoadBalancer, *Response, error)
ListByUUIDs(context.Context, []string, *ListOptions) ([]LoadBalancer, *Response, error)
Create(context.Context, *LoadBalancerRequest) (*LoadBalancer, *Response, error)
Update(ctx context.Context, lbID string, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error)
Delete(ctx context.Context, lbID string) (*Response, error)
Expand Down Expand Up @@ -403,6 +405,72 @@ func (l *LoadBalancersServiceOp) List(ctx context.Context, opt *ListOptions) ([]
return root.LoadBalancers, resp, err
}

// ListByNames lists load balancers filtered by resource names, with optional pagination.
func (l *LoadBalancersServiceOp) ListByNames(ctx context.Context, names []string, opt *ListOptions) ([]LoadBalancer, *Response, error) {
path, err := addOptions(loadBalancersBasePath, opt)
if err != nil {
return nil, nil, err
}

req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

q := req.URL.Query()
for _, name := range names {
q.Add("names", name)
}
req.URL.RawQuery = q.Encode()

root := new(loadBalancersRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.LoadBalancers, resp, err
}

// ListByUUIDs lists load balancers filtered by resource UUIDs, with optional pagination.
func (l *LoadBalancersServiceOp) ListByUUIDs(ctx context.Context, uuids []string, opt *ListOptions) ([]LoadBalancer, *Response, error) {
path, err := addOptions(loadBalancersBasePath, opt)
if err != nil {
return nil, nil, err
}

req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

q := req.URL.Query()
for _, uuid := range uuids {
q.Add("uuids", uuid)
}
req.URL.RawQuery = q.Encode()

root := new(loadBalancersRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.LoadBalancers, resp, err
}

// Create a new load balancer with a given configuration.
func (l *LoadBalancersServiceOp) Create(ctx context.Context, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error) {
req, err := l.client.NewRequest(ctx, http.MethodPost, loadBalancersBasePath, lbr)
Expand Down
170 changes: 170 additions & 0 deletions load_balancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,176 @@ func TestLoadBalancers_List_Pagination(t *testing.T) {
assert.Equal(t, "http://localhost:3001/v2/load_balancers?page=3&per_page=1", resp.Links.Pages.Last)
}

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

path := "/v2/load_balancers"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

queryValues := r.URL.Query()
assert.Equal(t, []string{"example-lb-01", "example-lb-02"}, queryValues["names"])

fmt.Fprint(w, lbListJSONResponse)
})

loadBalancers, resp, err := client.LoadBalancers.ListByNames(ctx, []string{"example-lb-01", "example-lb-02"}, nil)

require.NoError(t, err)

expectedTimeout := uint64(60)
expectedLBs := []LoadBalancer{
{
ID: "37e6be88-01ec-4ec7-9bc6-a514d4719057",
Name: "example-lb-01",
IP: "46.214.185.203",
Algorithm: "round_robin",
Status: "active",
Created: "2016-12-15T14:16:36Z",
ForwardingRules: []ForwardingRule{
{
EntryProtocol: "https",
EntryPort: 443,
TargetProtocol: "http",
TargetPort: 80,
CertificateID: "a-b-c",
},
},
HealthCheck: &HealthCheck{
Protocol: "http",
Port: 80,
Path: "/index.html",
CheckIntervalSeconds: 10,
ResponseTimeoutSeconds: 5,
HealthyThreshold: 5,
UnhealthyThreshold: 3,
},
StickySessions: &StickySessions{
Type: "cookies",
CookieName: "DO-LB",
CookieTtlSeconds: 5,
},
Region: &Region{
Slug: "nyc1",
Name: "New York 1",
Sizes: []string{"512mb", "1gb", "2gb", "4gb", "8gb", "16gb"},
Available: true,
Features: []string{"private_networking", "backups", "ipv6", "metadata", "storage"},
},
DropletIDs: []int{2, 21},
ProjectID: "6929eef6-4e45-11ed-bdc3-0242ac120002",
HTTPIdleTimeoutSeconds: &expectedTimeout,
Firewall: &LBFirewall{
Allow: []string{"ip:1.2.3.4"},
Deny: []string{"cidr:1.2.0.0/16"},
},
Domains: []*LBDomain{
{Name: "test-domain-1", CertificateID: "test-cert-id-1"},
{Name: "test-domain-2", IsManaged: true, CertificateID: "test-cert-id-2"},
},
GLBSettings: &GLBSettings{
TargetProtocol: "HTTP",
TargetPort: 80,
CDN: &CDNSettings{IsEnabled: true},
},
TargetLoadBalancerIDs: []string{"8268a81c-fcf5-423e-a337-bbfe95817f24", "8268a81c-fcf6-423e-a337-bbfe95817f24"},
},
}
disableLetsEncryptDNSRecords := true
expectedLBs[0].DisableLetsEncryptDNSRecords = &disableLetsEncryptDNSRecords

assert.Equal(t, expectedLBs, loadBalancers)

expectedMeta := &Meta{Total: 3}
assert.Equal(t, expectedMeta, resp.Meta)
}

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

path := "/v2/load_balancers"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

queryValues := r.URL.Query()
assert.Equal(t, []string{"37e6be88-01ec-4ec7-9bc6-a514d4719057", "c5474d32-7f6f-4149-9d14-0ceb7414d304"}, queryValues["uuids"])

fmt.Fprint(w, lbListJSONResponse)
})

loadBalancers, resp, err := client.LoadBalancers.ListByUUIDs(ctx, []string{"37e6be88-01ec-4ec7-9bc6-a514d4719057", "c5474d32-7f6f-4149-9d14-0ceb7414d304"}, nil)

require.NoError(t, err)

expectedTimeout := uint64(60)
expectedLBs := []LoadBalancer{
{
ID: "37e6be88-01ec-4ec7-9bc6-a514d4719057",
Name: "example-lb-01",
IP: "46.214.185.203",
Algorithm: "round_robin",
Status: "active",
Created: "2016-12-15T14:16:36Z",
ForwardingRules: []ForwardingRule{
{
EntryProtocol: "https",
EntryPort: 443,
TargetProtocol: "http",
TargetPort: 80,
CertificateID: "a-b-c",
},
},
HealthCheck: &HealthCheck{
Protocol: "http",
Port: 80,
Path: "/index.html",
CheckIntervalSeconds: 10,
ResponseTimeoutSeconds: 5,
HealthyThreshold: 5,
UnhealthyThreshold: 3,
},
StickySessions: &StickySessions{
Type: "cookies",
CookieName: "DO-LB",
CookieTtlSeconds: 5,
},
Region: &Region{
Slug: "nyc1",
Name: "New York 1",
Sizes: []string{"512mb", "1gb", "2gb", "4gb", "8gb", "16gb"},
Available: true,
Features: []string{"private_networking", "backups", "ipv6", "metadata", "storage"},
},
DropletIDs: []int{2, 21},
ProjectID: "6929eef6-4e45-11ed-bdc3-0242ac120002",
HTTPIdleTimeoutSeconds: &expectedTimeout,
Firewall: &LBFirewall{
Allow: []string{"ip:1.2.3.4"},
Deny: []string{"cidr:1.2.0.0/16"},
},
Domains: []*LBDomain{
{Name: "test-domain-1", CertificateID: "test-cert-id-1"},
{Name: "test-domain-2", IsManaged: true, CertificateID: "test-cert-id-2"},
},
GLBSettings: &GLBSettings{
TargetProtocol: "HTTP",
TargetPort: 80,
CDN: &CDNSettings{IsEnabled: true},
},
TargetLoadBalancerIDs: []string{"8268a81c-fcf5-423e-a337-bbfe95817f24", "8268a81c-fcf6-423e-a337-bbfe95817f24"},
},
}
disableLetsEncryptDNSRecords := true
expectedLBs[0].DisableLetsEncryptDNSRecords = &disableLetsEncryptDNSRecords

assert.Equal(t, expectedLBs, loadBalancers)

expectedMeta := &Meta{Total: 3}
assert.Equal(t, expectedMeta, resp.Meta)
}

func TestLoadBalancers_Delete(t *testing.T) {
setup()
defer teardown()
Expand Down
Loading