-
Notifications
You must be signed in to change notification settings - Fork 26
/
sevice_network.go
142 lines (133 loc) · 3.92 KB
/
sevice_network.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
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"github.com/google/uuid"
"github.com/nexodus-io/nexodus/internal/client"
"github.com/urfave/cli/v3"
)
func createServiceNetworkCommand() *cli.Command {
return &cli.Command{
Name: "service-network",
Usage: "Commands relating to service networks",
Commands: []*cli.Command{
{
Name: "list",
Usage: "List service networks",
Action: func(ctx context.Context, command *cli.Command) error {
return listServiceNetworks(ctx, command)
},
},
{
Name: "create",
Usage: "Create a service network",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "organization-id",
Required: false,
},
&cli.StringFlag{
Name: "description",
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
return createServiceNetwork(ctx, command, client.ModelsAddServiceNetwork{
Description: client.PtrString(command.String("description")),
OrganizationId: client.PtrString(command.String("organization-id")),
})
},
},
{
Name: "update",
Usage: "Update a service network",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "service-network-id",
Required: true,
},
&cli.StringFlag{
Name: "description",
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
id, err := getUUID(command, "service-network-id")
if err != nil {
return err
}
update := client.ModelsUpdateServiceNetwork{
Description: client.PtrString(command.String("description")),
}
return updateServiceNetwork(ctx, command, id, update)
},
},
{
Name: "delete",
Usage: "Delete a service network",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "service-network-id",
Required: true,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
serviceNetworkID, err := getUUID(command, "service-network-id")
if err != nil {
return err
}
return deleteServiceNetwork(ctx, command, serviceNetworkID)
},
},
},
}
}
func updateServiceNetwork(ctx context.Context, command *cli.Command, idStr string, update client.ModelsUpdateServiceNetwork) error {
id, err := uuid.Parse(idStr)
if err != nil {
Fatalf("failed to parse a valid UUID from %s %v", idStr, err)
}
c := createClient(ctx, command)
res := apiResponse(c.ServiceNetworkApi.
UpdateServiceNetwork(ctx, id.String()).
Update(update).
Execute())
show(command, invitationsTableFields(), res)
showSuccessfully(command, "updated")
return nil
}
func serviceNetworkTableFields() []TableField {
var fields []TableField
fields = append(fields, TableField{Header: "SERVICE NETWORK ID", Field: "Id"})
fields = append(fields, TableField{Header: "ORGANIZATION ID", Field: "OrganizationId"})
fields = append(fields, TableField{Header: "DESCRIPTION", Field: "Description"})
return fields
}
func listServiceNetworks(ctx context.Context, command *cli.Command) error {
c := createClient(ctx, command)
res := apiResponse(c.ServiceNetworkApi.
ListServiceNetworks(ctx).
Execute())
show(command, serviceNetworkTableFields(), res)
return nil
}
func createServiceNetwork(ctx context.Context, command *cli.Command, resource client.ModelsAddServiceNetwork) error {
c := createClient(ctx, command)
if resource.GetOrganizationId() == "" {
resource.OrganizationId = client.PtrString(getDefaultOrgId(ctx, c))
}
res := apiResponse(c.ServiceNetworkApi.
CreateServiceNetwork(ctx).
ServiceNetwork(resource).
Execute())
show(command, serviceNetworkTableFields(), res)
return nil
}
func deleteServiceNetwork(ctx context.Context, command *cli.Command, id string) error {
c := createClient(ctx, command)
res := apiResponse(c.ServiceNetworkApi.
DeleteServiceNetwork(ctx, id).
Execute())
show(command, serviceNetworkTableFields(), res)
showSuccessfully(command, "deleted")
return nil
}