-
Notifications
You must be signed in to change notification settings - Fork 3
Network #480
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
Open
cdalar
wants to merge
30
commits into
main
Choose a base branch
from
network
base: main
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.
Open
Network #480
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
6a6e903
wip: network
cdalar 0868db3
network funcs
cdalar 42be318
network
cdalar b2336c4
lint fix
cdalar 5ae722c
Merge remote-tracking branch 'origin' into network
cdalar 1b2f424
flags required
cdalar a39dc39
rm requiredFlags
cdalar 3d80c4a
Merge branch 'main' into network
cdalar 80517d8
Update internal/cloud/hetzner.go
cdalar 989dc67
jumphost initial
cdalar 7f96333
jumphost create
cdalar 4f15b54
aggregate sshinto
cdalar e1b449f
linter errors
cdalar d91b9eb
docs
cdalar b1bf809
gofmt
cdalar 2252f3c
cert
cdalar c171673
tmpl describe
cdalar 45b0470
auto complete
cdalar 10499ae
fix linter issues
cdalar 55704bf
pipelines
cdalar 076242a
resolve merge conflicts with main
cdalar 8a81a77
replace go-term-markdown with glamour for better markdown rendering
cdalar 7f03737
tests
cdalar 9f9ae96
lint error
cdalar c863071
cursor rules
cdalar d8dc21a
Merge local ConnectRPC dependencies with remote updates
cdalar 6b8bb3c
Add lint and coverage targets to Makefile
cdalar ca1c846
Update .gitignore to ignore coverage files
cdalar d61945d
Merge remote-tracking branch 'origin/network' into network
cdalar da8d33a
clinerules
cdalar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/briandowns/spinner" | ||
| "github.com/cdalar/onctl/internal/cloud" | ||
| "github.com/spf13/cobra" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| var ( | ||
| nOpt cloud.Network | ||
| ) | ||
|
|
||
| func init() { | ||
| networkCmd.AddCommand(networkCreateCmd) | ||
| networkCmd.AddCommand(networkListCmd) | ||
| networkCmd.AddCommand(networkDeleteCmd) | ||
| networkCreateCmd.Flags().StringVar(&nOpt.CIDR, "cidr", "", "CIDR for the network ex. 10.0.0.0/16 ") | ||
| networkCreateCmd.Flags().StringVarP(&nOpt.Name, "name", "n", "", "Name for the network") | ||
| } | ||
|
|
||
| var networkCmd = &cobra.Command{ | ||
| Use: "network", | ||
| Aliases: []string{"net"}, | ||
| Short: "Manage network resources", | ||
| Long: `Manage network resources`, | ||
| } | ||
|
|
||
| var networkCreateCmd = &cobra.Command{ | ||
| Use: "create", | ||
| Aliases: []string{"new", "add", "up"}, | ||
| Short: "Create a network", | ||
| Long: `Create a network`, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // Do network creation | ||
| log.Println("[DEBUG] Creating network") | ||
| _, err := networkManager.Create(cloud.Network{ | ||
| Name: nOpt.Name, | ||
| CIDR: nOpt.CIDR, | ||
| }) | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| var networkListCmd = &cobra.Command{ | ||
| Use: "list", | ||
| Aliases: []string{"ls"}, | ||
| Short: "List networks", | ||
| Long: `List networks`, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // Do network listing | ||
| log.Println("[DEBUG] Listing networks") | ||
| netlist, err := networkManager.List() | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| switch output { | ||
| case "json": | ||
| jsonList, err := json.Marshal(netlist) | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| fmt.Println(string(jsonList)) | ||
| case "yaml": | ||
| yamlList, err := yaml.Marshal(netlist) | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| fmt.Println(string(yamlList)) | ||
| default: | ||
| tmpl := "CLOUD\tID\tNAME\tCIDR\tSERVERS\tAGE\n{{range .}}{{.Provider}}\t{{.ID}}\t{{.Name}}\t{{.CIDR}}\t{{.Servers}}\t{{durationFromCreatedAt .CreatedAt}}\n{{end}}" | ||
| TabWriter(netlist, tmpl) | ||
| } | ||
|
|
||
| }, | ||
| } | ||
| var networkDeleteCmd = &cobra.Command{ | ||
| Use: "delete", | ||
| Aliases: []string{"rm", "remove", "destroy", "down", "del"}, | ||
| Short: "Delete a network", | ||
| Long: `Delete a network`, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // Do network deletion | ||
| // log.Println("Deleting network") | ||
| s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner | ||
| log.Println("[DEBUG] args: ", args) | ||
| if len(args) == 0 { | ||
| fmt.Println("Please provide a network id") | ||
| return | ||
| } | ||
| switch args[0] { | ||
| case "all": | ||
| // Delete all networks | ||
| if !force { | ||
| if !yesNo() { | ||
| os.Exit(0) | ||
| } | ||
| } | ||
| log.Println("[DEBUG] Delete All Networks") | ||
| networks, err := networkManager.List() | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| log.Println("[DEBUG] Networks: ", networks) | ||
| var wg sync.WaitGroup | ||
| for _, network := range networks { | ||
| wg.Add(1) | ||
| go func(network cloud.Network) { | ||
| defer wg.Done() | ||
| s.Start() | ||
| s.Suffix = " Destroying VM..." | ||
| if err := networkManager.Delete(network); err != nil { | ||
| fmt.Println("\033[31m\u2718\033[0m Could not delete Network: " + network.Name) | ||
| log.Println(err) | ||
| } | ||
| s.Stop() | ||
| fmt.Println("\033[32m\u2714\033[0m Network Deleted: " + network.Name) | ||
| }(network) | ||
| } | ||
| wg.Wait() | ||
| fmt.Println("\033[32m\u2714\033[0m ALL Network(s) are destroyed") | ||
| default: | ||
| // Tear down specific server | ||
| networkName := args[0] | ||
| netlist, err := networkManager.List() | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| for _, network := range netlist { | ||
| if network.Name == networkName { | ||
| log.Println("[DEBUG] Delete network: " + networkName) | ||
| s.Start() | ||
| s.Suffix = " Destroying Network..." | ||
| err := networkManager.Delete(cloud.Network{ | ||
| ID: network.ID, | ||
| }) | ||
| if err != nil { | ||
| s.Stop() | ||
| fmt.Println("\033[31m\u2718\033[0m Cannot destroy Network: " + networkName) | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
| s.Stop() | ||
| fmt.Println("\033[32m\u2714\033[0m Network Destroyed: " + networkName) | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| } |
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.
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] The string comparison
vm.IP == \"<nil>\"suggests inconsistent handling of nil/empty IP addresses. Consider standardizing how empty IP addresses are represented throughout the codebase (e.g., always use empty string or a dedicated constant).