-
Notifications
You must be signed in to change notification settings - Fork 32
/
envs.go
63 lines (53 loc) · 1.49 KB
/
envs.go
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
package registry
import (
"context"
"errors"
"net/url"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/primitive"
)
// EnvironmentsClient makes proxied requests to the registry's envs endpoints
type EnvironmentsClient struct {
client RoundTripper
}
// Create generates a new env object for an org/project ID
func (e *EnvironmentsClient) Create(ctx context.Context, orgID, projectID *identity.ID, name string) error {
if orgID == nil || projectID == nil {
return errors.New("invalid org or project")
}
envBody := primitive.Environment{
Name: name,
OrgID: orgID,
ProjectID: projectID,
}
ID, err := identity.NewMutable(&envBody)
if err != nil {
return err
}
env := envelope.Environment{
ID: &ID,
Version: 1,
Body: &envBody,
}
return e.client.RoundTrip(ctx, "POST", "/envs", nil, env, nil)
}
// List retrieves relevant envs by name and/or orgID and/or projectID
func (e *EnvironmentsClient) List(ctx context.Context, orgIDs, projectIDs, teamIDs []identity.ID, names []string) ([]envelope.Environment, error) {
v := &url.Values{}
for _, id := range orgIDs {
v.Add("org_id", id.String())
}
for _, id := range projectIDs {
v.Add("project_id", id.String())
}
for _, id := range teamIDs {
v.Add("team_id", id.String())
}
for _, n := range names {
v.Add("name", n)
}
var envs []envelope.Environment
err := e.client.RoundTrip(ctx, "GET", "/envs", v, nil, &envs)
return envs, err
}