Skip to content

Commit

Permalink
feat: merged create-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
bvdeenen committed Aug 2, 2021
2 parents 7aaae9a + 8c3d267 commit 10d2ccc
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
2 changes: 0 additions & 2 deletions cmd/strm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ func setupVerbs() {
RootCmd.AddCommand(cmd.VersionCmd)
}



func init() {
setConfigPath()
common.InitLogging(configPath)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"github.com/spf13/cobra"
"streammachine.io/strm/pkg/entity/batch_exporter"
"streammachine.io/strm/pkg/entity/event_contract"
"streammachine.io/strm/pkg/entity/kafka_exporter"
"streammachine.io/strm/pkg/entity/kafka_user"
"streammachine.io/strm/pkg/entity/schema"
"streammachine.io/strm/pkg/entity/sink"
"streammachine.io/strm/pkg/entity/stream"
)
Expand All @@ -21,4 +23,6 @@ func init() {
CreateCmd.AddCommand(batch_exporter.CreateCmd())
CreateCmd.AddCommand(kafka_exporter.CreateCmd())
CreateCmd.AddCommand(kafka_user.CreateCmd())
CreateCmd.AddCommand(schema.CreateCmd())
CreateCmd.AddCommand(event_contract.CreateCmd())
}
6 changes: 3 additions & 3 deletions pkg/cmd/egress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

// SimCmd represents the create command
var EgressCmd = &cobra.Command{
Use: "egress [stream-name]",
Short: "Read from egress",
Run: func(cmd *cobra.Command, args []string) {
Use: "egress [stream-name]",
Short: "Read from egress",
Run: func(cmd *cobra.Command, args []string) {
egress.Run(cmd, &args[0])
},
Args: cobra.ExactArgs(1), // the stream name
Expand Down
15 changes: 15 additions & 0 deletions pkg/entity/event_contract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ func ListCmd() *cobra.Command {
},
}
}
func CreateCmd() *cobra.Command {
createCmd := &cobra.Command{
Use: "event-contract (file)",
Short: "create a event-contract",
Long: `create an event contract from a json definition file`,
Run: func(cmd *cobra.Command, args []string) {
create(cmd, &args[0])
},
Args: cobra.ExactArgs(1), // the file name
}
flags := createCmd.Flags()
flags.String(definitionFlag, "", "filename of the definition")
return createCmd

}
21 changes: 20 additions & 1 deletion pkg/entity/event_contract/event_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"github.com/streammachineio/api-definitions-go/api/entities/v1"
"github.com/streammachineio/api-definitions-go/api/event_contracts/v1"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"io/ioutil"
"streammachine.io/strm/pkg/common"
"streammachine.io/strm/pkg/util"
"strings"
)

// strings used in the cli
const ()
const (
definitionFlag = "definition"
)

var client event_contracts.EventContractsServiceClient
var apiContext context.Context
Expand Down Expand Up @@ -57,6 +61,21 @@ func GetEventContract(name *string) *entities.EventContract {
return eventContract.EventContract
}

func create(cmd *cobra.Command, filename *string) {

definition, err := ioutil.ReadFile(*filename)
eventContract := entities.EventContract{}
err = protojson.Unmarshal(definition, &eventContract)
common.CliExit(err)
req := &event_contracts.CreateEventContractRequest{
BillingId: common.BillingId,
EventContract: &eventContract,
}
response, err := client.CreateEventContract(apiContext, req)
common.CliExit(err)
util.Print(response)
}

func refsCompletion(cmd *cobra.Command, args []string, complete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 || common.BillingIdIsMissing() {
return common.MissingBillingIdCompletionError(cmd.CommandPath())
Expand Down
21 changes: 20 additions & 1 deletion pkg/entity/schema/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package schema

import "github.com/spf13/cobra"

const ()

func GetCmd() *cobra.Command {
getSchema := &cobra.Command{
Use: "schema [name]",
Expand All @@ -25,4 +27,21 @@ func ListCmd() *cobra.Command {
list()
},
}
}
}

func CreateCmd() *cobra.Command {
createCmd := &cobra.Command{
Use: "schema (handle/name/version)",
Short: "create a schema",
Run: func(cmd *cobra.Command, args []string) {
create(cmd, &args[0])
},
Args: cobra.ExactArgs(1),
}
flags := createCmd.Flags()
flags.String(definitionFlag, "", "filename of the definition")
_ = createCmd.MarkFlagRequired(definitionFlag)
flags.Bool(publicFlag, false, "should the schema become public")
return createCmd

}
25 changes: 25 additions & 0 deletions pkg/entity/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/streammachineio/api-definitions-go/api/entities/v1"
"github.com/streammachineio/api-definitions-go/api/schemas/v1"
"google.golang.org/grpc"
"io/ioutil"
"streammachine.io/strm/pkg/common"
"streammachine.io/strm/pkg/util"
"strings"
Expand All @@ -16,6 +17,8 @@ import (
// strings used in the cli
const (
kafkaClusterFlag = "kafka-cluster"
definitionFlag = "definition"
publicFlag = "public"
)

var client schemas.SchemasServiceClient
Expand Down Expand Up @@ -83,6 +86,28 @@ func GetSchema(name *string, clusterRef *entities.KafkaClusterRef) *schemas.GetS
return response
}

func create(cmd *cobra.Command, args *string) {
flags := cmd.Flags()

definitionFilename := util.GetStringAndErr(flags, definitionFlag)
definition, err := ioutil.ReadFile(definitionFilename)

isPublic := util.GetBoolAndErr(flags, publicFlag)

ref := Ref(args)
req := &schemas.CreateSchemaRequest{
BillingId: common.BillingId,
Schema: &entities.Schema{
Ref: ref,
Definition: string(definition),
IsPublic: isPublic,
},
}
response, err := client.CreateSchema(apiContext, req)
common.CliExit(err)
util.Print(response)
}

func NamesCompletion(cmd *cobra.Command, args []string, complete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 || common.BillingIdIsMissing() {
return common.MissingBillingIdCompletionError(cmd.CommandPath())
Expand Down
4 changes: 3 additions & 1 deletion pkg/entity/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
var Client sinks.SinksServiceClient
var apiContext context.Context

func ref(n *string) *entities.SinkRef { return &entities.SinkRef{BillingId: common.BillingId, Name: *n} }
func ref(n *string) *entities.SinkRef {
return &entities.SinkRef{BillingId: common.BillingId, Name: *n}
}

func SetupClient(clientConnection *grpc.ClientConn, ctx context.Context) {
apiContext = ctx
Expand Down

0 comments on commit 10d2ccc

Please sign in to comment.