Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit 9aac7f2

Browse files
berndtjkars7e
authored andcommitted
create/delete batch operations for all resource types (#495)
* add batch/import operations for all resource types - makes demos much easier :) * refactoring commands a bit to enable unittesting - see create base image - this is a WIP
1 parent b480b7f commit 9aac7f2

13 files changed

+275
-91
lines changed

examples/seed.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,44 @@ secrets:
106106
tags:
107107
- key: role
108108
value: test
109+
---
110+
kind: API
111+
name: post-hello
112+
enabled: true
113+
function: hello-py
114+
methods:
115+
- POST
116+
protocols:
117+
- https
118+
uris:
119+
- /hello
120+
tags:
121+
- key: role
122+
value : test
123+
---
124+
kind: DriverType
125+
name: ticker
126+
image: kars7e/timer:latest
127+
tags:
128+
- key: role
129+
value : test
130+
---
131+
kind: Driver
132+
name: ticker
133+
type: ticker
134+
config:
135+
- key: seconds
136+
value: 2
137+
tags:
138+
- key: role
139+
value : test
140+
---
141+
kind: Subscription
142+
event-type: ticker.tick
143+
function: hello-py
144+
name: ticker-sub
145+
source-type: ticker
146+
tags:
147+
- key: role
148+
value : test
149+
---

pkg/dispatchcli/cmd/create.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func resolveFileReference(ref string) (string, error) {
5252
return ref, nil
5353
}
5454

55-
func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, actionMap map[string]modelAction) error {
55+
func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, actionMap map[string]modelAction, actionName string) error {
5656
fullPath := path.Join(workDir, file)
5757
b, err := ioutil.ReadFile(fullPath)
5858
if err != nil {
@@ -100,7 +100,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
100100
return err
101101
}
102102
o.APIs = append(o.APIs, m)
103-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
103+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
104104
case utils.BaseImageKind:
105105
m := &v1.BaseImage{}
106106
err = yaml.Unmarshal(doc, m)
@@ -112,7 +112,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
112112
return err
113113
}
114114
o.BaseImages = append(o.BaseImages, m)
115-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
115+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
116116
case utils.ImageKind:
117117
m := &v1.Image{}
118118
err = yaml.Unmarshal(doc, m)
@@ -131,7 +131,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
131131
return err
132132
}
133133
o.Images = append(o.Images, m)
134-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
134+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
135135
case utils.FunctionKind:
136136
m := &v1.Function{}
137137
err = yaml.Unmarshal(doc, m)
@@ -158,7 +158,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
158158
return err
159159
}
160160
o.Functions = append(o.Functions, m)
161-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
161+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
162162
case utils.DriverTypeKind:
163163
m := &v1.EventDriverType{}
164164
err = yaml.Unmarshal(doc, m)
@@ -170,7 +170,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
170170
return err
171171
}
172172
o.DriverTypes = append(o.DriverTypes, m)
173-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
173+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
174174
case utils.DriverKind:
175175
m := &v1.EventDriver{}
176176
err = yaml.Unmarshal(doc, m)
@@ -182,7 +182,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
182182
return err
183183
}
184184
o.Drivers = append(o.Drivers, m)
185-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
185+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
186186
case utils.SubscriptionKind:
187187
m := &v1.Subscription{}
188188
err = yaml.Unmarshal(doc, m)
@@ -194,7 +194,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
194194
return err
195195
}
196196
o.Subscriptions = append(o.Subscriptions, m)
197-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
197+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
198198
case utils.SecretKind:
199199
m := &v1.Secret{}
200200
err = yaml.Unmarshal(doc, m)
@@ -206,7 +206,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
206206
return err
207207
}
208208
o.Secrets = append(o.Secrets, m)
209-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
209+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
210210
case utils.PolicyKind:
211211
m := &v1.Policy{}
212212
err = yaml.Unmarshal(doc, m)
@@ -218,7 +218,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
218218
return err
219219
}
220220
o.Policies = append(o.Policies, m)
221-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
221+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
222222
case utils.ServiceInstanceKind:
223223
m := &v1.ServiceInstance{}
224224
err := yaml.Unmarshal(doc, m)
@@ -230,7 +230,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
230230
return err
231231
}
232232
o.ServiceInstances = append(o.ServiceInstances, m)
233-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
233+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
234234
case utils.ServiceAccountKind:
235235
m := &v1.ServiceAccount{}
236236
err = yaml.Unmarshal(doc, m)
@@ -242,7 +242,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
242242
return err
243243
}
244244
o.ServiceAccounts = append(o.ServiceAccounts, m)
245-
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
245+
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
246246
default:
247247
continue
248248
}
@@ -270,17 +270,22 @@ func NewCmdCreate(out io.Writer, errOut io.Writer) *cobra.Command {
270270
runHelp(cmd, args)
271271
return
272272
}
273-
274273
createMap := map[string]modelAction{
275274
utils.ImageKind: CallCreateImage,
276275
utils.BaseImageKind: CallCreateBaseImage,
277276
utils.FunctionKind: CallCreateFunction,
278277
utils.SecretKind: CallCreateSecret,
279278
utils.ServiceInstanceKind: CallCreateServiceInstance,
280279
utils.PolicyKind: CallCreatePolicy,
280+
utils.ApplicationKind: CallCreateApplication,
281+
utils.ServiceAccountKind: CallCreateServiceAccount,
282+
utils.DriverTypeKind: CallCreateEventDriverType,
283+
utils.DriverKind: CallCreateEventDriver,
284+
utils.SubscriptionKind: CallCreateSubscription,
285+
utils.APIKind: CallCreateAPI,
281286
}
282287

