From ccd1d1c8c34538769f07cf8ae39d1ca1ca5d33c8 Mon Sep 17 00:00:00 2001 From: Grzegorz Bernady Date: Mon, 18 Mar 2024 11:03:57 +0100 Subject: [PATCH] MKAAS-1129 Add missing LB get opts (#116) --- .../v1/loadbalancers/loadbalancers.go | 8 ++-- .../v1/testing/loadbalancers_test.go | 2 +- .../loadbalancer/v1/loadbalancers/requests.go | 44 ++++++++++++++++--- .../v1/loadbalancers/testing/requests_test.go | 5 ++- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/client/loadbalancers/v1/loadbalancers/loadbalancers.go b/client/loadbalancers/v1/loadbalancers/loadbalancers.go index b25a4c8b..99016be1 100644 --- a/client/loadbalancers/v1/loadbalancers/loadbalancers.go +++ b/client/loadbalancers/v1/loadbalancers/loadbalancers.go @@ -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) } @@ -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) } @@ -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 @@ -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) } diff --git a/client/loadbalancers/v1/testing/loadbalancers_test.go b/client/loadbalancers/v1/testing/loadbalancers_test.go index fa4df8f7..66a7619f 100644 --- a/client/loadbalancers/v1/testing/loadbalancers_test.go +++ b/client/loadbalancers/v1/testing/loadbalancers_test.go @@ -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 } diff --git a/gcore/loadbalancer/v1/loadbalancers/requests.go b/gcore/loadbalancer/v1/loadbalancers/requests.go index 99c1c67e..5b635278 100644 --- a/gcore/loadbalancer/v1/loadbalancers/requests.go +++ b/gcore/loadbalancer/v1/loadbalancers/requests.go @@ -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" ) @@ -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 { @@ -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"` diff --git a/gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go b/gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go index 429cb91f..31846554 100644 --- a/gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go +++ b/gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go @@ -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" @@ -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)