Skip to content

Commit

Permalink
feat: event-streams: create, update, list, delete (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Jan 18, 2024
1 parent b6b9d18 commit 2da4f08
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"gopls": {
"formatting.local": "github.com/ory",
}
}
76 changes: 76 additions & 0 deletions cmd/cloudx/client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,82 @@ func (h *CommandHelper) ListProjects() ([]cloud.ProjectMetadata, error) {
return projects, nil
}

func (h *CommandHelper) CreateEventStream(projectID string, body cloud.CreateEventStreamBody) (*cloud.EventStream, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}

c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}

stream, res, err := c.EventsAPI.CreateEventStream(h.Ctx, projectID).CreateEventStreamBody(body).Execute()
if err != nil {
return nil, handleError("unable to create event stream", res, err)
}

return stream, nil
}

func (h *CommandHelper) UpdateEventStream(projectID, streamID string, body cloud.SetEventStreamBody) (*cloud.EventStream, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}

c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}

stream, res, err := c.EventsAPI.SetEventStream(h.Ctx, projectID, streamID).SetEventStreamBody(body).Execute()
if err != nil {
return nil, handleError("unable to update event stream", res, err)
}

return stream, nil
}

func (h *CommandHelper) DeleteEventStream(projectID, streamID string) error {
ac, err := h.EnsureContext()
if err != nil {
return err
}

c, err := newCloudClient(ac.SessionToken)
if err != nil {
return err
}

res, err := c.EventsAPI.DeleteEventStream(h.Ctx, projectID, streamID).Execute()
if err != nil {
return handleError("unable to delete event stream", res, err)
}

return nil
}

func (h *CommandHelper) ListEventStreams(projectID string) (*cloud.ListEventStreams, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}

c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}

streams, res, err := c.EventsAPI.ListEventStreams(h.Ctx, projectID).Execute()
if err != nil {
return nil, handleError("unable to list event streams", res, err)
}

return streams, nil
}

func (h *CommandHelper) ListOrganizations(projectID string) (*cloud.ListOrganizationsResponse, error) {
ac, err := h.EnsureContext()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/cloudx/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cloudx
import (
"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/eventstreams"
"github.com/ory/cli/cmd/cloudx/oauth2"
"github.com/ory/cli/cmd/cloudx/organizations"
"github.com/ory/cli/cmd/cloudx/relationtuples"
Expand All @@ -26,6 +27,7 @@ func NewCreateCmd() *cobra.Command {
relationtuples.NewCreateCmd(),
oauth2.NewCreateJWK(),
organizations.NewCreateOrganizationCmd(),
eventstreams.NewCreateEventStreamCmd(),
)

client.RegisterConfigFlag(cmd.PersistentFlags())
Expand Down
2 changes: 2 additions & 0 deletions cmd/cloudx/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cloudx
import (
"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/eventstreams"
"github.com/ory/cli/cmd/cloudx/oauth2"
"github.com/ory/cli/cmd/cloudx/organizations"
"github.com/ory/cli/cmd/cloudx/relationtuples"
Expand All @@ -28,6 +29,7 @@ func NewDeleteCmd() *cobra.Command {
oauth2.NewDeleteAccessTokens(),
relationtuples.NewDeleteCmd(),
organizations.NewDeleteOrganizationCmd(),
eventstreams.NewDeleteEventStream(),
)

client.RegisterConfigFlag(cmd.PersistentFlags())
Expand Down
54 changes: 54 additions & 0 deletions cmd/cloudx/eventstreams/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package eventstreams

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/client"
cloud "github.com/ory/client-go"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
)

func NewCreateEventStreamCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "event-stream [--project=PROJECT_ID] --type=sns --aws-iam-role-arn=arn:aws:iam::123456789012:role/MyRole --aws-sns-topic-arn=arn:aws:sns:us-east-1:123456789012:MyTopic",
Short: "Create a new event stream",
RunE: func(cmd *cobra.Command, args []string) error {
h, err := client.NewCommandHelper(cmd)
if err != nil {
return err
}

projectID, err := client.ProjectOrDefault(cmd, h)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

stream, err := h.CreateEventStream(projectID, cloud.CreateEventStreamBody{
Type: flagx.MustGetString(cmd, "type"),
RoleArn: flagx.MustGetString(cmd, "aws-iam-role-arn"),
TopicArn: flagx.MustGetString(cmd, "aws-sns-topic-arn"),
})
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

_, _ = fmt.Fprintln(h.VerboseErrWriter, "Event stream created successfully!")
cmdx.PrintRow(cmd, output(*stream))
return nil
},
}

client.RegisterProjectFlag(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
cmd.Flags().String("type", "", `The type of the event stream destination. Only "sns" is supported at the moment.`)
cmd.Flags().String("aws-iam-role-arn", "", "The ARN of the AWS IAM role to assume when publishing messages to the SNS topic.")
cmd.Flags().String("aws-sns-topic-arn", "", "The ARN of the AWS SNS topic.")

return cmd
}
44 changes: 44 additions & 0 deletions cmd/cloudx/eventstreams/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package eventstreams

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/client"
"github.com/ory/x/cmdx"
)