283-
err := importFile(out, errOut, cmd, args, createMap)
288+
err := importFile(out, errOut, cmd, args, createMap, "Created")
284289
CheckErr(err)
285290
},
286291
}

pkg/dispatchcli/cmd/create_api.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/spf13/cobra"
1515
"golang.org/x/net/context"
1616

17-
apiclient "github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
17+
"github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
1818
"github.com/vmware/dispatch/pkg/api/v1"
1919
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
2020
)
@@ -64,6 +64,24 @@ func NewCmdCreateAPI(out io.Writer, errOut io.Writer) *cobra.Command {
6464
return cmd
6565
}
6666

67+
// CallCreateAPI makes the API call to create an API endpoint
68+
func CallCreateAPI(i interface{}) error {
69+
client := apiManagerClient()
70+
apiModel := i.(*v1.API)
71+
72+
params := &endpoint.AddAPIParams{
73+
Body: apiModel,
74+
Context: context.Background(),
75+
}
76+
77+
created, err := client.Endpoint.AddAPI(params, GetAuthInfoWriter())
78+
if err != nil {
79+
return formatAPIError(err, params)
80+
}
81+
*apiModel = *created.Payload
82+
return nil
83+
}
84+
6785
func createAPI(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
6886

6987
apiName := args[0]
@@ -93,21 +111,15 @@ func createAPI(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
93111
})
94112
}
95113

96-
params := &apiclient.AddAPIParams{
97-
Body: api,
98-
Context: context.Background(),
99-
}
100-
client := apiManagerClient()
101-
102-
created, err := client.Endpoint.AddAPI(params, GetAuthInfoWriter())
114+
err := CallCreateAPI(api)
103115
if err != nil {
104-
return formatAPIError(err, params)
116+
return err
105117
}
106118
if dispatchConfig.JSON {
107119
encoder := json.NewEncoder(out)
108120
encoder.SetIndent("", " ")
109-
return encoder.Encode(*created.Payload)
121+
return encoder.Encode(api)
110122
}
111-
fmt.Fprintf(out, "Created api: %s\n", *created.Payload.Name)
123+
fmt.Fprintf(out, "Created api: %s\n", *api.Name)
112124
return nil
113125
}

pkg/dispatchcli/cmd/create_eventdriver.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/vmware/dispatch/pkg/api/v1"
1919
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
20-
client "github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
20+
"github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
2121
)
2222

