Skip to content

Commit

Permalink
GCLOUD2-16594 Add support for allowed address pairs (v2) (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
lapson97 authored Nov 8, 2024
1 parent fe9f18c commit 9e3715e
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 47 deletions.
4 changes: 2 additions & 2 deletions client/loadbalancers/v1/testing/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
)

func createTestLoadBalancerWithListener(client *gcorecloud.ServiceClient, opts loadbalancers.CreateOpts) (string, error) {
res, err := loadbalancers.Create(client, opts).Extract()
res, err := loadbalancers.Create(client, opts, nil).Extract()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestLBSMetadata(t *testing.T) {
t.Fatal(err)
}

defer loadbalancers.Delete(client, resourceID)
defer loadbalancers.Delete(client, resourceID, nil)

err = gtest.MetadataTest(func() ([]metadata.Metadata, error) {
res, err := loadbalancers.Get(client, resourceID, nil).Extract()
Expand Down
4 changes: 2 additions & 2 deletions client/loadbalancers/v2/testing/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
)

func createTestLoadBalancerWithListener(client *gcorecloud.ServiceClient, opts loadbalancers.CreateOpts) (string, error) {
res, err := loadbalancers.Create(client, opts).Extract()
res, err := loadbalancers.Create(client, opts, nil).Extract()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestLBSMetadataV2(t *testing.T) {
t.Fatal(err)
}

defer loadbalancers.Delete(clientV1, resourceID)
defer loadbalancers.Delete(clientV1, resourceID, nil)

clientV2, err := v2client.NewLoadbalancerClientV2(ctx)
if err != nil {
Expand Down
30 changes: 23 additions & 7 deletions client/ports/v1/ports/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package ports

import (
"errors"
"fmt"

"github.com/G-Core/gcorelabscloud-go/client/ports/v1/client"
client2 "github.com/G-Core/gcorelabscloud-go/client/ports/v2/client"
"github.com/G-Core/gcorelabscloud-go/gcore/port/v1/ports"
ports1 "github.com/G-Core/gcorelabscloud-go/gcore/port/v1/ports"
ports2 "github.com/G-Core/gcorelabscloud-go/gcore/port/v2/ports"
"github.com/G-Core/gcorelabscloud-go/gcore/reservedfixedip/v1/reservedfixedips"
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"

"github.com/G-Core/gcorelabscloud-go/client/flags"
"github.com/G-Core/gcorelabscloud-go/client/utils"
Expand Down Expand Up @@ -73,7 +78,7 @@ var assignAllowedAddressPairsSubCommand = cli.Command{
Usage: "Assign allowed address pairs for instance port",
ArgsUsage: "<port_id>",
Category: "portsecurity",
Flags: []cli.Flag{
Flags: append([]cli.Flag{
&cli.StringSliceFlag{
Name: "ip-address",
Usage: "IP address of the port specified in allowed_address_pairs",
Expand All @@ -82,14 +87,19 @@ var assignAllowedAddressPairsSubCommand = cli.Command{
Name: "mac-address",
Usage: "MAC address of the port specified in allowed_address_pairs",
},
},
}, flags.WaitCommandFlags...),
Action: func(c *cli.Context) error {
portID, err := flags.GetFirstStringArg(c, portIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "assign")
return err
}
client, err := client.NewPortClientV1(c)
clientV1, err := client.NewPortClientV1(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.NewExitError(err, 1)
}
clientV2, err := client2.NewPortClientV2(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.NewExitError(err, 1)
Expand All @@ -101,16 +111,22 @@ var assignAllowedAddressPairsSubCommand = cli.Command{
return cli.NewExitError(err, 1)
}

opts := ports.AllowAddressPairsOpts{
opts := ports1.AllowAddressPairsOpts{
AllowedAddressPairs: addressPairs,
}

result, err := ports.AllowAddressPairs(client, portID, opts).Extract()
results, err := ports2.AllowAddressPairs(clientV2, portID, opts).Extract()
if err != nil {
return cli.NewExitError(err, 1)
}
utils.ShowResults(result, c.String("format"))
return nil

return utils.WaitTaskAndShowResult(c, clientV1, results, true, func(task tasks.TaskID) (interface{}, error) {
_, err := tasks.Get(clientV1, string(task)).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get task with ID: %s. Error: %w", task, err)
}
return nil, nil
})
},
}

Expand Down
11 changes: 11 additions & 0 deletions client/ports/v2/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package client

import (
gcorecloud "github.com/G-Core/gcorelabscloud-go"
"github.com/G-Core/gcorelabscloud-go/client/common"
"github.com/urfave/cli/v2"
)

func NewPortClientV2(c *cli.Context) (*gcorecloud.ServiceClient, error) {
return common.BuildClient(c, "ports", "v2")
}
4 changes: 2 additions & 2 deletions client/subnets/v1/subnets/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var subnetDeleteCommand = cli.Command{
return cli.NewExitError(err, 1)
}
results, err := subnets.Delete(client, subnetID, &gcorecloud.RequestOpts{
ConflictRetryAmount: c.Int("retry-amount"),
ConflictRetryAmount: c.Int("retry-amount"),
ConflictRetryInterval: c.Int("retry-interval"),
}).Extract()
if err != nil {
Expand Down Expand Up @@ -322,7 +322,7 @@ var subnetCreateCommand = cli.Command{
}

results, err := subnets.Create(client, opts, &gcorecloud.RequestOpts{
ConflictRetryAmount: c.Int("retry-amount"),
ConflictRetryAmount: c.Int("retry-amount"),
ConflictRetryInterval: c.Int("retry-interval"),
}).Extract()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/subnets/v1/testing/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createTestSubnet(client *gcorecloud.ServiceClient, opts subnets.CreateOpts,
gccidr.Mask = netIPNet.Mask
opts.CIDR = gccidr

res, err := subnets.Create(client, opts).Extract()
res, err := subnets.Create(client, opts, nil).Extract()
if err != nil {
return "", err
}
Expand All @@ -51,7 +51,7 @@ func createTestSubnet(client *gcorecloud.ServiceClient, opts subnets.CreateOpts,
}

func deleteTestSubnet(client *gcorecloud.ServiceClient, subnetID string) error {
results, err := subnets.Delete(client, subnetID).Extract()
results, err := subnets.Delete(client, subnetID, nil).Extract()
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions gcore/instance/v1/instances/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/G-Core/gcorelabscloud-go/gcore/ddos/v1/ddos"
"github.com/G-Core/gcorelabscloud-go/gcore/flavor/v1/flavors"
"github.com/G-Core/gcorelabscloud-go/gcore/instance/v1/types"
"github.com/G-Core/gcorelabscloud-go/gcore/reservedfixedip/v1/reservedfixedips"
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
"github.com/G-Core/gcorelabscloud-go/pagination"
Expand Down Expand Up @@ -167,15 +168,16 @@ type Instance struct {

// Interface represents a instance port interface.
type Interface struct {
Name *string `json:"interface_name"`
PortID string `json:"port_id"`
MacAddress gcorecloud.MAC `json:"mac_address"`
NetworkID string `json:"network_id"`
PortSecurityEnabled bool `json:"port_security_enabled"`
IPAssignments []PortIP `json:"ip_assignments"`
NetworkDetails NetworkDetail `json:"network_details"`
FloatingIPDetails []FloatingIP `json:"floatingip_details"`
SubPorts []SubPort `json:"sub_ports"`
Name *string `json:"interface_name"`
PortID string `json:"port_id"`
MacAddress gcorecloud.MAC `json:"mac_address"`
NetworkID string `json:"network_id"`
PortSecurityEnabled bool `json:"port_security_enabled"`
IPAssignments []PortIP `json:"ip_assignments"`
NetworkDetails NetworkDetail `json:"network_details"`
FloatingIPDetails []FloatingIP `json:"floatingip_details"`
SubPorts []SubPort `json:"sub_ports"`
AllowedAddressPairs []reservedfixedips.AllowedAddressPairs `json:"allowed_address_pairs"`
}

// SubPort represent a instance sub port interface
Expand Down
9 changes: 9 additions & 0 deletions gcore/loadbalancer/v1/lbpools/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func Update(c *gcorecloud.ServiceClient, lbpoolID string, opts UpdateOptsBuilder
r.Err = err
return
}
if reqOpts == nil {
reqOpts = &gcorecloud.RequestOpts{}
}
reqOpts.OkCodes = []int{200, 201}
_, r.Err = c.Patch(updateURL(c, lbpoolID), b, &r.Body, reqOpts)
return
Expand Down Expand Up @@ -222,6 +225,9 @@ func Unset(c *gcorecloud.ServiceClient, lbpoolID string, opts UnsetOptsBuilder,
r.Err = err
return
}
if reqOpts == nil {
reqOpts = &gcorecloud.RequestOpts{}
}
reqOpts.OkCodes = []int{200, 201}
_, r.Err = c.Patch(updateURL(c, lbpoolID), b, &r.Body, reqOpts)
return
Expand Down Expand Up @@ -261,6 +267,9 @@ func DeleteMember(c *gcorecloud.ServiceClient, lbpoolID string, memberID string,

// DeleteHealthMonitor accepts a unique ID and deletes the lbpool's healthmonitor associated with it.
func DeleteHealthMonitor(c *gcorecloud.ServiceClient, lbpoolID string, reqOpts *gcorecloud.RequestOpts) (r DeleteHealthMonitorResult) {
if reqOpts == nil {
reqOpts = &gcorecloud.RequestOpts{}
}
reqOpts.OkCodes = []int{http.StatusNoContent}
_, r.Err = c.Delete(healthMonitorURL(c, lbpoolID), reqOpts)
return
Expand Down
5 changes: 1 addition & 4 deletions gcore/loadbalancer/v1/lbpools/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ const CreatePoolMemberRequest = `

const UpdateRequest = `
{
"name": "lbaas_test_pool",
"timeout_client_data": 0,
"timeout_member_connect": 0,
"timeout_member_data": 0
"name": "lbaas_test_pool"
}
`

Expand Down
16 changes: 8 additions & 8 deletions gcore/loadbalancer/v1/lbpools/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestCreate(t *testing.T) {
}

client := fake.ServiceTokenClient("lbpools", "v1")
tasks, err := lbpools.Create(client, options).Extract()
tasks, err := lbpools.Create(client, options, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
Expand All @@ -214,7 +214,7 @@ func TestDelete(t *testing.T) {
})

client := fake.ServiceTokenClient("lbpools", "v1")
tasks, err := lbpools.Delete(client, LBPool1.ID).Extract()
tasks, err := lbpools.Delete(client, LBPool1.ID, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

Expand Down Expand Up @@ -248,7 +248,7 @@ func TestUpdate(t *testing.T) {
Name: LBPool1.Name,
}

tasks, err := lbpools.Update(client, LBPool1.ID, opts).Extract()
tasks, err := lbpools.Update(client, LBPool1.ID, opts, nil).Extract()

require.NoError(t, err)
require.NoError(t, err)
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestUnset(t *testing.T) {
SessionPersistence: true,
}

tasks, err := lbpools.Unset(client, LBPool1.ID, opts).Extract()
tasks, err := lbpools.Unset(client, LBPool1.ID, opts, nil).Extract()

require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
Expand All @@ -308,7 +308,7 @@ func TestDeleteMember(t *testing.T) {
})

client := fake.ServiceTokenClient("lbpools", "v1")
tasks, err := lbpools.DeleteMember(client, LBPool1.ID, memberID).Extract()
tasks, err := lbpools.DeleteMember(client, LBPool1.ID, memberID, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

Expand Down Expand Up @@ -342,7 +342,7 @@ func TestCreateMember(t *testing.T) {
}

client := fake.ServiceTokenClient("lbpools", "v1")
tasks, err := lbpools.CreateMember(client, LBPool1.ID, options).Extract()
tasks, err := lbpools.CreateMember(client, LBPool1.ID, options, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
Expand All @@ -358,7 +358,7 @@ func TestDeleteHealthMonitor(t *testing.T) {
})

client := fake.ServiceTokenClient("lbpools", "v1")
err := lbpools.DeleteHealthMonitor(client, LBPool1.ID).ExtractErr()
err := lbpools.DeleteHealthMonitor(client, LBPool1.ID, nil).ExtractErr()
require.NoError(t, err)

}
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestCreateHealthMonitor(t *testing.T) {
}

client := fake.ServiceTokenClient("lbpools", "v1")
tasks, err := lbpools.CreateHealthMonitor(client, LBPool1.ID, opts).Extract()
tasks, err := lbpools.CreateHealthMonitor(client, LBPool1.ID, opts, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
6 changes: 6 additions & 0 deletions gcore/loadbalancer/v1/listeners/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func Update(c *gcorecloud.ServiceClient, listenerID string, opts UpdateOptsBuild
r.Err = err
return
}
if reqOpts == nil {
reqOpts = &gcorecloud.RequestOpts{}
}
reqOpts.OkCodes = []int{200, 201}
_, r.Err = c.Patch(updateURL(c, listenerID), b, &r.Body, reqOpts)
return
Expand Down Expand Up @@ -173,6 +176,9 @@ func Unset(c *gcorecloud.ServiceClient, listenerID string, opts UnsetOptsBuilder
} else {
delete(b, "user_list")
}
if reqOpts == nil {
reqOpts = &gcorecloud.RequestOpts{}
}
reqOpts.OkCodes = []int{200, 201}
_, r.Err = c.Patch(updateURL(c, listenerID), b, &r.Body, reqOpts)
return
Expand Down
10 changes: 5 additions & 5 deletions gcore/loadbalancer/v1/listeners/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ func TestCreate(t *testing.T) {
Protocol: types.ProtocolTypeTCP,
ProtocolPort: 80,
LoadBalancerID: Listener1.ID,
AllowedCIDRS: []string{"10.10.0.0/24"},
AllowedCIDRS: []string{"10.10.0.0/24"},
}

client := fake.ServiceTokenClient("lblisteners", "v1")
tasks, err := listeners.Create(client, options).Extract()
tasks, err := listeners.Create(client, options, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
Expand All @@ -183,7 +183,7 @@ func TestDelete(t *testing.T) {
})

client := fake.ServiceTokenClient("lblisteners", "v1")
tasks, err := listeners.Delete(client, Listener1.ID).Extract()
tasks, err := listeners.Delete(client, Listener1.ID, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

Expand Down Expand Up @@ -217,7 +217,7 @@ func TestUpdate(t *testing.T) {
Name: Listener1.Name,
}

task, err := listeners.Update(client, Listener1.ID, opts).Extract()
task, err := listeners.Update(client, Listener1.ID, opts, nil).Extract()

require.NoError(t, err)
require.Equal(t, Tasks1, *task)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestUnset(t *testing.T) {
AllowedCIDRS: true,
}

tasks, err := listeners.Unset(client, Listener1.ID, opts).Extract()
tasks, err := listeners.Unset(client, Listener1.ID, opts, nil).Extract()

require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
Expand Down
6 changes: 3 additions & 3 deletions gcore/loadbalancer/v1/loadbalancers/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestCreate(t *testing.T) {
}

client := fake.ServiceTokenClient("loadbalancers", "v1")
tasks, err := loadbalancers.Create(client, options).Extract()
tasks, err := loadbalancers.Create(client, options, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
Expand All @@ -236,7 +236,7 @@ func TestDelete(t *testing.T) {
})

client := fake.ServiceTokenClient("loadbalancers", "v1")
tasks, err := loadbalancers.Delete(client, LoadBalancer1.ID).Extract()
tasks, err := loadbalancers.Delete(client, LoadBalancer1.ID, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

Expand Down Expand Up @@ -347,7 +347,7 @@ func TestResizeLoadBalancer(t *testing.T) {
Flavor: Flavor,
}

tasks, err := loadbalancers.Resize(client, LoadBalancer1.ID, opts).Extract()
tasks, err := loadbalancers.Resize(client, LoadBalancer1.ID, opts, nil).Extract()
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)
}
Expand Down
Loading

0 comments on commit 9e3715e

Please sign in to comment.