func NewDeleteEventStream() *cobra.Command {
cmd := &cobra.Command{
Use: "event-stream id [--project=PROJECT_ID]",
Args: cobra.ExactArgs(1),
Short: "Delete the event stream with the given ID",
RunE: func(cmd *cobra.Command, args []string) error {
h, err := client.NewCommandHelper(cmd)
if err != nil {
return err
}

projectID, err := client.ProjectOrDefault(cmd, h)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
streamID := args[0]

err = h.DeleteEventStream(projectID, streamID)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

_, _ = fmt.Fprintln(h.VerboseErrWriter, "Event stream deleted successfully!")
return nil
},
}

client.RegisterProjectFlag(cmd.Flags())
return cmd
}
42 changes: 42 additions & 0 deletions cmd/cloudx/eventstreams/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package eventstreams

import (
"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/client"
"github.com/ory/x/cmdx"
)

func NewListEventStreamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "event-streams",
Args: cobra.NoArgs,
Short: "List your event streams",
RunE: func(cmd *cobra.Command, args []string) error {
h, err := client.NewCommandHelper(cmd)
if err != nil {
return err
}

id, err := client.ProjectOrDefault(cmd, h)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

streams, err := h.ListEventStreams(id)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

cmdx.PrintTable(cmd, outputList(*streams))
return nil
},
}

client.RegisterProjectFlag(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
return cmd
}
56 changes: 56 additions & 0 deletions cmd/cloudx/eventstreams/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package eventstreams

import (
"encoding/json"

client "github.com/ory/client-go"
)

type (
outputList client.ListEventStreams
output client.EventStream
)

func (output) Header() []string {
return []string{"ID", "TYPE", "IAM_ROLE_ARN", "SNS_TOPIC_ARN"}
}

func (o output) Columns() []string {
return []string{
*o.Id,
*o.Type,
*o.RoleArn,
*o.TopicArn,
}
}

func (o output) Interface() interface{} {
return o
}

func (outputList) Header() []string {
return new(output).Header()
}

func (o outputList) Table() [][]string {
rows := make([][]string, len(o.EventStreams))
for i, stream := range o.EventStreams {
rows[i] = (output)(stream).Columns()
}
return rows
}

func (o outputList) Interface() interface{} {
return o
}

func (o outputList) Len() int {
return len(o.EventStreams)
}

func (o outputList) MarshalJSON() ([]byte, error) {
return json.Marshal(o.EventStreams)
}
57 changes: 57 additions & 0 deletions cmd/cloudx/eventstreams/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package eventstreams

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/client"
cloud "github.com/ory/client-go"

"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
)

func NewUpdateEventStreamCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "event-stream id [--project=PROJECT_ID] [--type=sns] [--aws-iam-role-arn=arn:aws:iam::123456789012:role/MyRole] [--aws-sns-topic-arn=arn:aws:sns:us-east-1:123456789012:MyTopic]",
Args: cobra.ExactArgs(1),
Short: "Update the event stream with the given ID",
RunE: func(cmd *cobra.Command, args []string) error {
h, err := client.NewCommandHelper(cmd)
if err != nil {
return err
}

projectID, err := client.ProjectOrDefault(cmd, h)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
streamID := args[0]

stream, err := h.UpdateEventStream(projectID, streamID, cloud.SetEventStreamBody{
Type: flagx.MustGetString(cmd, "type"),
RoleArn: flagx.MustGetString(cmd, "aws-iam-role-arn"),
TopicArn: flagx.MustGetString(cmd, "aws-sns-topic-arn"),
})
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}

_, _ = fmt.Fprintln(h.VerboseErrWriter, "Event stream updated successfully!")
cmdx.PrintRow(cmd, output(*stream))
return nil
},
}

cmd.Flags().String("type", "", `The type of the event stream destination. Only "sns" is supported at the moment.`)
cmd.Flags().String("aws-iam-role-arn", "", "The ARN of the AWS IAM role to assume when publishing messages to the SNS topic.")
cmd.Flags().String("aws-sns-topic-arn", "", "The ARN of the AWS SNS topic.")
client.RegisterProjectFlag(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())

return cmd
}
2 changes: 2 additions & 0 deletions cmd/cloudx/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cloudx
import (
"github.com/spf13/cobra"

"github.com/ory/cli/cmd/cloudx/eventstreams"
"github.com/ory/cli/cmd/cloudx/identity"
"github.com/ory/cli/cmd/cloudx/oauth2"
"github.com/ory/cli/cmd/cloudx/organizations"
Expand All @@ -29,6 +30,7 @@ func NewListCmd() *cobra.Command {
identity.NewListIdentityCmd(),
oauth2.NewListOAuth2Clients(),
relationtuples.NewListCmd(),
eventstreams.NewListEventStreamsCmd(),
)

client.RegisterConfigFlag(cmd.PersistentFlags())
Expand Down
Loading

0 comments on commit 2da4f08

Please sign in to comment.