Skip to content

Commit

Permalink
Added eventtype builder and create command
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun committed Feb 14, 2022
1 parent bdcaec9 commit 2ab4478
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/eventing/v1beta1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1"
"knative.dev/eventing/pkg/client/clientset/versioned/scheme"
beta1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta1"
"knative.dev/pkg/apis"
)

// KnEventingV1Beta1Client to Eventing Sources. All methods are relative to the
Expand All @@ -35,6 +36,8 @@ type KnEventingV1Beta1Client interface {
ListEventtypes(ctx context.Context) (*eventingv1beta1.EventTypeList, error)
// GetEventtype is used to describe an eventtype
GetEventtype(ctx context.Context, name string) (*eventingv1beta1.EventType, error)
// CreateEventtype is used to c reate an eventtype
CreateEventtype(ctx context.Context, eventtype *eventingv1beta1.EventType) error
}

// KnEventingV1Beta1Client is a client for eventing v1beta1 resources
Expand All @@ -43,6 +46,14 @@ type knEventingV1Beta1Client struct {
namespace string
}

func (c *knEventingV1Beta1Client) CreateEventtype(ctx context.Context, eventtype *eventingv1beta1.EventType) error {
_, err := c.client.EventTypes(c.namespace).Create(ctx, eventtype, apis_v1.CreateOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}

// NewKnEventingV1Beta1Client is to invoke Eventing Types Client API to create object
func NewKnEventingV1Beta1Client(client *beta1.EventingV1beta1Client, namespace string) KnEventingV1Beta1Client {
return &knEventingV1Beta1Client{
Expand Down Expand Up @@ -93,3 +104,57 @@ func (c *knEventingV1Beta1Client) GetEventtype(ctx context.Context, name string)
}
return eventType, nil
}

// EventtypeBuilder is for building the eventtype
type EventtypeBuilder struct {
eventtype *eventingv1beta1.EventType
}

// NewEventtypeBuilder for building eventtype object
func NewEventtypeBuilder(name string) *EventtypeBuilder {
return &EventtypeBuilder{eventtype: &eventingv1beta1.EventType{
ObjectMeta: apis_v1.ObjectMeta{
Name: name,
},
}}
}

// WithGvk add the GVK coordinates for read tests
func (e *EventtypeBuilder) WithGvk() *EventtypeBuilder {
_ = updateEventingBeta1GVK(e.eventtype)
return e
}

// Namespace for eventtype builder
func (e *EventtypeBuilder) Namespace(ns string) *EventtypeBuilder {
e.eventtype.Namespace = ns
return e
}

// Type for eventtype builder
func (e *EventtypeBuilder) Type(ceType string) *EventtypeBuilder {
e.eventtype.Spec.Type = ceType
return e
}

// Source for eventtype builder
func (e *EventtypeBuilder) Source(source *apis.URL) *EventtypeBuilder {
e.eventtype.Spec.Source = source
return e
}

// Broker for eventtype builder
func (e *EventtypeBuilder) Broker(broker string) *EventtypeBuilder {
e.eventtype.Spec.Broker = broker
return e
}

// Build to return an instance of eventtype object
func (e *EventtypeBuilder) Build() *eventingv1beta1.EventType {
return e.eventtype
}

// update with the eventingv1beta1 group + version
func updateEventingv1beta1GVK(obj runtime.Object) error {
return util.UpdateGroupVersionKindWithScheme(obj, eventingv1beta1.SchemeGroupVersion, scheme.Scheme)
}
81 changes: 81 additions & 0 deletions pkg/kn/commands/eventtype/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2022 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventtype

import (
"errors"

"github.com/spf13/cobra"
clienteventingv1beta1 "knative.dev/client/pkg/eventing/v1beta1"
"knative.dev/client/pkg/kn/commands"
knflags "knative.dev/client/pkg/kn/commands/flags"
"knative.dev/pkg/apis"
)

var createExample = `
# Create eventtype 'myeventtype' of type example.type in the current namespace
kn eventtype create myeventtype --type example.type
# Create eventtype 'myeventtype' of type example.type in the 'myproject' namespace
kn eventtype create myeventtype --namespace myproject -t example.type
`

// NewEventtypeCreateCommand represents command to describe the details of an eventtype instance
func NewEventtypeCreateCommand(p *commands.KnParams) *cobra.Command {

var eventtypeFlags knflags.EventtypeFlags
cmd := &cobra.Command{
Use: "create",
Short: "Create eventtype",
Example: createExample,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'broker describe' requires the broker name given as single argument")
}
name := args[0]

namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
}

eventingV1Beta1Client, err := p.NewEventingV1beta1Client(namespace)
if err != nil {
return err
}
var source *apis.URL
if eventtypeFlags.Source != "" {
source, err = apis.ParseURL(eventtypeFlags.Source)
if err != nil {
return err
}
}
eventtype := clienteventingv1beta1.NewEventtypeBuilder(name).
Namespace(namespace).
Type(eventtypeFlags.Type).
Source(source).
Broker(eventtypeFlags.Broker).
Build()

return eventingV1Beta1Client.CreateEventtype(cmd.Context(), eventtype)
},
}
commands.AddNamespaceFlags(cmd.Flags(), false)

eventtypeFlags.Add(cmd)
return cmd
}
1 change: 1 addition & 0 deletions pkg/kn/commands/eventtype/eventtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func NewEventTypeCommand(p *commands.KnParams) *cobra.Command {
}
eventCmd.AddCommand(NewEventtypeListCommand(p))
eventCmd.AddCommand(NewEventtypeDescribeCommand(p))
eventCmd.AddCommand(NewEventtypeCreateCommand(p))
return eventCmd
}
30 changes: 30 additions & 0 deletions pkg/kn/commands/flags/eventtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © 2022 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

import "github.com/spf13/cobra"

type EventtypeFlags struct {
Type string
Source string
Broker string
}

func (e *EventtypeFlags) Add(cmd *cobra.Command) {
cmd.Flags().StringVarP(&e.Type, "type", "t", "", "Cloud Event type")
cmd.Flags().StringVar(&e.Source, "source", "", "Cloud Event source")
cmd.Flags().StringVarP(&e.Broker, "broker", "b", "", "Cloud Event broker")
cmd.MarkFlagRequired("type")
}

0 comments on commit 2ab4478

Please sign in to comment.