-
Notifications
You must be signed in to change notification settings - Fork 23
feat(dbdr/net-peering) [STORY-3557] Add commands to configure net peerings of DB-DR #1208
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
Draft
leo-scalingo
wants to merge
1
commit into
master
Choose a base branch
from
feat/STORY-3557/db-dr-net-peering
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
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,172 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
|
|
||
| "github.com/Scalingo/cli/cmd/autocomplete" | ||
| "github.com/Scalingo/cli/dbng" | ||
| "github.com/Scalingo/cli/detect" | ||
| "github.com/Scalingo/cli/utils" | ||
| "github.com/Scalingo/go-scalingo/v11" | ||
| ) | ||
|
|
||
| var ( | ||
| databaseNetPeeringsListCommand = cli.Command{ | ||
| Name: "database-net-peerings", | ||
| Category: "Databases DR", | ||
| Usage: "List net peerings of a database", | ||
| ArgsUsage: "database-id", | ||
| Flags: []cli.Flag{databaseFlag()}, | ||
| Description: CommandDescription{ | ||
| Description: "List all net peerings of a database", | ||
| Examples: []string{ | ||
| "scalingo database-net-peerings my-db-id", | ||
| "scalingo --database my-db database-net-peerings", | ||
| }, | ||
| SeeAlso: []string{"database-net-peerings-add", "database-net-peerings-remove", "database-network-configuration"}, | ||
| }.Render(), | ||
| Action: func(ctx context.Context, c *cli.Command) error { | ||
| databaseID := detect.ExtractDatabaseNameFromCommandLineOrEnv(c) | ||
| if databaseID == "" { | ||
| return cli.ShowCommandHelp(ctx, c, "database-net-peerings") | ||
| } | ||
| utils.CheckForConsent(ctx, databaseID, utils.ConsentTypeDBs) | ||
|
|
||
| err := dbng.DatabaseNetPeeringsList(ctx, databaseID) | ||
| if err != nil { | ||
| errorQuit(ctx, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| ShellComplete: func(ctx context.Context, c *cli.Command) { | ||
| _ = autocomplete.CmdFlagsAutoComplete(c, "database-net-peerings") | ||
| _ = autocomplete.DatabasesNgListAutoComplete(ctx) | ||
| }, | ||
| } | ||
|
|
||
| databaseNetPeeringsAddCommand = cli.Command{ | ||
| Name: "database-net-peerings-add", | ||
| Category: "Databases DR", | ||
| Usage: "Add a net peering to a database", | ||
| ArgsUsage: "database-id", | ||
| Flags: []cli.Flag{ | ||
| databaseFlag(), | ||
| &cli.StringFlag{ | ||
| Name: "outscale-net-peering-id", | ||
| Usage: "Outscale net peering ID", | ||
| Required: true, | ||
| }, | ||
| }, | ||
| Description: CommandDescription{ | ||
| Description: "Initiate the creation of a net peering for a database", | ||
| Examples: []string{ | ||
| "scalingo database-net-peerings-add my-db-id --outscale-net-peering-id pcx-123456789", | ||
| "scalingo --database my-db database-net-peerings-add --outscale-net-peering-id pcx-123456789", | ||
| }, | ||
| SeeAlso: []string{"database-net-peerings", "database-net-peerings-remove", "database-network-configuration"}, | ||
| }.Render(), | ||
| Action: func(ctx context.Context, c *cli.Command) error { | ||
| databaseID := detect.ExtractDatabaseNameFromCommandLineOrEnv(c) | ||
| if databaseID == "" { | ||
| return cli.ShowCommandHelp(ctx, c, "database-net-peerings-add") | ||
| } | ||
|
|
||
| utils.CheckForConsent(ctx, databaseID, utils.ConsentTypeDBs) | ||
|
|
||
| params := scalingo.DatabaseNetPeeringCreateParams{ | ||
| OutscaleNetPeeringID: c.String("outscale-net-peering-id"), | ||
| } | ||
|
|
||
| err := dbng.DatabaseNetPeeringsAdd(ctx, databaseID, params) | ||
| if err != nil { | ||
| errorQuit(ctx, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| ShellComplete: func(ctx context.Context, c *cli.Command) { | ||
| _ = autocomplete.CmdFlagsAutoComplete(c, "database-net-peerings-add") | ||
| _ = autocomplete.DatabasesNgListAutoComplete(ctx) | ||
| }, | ||
| } | ||
|
|
||
| databaseNetPeeringsRemoveCommand = cli.Command{ | ||
| Name: "database-net-peerings-remove", | ||
| Category: "Databases DR", | ||
| Usage: "Remove a net peering from a database", | ||
| ArgsUsage: "database-id net-peering-id", | ||
| Flags: []cli.Flag{ | ||
| databaseFlag(), | ||
| &cli.StringFlag{ | ||
| Name: "net-peering", | ||
| Aliases: []string{"np"}, | ||
| Usage: "Net peering ID to remove", | ||
| }, | ||
| }, | ||
| Description: CommandDescription{ | ||
| Description: "Delete an existing net peering from a database", | ||
| Examples: []string{ | ||
| "scalingo database-net-peerings-remove my-db-id np-id", | ||
| "scalingo --database my-db database-net-peerings-remove np-id", | ||
| }, | ||
| SeeAlso: []string{"database-net-peerings", "database-net-peerings-add", "database-network-configuration"}, | ||
| }.Render(), | ||
| Action: func(ctx context.Context, c *cli.Command) error { | ||
| databaseID := detect.ExtractDatabaseNameFromCommandLineOrEnv(c) | ||
| if databaseID == "" { | ||
| return cli.ShowCommandHelp(ctx, c, "database-net-peerings-remove") | ||
| } | ||
|
|
||
| utils.CheckForConsent(ctx, databaseID, utils.ConsentTypeDBs) | ||
|
|
||
| err := dbng.DatabaseNetPeeringsRemove(ctx, databaseID, c.String("net-peering")) | ||
| if err != nil { | ||
| errorQuit(ctx, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| ShellComplete: func(ctx context.Context, c *cli.Command) { | ||
| _ = autocomplete.CmdFlagsAutoComplete(c, "database-net-peerings-remove") | ||
| _ = autocomplete.DatabasesNgListAutoComplete(ctx) | ||
| }, | ||
| } | ||
|
|
||
| databaseNetworkConfigurationShowCommand = cli.Command{ | ||
| Name: "database-network-configuration", | ||
| Category: "Databases DR", | ||
| Usage: "Show network configuration of a database", | ||
| ArgsUsage: "database-id", | ||
| Flags: []cli.Flag{databaseFlag()}, | ||
| Description: CommandDescription{ | ||
| Description: "Get network configuration of a database", | ||
| Examples: []string{ | ||
| "scalingo database-network-configuration my-db-id", | ||
| "scalingo --database my-db database-network-configuration", | ||
| }, | ||
| SeeAlso: []string{"database-net-peerings", "database-net-peerings-add", "database-net-peerings-remove"}, | ||
| }.Render(), | ||
| Action: func(ctx context.Context, c *cli.Command) error { | ||
| databaseID := detect.ExtractDatabaseNameFromCommandLineOrEnv(c) | ||
| if databaseID == "" { | ||
| return cli.ShowCommandHelp(ctx, c, "database-network-configuration") | ||
| } | ||
|
|
||
| utils.CheckForConsent(ctx, databaseID, utils.ConsentTypeDBs) | ||
|
|
||
| err := dbng.DatabaseNetworkConfigurationShow(ctx, databaseID) | ||
| if err != nil { | ||
| errorQuit(ctx, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| ShellComplete: func(ctx context.Context, c *cli.Command) { | ||
| _ = autocomplete.CmdFlagsAutoComplete(c, "database-network-configuration") | ||
| _ = autocomplete.DatabasesNgListAutoComplete(ctx) | ||
| }, | ||
| } | ||
| ) |
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 |
|---|---|---|
|
|
@@ -135,6 +135,9 @@ func displayRequestFailedError(requestFailedErr *httpclient.RequestFailedError, | |
| fmt.Println(io.Indent(err.Error(), 7)) | ||
| } | ||
|
|
||
| case http.StatusConflict: // 409 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: do we want to also handle |
||
| // In case of conflict error, we only want to display the API error. | ||
| io.Errorf("%s\n", strings.ReplaceAll(requestFailedErr.Error(), `\n`, "\n")) | ||
| case http.StatusUnprocessableEntity: | ||
| unprocessableEntityErr, ok := requestFailedErr.APIError.(httpclient.UnprocessableEntity) | ||
| if !ok { | ||
|
|
||
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,104 @@ | ||
| package dbng | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
|
|
||
| "github.com/olekukonko/tablewriter" | ||
|
|
||
| "github.com/Scalingo/cli/config" | ||
| "github.com/Scalingo/cli/io" | ||
| "github.com/Scalingo/go-scalingo/v11" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| func DatabaseNetPeeringsList(ctx context.Context, databaseID string) error { | ||
| c, err := config.ScalingoClient(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "get Scalingo client") | ||
| } | ||
|
|
||
| netPeerings, err := c.Preview().DatabaseNetPeeringsList(ctx, databaseID) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "list database net peerings") | ||
| } | ||
|
|
||
| if len(netPeerings) == 0 { | ||
| io.Status("No net peering configured for this database.") | ||
| return nil | ||
| } | ||
|
|
||
| t := tablewriter.NewWriter(os.Stdout) | ||
| t.Header([]string{"ID", "Status", "Outscale Net Peering ID", "Source Net ID", "Source Net IP Range", "Source Account ID"}) | ||
|
|
||
| for _, netPeering := range netPeerings { | ||
| _ = t.Append([]string{ | ||
| netPeering.ID, | ||
| string(netPeering.Status), | ||
| netPeering.OutscaleNetPeeringID, | ||
| netPeering.OutscaleSourceNetID, | ||
| netPeering.OutscaleSourceNetIPRange, | ||
| netPeering.OutscaleSourceAccountID, | ||
| }) | ||
| } | ||
|
|
||
| _ = t.Render() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func DatabaseNetPeeringsAdd(ctx context.Context, databaseID string, params scalingo.DatabaseNetPeeringCreateParams) error { | ||
| c, err := config.ScalingoClient(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "get Scalingo client") | ||
| } | ||
|
|
||
| netPeering, err := c.Preview().DatabaseNetPeeringCreate(ctx, databaseID, params) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "add database net peering") | ||
| } | ||
|
|
||
| io.Statusf("Net peering '%s' creation has been initiated for database '%s'.\n", netPeering.ID, databaseID) | ||
| io.Warning("Expect some delay for the net peering to be applied.") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func DatabaseNetPeeringsRemove(ctx context.Context, databaseID, netPeeringID string) error { | ||
| c, err := config.ScalingoClient(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "get Scalingo client") | ||
| } | ||
|
|
||
| err = c.Preview().DatabaseNetPeeringDestroy(ctx, databaseID, netPeeringID) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "remove database net peering") | ||
| } | ||
|
|
||
| io.Statusf("Net peering '%s' has been removed from database '%s'.\n", netPeeringID, databaseID) | ||
| io.Warning("Expect some delay for the net peering removal to be applied.") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func DatabaseNetworkConfigurationShow(ctx context.Context, databaseID string) error { | ||
| c, err := config.ScalingoClient(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "get Scalingo client") | ||
| } | ||
|
|
||
| networkConfiguration, err := c.Preview().DatabaseNetworkConfigurationShow(ctx, databaseID) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "show database network configuration") | ||
| } | ||
|
|
||
| t := tablewriter.NewWriter(os.Stdout) | ||
| _ = t.Bulk([][]string{ | ||
| {"Outscale account ID", networkConfiguration.OutscaleAccountID}, | ||
| {"Outscale net ID", networkConfiguration.OutscaleNetID}, | ||
| {"IP range", networkConfiguration.IPRange}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: no need for the |
||
| }) | ||
| _ = t.Render() | ||
|
|
||
| return nil | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: most of the time we are not mentioning story number on the public changelog.