-
Notifications
You must be signed in to change notification settings - Fork 55
/
machines.go
129 lines (104 loc) · 2.69 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/infrahq/infra/internal/api"
)
type machineOptions struct {
Description string `mapstructure:"description"`
}
func newMachinesCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create NAME",
Short: "Create a machine identity, e.g. a service that needs to access infrastructure",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
var options machineOptions
if err := parseOptions(cmd, &options, "INFRA_MACHINE"); err != nil {
return err
}
return createMachine(name, &options)
},
}
cmd.Flags().StringP("description", "d", "", "Description of the machine identity")
return cmd
}
func newMachinesListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List machines",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := defaultAPIClient()
if err != nil {
return err
}
machines, err := client.ListMachines(api.ListMachinesRequest{})
if err != nil {
return err
}
type row struct {
Name string `header:"Name"`
Description string `header:"Description"`
}
var rows []row
for _, m := range machines {
rows = append(rows, row{
Name: m.Name,
Description: m.Description,
})
}
printTable(rows)
return nil
},
}
}
func newMachinesDeleteCmd() *cobra.Command {
return &cobra.Command{
Use: "remove MACHINE",
Short: "Remove a machine identity",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := defaultAPIClient()
if err != nil {
return err
}
machines, err := client.ListMachines(api.ListMachinesRequest{Name: args[0]})
if err != nil {
return err
}
for _, m := range machines {
err := client.DeleteMachine(m.ID)
if err != nil {
return err
}
}
return nil
},
}
}
func createMachine(name string, options *machineOptions) error {
client, err := defaultAPIClient()
if err != nil {
return err
}
_, err = client.CreateMachine(&api.CreateMachineRequest{Name: name, Description: options.Description})
if err != nil {
return err
}
return nil
}
func getMachineFromName(client *api.Client, name string) (*api.Machine, error) {
machines, err := client.ListMachines(api.ListMachinesRequest{Name: name})
if err != nil {
return nil, err
}
if len(machines) == 0 {
return nil, fmt.Errorf("no machine found with this name")
}
if len(machines) != 1 {
return nil, fmt.Errorf("invalid machines response, there should only be one machine that matches a name, but multiple were found")
}
return &machines[0], nil
}