Skip to content

Commit

Permalink
Merge branch 'main' into feat-list-tags-of-machines
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby authored May 15, 2022
2 parents b511295 + 0d31ea0 commit a28eebf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added more configuration parameters for OpenID Connect (scopes, free-form paramters, domain and user allowlist)
- Add command to set tags on a node [#525](https://github.com/juanfont/headscale/issues/525)
- Add command to view tags of nodes [#356](https://github.com/juanfont/headscale/issues/356)
- Add --all (-a) flag to enable routes command [#360](https://github.com/juanfont/headscale/issues/360)

## 0.15.0 (2022-03-20)

Expand Down
46 changes: 35 additions & 11 deletions cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func init() {
enableRouteCmd.Flags().
StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to enable")
enableRouteCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
enableRouteCmd.Flags().BoolP("all", "a", false, "All routes from host")

err = enableRouteCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
Expand Down Expand Up @@ -125,21 +127,43 @@ omit the route you do not want to enable.
return
}

routes, err := cmd.Flags().GetStringSlice("route")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error getting routes from flag: %s", err),
output,
)

return
}

ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()

var routes []string

isAll, _ := cmd.Flags().GetBool("all")
if isAll {
response, err := client.GetMachineRoute(ctx, &v1.GetMachineRouteRequest{
MachineId: machineID,
})
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Cannot get machine routes: %s\n",
status.Convert(err).Message(),
),
output,
)

return
}
routes = response.GetRoutes().GetAdvertisedRoutes()
} else {
routes, err = cmd.Flags().GetStringSlice("route")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error getting routes from flag: %s", err),
output,
)

return
}
}

request := &v1.EnableMachineRoutesRequest{
MachineId: machineID,
Routes: routes,
Expand Down
29 changes: 29 additions & 0 deletions integration_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,35 @@ func (s *IntegrationCLITestSuite) TestRouteCommand() {
string(failEnableNonAdvertisedRoute),
"route (route-machine) is not available on node",
)

// Enable all routes on host
enableAllRouteResult, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"enable",
"--output",
"json",
"--identifier",
"0",
"--all",
},
[]string{},
)
assert.Nil(s.T(), err)

var enableAllRoute v1.Routes
err = json.Unmarshal([]byte(enableAllRouteResult), &enableAllRoute)
assert.Nil(s.T(), err)

assert.Len(s.T(), enableAllRoute.AdvertisedRoutes, 2)
assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "10.0.0.0/8")
assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "192.168.1.0/24")

assert.Len(s.T(), enableAllRoute.EnabledRoutes, 2)
assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "10.0.0.0/8")
assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "192.168.1.0/24")
}

func (s *IntegrationCLITestSuite) TestApiKeyCommand() {
Expand Down

0 comments on commit a28eebf

Please sign in to comment.