-
Notifications
You must be signed in to change notification settings - Fork 32
/
services.go
63 lines (53 loc) · 1.5 KB
/
services.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"
)
// ServicesClient makes proxied requests to the registry's services endpoints
type ServicesClient struct {
client RoundTripper
}
// List retrieves relevant services by name and/or orgID and/or projectID
func (s *ServicesClient) List(ctx context.Context, orgIDs, projectIDs, teamIDs []identity.ID, names []string) ([]envelope.Service, 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 services []envelope.Service
err := s.client.RoundTrip(ctx, "GET", "/services", v, nil, &services)
return services, err
}
// Create performs a request to create a new service object
func (s *ServicesClient) Create(ctx context.Context, orgID, projectID *identity.ID, name string) error {
if orgID == nil || projectID == nil {
return errors.New("invalid org or project")
}
serviceBody := primitive.Service{
Name: name,
OrgID: orgID,
ProjectID: projectID,
}
ID, err := identity.NewMutable(&serviceBody)
if err != nil {
return err
}
service := envelope.Service{
ID: &ID,
Version: 1,
Body: &serviceBody,
}
return s.client.RoundTrip(ctx, "POST", "/services", nil, service, nil)
}