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

Feature/GCLOUD2-13858: Enable unsetting listener allowed_cidrs and user_list #121

Merged
merged 5 commits into from
Apr 4, 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
46 changes: 46 additions & 0 deletions client/loadbalancers/v1/listeners/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,51 @@ var listenerUpdateSubCommand = cli.Command{
},
}

var listenerUnsetSubCommand = cli.Command{
Name: "unset",
Usage: "clear loadbalancer listener settings",
ArgsUsage: "<listener_id>",
Category: "listener",
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "allowed-cidrs",
Usage: "Clear all allowed CIDRs from the listener",
Required: false,
},
}, flags.WaitCommandFlags...),
Action: func(c *cli.Context) error {
listenerID, err := flags.GetFirstStringArg(c, listenerIDText)
if err != nil {
_ = cli.ShowCommandHelp(c, "unset")
return err
}
client, err := client.NewLBListenerClientV2(c)
if err != nil {
_ = cli.ShowAppHelp(c)
return cli.NewExitError(err, 1)
}
opts := listeners.UnsetOpts{
AllowedCIDRS: c.Bool("allowed-cidrs"),
}
results, err := listeners.Unset(client, listenerID, opts).Extract()
if err != nil {
return cli.NewExitError(err, 1)
}
return utils.WaitTaskAndShowResult(c, client, results, true, func(task tasks.TaskID) (interface{}, error) {
_, err := tasks.Get(client, string(task)).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get task with ID: %s. Error: %w", task, err)
}
listener, err := listeners.Get(client, listenerID).Extract()
if err != nil {
return nil, fmt.Errorf("cannot get listener with ID: %s. Error: %w", listenerID, err)
}
utils.ShowResults(listener, c.String("format"))
return nil, nil
})
},
}

var ListenerCommands = cli.Command{
Name: "listener",
Usage: "GCloud loadbalancer listeners API",
Expand All @@ -351,5 +396,6 @@ var ListenerCommands = cli.Command{
&listenerUpdateSubCommand,
&listenerDeleteSubCommand,
&listenerCreateSubCommand,
&listenerUnsetSubCommand,
},
}
1 change: 0 additions & 1 deletion gcore/loadbalancer/v1/lbpools/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func TestUnset(t *testing.T) {

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

require.NoError(t, err)
require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

Expand Down
43 changes: 43 additions & 0 deletions gcore/loadbalancer/v1/listeners/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,49 @@ func Update(c *gcorecloud.ServiceClient, listenerID string, opts UpdateOptsBuild
return
}

// UnsetOptsBuilder allows extensions to add additional parameters to the Unset request.
type UnsetOptsBuilder interface {
ToListenerUnsetMap() (map[string]interface{}, error)
}

// UnsetOpts represents options used to unset lbpool fields.
type UnsetOpts struct {
AllowedCIDRS bool `json:"allowed_cidrs"`
UserList bool `json:"user_list"`
}

// ToLbListenerUnsetMap builds a request body from UnsetOpts.
func (opts UnsetOpts) ToListenerUnsetMap() (map[string]interface{}, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return nil, err
}
return gcorecloud.BuildRequestBody(opts, "")
}

// Unset accepts an UnsetOpts struct and unsets an existing listner fields using
// values provided.
func Unset(c *gcorecloud.ServiceClient, listenerID string, opts UnsetOptsBuilder) (r tasks.Result) {
b, err := opts.ToListenerUnsetMap()
if err != nil {
r.Err = err
return
}
allowedCIDRS, ok := b["allowed_cidrs"]
if ok && allowedCIDRS.(bool) {
b["allowed_cidrs"] = nil
} else {
delete(b, "allowed_cidrs")
}
userList, ok := b["user_list"]
if ok && userList.(bool) {
b["user_list"] = make([]CreateUserListOpts, 0)
}
_, r.Err = c.Patch(updateURL(c, listenerID), b, &r.Body, &gcorecloud.RequestOpts{
OkCodes: []int{200, 201},
})
return
}

// Delete accepts a unique ID and deletes the listener associated with it.
func Delete(c *gcorecloud.ServiceClient, listenerID string) (r tasks.Result) {
_, r.Err = c.DeleteWithResponse(deleteURL(c, listenerID), &r.Body, nil)
Expand Down
14 changes: 14 additions & 0 deletions gcore/loadbalancer/v1/listeners/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const UpdateRequest = `
}
`

const UnsetRequest = `
{
"allowed_cidrs": null
}
`

const CreateResponse = `
{
"tasks": [
Expand All @@ -101,6 +107,14 @@ const DeleteResponse = `
}
`

const UnsetResponse = `
{
"tasks": [
"50f53a35-42ed-40c4-82b2-5a37fb3e00bc"
]
}
`

var (
creatorTaskID = "9f3ec11e-bcd4-4fe6-924a-a4439a56ad22"
taskID = "9f3ec11e-bcd4-4fe6-924a-a4439a56ad22"
Expand Down
35 changes: 35 additions & 0 deletions gcore/loadbalancer/v1/listeners/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,38 @@ func TestUpdate(t *testing.T) {
require.Equal(t, Tasks1, *task)

}

func TestUnset(t *testing.T) {

th.SetupHTTP()
defer th.TeardownHTTP()

testURL := prepareGetTestURLv2(Listener1.ID)

th.Mux.HandleFunc(testURL, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PATCH")
th.TestHeader(t, r, "Authorization", fmt.Sprintf("Bearer %s", fake.AccessToken))
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, UnsetRequest)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

_, err := fmt.Fprint(w, UnsetResponse)
if err != nil {
log.Error(err)
}
})

client := fake.ServiceTokenClient("lblisteners", "v2")

opts := listeners.UnsetOpts{
AllowedCIDRS: true,
}

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

require.NoError(t, err)
require.Equal(t, Tasks1, *tasks)

}
Loading