forked from manifoldco/torus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
84 lines (71 loc) · 1.9 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package api
import (
"context"
"errors"
"net/url"
"github.com/manifoldco/torus-cli/apitypes"
"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 *Client
}
// ServiceResult is the payload returned for a service object
type ServiceResult struct {
ID *identity.ID `json:"id"`
Version uint8 `json:"version"`
Body *primitive.Service `json:"body"`
}
// List retrieves relevant services by name and/or orgID and/or projectID
func (s *ServicesClient) List(ctx context.Context, orgIDs, projectIDs *[]*identity.ID, names *[]string) ([]ServiceResult, error) {
v := &url.Values{}
if orgIDs != nil {
for _, id := range *orgIDs {
v.Add("org_id", id.String())
}
}
if projectIDs != nil {
for _, id := range *projectIDs {
v.Add("project_id", id.String())
}
}
if names != nil {
for _, n := range *names {
v.Add("name", n)
}
}
req, _, err := s.client.NewRequest("GET", "/services", v, nil, true)
if err != nil {
return nil, err
}
services := []ServiceResult{}
_, err = s.client.Do(ctx, req, &services, nil, nil)
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 := apitypes.Service{
ID: &ID,
Version: 1,
Body: &serviceBody,
}
req, _, err := s.client.NewRequest("POST", "/services", nil, service, true)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil, nil, nil)
return err
}