Skip to content

Commit

Permalink
feat: wIP creation of schemas and event-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
bvdeenen committed Jul 23, 2021
1 parent 3dd41c9 commit afdce5d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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"
Expand All @@ -23,4 +24,5 @@ func init() {
CreateCmd.AddCommand(kafka_exporter.CreateCmd())
CreateCmd.AddCommand(kafka_user.CreateCmd())
CreateCmd.AddCommand(schema.CreateCmd())
}
CreateCmd.AddCommand(event_contract.CreateCmd())
}
13 changes: 13 additions & 0 deletions pkg/entity/event_contract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ func ListCmd() *cobra.Command {
},
}
}
func CreateCmd() *cobra.Command {
createCmd := &cobra.Command{
Use: "event-contract [name] [version]",
Short: "create a event-contract",
Run: func(cmd *cobra.Command, args []string) {
create(cmd, args)
},
}
flags := createCmd.Flags()
flags.String(definitionFlag, "", "filename of the definition")
return createCmd

}
26 changes: 24 additions & 2 deletions pkg/entity/event_contract/event_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ 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 +62,23 @@ func GetEventContract(name *string) *entities.EventContract {
return eventContract.EventContract
}

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

definitionFilename := util.GetStringAndErr(flags, definitionFlag)
definition, err := ioutil.ReadFile(definitionFilename)
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 All @@ -75,4 +97,4 @@ func refsCompletion(cmd *cobra.Command, args []string, complete string) ([]strin
}

return names, cobra.ShellCompDirectiveNoFileComp
}
}
10 changes: 6 additions & 4 deletions pkg/entity/schema/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func GetCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
get(&args[0], cmd)
},
Args: cobra.ExactArgs(1), // the stream name
Args: cobra.ExactArgs(1), // the schema name
ValidArgsFunction: namesCompletion,
}
flags := getSchema.Flags()
Expand All @@ -28,16 +28,18 @@ func ListCmd() *cobra.Command {
},
}
}

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

}
}
11 changes: 5 additions & 6 deletions pkg/entity/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,20 @@ func GetSchema(name *string, clusterRef *entities.KafkaClusterRef) *schemas.GetS
return response
}

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

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

ref := &entities.SchemaRef{
Name: args[0],
Version: args[1],
}
ref := ref(args)
ref.SchemaType = entities.SchemaType_AVRO
req := &schemas.CreateSchemaRequest{
BillingId: common.BillingId,
Schema: &entities.Schema{
Ref: ref,
Definition: string(definition),

},
}
response, err := client.CreateSchema(apiContext, req)
Expand All @@ -125,4 +124,4 @@ func namesCompletion(cmd *cobra.Command, args []string, complete string) ([]stri
}

return names, cobra.ShellCompDirectiveNoFileComp
}
}

0 comments on commit afdce5d

Please sign in to comment.