diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f8399b9..ad78da08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index dc060fba..105175a1 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -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()) @@ -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, diff --git a/integration_cli_test.go b/integration_cli_test.go index e79a4ea4..9ce31318 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -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() {