2323
var (
@@ -57,6 +57,24 @@ func NewCmdCreateEventDriver(out io.Writer, errOut io.Writer) *cobra.Command {
5757
return cmd
5858
}
5959

60+
// CallCreateEventDriver makes the API call to create an event driver
61+
func CallCreateEventDriver(i interface{}) error {
62+
client := eventManagerClient()
63+
driverModel := i.(*v1.EventDriver)
64+
65+
params := &drivers.AddDriverParams{
66+
Body: driverModel,
67+
Context: context.Background(),
68+
}
69+
70+
created, err := client.Drivers.AddDriver(params, GetAuthInfoWriter())
71+
if err != nil {
72+
return formatAPIError(err, params)
73+
}
74+
*driverModel = *created.Payload
75+
return nil
76+
}
77+
6078
func createEventDriver(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
6179

6280
driverType := args[0]
@@ -93,20 +111,15 @@ func createEventDriver(out, errOut io.Writer, cmd *cobra.Command, args []string)
93111
})
94112
}
95113

96-
params := &client.AddDriverParams{
97-
Body: eventDriver,
98-
Context: context.Background(),
99-
}
100-
101-
created, err := eventManagerClient().Drivers.AddDriver(params, GetAuthInfoWriter())
114+
err := CallCreateEventDriver(eventDriver)
102115
if err != nil {
103-
return formatAPIError(err, params)
116+
return err
104117
}
105118
if dispatchConfig.JSON {
106119
encoder := json.NewEncoder(out)
107120
encoder.SetIndent("", " ")
108-
return encoder.Encode(*created.Payload)
121+
return encoder.Encode(eventDriver)
109122
}
110-
fmt.Fprintf(out, "Created event driver: %s\n", *created.Payload.Name)
123+
fmt.Fprintf(out, "Created event driver: %s\n", *eventDriver.Name)
111124
return nil
112125
}

pkg/dispatchcli/cmd/create_eventdrivertype.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/vmware/dispatch/pkg/api/v1"
1818
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
19-
client "github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
19+
"github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
2020
)
2121

2222
var (
@@ -43,6 +43,24 @@ func NewCmdCreateEventDriverType(out io.Writer, errOut io.Writer) *cobra.Command
4343
return cmd
4444
}
4545

46+
// CallCreateEventDriverType makes the API call to create an event driver type
47+
func CallCreateEventDriverType(i interface{}) error {
48+
client := eventManagerClient()
49+
driverTypeModel := i.(*v1.EventDriverType)
50+
51+
params := &drivers.AddDriverTypeParams{
52+
Body: driverTypeModel,
53+
Context: context.Background(),
54+
}
55+
56+
created, err := client.Drivers.AddDriverType(params, GetAuthInfoWriter())
57+
if err != nil {
58+
return formatAPIError(err, params)
59+
}
60+
*driverTypeModel = *created.Payload
61+
return nil
62+
}
63+
4664
func createEventDriverType(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
4765

4866
typeName := args[0]
@@ -60,21 +78,15 @@ func createEventDriverType(out, errOut io.Writer, cmd *cobra.Command, args []str
6078
})
6179
}
6280

63-
params := &client.AddDriverTypeParams{
64-
Body: eventDriverType,
65-
Context: context.Background(),
66-
}
67-
client := eventManagerClient()
68-
69-
created, err := client.Drivers.AddDriverType(params, GetAuthInfoWriter())
81+
err := CallCreateEventDriverType(eventDriverType)
7082
if err != nil {
71-
return formatAPIError(err, params)
83+
return err
7284
}
7385
if dispatchConfig.JSON {
7486
encoder := json.NewEncoder(out)
7587
encoder.SetIndent("", " ")
76-
return encoder.Encode(*created.Payload)
88+
return encoder.Encode(eventDriverType)
7789
}
78-
fmt.Fprintf(out, "Created event driver type: %s\n", *created.Payload.Name)
90+
fmt.Fprintf(out, "Created event driver type: %s\n", *eventDriverType.Name)
7991
return nil
8092
}

0 commit comments

Comments
 (0)