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

feat(completion): read network zones from API #426

Merged
merged 1 commit into from
Nov 28, 2022
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
2 changes: 1 addition & 1 deletion internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCreateCommand(cli *state.State) *cobra.Command {
cmd.RegisterFlagCompletionFunc("location", cmpl.SuggestCandidatesF(cli.LocationNames))

cmd.Flags().String("network-zone", "", "Network Zone")
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidates("eu-central"))
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidatesF(cli.NetworkZoneNames))

cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/network/add_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newAddSubnetCommand(cli *state.State) *cobra.Command {
cmd.MarkFlagRequired("type")

cmd.Flags().String("network-zone", "", "Name of network zone (required)")
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidates("eu-central", "us-east"))
cmd.RegisterFlagCompletionFunc("network-zone", cmpl.SuggestCandidatesF(cli.NetworkZoneNames))
cmd.MarkFlagRequired("network-zone")

cmd.Flags().IPNet("ip-range", net.IPNet{}, "Range to allocate IPs from")
Expand Down
23 changes: 23 additions & 0 deletions internal/hcapi/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ func (c *LocationClient) LocationNames() []string {
}
return names
}

// NetworkZoneNames obtains a list of available network zones. It returns nil if
// network zone names could not be fetched.
func (c *LocationClient) NetworkZoneNames() []string {
locs, err := c.All(context.Background())
if err != nil || len(locs) == 0 {
return nil
}
// Use map to get unique elements
namesMap := map[hcloud.NetworkZone]bool{}
for _, loc := range locs {
name := loc.NetworkZone
namesMap[name] = true
}

// Unique names from map to slice
names := make([]string, len(namesMap))
for name := range namesMap {
names = append(names, string(name))
}

return names
}
8 changes: 8 additions & 0 deletions internal/state/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ func (c *State) LocationNames() []string {
return c.locationClient.LocationNames()
}

func (c *State) NetworkZoneNames() []string {
if c.locationClient == nil {
client := c.Client()
c.locationClient = &hcapi.LocationClient{LocationClient: &client.Location}
}
return c.locationClient.NetworkZoneNames()
}

func (c *State) DataCenterNames() []string {
if c.dataCenterClient == nil {
client := c.Client()
Expand Down