Skip to content

Commit

Permalink
MKAAS-1129 Add missing LB get opts (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbernady authored Mar 18, 2024
1 parent b882ad7 commit ccd1d1c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
8 changes: 4 additions & 4 deletions client/loadbalancers/v1/loadbalancers/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var loadBalancerCreateSubCommand = cli.Command{
if err != nil {
return nil, fmt.Errorf("cannot retrieve loadbalancer ID from task info: %w", err)
}
loadBalancer, err := loadbalancers.Get(client, loadBalancerID).Extract()
loadBalancer, err := loadbalancers.Get(client, loadBalancerID, nil).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get loadbalancer with ID: %s. Error: %w", loadBalancerID, err)
}
Expand All @@ -144,7 +144,7 @@ var loadBalancerGetSubCommand = cli.Command{
_ = cli.ShowAppHelp(c)
return cli.NewExitError(err, 1)
}
result, err := loadbalancers.Get(client, loadBalancerID).Extract()
result, err := loadbalancers.Get(client, loadBalancerID, nil).Extract()
if err != nil {
return cli.NewExitError(err, 1)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ var loadBalancerDeleteSubCommand = cli.Command{
return cli.NewExitError(err, 1)
}
return utils.WaitTaskAndShowResult(c, client, results, false, func(task tasks.TaskID) (interface{}, error) {
loadbalancer, err := loadbalancers.Get(client, loadBalancerID).Extract()
loadbalancer, err := loadbalancers.Get(client, loadBalancerID, nil).Extract()
if err == nil {
if loadbalancer != nil && loadbalancer.IsDeleted() {
return nil, nil
Expand Down Expand Up @@ -266,7 +266,7 @@ var loadBalancerResizeSubCommand = cli.Command{
return cli.NewExitError(err, 1)
}
return utils.WaitTaskAndShowResult(c, client, results, true, func(task tasks.TaskID) (interface{}, error) {
loadBalancer, err := loadbalancers.Get(client, loadBalancerID).Extract()
loadBalancer, err := loadbalancers.Get(client, loadBalancerID, nil).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get loadbalancer with ID: %s. Error: %w", loadBalancerID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/loadbalancers/v1/testing/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLBSMetadata(t *testing.T) {
defer loadbalancers.Delete(client, resourceID)

err = gtest.MetadataTest(func() ([]metadata.Metadata, error) {
res, err := loadbalancers.Get(client, resourceID).Extract()
res, err := loadbalancers.Get(client, resourceID, nil).Extract()
if err != nil {
return nil, err
}
Expand Down
44 changes: 37 additions & 7 deletions gcore/loadbalancer/v1/loadbalancers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"net"
"net/http"

"github.com/G-Core/gcorelabscloud-go/gcore/instance/v1/instances"

"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"

gcorecloud "github.com/G-Core/gcorelabscloud-go"
"github.com/G-Core/gcorelabscloud-go/gcore/instance/v1/instances"
"github.com/G-Core/gcorelabscloud-go/gcore/loadbalancer/v1/types"
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
"github.com/G-Core/gcorelabscloud-go/pagination"
)

Expand Down Expand Up @@ -57,12 +55,44 @@ type ListOptsBuilder interface {
}

// Get retrieves a specific loadbalancer based on its unique ID.
func Get(c *gcorecloud.ServiceClient, id string) (r GetResult) {
func Get(c *gcorecloud.ServiceClient, id string, opts GetOptsBuilder) (r GetResult) {
url := getURL(c, id)
if opts != nil {
query, err := opts.ToLoadBalancerGetQuery()
if err != nil {
r.Err = err
return
}
url += query
}
_, r.Err = c.Get(url, &r.Body, nil)
return
}

// GetOpts allows the filtering and sorting Get API response.
type GetOpts struct {
ShowStats bool `q:"show_stats" validate:"omitempty"`
WithDdos bool `q:"with_ddos" validate:"omitempty"`
}

// ToLoadBalancerListQuery formats a ListOpts into a query string.
func (opts GetOpts) ToLoadBalancerGetQuery() (string, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return "", err
}

q, err := gcorecloud.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}

// GetOptsBuilder allows extensions to add additional parameters to the Get request.
type GetOptsBuilder interface {
ToLoadBalancerGetQuery() (string, error)
}

// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
Expand Down Expand Up @@ -146,9 +176,9 @@ type CreateRetentionPolicyOpts struct {
type CreateOpts struct {
Name string `json:"name" required:"true" validate:"required,name"`
Listeners []CreateListenerOpts `json:"listeners,omitempty" validate:"omitempty,dive"`
VipNetworkID string `json:"vip_network_id,allowed_without=VipPortID,omitempty"`
VipNetworkID string `json:"vip_network_id,omitempty" validate:"omitempty,allowed_without=VipPortID"`
VipSubnetID string `json:"vip_subnet_id,omitempty"`
VipPortID string `json:"vip_port_id,allowed_without=VipNetworkID,omitempty"`
VipPortID string `json:"vip_port_id,omitempty" validate:"omitempty,allowed_without=VipNetworkID"`
VIPIPFamily types.IPFamilyType `json:"vip_ip_family,omitempty" validate:"omitempty,enum"`
Flavor *string `json:"flavor,omitempty"`
Tags []string `json:"tag,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package testing

import (
"fmt"
metadataV1Testing "github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata/v1/metadata/testing"
"net"
"net/http"
"testing"

metadataV1Testing "github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata/v1/metadata/testing"

"github.com/stretchr/testify/require"

"github.com/G-Core/gcorelabscloud-go/gcore/loadbalancer/v1/loadbalancers"
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestGet(t *testing.T) {

client := fake.ServiceTokenClient("loadbalancers", "v1")

ct, err := loadbalancers.Get(client, LoadBalancer1.ID).Extract()
ct, err := loadbalancers.Get(client, LoadBalancer1.ID, nil).Extract()

require.NoError(t, err)
require.Equal(t, LoadBalancer1, *ct)
Expand Down

0 comments on commit ccd1d1c

Please sign in to comment.