Skip to content

Commit

Permalink
feat(strm-1766): create resources in other project than active one; (#…
Browse files Browse the repository at this point in the history
…122)

* feat: create resources in other project than active one; get and delete projects

* feat: add global project flag to all crud operations

* fix: cyclic reference

* fix: solve comments

* fix: editorconfig

* fix: bug fix

Co-authored-by: Bob van den Hoogen <bob@strmprivacy.io>
  • Loading branch information
bobvandenhoogen and Bob van den Hoogen authored Oct 4, 2022
1 parent 9e12943 commit 54f95b9
Show file tree
Hide file tree
Showing 34 changed files with 375 additions and 269 deletions.
5 changes: 2 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 4

[*.md]
indent_size = 2
tab_width = 2

[*.go]
indent_size = 4
indent_style = tab

[*.sh]
indent_size = 2
tab_width = 2
2 changes: 1 addition & 1 deletion cmd/strm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func init() {
"Token file that contains an access token (default is $HOME/.config/strmprivacy/credentials-<api-auth-url>.json)")
persistentFlags.String(web_socket.WebSocketUrl, "wss://websocket.strmprivacy.io/ws", "Websocket to receive events from")
persistentFlags.String(kafkaconsumer.KafkaBootstrapHostFlag, "export-bootstrap.kafka.strmprivacy.io:9092", "Kafka bootstrap brokers, separated by comma")
persistentFlags.String(context.ProjectFlag, "", "Project to use (defaults to context-configured project)")
persistentFlags.StringP(common.ProjectNameFlag, common.ProjectNameFlagShort, "", "Project to use (defaults to context-configured project)")
persistentFlags.StringP(common.OutputFormatFlag, common.OutputFormatFlagShort, common.OutputFormatTable, fmt.Sprintf("Output format [%v]", common.OutputFormatFlagAllowedValuesText))

err := RootCmd.RegisterFlagCompletionFunc(common.OutputFormatFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strmprivacy/strm/pkg/entity/event_contract"
"strmprivacy/strm/pkg/entity/kafka_exporter"
"strmprivacy/strm/pkg/entity/kafka_user"
"strmprivacy/strm/pkg/entity/project"
"strmprivacy/strm/pkg/entity/schema"
"strmprivacy/strm/pkg/entity/stream"
)
Expand All @@ -35,6 +36,7 @@ func init() {
DeleteCmd.AddCommand(schema.DeleteCmd())
DeleteCmd.AddCommand(data_subject.DeleteCmd())
DeleteCmd.AddCommand(data_contract.DeleteCmd())
DeleteCmd.AddCommand(project.DeleteCmd())

DeleteCmd.PersistentFlags().BoolP(common.RecursiveFlagName, common.RecursiveFlagShorthand, false, common.RecursiveFlagUsage)
}
2 changes: 2 additions & 0 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strmprivacy/strm/pkg/entity/kafka_user"
"strmprivacy/strm/pkg/entity/key_stream"
"strmprivacy/strm/pkg/entity/member"
"strmprivacy/strm/pkg/entity/project"
"strmprivacy/strm/pkg/entity/schema"
"strmprivacy/strm/pkg/entity/schema_code"
"strmprivacy/strm/pkg/entity/stream"
Expand Down Expand Up @@ -44,6 +45,7 @@ func init() {
GetCmd.AddCommand(installation.GetCmd())
GetCmd.AddCommand(member.GetCmd())
GetCmd.AddCommand(data_contract.GetCmd())
GetCmd.AddCommand(project.GetCmd())

GetCmd.PersistentFlags().BoolP(common.RecursiveFlagName, common.RecursiveFlagShorthand, false, common.RecursiveFlagUsage)
}
3 changes: 3 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const OutputFormatFilepath = "path"
const OutputFormatFlag = "output"
const OutputFormatFlagShort = "o"

const ProjectNameFlag = "project"
const ProjectNameFlagShort = "p"

var OutputFormatFlagAllowedValues = []string{OutputFormatJson, OutputFormatJsonRaw, OutputFormatTable, OutputFormatPlain}
var OutputFormatFlagAllowedValuesText = strings.Join(OutputFormatFlagAllowedValues, ", ")

Expand Down
2 changes: 0 additions & 2 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strmprivacy/strm/pkg/entity/account"
)

const ProjectFlag = "project"

