client/tailscale: document ServeConfig accessors a bit more

Updates tailscale/corp#7515

Change-Id: Iecae581e4b34ce70b2df531bc95c6c390a398c38
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/6277/head
Brad Fitzpatrick 2 years ago committed by Brad Fitzpatrick
parent 9bf3ef4167
commit 69e4b8a359

@ -862,7 +862,7 @@ func (lc *LocalClient) NetworkLockSign(ctx context.Context, nodeKey key.NodePubl
// SetServeConfig sets or replaces the serving settings. // SetServeConfig sets or replaces the serving settings.
// If config is nil, settings are cleared and serving is disabled. // If config is nil, settings are cleared and serving is disabled.
func (lc *LocalClient) SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error { func (lc *LocalClient) SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error {
b, err := json.Marshal(&config) b, err := json.Marshal(config)
if err != nil { if err != nil {
return fmt.Errorf("encoding config: %w", err) return fmt.Errorf("encoding config: %w", err)
} }
@ -874,13 +874,18 @@ func (lc *LocalClient) SetServeConfig(ctx context.Context, config *ipn.ServeConf
} }
// GetServeConfig return the current serve config. // GetServeConfig return the current serve config.
//
// If the serve config is empty, it returns (nil, nil).
func (lc *LocalClient) GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) { func (lc *LocalClient) GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) {
body, err := lc.send(ctx, "GET", "/localapi/v0/serve-config", 200, nil) body, err := lc.send(ctx, "GET", "/localapi/v0/serve-config", 200, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("getting serve config: %w", err) return nil, fmt.Errorf("getting serve config: %w", err)
} }
sc := new(ipn.ServeConfig) return getServeConfigFromJSON(body)
if err := json.Unmarshal(body, sc); err != nil { }
func getServeConfigFromJSON(body []byte) (sc *ipn.ServeConfig, err error) {
if err := json.Unmarshal(body, &sc); err != nil {
return nil, err return nil, err
} }
return sc, nil return sc, nil

@ -0,0 +1,28 @@
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.19
package tailscale
import "testing"
func TestGetServeConfigFromJSON(t *testing.T) {
sc, err := getServeConfigFromJSON([]byte("null"))
if sc != nil {
t.Errorf("want nil for null")
}
if err != nil {
t.Errorf("reading null: %v", err)
}
sc, err = getServeConfigFromJSON([]byte(`{"TCP":{}}`))
if err != nil {
t.Errorf("reading object: %v", err)
} else if sc == nil {
t.Errorf("want non-nil for object")
} else if sc.TCP == nil {
t.Errorf("want non-nil TCP for object")
}
}
Loading…
Cancel
Save