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

create/delete batch operations for all resource types #495

Merged
merged 1 commit into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/seed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,44 @@ secrets:
tags:
- key: role
value: test
---
kind: API
name: post-hello
enabled: true
function: hello-py
methods:
- POST
protocols:
- https
uris:
- /hello
tags:
- key: role
value : test
---
kind: DriverType
name: ticker
image: kars7e/timer:latest
tags:
- key: role
value : test
---
kind: Driver
name: ticker
type: ticker
config:
- key: seconds
value: 2
tags:
- key: role
value : test
---
kind: Subscription
event-type: ticker.tick
function: hello-py
name: ticker-sub
source-type: ticker
tags:
- key: role
value : test
---
33 changes: 19 additions & 14 deletions pkg/dispatchcli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func resolveFileReference(ref string) (string, error) {
return ref, nil
}

func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, actionMap map[string]modelAction) error {
func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, actionMap map[string]modelAction, actionName string) error {
fullPath := path.Join(workDir, file)
b, err := ioutil.ReadFile(fullPath)
if err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.APIs = append(o.APIs, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.BaseImageKind:
m := &v1.BaseImage{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -112,7 +112,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.BaseImages = append(o.BaseImages, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.ImageKind:
m := &v1.Image{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -131,7 +131,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Images = append(o.Images, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.FunctionKind:
m := &v1.Function{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -158,7 +158,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Functions = append(o.Functions, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.DriverTypeKind:
m := &v1.EventDriverType{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -170,7 +170,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.DriverTypes = append(o.DriverTypes, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.DriverKind:
m := &v1.EventDriver{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -182,7 +182,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Drivers = append(o.Drivers, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.SubscriptionKind:
m := &v1.Subscription{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -194,7 +194,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Subscriptions = append(o.Subscriptions, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.SecretKind:
m := &v1.Secret{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -206,7 +206,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Secrets = append(o.Secrets, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.PolicyKind:
m := &v1.Policy{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -218,7 +218,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.Policies = append(o.Policies, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.ServiceInstanceKind:
m := &v1.ServiceInstance{}
err := yaml.Unmarshal(doc, m)
Expand All @@ -230,7 +230,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.ServiceInstances = append(o.ServiceInstances, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
case utils.ServiceAccountKind:
m := &v1.ServiceAccount{}
err = yaml.Unmarshal(doc, m)
Expand All @@ -242,7 +242,7 @@ func importFile(out io.Writer, errOut io.Writer, cmd *cobra.Command, args []stri
return err
}
o.ServiceAccounts = append(o.ServiceAccounts, m)
fmt.Fprintf(out, "Created %s: %s\n", docKind, *m.Name)
fmt.Fprintf(out, "%s %s: %s\n", actionName, docKind, *m.Name)
default:
continue
}
Expand Down Expand Up @@ -270,17 +270,22 @@ func NewCmdCreate(out io.Writer, errOut io.Writer) *cobra.Command {
runHelp(cmd, args)
return
}

createMap := map[string]modelAction{
utils.ImageKind: CallCreateImage,
utils.BaseImageKind: CallCreateBaseImage,
utils.FunctionKind: CallCreateFunction,
utils.SecretKind: CallCreateSecret,
utils.ServiceInstanceKind: CallCreateServiceInstance,
utils.PolicyKind: CallCreatePolicy,
utils.ApplicationKind: CallCreateApplication,
utils.ServiceAccountKind: CallCreateServiceAccount,
utils.DriverTypeKind: CallCreateEventDriverType,
utils.DriverKind: CallCreateEventDriver,
utils.SubscriptionKind: CallCreateSubscription,
utils.APIKind: CallCreateAPI,
}

err := importFile(out, errOut, cmd, args, createMap)
err := importFile(out, errOut, cmd, args, createMap, "Created")
CheckErr(err)
},
}
Expand Down
34 changes: 23 additions & 11 deletions pkg/dispatchcli/cmd/create_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"

apiclient "github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
"github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
Expand Down Expand Up @@ -64,6 +64,24 @@ func NewCmdCreateAPI(out io.Writer, errOut io.Writer) *cobra.Command {
return cmd
}

// CallCreateAPI makes the API call to create an API endpoint
func CallCreateAPI(i interface{}) error {
client := apiManagerClient()
apiModel := i.(*v1.API)

params := &endpoint.AddAPIParams{
Body: apiModel,
Context: context.Background(),
}

created, err := client.Endpoint.AddAPI(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
*apiModel = *created.Payload
return nil
}

func createAPI(out, errOut io.Writer, cmd *cobra.Command, args []string) error {

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

params := &apiclient.AddAPIParams{
Body: api,
Context: context.Background(),
}
client := apiManagerClient()

created, err := client.Endpoint.AddAPI(params, GetAuthInfoWriter())
err := CallCreateAPI(api)
if err != nil {
return formatAPIError(err, params)
return err
}
if dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(*created.Payload)
return encoder.Encode(api)
}
fmt.Fprintf(out, "Created api: %s\n", *created.Payload.Name)
fmt.Fprintf(out, "Created api: %s\n", *api.Name)
return nil
}
33 changes: 23 additions & 10 deletions pkg/dispatchcli/cmd/create_eventdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
client "github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
"github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
)

var (
Expand Down Expand Up @@ -57,6 +57,24 @@ func NewCmdCreateEventDriver(out io.Writer, errOut io.Writer) *cobra.Command {
return cmd
}

// CallCreateEventDriver makes the API call to create an event driver
func CallCreateEventDriver(i interface{}) error {
client := eventManagerClient()
driverModel := i.(*v1.EventDriver)

params := &drivers.AddDriverParams{
Body: driverModel,
Context: context.Background(),
}

created, err := client.Drivers.AddDriver(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
*driverModel = *created.Payload
return nil
}

func createEventDriver(out, errOut io.Writer, cmd *cobra.Command, args []string) error {

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

params := &client.AddDriverParams{
Body: eventDriver,
Context: context.Background(),
}

created, err := eventManagerClient().Drivers.AddDriver(params, GetAuthInfoWriter())
err := CallCreateEventDriver(eventDriver)
if err != nil {
return formatAPIError(err, params)
return err
}
if dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(*created.Payload)
return encoder.Encode(eventDriver)
}
fmt.Fprintf(out, "Created event driver: %s\n", *created.Payload.Name)
fmt.Fprintf(out, "Created event driver: %s\n", *eventDriver.Name)
return nil
}
34 changes: 23 additions & 11 deletions pkg/dispatchcli/cmd/create_eventdrivertype.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
client "github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
"github.com/vmware/dispatch/pkg/event-manager/gen/client/drivers"
)

var (
Expand All @@ -43,6 +43,24 @@ func NewCmdCreateEventDriverType(out io.Writer, errOut io.Writer) *cobra.Command
return cmd
}

// CallCreateEventDriverType makes the API call to create an event driver type
func CallCreateEventDriverType(i interface{}) error {
client := eventManagerClient()
driverTypeModel := i.(*v1.EventDriverType)

params := &drivers.AddDriverTypeParams{
Body: driverTypeModel,
Context: context.Background(),
}

created, err := client.Drivers.AddDriverType(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
*driverTypeModel = *created.Payload
return nil
}

func createEventDriverType(out, errOut io.Writer, cmd *cobra.Command, args []string) error {

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

params := &client.AddDriverTypeParams{
Body: eventDriverType,
Context: context.Background(),
}
client := eventManagerClient()

created, err := client.Drivers.AddDriverType(params, GetAuthInfoWriter())
err := CallCreateEventDriverType(eventDriverType)
if err != nil {
return formatAPIError(err, params)
return err
}
if dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(*created.Payload)
return encoder.Encode(eventDriverType)
}
fmt.Fprintf(out, "Created event driver type: %s\n", *created.Payload.Name)
fmt.Fprintf(out, "Created event driver type: %s\n", *eventDriverType.Name)
return nil
}
Loading