-
Notifications
You must be signed in to change notification settings - Fork 152
feat(baremetal): add support for add flexible ip #3670
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
Merged
remyleone
merged 7 commits into
scaleway:master
from
Laure-di:feat/add-support-for-add-flexible-ip
Apr 18, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
88f54eb
feat(baremetal): add support for add flexible ip
Laure-di 357302a
feat(baremetal):add of tests
Laure-di 3f714c3
feat:remove of description and tags prompt
Laure-di 47f8e99
fix test
Laure-di c8942c6
fix golangci-lint
Laure-di 808d9c1
fix format test
Laure-di d7a4006
fix format test
Laure-di File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
cmd/scw/testdata/test-all-usage-baremetal-server-add-flexible-ip-usage.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 | ||
🟥🟥🟥 STDERR️️ 🟥🟥🟥️ | ||
Create and attach a new flexible IP to a server | ||
|
||
USAGE: | ||
scw baremetal server add-flexible-ip <server-id ...> [arg=value ...] | ||
|
||
ARGS: | ||
server-id ID of the server to which the newly created flexible IP will be attached. | ||
[description] Flexible IP description (max. of 255 characters) | ||
[ip-type] Define whether the flexible IP is an IPv4 or IPv6 (IPv4 | IPv6) | ||
[tags.{index}] Tags to associate to the flexible IP | ||
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | nl-ams-1) | ||
|
||
FLAGS: | ||
-h, --help help for add-flexible-ip | ||
|
||
GLOBAL FLAGS: | ||
-c, --config string The path to the config file | ||
-D, --debug Enable debug mode | ||
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human") | ||
-p, --profile string The config profile to use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/scaleway/scaleway-cli/v2/internal/core" | ||
flexibleip "github.com/scaleway/scaleway-cli/v2/internal/namespaces/flexibleip/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1" | ||
fip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
type serverAddFlexibleIPRequest struct { | ||
ServerID string | ||
Description string | ||
IPType string | ||
Tags []string | ||
Zone scw.Zone | ||
} | ||
|
||
func serverAddFlexibleIP() *core.Command { | ||
return &core.Command{ | ||
Short: "Attach a new flexible IP to a server", | ||
Long: "Create and attach a new flexible IP to a server", | ||
Namespace: "baremetal", | ||
Resource: "server", | ||
Verb: "add-flexible-ip", | ||
Groups: []string{"utility"}, | ||
ArgsType: reflect.TypeOf(serverAddFlexibleIPRequest{}), | ||
ArgSpecs: core.ArgSpecs{ | ||
{ | ||
Name: "server-id", | ||
Short: `ID of the server to which the newly created flexible IP will be attached.`, | ||
Required: true, | ||
Deprecated: false, | ||
Positional: true, | ||
}, | ||
{ | ||
Name: "description", | ||
Short: `Flexible IP description (max. of 255 characters)`, | ||
Required: false, | ||
Deprecated: false, | ||
Positional: false, | ||
}, | ||
{ | ||
Name: "ip-type", | ||
Short: `Define whether the flexible IP is an IPv4 or IPv6`, | ||
Required: false, | ||
Deprecated: false, | ||
Positional: false, | ||
EnumValues: ipTypeOption, | ||
ValidateFunc: func(_ *core.ArgSpec, value interface{}) error { | ||
if value == "IPv4" || value == "IPv6" || value == "" { | ||
return nil | ||
} | ||
return &core.CliError{ | ||
Err: fmt.Errorf("error looks like the IP type isn't correct"), | ||
Hint: "Two type available : IPv6 or IPv4", | ||
} | ||
}, | ||
}, | ||
{ | ||
Name: "tags.{index}", | ||
Short: `Tags to associate to the flexible IP`, | ||
Required: false, | ||
Deprecated: false, | ||
Positional: false, | ||
}, | ||
core.ZoneArgSpec(scw.ZoneFrPar1, scw.ZoneFrPar2, scw.ZoneNlAms1), | ||
}, | ||
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) { | ||
request := argsI.(*serverAddFlexibleIPRequest) | ||
client := core.ExtractClient(ctx) | ||
api := baremetal.NewAPI(client) | ||
server, err := api.GetServer(&baremetal.GetServerRequest{ | ||
Zone: request.Zone, | ||
ServerID: request.ServerID, | ||
}, scw.WithContext(ctx)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
args := request | ||
if args.IPType == "" { | ||
args, err = promptIPFlexibleServer(ctx, request) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
apiFip := fip.NewAPI(client) | ||
IsIPv6 := args.IPType == "IPv6" | ||
flexibleIP, err := apiFip.CreateFlexibleIP(&fip.CreateFlexibleIPRequest{ | ||
ServerID: &server.ID, | ||
Zone: server.Zone, | ||
ProjectID: server.ProjectID, | ||
Description: args.Description, | ||
Tags: args.Tags, | ||
IsIPv6: IsIPv6, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return apiFip.WaitForFlexibleIP(&fip.WaitForFlexibleIPRequest{ | ||
FipID: flexibleIP.ID, | ||
Zone: flexibleIP.Zone, | ||
Timeout: scw.TimeDurationPtr(flexibleip.FlexibleIPTimeout), | ||
RetryInterval: core.DefaultRetryInterval, | ||
}) | ||
}, | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
internal/namespaces/baremetal/v1/custom_server_fip_prompt.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/fatih/color" | ||
"github.com/scaleway/scaleway-cli/v2/internal/core" | ||
"github.com/scaleway/scaleway-cli/v2/internal/interactive" | ||
"github.com/scaleway/scaleway-cli/v2/internal/terminal" | ||
) | ||
|
||
var ipTypeOption = []string{"IPv4", "IPv6"} | ||
|
||
func promptIPFlexibleServer(ctx context.Context, req *serverAddFlexibleIPRequest) (*serverAddFlexibleIPRequest, error) { | ||
if !interactive.IsInteractive { | ||
Laure-di marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, &core.CliError{ | ||
Err: fmt.Errorf("failed to create and attach a new flexible IP"), | ||
Hint: "Missing argument 'ip-type'", | ||
} | ||
} | ||
var err error | ||
quitMessage := terminal.Style("Type Ctrl+c or q to quit", color.FgHiBlue, color.Bold) | ||
fmt.Println(quitMessage) | ||
req.IPType, err = promptChooseIPType(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return req, nil | ||
} | ||
|
||
func promptChooseIPType(ctx context.Context) (string, error) { | ||
prompt := interactive.ListPrompt{ | ||
Prompt: "Choose your internet protocol", | ||
Choices: ipTypeOption, | ||
DefaultIndex: 0, | ||
} | ||
ipTypeIndex, err := prompt.Execute(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
return ipTypeOption[ipTypeIndex], nil | ||
} |
54 changes: 54 additions & 0 deletions
54
internal/namespaces/baremetal/v1/custom_server_fip_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package baremetal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/scaleway/scaleway-cli/v2/internal/core" | ||
"github.com/scaleway/scaleway-cli/v2/internal/interactive" | ||
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1" | ||
flexibleip "github.com/scaleway/scaleway-cli/v2/internal/namespaces/flexibleip/v1alpha1" | ||
) | ||
|
||
func Test_CreateFlexibleIPInteractive(t *testing.T) { | ||
promptResponse := []string{ | ||
`" "`, | ||
} | ||
interactive.IsInteractive = true | ||
cmds := baremetal.GetCommands() | ||
cmds.Merge(flexibleip.GetCommands()) | ||
t.Run("Simple Interactive", core.Test(&core.TestConfig{ | ||
Commands: cmds, | ||
BeforeFunc: core.BeforeFuncCombine( | ||
createServerAndWaitDefault("Server"), | ||
), | ||
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }}", | ||
Check: core.TestCheckCombine( | ||
core.TestCheckGolden(), | ||
), | ||
AfterFunc: core.AfterFuncCombine( | ||
deleteServerDefault("Server"), | ||
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"), | ||
), | ||
PromptResponseMocks: promptResponse, | ||
})) | ||
} | ||
|
||
func Test_CreateFlexibleIP(t *testing.T) { | ||
interactive.IsInteractive = false | ||
cmds := baremetal.GetCommands() | ||
cmds.Merge(flexibleip.GetCommands()) | ||
t.Run("Simple", core.Test(&core.TestConfig{ | ||
Commands: cmds, | ||
BeforeFunc: core.BeforeFuncCombine( | ||
createServerAndWaitDefault("Server"), | ||
), | ||
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} ip-type=IPv4", | ||
Check: core.TestCheckCombine( | ||
core.TestCheckGolden(), | ||
), | ||
AfterFunc: core.AfterFuncCombine( | ||
deleteServerDefault("Server"), | ||
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"), | ||
), | ||
})) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.