type configuration struct {
ConfigPath string
ConfigFilepath string
Expand Down
112 changes: 56 additions & 56 deletions pkg/context/project.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package context

import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"io/ioutil"
"os"
"path"
"strmprivacy/strm/pkg/common"
"strmprivacy/strm/pkg/entity/project"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"io/ioutil"
"os"
"path"
"strmprivacy/strm/pkg/common"
"strmprivacy/strm/pkg/entity/project"
)

const activeProjectFilename = "active_project"
Expand All @@ -19,63 +19,63 @@ const activeProjectFilename = "active_project"
// a fallback to default project.
func ResolveProject(f *pflag.FlagSet) {

activeProjectFilePath := path.Join(common.ConfigPath, activeProjectFilename)
projectFlagValue, _ := f.GetString(ProjectFlag)
activeProjectFilePath := path.Join(common.ConfigPath, activeProjectFilename)
projectFlagValue, _ := f.GetString(common.ProjectNameFlag)

if _, err := os.Stat(activeProjectFilePath); (os.IsNotExist(err) || common.GetActiveProject() == "") && projectFlagValue == "" {
initActiveProject()
fmt.Println(fmt.Sprintf("Active project was not yet set, has been set to '%v'. You can set a project "+
"with 'strm context project <project-name>'\n", common.GetActiveProject()))
}
if _, err := os.Stat(activeProjectFilePath); (os.IsNotExist(err) || common.GetActiveProject() == "") && projectFlagValue == "" {
initActiveProject()
fmt.Println(fmt.Sprintf("Active project was not yet set, has been set to '%v'. You can set a project "+
"with 'strm context project <project-name>'\n", common.GetActiveProject()))
}

if projectFlagValue != "" {
resolvedProject := project.GetProject(projectFlagValue)
if len(resolvedProject.Projects) == 0 {
message := fmt.Sprintf("Specified project '%v' does not exist, or you do not have access to it.", projectFlagValue)
common.CliExit(errors.New(message))
}
common.ProjectId = resolvedProject.Projects[0].Id
} else {
activeProject := common.GetActiveProject()
resolvedProject := project.GetProject(activeProject)
if len(resolvedProject.Projects) == 0 {
initActiveProject()
common.CliExit(errors.New(fmt.Sprintf("Active project '%v' does not exist, or you do not have access "+
"to it. The following project has been set instead: %v", activeProject, common.GetActiveProject())))
}
common.ProjectId = resolvedProject.Projects[0].Id
}
if projectFlagValue != "" {
resolvedProject := project.GetProject(projectFlagValue)
if len(resolvedProject.Projects) == 0 {
message := fmt.Sprintf("Specified project '%v' does not exist, or you do not have access to it.", projectFlagValue)
common.CliExit(errors.New(message))
}
common.ProjectId = resolvedProject.Projects[0].Id
} else {
activeProject := common.GetActiveProject()
resolvedProject := project.GetProject(activeProject)
if len(resolvedProject.Projects) == 0 {
initActiveProject()
common.CliExit(errors.New(fmt.Sprintf("Active project '%v' does not exist, or you do not have access "+
"to it. The following project has been set instead: %v", activeProject, common.GetActiveProject())))
}
common.ProjectId = resolvedProject.Projects[0].Id
}
}

func SetActiveProject(projectName string) {
if len(project.GetProject(projectName).Projects) != 0 {
saveActiveProject(projectName)
message := "Active project set to: " + projectName
log.Infoln(message)
fmt.Println(message)
} else {
message := fmt.Sprintf("No project '%v' found, or you do not have access to it.", projectName)
log.Warnln(message)
common.CliExit(errors.New(message))
}
if len(project.GetProject(projectName).Projects) != 0 {
saveActiveProject(projectName)
message := "Active project set to: " + projectName
log.Infoln(message)
fmt.Println(message)
} else {
message := fmt.Sprintf("No project '%v' found, or you do not have access to it.", projectName)
log.Warnln(message)
common.CliExit(errors.New(message))
}
}

func initActiveProject() {
projects := project.ListProjects()
if len(projects) == 0 {
common.CliExit(errors.New("you do not have access to any projects; create a project first, or ask to be granted access to one"))
}
firstProjectName := projects[0].Name
saveActiveProject(firstProjectName)
projects := project.ListProjects()
if len(projects) == 0 {
common.CliExit(errors.New("you do not have access to any projects; create a project first, or ask to be granted access to one"))
}
firstProjectName := projects[0].Name
saveActiveProject(firstProjectName)
}

