-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathworkspaceagentportshare.go
More file actions
148 lines (133 loc) · 5.26 KB
/
workspaceagentportshare.go
File metadata and controls
148 lines (133 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package codersdk
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/google/uuid"
"golang.org/x/xerrors"
)
const (
WorkspaceAgentPortShareLevelOwner WorkspaceAgentPortShareLevel = "owner"
WorkspaceAgentPortShareLevelAuthenticated WorkspaceAgentPortShareLevel = "authenticated"
WorkspaceAgentPortShareLevelOrganization WorkspaceAgentPortShareLevel = "organization"
WorkspaceAgentPortShareLevelPublic WorkspaceAgentPortShareLevel = "public"
WorkspaceAgentPortShareProtocolHTTP WorkspaceAgentPortShareProtocol = "http"
WorkspaceAgentPortShareProtocolHTTPS WorkspaceAgentPortShareProtocol = "https"
)
type (
WorkspaceAgentPortShareLevel string
WorkspaceAgentPortShareProtocol string
UpsertWorkspaceAgentPortShareRequest struct {
AgentName string `json:"agent_name"`
Port int32 `json:"port"`
ShareLevel WorkspaceAgentPortShareLevel `json:"share_level" enums:"owner,authenticated,organization,public"`
Protocol WorkspaceAgentPortShareProtocol `json:"protocol" enums:"http,https"`
}
WorkspaceAgentPortShares struct {
Shares []WorkspaceAgentPortShare `json:"shares"`
}
WorkspaceAgentPortShare struct {
WorkspaceID uuid.UUID `json:"workspace_id" format:"uuid"`
AgentName string `json:"agent_name"`
Port int32 `json:"port"`
ShareLevel WorkspaceAgentPortShareLevel `json:"share_level" enums:"owner,authenticated,organization,public"`
Protocol WorkspaceAgentPortShareProtocol `json:"protocol" enums:"http,https"`
}
DeleteWorkspaceAgentPortShareRequest struct {
AgentName string `json:"agent_name"`
Port int32 `json:"port"`
}
)
func (l WorkspaceAgentPortShareLevel) ValidMaxLevel() bool {
return l == WorkspaceAgentPortShareLevelOwner ||
l == WorkspaceAgentPortShareLevelAuthenticated ||
l == WorkspaceAgentPortShareLevelOrganization ||
l == WorkspaceAgentPortShareLevelPublic
}
func (l WorkspaceAgentPortShareLevel) ValidPortShareLevel() bool {
return l == WorkspaceAgentPortShareLevelAuthenticated ||
l == WorkspaceAgentPortShareLevelOrganization ||
l == WorkspaceAgentPortShareLevelPublic
}
// IsCompatibleWithMaxLevel determines whether the sharing level is valid under
// the specified maxLevel. The values are fully ordered, from "highest" to
// "lowest" as
// 1. Public
// 2. Authenticated
// 3. Organization
// 4. Owner
// Returns an error if either level is invalid.
func (l WorkspaceAgentPortShareLevel) IsCompatibleWithMaxLevel(maxLevel WorkspaceAgentPortShareLevel) error {
// Owner is always allowed.
if l == WorkspaceAgentPortShareLevelOwner {
return nil
}
// If public is allowed, anything is allowed.
if maxLevel == WorkspaceAgentPortShareLevelPublic {
return nil
}
// Public is not allowed.
if l == WorkspaceAgentPortShareLevelPublic {
return xerrors.Errorf("%q sharing level is not allowed under max level %q", l, maxLevel)
}
// If authenticated is allowed, public has already been filtered out so
// anything is allowed.
if maxLevel == WorkspaceAgentPortShareLevelAuthenticated {
return nil
}
// Authenticated is not allowed.
if l == WorkspaceAgentPortShareLevelAuthenticated {
return xerrors.Errorf("%q sharing level is not allowed under max level %q", l, maxLevel)
}
// If organization is allowed, public and authenticated have already been
// filtered out so anything is allowed.
if maxLevel == WorkspaceAgentPortShareLevelOrganization {
return nil
}
// Organization is not allowed.
if l == WorkspaceAgentPortShareLevelOrganization {
return xerrors.Errorf("%q sharing level is not allowed under max level %q", l, maxLevel)
}
// An invalid value was provided.
return xerrors.New("port sharing level is invalid.")
}
func (p WorkspaceAgentPortShareProtocol) ValidPortProtocol() bool {
return p == WorkspaceAgentPortShareProtocolHTTP ||
p == WorkspaceAgentPortShareProtocolHTTPS
}
func (c *Client) GetWorkspaceAgentPortShares(ctx context.Context, workspaceID uuid.UUID) (WorkspaceAgentPortShares, error) {
var shares WorkspaceAgentPortShares
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/port-share", workspaceID), nil)
if err != nil {
return shares, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return shares, ReadBodyAsError(res)
}
return shares, json.NewDecoder(res.Body).Decode(&shares)
}
func (c *Client) UpsertWorkspaceAgentPortShare(ctx context.Context, workspaceID uuid.UUID, req UpsertWorkspaceAgentPortShareRequest) (WorkspaceAgentPortShare, error) {
var share WorkspaceAgentPortShare
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaces/%s/port-share", workspaceID), req)
if err != nil {
return share, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return share, ReadBodyAsError(res)
}
return share, json.NewDecoder(res.Body).Decode(&share)
}
func (c *Client) DeleteWorkspaceAgentPortShare(ctx context.Context, workspaceID uuid.UUID, req DeleteWorkspaceAgentPortShareRequest) error {
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/workspaces/%s/port-share", workspaceID), req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
return nil
}