-
Notifications
You must be signed in to change notification settings - Fork 32
/
machines.go
56 lines (44 loc) · 1.29 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
package api
import (
"context"
"crypto/rand"
"github.com/manifoldco/go-base64"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/registry"
)
const tokenSecretSize = 18
// MachinesClient makes requests to the Daemon on behalf of the user to
// manipulate Machine resources.
type MachinesClient struct {
*registry.MachinesClient
client *apiRoundTripper
}
func newMachinesClient(upstream *registry.MachinesClient, rt *apiRoundTripper) *MachinesClient {
return &MachinesClient{upstream, rt}
}
// Create a new machine in the given org
func (m *MachinesClient) Create(ctx context.Context, orgID, teamID *identity.ID,
name string, output ProgressFunc) (*apitypes.MachineSegment, *base64.Value, error) {
secret, err := createTokenSecret()
if err != nil {
return nil, nil, err
}
mcr := apitypes.MachinesCreateRequest{
Name: name,
OrgID: orgID,
TeamID: teamID,
Secret: secret,
}
result := &apitypes.MachineSegment{}
err = m.client.DaemonRoundTrip(ctx, "POST", "/machines", nil, &mcr, &result, output)
return result, secret, err
}
func createTokenSecret() (*base64.Value, error) {
value := make([]byte, tokenSecretSize)
_, err := rand.Read(value)
if err != nil {
return nil, err
}
return base64.New(value), nil
}