func saveActiveProject(projectName string) {
activeProjectFilepath := path.Join(common.ConfigPath, activeProjectFilename)
activeProjectFilepath := path.Join(common.ConfigPath, activeProjectFilename)

err := ioutil.WriteFile(
activeProjectFilepath,
[]byte(projectName),
0644,
)
common.CliExit(err)
err := ioutil.WriteFile(
activeProjectFilepath,
[]byte(projectName),
0644,
)
common.CliExit(err)
}
32 changes: 16 additions & 16 deletions pkg/entity/batch_exporter/batch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package batch_exporter
import (
"context"
"errors"
"github.com/spf13/pflag"
"github.com/strmprivacy/api-definitions-go/v2/api/data_connectors/v1"
"strings"
"strmprivacy/strm/pkg/entity/data_connector"

"github.com/golang/protobuf/ptypes/duration"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/strmprivacy/api-definitions-go/v2/api/batch_exporters/v1"
"github.com/strmprivacy/api-definitions-go/v2/api/data_connectors/v1"
"github.com/strmprivacy/api-definitions-go/v2/api/entities/v1"
"google.golang.org/grpc"
"strings"
"strmprivacy/strm/pkg/common"
"strmprivacy/strm/pkg/entity/data_connector"
"strmprivacy/strm/pkg/entity/project"
"strmprivacy/strm/pkg/util"
)

Expand All @@ -25,19 +25,19 @@ func SetupClient(clientConnection *grpc.ClientConn, ctx context.Context) {
client = batch_exporters.NewBatchExportersServiceClient(clientConnection)
}

func list() {
func list(cmd *cobra.Command) {
req := &batch_exporters.ListBatchExportersRequest{
ProjectId: common.ProjectId,
ProjectId: project.GetProjectId(cmd),
}
response, err := client.ListBatchExporters(apiContext, req)
common.CliExit(err)

printer.Print(response)
}

func get(name *string, _ *cobra.Command) {
func get(name *string, cmd *cobra.Command) {
ref := &entities.BatchExporterRef{
ProjectId: common.ProjectId,
ProjectId: project.GetProjectId(cmd),
Name: *name,
}
req := &batch_exporters.GetBatchExporterRequest{Ref: ref}
Expand All @@ -46,9 +46,9 @@ func get(name *string, _ *cobra.Command) {
printer.Print(response)
}

func del(name *string) {
func del(name *string, cmd *cobra.Command) {
req := &batch_exporters.DeleteBatchExporterRequest{Ref: &entities.BatchExporterRef{
ProjectId: common.ProjectId,
ProjectId: project.GetProjectId(cmd),
Name: *name,
}}
response, err := client.DeleteBatchExporter(apiContext, req)
Expand All @@ -68,14 +68,14 @@ func create(streamName *string, cmd *cobra.Command) {
interval := duration.Duration{Seconds: i}

pathPrefix := util.GetStringAndErr(flags, pathPrefix)

projectId := project.GetProjectId(cmd)
exporter := &entities.BatchExporter{
Ref: &entities.BatchExporterRef{
ProjectId: common.ProjectId,
ProjectId: projectId,
Name: exporterName,
},
DataConnectorRef: &entities.DataConnectorRef{
ProjectId: common.ProjectId,
ProjectId: projectId,
Name: dataConnectorName,
},
Interval: &interval,
Expand All @@ -85,13 +85,13 @@ func create(streamName *string, cmd *cobra.Command) {
if keyStream {
exporter.StreamOrKeyStreamRef = &entities.BatchExporter_KeyStreamRef{
KeyStreamRef: &entities.KeyStreamRef{
ProjectId: common.ProjectId,
ProjectId: projectId,
Name: *streamName,
}}
} else {
exporter.StreamOrKeyStreamRef = &entities.BatchExporter_StreamRef{
StreamRef: &entities.StreamRef{
ProjectId: common.ProjectId,
ProjectId: projectId,
Name: *streamName,
}}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/entity/batch_exporter/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func DeleteCmd() *cobra.Command {
printer = configurePrinter(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
for i, _ := range args {
del(&args[i])
for _, arg := range args {
del(&arg, cmd)
}
},
Args: cobra.MinimumNArgs(1), // the stream names
Expand Down Expand Up @@ -78,7 +78,7 @@ func ListCmd() *cobra.Command {
},
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
list()
list(cmd)
},
}
}
Expand Down
Loading

0 comments on commit 54f95b9

Please sign in to comment.