|
| 1 | +package tsclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | +) |
| 7 | + |
| 8 | +// TailnetSettingsResource provides an API to view/control settings for a tailnet. |
| 9 | +type TailnetSettingsResource struct { |
| 10 | + *Client |
| 11 | +} |
| 12 | + |
| 13 | +type ( |
| 14 | + // TailnetSettings represents the current settings of a tailnet. |
| 15 | + // See https://tailscale.com/api#model/tailnetsettings. |
| 16 | + TailnetSettings struct { |
| 17 | + DevicesApprovalOn bool `json:"devicesApprovalOn"` |
| 18 | + DevicesAutoUpdatesOn bool `json:"devicesAutoUpdatesOn"` |
| 19 | + DevicesKeyDurationDays int `json:"devicesKeyDurationDays"` // days before device key expiry |
| 20 | + |
| 21 | + UsersApprovalOn bool `json:"usersApprovalOn"` |
| 22 | + UsersRoleAllowedToJoinExternalTailnets RoleAllowedToJoinExternalTailnets `json:"usersRoleAllowedToJoinExternalTailnets"` |
| 23 | + |
| 24 | + NetworkFlowLoggingOn bool `json:"networkFlowLoggingOn"` |
| 25 | + RegionalRoutingOn bool `json:"regionalRoutingOn"` |
| 26 | + PostureIdentityCollectionOn bool `json:"postureIdentityCollectionOn"` |
| 27 | + } |
| 28 | + |
| 29 | + // UpdateTailnetSettingsRequest is a request to update the settings of a tailnet. |
| 30 | + // Nil values indicate that the existing setting should be left unchanged. |
| 31 | + UpdateTailnetSettingsRequest struct { |
| 32 | + DevicesApprovalOn *bool `json:"devicesApprovalOn,omitempty"` |
| 33 | + DevicesAutoUpdatesOn *bool `json:"devicesAutoUpdatesOn,omitempty"` |
| 34 | + DevicesKeyDurationDays *int `json:"devicesKeyDurationDays,omitempty"` // days before device key expiry |
| 35 | + |
| 36 | + UsersApprovalOn *bool `json:"usersApprovalOn,omitempty"` |
| 37 | + UsersRoleAllowedToJoinExternalTailnets *RoleAllowedToJoinExternalTailnets `json:"usersRoleAllowedToJoinExternalTailnets,omitempty"` |
| 38 | + |
| 39 | + NetworkFlowLoggingOn *bool `json:"networkFlowLoggingOn,omitempty"` |
| 40 | + RegionalRoutingOn *bool `json:"regionalRoutingOn,omitempty"` |
| 41 | + PostureIdentityCollectionOn *bool `json:"postureIdentityCollectionOn,omitempty"` |
| 42 | + } |
| 43 | + |
| 44 | + // RoleAllowedToJoinExternalTailnets constrains which users are allowed to join external tailnets |
| 45 | + // based on their role. |
| 46 | + RoleAllowedToJoinExternalTailnets string |
| 47 | +) |
| 48 | + |
| 49 | +const ( |
| 50 | + RoleAllowedToJoinExternalTailnetsNone RoleAllowedToJoinExternalTailnets = "none" |
| 51 | + RoleAllowedToJoinExternalTailnetsAdmin RoleAllowedToJoinExternalTailnets = "admin" |
| 52 | + RoleAllowedToJoinExternalTailnetsMember RoleAllowedToJoinExternalTailnets = "member" |
| 53 | +) |
| 54 | + |
| 55 | +// Get retrieves the current TailnetSettings. |
| 56 | +// See https://tailscale.com/api#tag/tailnetsettings/GET/tailnet/{tailnet}/settings. |
| 57 | +func (tsr *TailnetSettingsResource) Get(ctx context.Context) (*TailnetSettings, error) { |
| 58 | + req, err := tsr.buildRequest(ctx, http.MethodGet, tsr.buildTailnetURL("settings")) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + var resp TailnetSettings |
| 64 | + return &resp, tsr.do(req, &resp) |
| 65 | +} |
| 66 | + |
| 67 | +// Update updates the tailnet settings. |
| 68 | +// See https://tailscale.com/api#tag/tailnetsettings/PATCH/tailnet/{tailnet}/settings. |
| 69 | +func (tsr *TailnetSettingsResource) Update(ctx context.Context, request UpdateTailnetSettingsRequest) error { |
| 70 | + req, err := tsr.buildRequest(ctx, http.MethodPatch, tsr.buildTailnetURL("settings"), requestBody(request)) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + return tsr.do(req, nil) |
| 76 | +} |
0 commit comments