Files
coder/codersdk/workspacesharing.go
T
dylanhuff-at-coder 76ae64391a refactor(codersdk): use ReadBodyAsJSON in typed endpoints (#27857)
This PR migrates 224 typed JSON response sites across 46 files to
`codersdk.ReadBodyAsJSON`, so invalid 2xx bodies return structured
errors while preserving URL credential redaction.

It intentionally excludes agent-direct HTTP, Azure IMDS, `UseNumber`,
and chat paths; stacked on coder/coder#27804, with chat and lint
follow-ups in coder/coder#27858 and coder/coder#27859. Refs
coder/coder#27044.

Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
2026-08-05 13:22:23 -07:00

75 lines
3.0 KiB
Go

package codersdk
import (
"context"
"fmt"
"net/http"
)
// ShareableWorkspaceOwners controls whose workspaces can be shared
// within an organization.
type ShareableWorkspaceOwners string
const (
ShareableWorkspaceOwnersNone ShareableWorkspaceOwners = "none"
ShareableWorkspaceOwnersEveryone ShareableWorkspaceOwners = "everyone"
ShareableWorkspaceOwnersServiceAccounts ShareableWorkspaceOwners = "service_accounts"
)
// WorkspaceSharingSettings represents workspace sharing settings affecting an
// organization.
type WorkspaceSharingSettings struct {
// SharingGloballyDisabled is true if sharing has been disabled for this
// organization because of a deployment-wide setting.
SharingGloballyDisabled bool `json:"sharing_globally_disabled"`
// SharingDisabled is deprecated and left for backward compatibility
// purposes.
// Deprecated: use `ShareableWorkspaceOwners` instead
SharingDisabled bool `json:"sharing_disabled"`
// ShareableWorkspaceOwners controls whose workspaces can be shared
// within the organization.
ShareableWorkspaceOwners ShareableWorkspaceOwners `json:"shareable_workspace_owners" enums:"none,everyone,service_accounts"`
}
// UpdateWorkspaceSharingSettingsRequest represents workspace sharing settings
// that can be updated for an organization.
type UpdateWorkspaceSharingSettingsRequest struct {
// SharingDisabled is deprecated and left for backward compatibility
// purposes.
// Deprecated: use `ShareableWorkspaceOwners` instead
SharingDisabled bool `json:"sharing_disabled,omitempty"`
// ShareableWorkspaceOwners controls whose workspaces can be shared
// within the organization.
ShareableWorkspaceOwners ShareableWorkspaceOwners `json:"shareable_workspace_owners,omitempty" enums:"none,everyone,service_accounts"`
}
// WorkspaceSharingSettings retrieves the workspace sharing settings for an organization.
func (c *Client) WorkspaceSharingSettings(ctx context.Context, orgID string) (WorkspaceSharingSettings, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/settings/workspace-sharing", orgID), nil)
if err != nil {
return WorkspaceSharingSettings{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return WorkspaceSharingSettings{}, ReadBodyAsError(res)
}
var resp WorkspaceSharingSettings
return resp, ReadBodyAsJSON(res, &resp)
}
// PatchWorkspaceSharingSettings modifies the workspace sharing settings for an organization.
func (c *Client) PatchWorkspaceSharingSettings(ctx context.Context, orgID string, req UpdateWorkspaceSharingSettingsRequest) (WorkspaceSharingSettings, error) {
res, err := c.Request(ctx, http.MethodPatch, fmt.Sprintf("/api/v2/organizations/%s/settings/workspace-sharing", orgID), req)
if err != nil {
return WorkspaceSharingSettings{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return WorkspaceSharingSettings{}, ReadBodyAsError(res)
}
var resp WorkspaceSharingSettings
return resp, ReadBodyAsJSON(res, &resp)
}