-
Notifications
You must be signed in to change notification settings - Fork 32
/
machines.go
83 lines (71 loc) · 2.56 KB
/
machines.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
package registry
import (
"context"
"net/url"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/identity"
)
// MachinesClient represents the `/machines` registry endpoint, used for
// creating, listing, authorizing, and destroying machines and their tokens.
type MachinesClient struct {
client RoundTripper
}
// MachineCreationSegment represents the request sent to create the registry to
// create a machine and it's first token
type MachineCreationSegment struct {
apitypes.MachineSegment
Tokens []MachineTokenCreationSegment `json:"tokens"`
}
// MachineTokenCreationSegment represents the request send to the registry to
// create a Machine Token
type MachineTokenCreationSegment struct {
Token *envelope.MachineToken `json:"token"`
Keypairs []*ClaimedKeyPair `json:"keypairs"`
}
// Create requests the registry to create a MachineSegment.
//
// The MachineSegment includes the Machine, it's Memberships, and authorization
// tokens.
func (m *MachinesClient) Create(ctx context.Context, machine *envelope.Machine,
memberships []envelope.Membership, token *MachineTokenCreationSegment) (*apitypes.MachineSegment, error) {
segment := MachineCreationSegment{
MachineSegment: apitypes.MachineSegment{
Machine: machine,
Memberships: memberships,
},
Tokens: []MachineTokenCreationSegment{*token},
}
resp := &apitypes.MachineSegment{}
err := m.client.RoundTrip(ctx, "POST", "/machines", nil, &segment, &resp)
return resp, err
}
// Get requests a single machine from the registry
func (m *MachinesClient) Get(ctx context.Context, machineID *identity.ID) (*apitypes.MachineSegment, error) {
resp := &apitypes.MachineSegment{}
err := m.client.RoundTrip(ctx, "GET", "/machines/"+(*machineID).String(), nil, nil, &resp)
return resp, err
}
// List machines in the given org and state
func (m *MachinesClient) List(ctx context.Context, orgID *identity.ID, state *string, name *string, teamID *identity.ID) ([]apitypes.MachineSegment, error) {
v := &url.Values{}
if orgID != nil {
v.Add("org_id", (*orgID).String())
}
if state != nil {
v.Add("state", *state)
}
if teamID != nil {
v.Add("team_id", (*teamID).String())
}
if name != nil {
v.Add("name", *name)
}
var results []apitypes.MachineSegment
err := m.client.RoundTrip(ctx, "GET", "/machines", v, nil, &results)
return results, err
}
// Destroy machine by ID
func (m *MachinesClient) Destroy(ctx context.Context, machineID *identity.ID) error {
return m.client.RoundTrip(ctx, "DELETE", "/machines/"+machineID.String(), nil, nil, nil)
}