diff --git a/cmd/attributes.go b/cmd/attributes.go index 757472b6..6304bf8b 100644 --- a/cmd/attributes.go +++ b/cmd/attributes.go @@ -3,259 +3,259 @@ Copyright © 2023 NAME HERE */ package cmd -import ( - "fmt" - "os" - "strconv" - "strings" - - "github.com/opentdf/tructl/pkg/cli" - "github.com/spf13/cobra" -) - -var ( - attrValues []string - groupBy []string - resourceDependencies []string - - attributeCommands = []string{ - attributesCreateCmd.Use, - attributeGetCmd.Use, - attributesListCmd.Use, - attributeUpdateCmd.Use, - attributesDeleteCmd.Use, - } - - attributesCmd = &cobra.Command{ - Use: "attributes", - Short: "Manage attributes [" + strings.Join(attributeCommands, ", ") + "]", - Long: ` -Attributes - commands to manage attributes within the platform. - -Attributes are used to to define the properties of a piece of data. These attributes will then be -used to define the access controls based on subject encodings and entity entitlements. -`, - } - - attributeGetCmd = &cobra.Command{ - Use: "get ", - Short: "Get an attribute", - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - h := cli.NewHandler(cmd) - defer h.Close() - - id, err := strconv.Atoi(args[0]) - if err != nil { - cli.ExitWithError("Invalid ID", err) - } - - attr, err := h.GetAttribute(id) - if err != nil { - errMsg := fmt.Sprintf("Could not find attribute (%d)", id) - cli.ExitWithNotFoundError(errMsg, err) - cli.ExitWithError(errMsg, err) - } - - fmt.Println(cli.SuccessMessage("Attribute found")) - fmt.Println( - cli.NewTabular(). - Rows([][]string{ - {"Id", strconv.Itoa(int(attr.Id))}, - {"Name", attr.Name}, - {"Rule", attr.Rule}, - {"Values", cli.CommaSeparated(attr.Values)}, - {"Namespace", attr.Namespace}, - {"Description", attr.Description}, - }...).Render(), - ) - }, - } - - // List attributes - attributesListCmd = &cobra.Command{ - Use: "list", - Short: "List attributes", - Run: func(cmd *cobra.Command, args []string) { - h := cli.NewHandler(cmd) - defer h.Close() - - attrs, err := h.ListAttributes() - if err != nil { - cli.ExitWithError("Could not get attributes", err) - } - - t := cli.NewTable() - t.Headers("Id", "Namespace", "Name", "Rule", "Values") - for _, attr := range attrs { - t.Row( - strconv.Itoa(int(attr.Id)), - attr.Namespace, - attr.Name, - attr.Rule, - cli.CommaSeparated(attr.Values), - ) - } - fmt.Println(t.Render()) - }, - } - - // Create an attribute - attributesCreateCmd = &cobra.Command{ - Use: "create", - Short: "Create an attribute", - Run: func(cmd *cobra.Command, args []string) { - h := cli.NewHandler(cmd) - defer h.Close() - - flagHelper := cli.NewFlagHelper(cmd) - name := flagHelper.GetRequiredString("name") - rule := flagHelper.GetRequiredString("rule") - values := flagHelper.GetStringSlice("values", attrValues, cli.FlagHelperStringSliceOptions{ - Min: 1, - }) - namespace := flagHelper.GetRequiredString("namespace") - description := flagHelper.GetRequiredString("description") - - if _, err := h.CreateAttribute(name, rule, values, namespace, description); err != nil { - cli.ExitWithError("Could not create attribute", err) - } - - fmt.Println(cli.SuccessMessage("Attribute created")) - fmt.Println( - cli.NewTabular().Rows([][]string{ - {"Name", name}, - {"Rule", rule}, - {"Values", cli.CommaSeparated(values)}, - {"Namespace", namespace}, - {"Description", description}, - }...).Render(), - ) - }, - } - - attributesDeleteCmd = &cobra.Command{ - Use: "delete ", - Short: "Delete an attribute", - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - h := cli.NewHandler(cmd) - defer h.Close() - - id, err := strconv.Atoi(args[0]) - if err != nil { - fmt.Println(cli.ErrorMessage("Invalid ID", err)) - os.Exit(1) - } - attr, err := h.GetAttribute(id) - if err != nil { - errMsg := fmt.Sprintf("Could not find attribute (%d)", id) - cli.ExitWithNotFoundError(errMsg, err) - cli.ExitWithError(errMsg, err) - } - - cli.ConfirmDelete("attribute", attr.Fqn) - - if err := h.DeleteAttribute(id); err != nil { - errMsg := fmt.Sprintf("Could not delete attribute (%d)", id) - cli.ExitWithNotFoundError(errMsg, err) - cli.ExitWithError(errMsg, err) - } - - fmt.Println(cli.SuccessMessage("Attribute deleted")) - fmt.Println( - cli.NewTabular(). - Rows([][]string{ - {"Name", attr.Name}, - {"Rule", attr.Rule}, - {"Values", cli.CommaSeparated(attr.Values)}, - {"Namespace", attr.Namespace}, - {"Description", attr.Description}, - }...).Render(), - ) - }, - } - - // Update one attribute - attributeUpdateCmd = &cobra.Command{ - Use: "update", - Short: "Update an attribute", - Run: func(cmd *cobra.Command, args []string) { - h := cli.NewHandler(cmd) - defer h.Close() - - flagHelper := cli.NewFlagHelper(cmd) - - id := flagHelper.GetRequiredInt32("id") - name := flagHelper.GetRequiredString("name") - rule := flagHelper.GetRequiredString("rule") - - values := flagHelper.GetStringSlice("values", attrValues, cli.FlagHelperStringSliceOptions{ - Min: 1, - }) - - groupBy := flagHelper.GetStringSlice("group-by", groupBy, cli.FlagHelperStringSliceOptions{ - Min: 0, - }) - - resourceDependencies := flagHelper.GetStringSlice("resource-dependencies", resourceDependencies, cli.FlagHelperStringSliceOptions{ - Min: 0, - }) - - resourceId := flagHelper.GetRequiredInt32("resource-id") - resourceVersion := flagHelper.GetRequiredInt32("resource-version") - resourceName := flagHelper.GetRequiredString("resource-name") - resourceNamespace := flagHelper.GetRequiredString("resource-namespace") - resourceDescription := flagHelper.GetRequiredString("resource-description") - - if _, err := h.UpdateAttribute( - id, - name, - rule, - values, - groupBy, - resourceId, - resourceVersion, - resourceName, - resourceNamespace, - resourceDescription, - resourceDependencies, - ); err != nil { - cli.ExitWithError("Could not update attribute", err) - return - } else { - fmt.Println(cli.SuccessMessage(fmt.Sprintf("Attribute id: %d updated.", id))) - } - }, - } -) - -func init() { - rootCmd.AddCommand(attributesCmd) - - attributesCmd.AddCommand(attributeGetCmd) - - attributesCmd.AddCommand(attributesListCmd) - - attributesCmd.AddCommand(attributesCreateCmd) - attributesCreateCmd.Flags().StringP("name", "n", "", "Name of the attribute") - attributesCreateCmd.Flags().StringP("rule", "r", "", "Rule of the attribute") - attributesCreateCmd.Flags().StringSliceVarP(&attrValues, "values", "v", []string{}, "Values of the attribute") - attributesCreateCmd.Flags().StringP("namespace", "s", "", "Namespace of the attribute") - attributesCreateCmd.Flags().StringP("description", "d", "", "Description of the attribute") - - attributesCmd.AddCommand(attributeUpdateCmd) - attributeUpdateCmd.Flags().Int32P("id", "i", 0, "Id of the attribute") - attributeUpdateCmd.Flags().StringP("name", "n", "", "Name of the attribute") - attributeUpdateCmd.Flags().StringP("rule", "r", "", "Rule of the attribute") - attributeUpdateCmd.Flags().StringSliceVarP(&attrValues, "values", "v", []string{}, "Values of the attribute") - attributeUpdateCmd.Flags().StringSliceVarP(&groupBy, "group-by", "g", []string{}, "GroupBy of the attribute") - attributeUpdateCmd.Flags().StringSliceVarP(&resourceDependencies, "resource-dependencies", "d", []string{}, "ResourceDependencies of the attribute definition descriptor") - attributeUpdateCmd.Flags().Int32P("resource-id", "I", 0, "ResourceId of the attribute definition descriptor") - attributeUpdateCmd.Flags().Int32P("resource-version", "V", 0, "ResourceVersion of the attribute definition descriptor") - attributeUpdateCmd.Flags().StringP("resource-name", "N", "", "ResourceName of the attribute definition descriptor") - attributeUpdateCmd.Flags().StringP("resource-namespace", "S", "", "ResourceNamespace of the attribute definition descriptor") - attributeUpdateCmd.Flags().StringP("resource-description", "D", "", "ResourceDescription of the attribute definition descriptor") - - attributesCmd.AddCommand(attributesDeleteCmd) -} +// import ( +// "fmt" +// "os" +// "strconv" +// "strings" + +// "github.com/opentdf/tructl/pkg/cli" +// "github.com/spf13/cobra" +// ) + +// var ( +// attrValues []string +// groupBy []string +// resourceDependencies []string + +// attributeCommands = []string{ +// attributesCreateCmd.Use, +// attributeGetCmd.Use, +// attributesListCmd.Use, +// attributeUpdateCmd.Use, +// attributesDeleteCmd.Use, +// } + +// attributesCmd = &cobra.Command{ +// Use: "attributes", +// Short: "Manage attributes [" + strings.Join(attributeCommands, ", ") + "]", +// Long: ` +// Attributes - commands to manage attributes within the platform. + +// Attributes are used to to define the properties of a piece of data. These attributes will then be +// used to define the access controls based on subject encodings and entity entitlements. +// `, +// } + +// attributeGetCmd = &cobra.Command{ +// Use: "get ", +// Short: "Get an attribute", +// Args: cobra.ExactArgs(1), +// Run: func(cmd *cobra.Command, args []string) { +// h := cli.NewHandler(cmd) +// defer h.Close() + +// id, err := strconv.Atoi(args[0]) +// if err != nil { +// cli.ExitWithError("Invalid ID", err) +// } + +// attr, err := h.GetAttribute(id) +// if err != nil { +// errMsg := fmt.Sprintf("Could not find attribute (%d)", id) +// cli.ExitWithNotFoundError(errMsg, err) +// cli.ExitWithError(errMsg, err) +// } + +// fmt.Println(cli.SuccessMessage("Attribute found")) +// fmt.Println( +// cli.NewTabular(). +// Rows([][]string{ +// {"Id", strconv.Itoa(int(attr.Id))}, +// {"Name", attr.Name}, +// {"Rule", attr.Rule}, +// {"Values", cli.CommaSeparated(attr.Values)}, +// {"Namespace", attr.Namespace}, +// {"Description", attr.Description}, +// }...).Render(), +// ) +// }, +// } + +// // List attributes +// attributesListCmd = &cobra.Command{ +// Use: "list", +// Short: "List attributes", +// Run: func(cmd *cobra.Command, args []string) { +// h := cli.NewHandler(cmd) +// defer h.Close() + +// attrs, err := h.ListAttributes() +// if err != nil { +// cli.ExitWithError("Could not get attributes", err) +// } + +// t := cli.NewTable() +// t.Headers("Id", "Namespace", "Name", "Rule", "Values") +// for _, attr := range attrs { +// t.Row( +// strconv.Itoa(int(attr.Id)), +// attr.Namespace, +// attr.Name, +// attr.Rule, +// cli.CommaSeparated(attr.Values), +// ) +// } +// fmt.Println(t.Render()) +// }, +// } + +// // Create an attribute +// attributesCreateCmd = &cobra.Command{ +// Use: "create", +// Short: "Create an attribute", +// Run: func(cmd *cobra.Command, args []string) { +// h := cli.NewHandler(cmd) +// defer h.Close() + +// flagHelper := cli.NewFlagHelper(cmd) +// name := flagHelper.GetRequiredString("name") +// rule := flagHelper.GetRequiredString("rule") +// values := flagHelper.GetStringSlice("values", attrValues, cli.FlagHelperStringSliceOptions{ +// Min: 1, +// }) +// namespace := flagHelper.GetRequiredString("namespace") +// description := flagHelper.GetRequiredString("description") + +// if _, err := h.CreateAttribute(name, rule, values, namespace, description); err != nil { +// cli.ExitWithError("Could not create attribute", err) +// } + +// fmt.Println(cli.SuccessMessage("Attribute created")) +// fmt.Println( +// cli.NewTabular().Rows([][]string{ +// {"Name", name}, +// {"Rule", rule}, +// {"Values", cli.CommaSeparated(values)}, +// {"Namespace", namespace}, +// {"Description", description}, +// }...).Render(), +// ) +// }, +// } + +// attributesDeleteCmd = &cobra.Command{ +// Use: "delete ", +// Short: "Delete an attribute", +// Args: cobra.ExactArgs(1), +// Run: func(cmd *cobra.Command, args []string) { +// h := cli.NewHandler(cmd) +// defer h.Close() + +// id, err := strconv.Atoi(args[0]) +// if err != nil { +// fmt.Println(cli.ErrorMessage("Invalid ID", err)) +// os.Exit(1) +// } +// attr, err := h.GetAttribute(id) +// if err != nil { +// errMsg := fmt.Sprintf("Could not find attribute (%d)", id) +// cli.ExitWithNotFoundError(errMsg, err) +// cli.ExitWithError(errMsg, err) +// } + +// cli.ConfirmDelete("attribute", attr.Fqn) + +// if err := h.DeleteAttribute(id); err != nil { +// errMsg := fmt.Sprintf("Could not delete attribute (%d)", id) +// cli.ExitWithNotFoundError(errMsg, err) +// cli.ExitWithError(errMsg, err) +// } + +// fmt.Println(cli.SuccessMessage("Attribute deleted")) +// fmt.Println( +// cli.NewTabular(). +// Rows([][]string{ +// {"Name", attr.Name}, +// {"Rule", attr.Rule}, +// {"Values", cli.CommaSeparated(attr.Values)}, +// {"Namespace", attr.Namespace}, +// {"Description", attr.Description}, +// }...).Render(), +// ) +// }, +// } + +// // Update one attribute +// attributeUpdateCmd = &cobra.Command{ +// Use: "update", +// Short: "Update an attribute", +// Run: func(cmd *cobra.Command, args []string) { +// h := cli.NewHandler(cmd) +// defer h.Close() + +// flagHelper := cli.NewFlagHelper(cmd) + +// id := flagHelper.GetRequiredInt32("id") +// name := flagHelper.GetRequiredString("name") +// rule := flagHelper.GetRequiredString("rule") + +// values := flagHelper.GetStringSlice("values", attrValues, cli.FlagHelperStringSliceOptions{ +// Min: 1, +// }) + +// groupBy := flagHelper.GetStringSlice("group-by", groupBy, cli.FlagHelperStringSliceOptions{ +// Min: 0, +// }) + +// resourceDependencies := flagHelper.GetStringSlice("resource-dependencies", resourceDependencies, cli.FlagHelperStringSliceOptions{ +// Min: 0, +// }) + +// resourceId := flagHelper.GetRequiredInt32("resource-id") +// resourceVersion := flagHelper.GetRequiredInt32("resource-version") +// resourceName := flagHelper.GetRequiredString("resource-name") +// resourceNamespace := flagHelper.GetRequiredString("resource-namespace") +// resourceDescription := flagHelper.GetRequiredString("resource-description") + +// if _, err := h.UpdateAttribute( +// id, +// name, +// rule, +// values, +// groupBy, +// resourceId, +// resourceVersion, +// resourceName, +// resourceNamespace, +// resourceDescription, +// resourceDependencies, +// ); err != nil { +// cli.ExitWithError("Could not update attribute", err) +// return +// } else { +// fmt.Println(cli.SuccessMessage(fmt.Sprintf("Attribute id: %d updated.", id))) +// } +// }, +// } +// ) + +// func init() { +// rootCmd.AddCommand(attributesCmd) + +// attributesCmd.AddCommand(attributeGetCmd) + +// attributesCmd.AddCommand(attributesListCmd) + +// attributesCmd.AddCommand(attributesCreateCmd) +// attributesCreateCmd.Flags().StringP("name", "n", "", "Name of the attribute") +// attributesCreateCmd.Flags().StringP("rule", "r", "", "Rule of the attribute") +// attributesCreateCmd.Flags().StringSliceVarP(&attrValues, "values", "v", []string{}, "Values of the attribute") +// attributesCreateCmd.Flags().StringP("namespace", "s", "", "Namespace of the attribute") +// attributesCreateCmd.Flags().StringP("description", "d", "", "Description of the attribute") + +// attributesCmd.AddCommand(attributeUpdateCmd) +// attributeUpdateCmd.Flags().Int32P("id", "i", 0, "Id of the attribute") +// attributeUpdateCmd.Flags().StringP("name", "n", "", "Name of the attribute") +// attributeUpdateCmd.Flags().StringP("rule", "r", "", "Rule of the attribute") +// attributeUpdateCmd.Flags().StringSliceVarP(&attrValues, "values", "v", []string{}, "Values of the attribute") +// attributeUpdateCmd.Flags().StringSliceVarP(&groupBy, "group-by", "g", []string{}, "GroupBy of the attribute") +// attributeUpdateCmd.Flags().StringSliceVarP(&resourceDependencies, "resource-dependencies", "d", []string{}, "ResourceDependencies of the attribute definition descriptor") +// attributeUpdateCmd.Flags().Int32P("resource-id", "I", 0, "ResourceId of the attribute definition descriptor") +// attributeUpdateCmd.Flags().Int32P("resource-version", "V", 0, "ResourceVersion of the attribute definition descriptor") +// attributeUpdateCmd.Flags().StringP("resource-name", "N", "", "ResourceName of the attribute definition descriptor") +// attributeUpdateCmd.Flags().StringP("resource-namespace", "S", "", "ResourceNamespace of the attribute definition descriptor") +// attributeUpdateCmd.Flags().StringP("resource-description", "D", "", "ResourceDescription of the attribute definition descriptor") + +// attributesCmd.AddCommand(attributesDeleteCmd) +// } diff --git a/cmd/namespaces.go b/cmd/namespaces.go new file mode 100644 index 00000000..fe2c76bc --- /dev/null +++ b/cmd/namespaces.go @@ -0,0 +1,194 @@ +package cmd + +import ( + "errors" + "fmt" + "os" + "strings" + + "github.com/opentdf/tructl/pkg/cli" + "github.com/spf13/cobra" +) + +var ( + namespacesCommands = []string{ + namespacesCreateCmd.Use, + namespaceGetCmd.Use, + namespacesListCmd.Use, + namespaceUpdateCmd.Use, + namespaceDeleteCmd.Use, + } + + namespacesCmd = &cobra.Command{ + Use: "namespaces", + Short: "Manage namespaces [" + strings.Join(namespacesCommands, ", ") + "]", + Long: ` +Namespaces - commands to manage attribute namespaces within the platform. + +Namespaces drive associations of attributes and their values and differentiate between them. +For example: "bob.com" and "alice.net" are different namespaces that may have the same +or different attributes tied to each. +`, + } + + namespaceGetCmd = &cobra.Command{ + Use: "get", + Short: "Get a namespace by id", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + h := cli.NewHandler(cmd) + defer h.Close() + + id := args[0] + if id == "" { + cli.ExitWithError("Invalid ID", errors.New(id)) + } + + ns, err := h.GetNamespace(id) + if err != nil { + errMsg := fmt.Sprintf("Could not find namespace (%s)", id) + cli.ExitWithNotFoundError(errMsg, err) + cli.ExitWithError(errMsg, err) + } + + fmt.Println(cli.SuccessMessage("Namespace found")) + fmt.Println( + cli.NewTabular(). + Rows([][]string{ + {"Id", ns.Id}, + {"Name", ns.Name}, + }...).Render(), + ) + }, + } + + namespacesListCmd = &cobra.Command{ + Use: "list", + Short: "List namespaces", + Run: func(cmd *cobra.Command, args []string) { + h := cli.NewHandler(cmd) + defer h.Close() + + list, err := h.ListNamespaces() + if err != nil { + cli.ExitWithError("Could not get namespaces", err) + } + + t := cli.NewTable() + t.Headers("Id", "Name") + for _, ns := range list { + t.Row( + ns.Id, + ns.Name, + ) + } + fmt.Println(t.Render()) + }, + } + + namespacesCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create a new namespace, i.e. 'https://example.com'", + Run: func(cmd *cobra.Command, args []string) { + h := cli.NewHandler(cmd) + defer h.Close() + + flagHelper := cli.NewFlagHelper(cmd) + name := flagHelper.GetRequiredString("name") + + created, err := h.CreateNamespace(name) + if err != nil { + cli.ExitWithError("Could not create namespace", err) + } + + fmt.Println(cli.SuccessMessage("Namespace created")) + fmt.Println( + cli.NewTabular().Rows([][]string{ + {"Name", name}, + {"Id", created.Id}, + }...).Render(), + ) + }, + } + + namespaceDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete a namespace by id", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + h := cli.NewHandler(cmd) + defer h.Close() + + id := args[0] + if id == "" { + fmt.Println(cli.ErrorMessage("Invalid ID", errors.New(id))) + os.Exit(1) + } + ns, err := h.GetNamespace(id) + if err != nil { + errMsg := fmt.Sprintf("Could not find namespace (%s)", id) + cli.ExitWithNotFoundError(errMsg, err) + cli.ExitWithError(errMsg, err) + } + + cli.ConfirmDelete("namespace", ns.Name) + + if err := h.DeleteNamespace(id); err != nil { + errMsg := fmt.Sprintf("Could not delete namespace (%s)", id) + cli.ExitWithNotFoundError(errMsg, err) + cli.ExitWithError(errMsg, err) + } + + fmt.Println(cli.SuccessMessage("Namespace deleted")) + fmt.Println( + cli.NewTabular(). + Rows([][]string{ + {"Id", ns.Id}, + {"Name", ns.Name}, + }...).Render(), + ) + }, + } + + // Update one namespace + namespaceUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update a namespace", + Run: func(cmd *cobra.Command, args []string) { + h := cli.NewHandler(cmd) + defer h.Close() + + flagHelper := cli.NewFlagHelper(cmd) + + id := flagHelper.GetRequiredString("id") + name := flagHelper.GetRequiredString("name") + + if _, err := h.UpdateNamespace( + id, + name, + ); err != nil { + cli.ExitWithError("Could not update namespace", err) + return + } else { + fmt.Println(cli.SuccessMessage(fmt.Sprintf("Namespace id: (%s) updated. Name set to (%s).", id, name))) + } + }, + } +) + +func init() { + rootCmd.AddCommand(namespacesCmd) + + namespacesCmd.AddCommand(namespaceGetCmd) + + namespacesCmd.AddCommand(namespacesListCmd) + + namespacesCmd.AddCommand(namespacesCreateCmd) + namespacesCreateCmd.Flags().StringP("name", "n", "", "Name value of the namespace") + + namespacesCmd.AddCommand(namespaceUpdateCmd) + namespaceUpdateCmd.Flags().StringP("id", "i", "", "Id of the namespace") + namespaceUpdateCmd.Flags().StringP("name", "n", "", "Name value of the namespace") + + namespacesCmd.AddCommand(namespaceDeleteCmd) +} diff --git a/go.mod b/go.mod index 863d91b4..37f89293 100644 --- a/go.mod +++ b/go.mod @@ -52,3 +52,6 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect google.golang.org/protobuf v1.32.0 // indirect ) + +// temporarily use local platform sdk until branch `policy-config-changes` is merged into `main` there +replace github.com/opentdf/opentdf-v2-poc/sdk => ../opentdf-v2-poc/sdk diff --git a/pkg/handlers/attribute.go b/pkg/handlers/attribute.go index d8435f24..c20e85b9 100644 --- a/pkg/handlers/attribute.go +++ b/pkg/handlers/attribute.go @@ -1,216 +1,216 @@ package handlers -import ( - "fmt" - - "github.com/opentdf/opentdf-v2-poc/sdk/attributes" - "github.com/opentdf/opentdf-v2-poc/sdk/common" - "github.com/opentdf/tructl/pkg/grpc" -) - -// TODO: Might be useful to map out the attribute rule definitions for help text in the CLI and TUI - -const ( - AttributeRuleAllOf = "ALL_OF" - AttributeRuleAnyOf = "ANY_OF" - AttributeRuleHierarchy = "HIERARCHY" - AttributeRuleUnspecified = "UNSPECIFIED" -) - -type Attribute struct { - Id int32 - Name string - Rule string - Values []string - Namespace string - Description string - Fqn string -} - -func (h Handler) GetAttribute(id int) (Attribute, error) { - resp, err := h.sdk.Attributes.GetAttribute(h.ctx, &attributes.GetAttributeRequest{ - Id: int32(id), - }) - if err != nil { - return Attribute{}, err - } - - values := []string{} - for _, v := range resp.Definition.Values { - values = append(values, v.Value) - } - - return Attribute{ - Id: resp.Definition.Descriptor_.Id, - Name: resp.Definition.Name, - Rule: GetAttributeRuleFromAttributeType(resp.Definition.Rule), - Values: values, - Namespace: resp.Definition.Descriptor_.Namespace, - Description: resp.Definition.Descriptor_.Description, - Fqn: GetAttributeFqn(resp.Definition.Descriptor_.Namespace, resp.Definition.Name), - }, nil -} - -func (h Handler) ListAttributes() ([]Attribute, error) { - resp, err := h.sdk.Attributes.ListAttributes(h.ctx, &attributes.ListAttributesRequest{}) - if err != nil { - return nil, err - } - - var attrs []Attribute - for _, attr := range resp.Definitions { - values := []string{} - for _, v := range attr.Values { - values = append(values, v.Value) - } - attrs = append(attrs, Attribute{ - Id: attr.Descriptor_.Id, - Name: attr.Name, - Rule: GetAttributeRuleFromAttributeType(attr.Rule), - Values: values, - Namespace: attr.Descriptor_.Namespace, - Description: attr.Descriptor_.Description, - }) - } - - return attrs, err -} - -func (h Handler) CreateAttribute(name string, rule string, values []string, namespace string, description string) (Attribute, error) { - var attrValues []*attributes.AttributeDefinitionValue - for _, v := range values { - if v != "" { - attrValues = append(attrValues, &attributes.AttributeDefinitionValue{Value: v}) - } - } - - _, err := h.sdk.Attributes.CreateAttribute(h.ctx, &attributes.CreateAttributeRequest{ - Definition: &attributes.AttributeDefinition{ - Name: name, - Rule: GetAttributeRuleFromReadableString(rule), - Values: attrValues, - Descriptor_: &common.ResourceDescriptor{ - Namespace: namespace, - Name: name, - Type: common.PolicyResourceType_POLICY_RESOURCE_TYPE_ATTRIBUTE_DEFINITION, - Description: description, - }, - }, - }) - if err != nil { - return Attribute{}, err - } - - return Attribute{ - Name: name, - Rule: rule, - Values: values, - Namespace: namespace, - Description: description, - }, nil -} - -func (h *Handler) UpdateAttribute( - id int32, - name string, - rule string, - values []string, - groupBy []string, - resourceId int32, - resourceVersion int32, - resourceName string, - resourceNamespace string, - resourceDescription string, - resourceDependencies []string, -) (*attributes.UpdateAttributeResponse, error) { - var attrValues []*attributes.AttributeDefinitionValue - for _, v := range values { - if v != "" { - attrValues = append(attrValues, &attributes.AttributeDefinitionValue{Value: v}) - } - } - - var attrGroupBy []*attributes.AttributeDefinitionValue - for _, v := range groupBy { - if v != "" { - attrGroupBy = append(attrGroupBy, &attributes.AttributeDefinitionValue{Value: v}) - } - } - - var dependencies []*common.ResourceDependency - for _, v := range resourceDependencies { - if v != "" { - dependencies = append(dependencies, &common.ResourceDependency{Namespace: v}) - } - } - - return h.sdk.Attributes.UpdateAttribute(grpc.Context, &attributes.UpdateAttributeRequest{ - Id: id, - Definition: &attributes.AttributeDefinition{ - Name: name, - Rule: GetAttributeRuleFromReadableString(rule), - Values: attrValues, - GroupBy: attrGroupBy, - Descriptor_: &common.ResourceDescriptor{ - Type: common.PolicyResourceType_POLICY_RESOURCE_TYPE_ATTRIBUTE_DEFINITION, - Id: resourceId, - Version: resourceVersion, - Name: resourceName, - Namespace: resourceNamespace, - Fqn: GetAttributeFqn(resourceNamespace, resourceName), - Description: resourceDescription, - Dependencies: dependencies, - }, - }, - }) -} - -func (h Handler) DeleteAttribute(id int) error { - _, err := h.sdk.Attributes.DeleteAttribute(h.ctx, &attributes.DeleteAttributeRequest{ - Id: int32(id), - }) - - return err -} - -func GetAttributeFqn(namespace string, name string) string { - return fmt.Sprintf("https://%s/attr/%s", namespace, name) -} - -func GetAttributeRuleOptions() []string { - return []string{ - AttributeRuleAllOf, - AttributeRuleAnyOf, - AttributeRuleHierarchy, - AttributeRuleUnspecified, - } -} - -func GetAttributeRuleFromAttributeType(rule attributes.AttributeDefinition_AttributeRuleType) string { - switch rule { - case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ALL_OF: - return AttributeRuleAllOf - case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ANY_OF: - return AttributeRuleAnyOf - case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_HIERARCHICAL: - return AttributeRuleHierarchy - case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED: - return AttributeRuleUnspecified - default: - return "" - } -} - -func GetAttributeRuleFromReadableString(rule string) attributes.AttributeDefinition_AttributeRuleType { - switch rule { - case AttributeRuleAllOf: - return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ALL_OF - case AttributeRuleAnyOf: - return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ANY_OF - case AttributeRuleHierarchy: - return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_HIERARCHICAL - case AttributeRuleUnspecified: - return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED - } - return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED -} +// import ( +// "fmt" + +// "github.com/opentdf/opentdf-v2-poc/sdk/attributes" +// "github.com/opentdf/opentdf-v2-poc/sdk/common" +// "github.com/opentdf/tructl/pkg/grpc" +// ) + +// // TODO: Might be useful to map out the attribute rule definitions for help text in the CLI and TUI + +// const ( +// AttributeRuleAllOf = "ALL_OF" +// AttributeRuleAnyOf = "ANY_OF" +// AttributeRuleHierarchy = "HIERARCHY" +// AttributeRuleUnspecified = "UNSPECIFIED" +// ) + +// type Attribute struct { +// Id int32 +// Name string +// Rule string +// Values []string +// Namespace string +// Description string +// Fqn string +// } + +// func (h Handler) GetAttribute(id int) (Attribute, error) { +// resp, err := h.sdk.Attributes.GetAttribute(h.ctx, &attributes.GetAttributeRequest{ +// Id: int32(id), +// }) +// if err != nil { +// return Attribute{}, err +// } + +// values := []string{} +// for _, v := range resp.Definition.Values { +// values = append(values, v.Value) +// } + +// return Attribute{ +// Id: resp.Definition.Descriptor_.Id, +// Name: resp.Definition.Name, +// Rule: GetAttributeRuleFromAttributeType(resp.Definition.Rule), +// Values: values, +// Namespace: resp.Definition.Descriptor_.Namespace, +// Description: resp.Definition.Descriptor_.Description, +// Fqn: GetAttributeFqn(resp.Definition.Descriptor_.Namespace, resp.Definition.Name), +// }, nil +// } + +// func (h Handler) ListAttributes() ([]Attribute, error) { +// resp, err := h.sdk.Attributes.ListAttributes(h.ctx, &attributes.ListAttributesRequest{}) +// if err != nil { +// return nil, err +// } + +// var attrs []Attribute +// for _, attr := range resp.Definitions { +// values := []string{} +// for _, v := range attr.Values { +// values = append(values, v.Value) +// } +// attrs = append(attrs, Attribute{ +// Id: attr.Descriptor_.Id, +// Name: attr.Name, +// Rule: GetAttributeRuleFromAttributeType(attr.Rule), +// Values: values, +// Namespace: attr.Descriptor_.Namespace, +// Description: attr.Descriptor_.Description, +// }) +// } + +// return attrs, err +// } + +// func (h Handler) CreateAttribute(name string, rule string, values []string, namespace string, description string) (Attribute, error) { +// var attrValues []*attributes.AttributeDefinitionValue +// for _, v := range values { +// if v != "" { +// attrValues = append(attrValues, &attributes.AttributeDefinitionValue{Value: v}) +// } +// } + +// _, err := h.sdk.Attributes.CreateAttribute(h.ctx, &attributes.CreateAttributeRequest{ +// Definition: &attributes.AttributeDefinition{ +// Name: name, +// Rule: GetAttributeRuleFromReadableString(rule), +// Values: attrValues, +// Descriptor_: &common.ResourceDescriptor{ +// Namespace: namespace, +// Name: name, +// Type: common.PolicyResourceType_POLICY_RESOURCE_TYPE_ATTRIBUTE_DEFINITION, +// Description: description, +// }, +// }, +// }) +// if err != nil { +// return Attribute{}, err +// } + +// return Attribute{ +// Name: name, +// Rule: rule, +// Values: values, +// Namespace: namespace, +// Description: description, +// }, nil +// } + +// func (h *Handler) UpdateAttribute( +// id int32, +// name string, +// rule string, +// values []string, +// groupBy []string, +// resourceId int32, +// resourceVersion int32, +// resourceName string, +// resourceNamespace string, +// resourceDescription string, +// resourceDependencies []string, +// ) (*attributes.UpdateAttributeResponse, error) { +// var attrValues []*attributes.AttributeDefinitionValue +// for _, v := range values { +// if v != "" { +// attrValues = append(attrValues, &attributes.AttributeDefinitionValue{Value: v}) +// } +// } + +// var attrGroupBy []*attributes.AttributeDefinitionValue +// for _, v := range groupBy { +// if v != "" { +// attrGroupBy = append(attrGroupBy, &attributes.AttributeDefinitionValue{Value: v}) +// } +// } + +// var dependencies []*common.ResourceDependency +// for _, v := range resourceDependencies { +// if v != "" { +// dependencies = append(dependencies, &common.ResourceDependency{Namespace: v}) +// } +// } + +// return h.sdk.Attributes.UpdateAttribute(grpc.Context, &attributes.UpdateAttributeRequest{ +// Id: id, +// Definition: &attributes.AttributeDefinition{ +// Name: name, +// Rule: GetAttributeRuleFromReadableString(rule), +// Values: attrValues, +// GroupBy: attrGroupBy, +// Descriptor_: &common.ResourceDescriptor{ +// Type: common.PolicyResourceType_POLICY_RESOURCE_TYPE_ATTRIBUTE_DEFINITION, +// Id: resourceId, +// Version: resourceVersion, +// Name: resourceName, +// Namespace: resourceNamespace, +// Fqn: GetAttributeFqn(resourceNamespace, resourceName), +// Description: resourceDescription, +// Dependencies: dependencies, +// }, +// }, +// }) +// } + +// func (h Handler) DeleteAttribute(id int) error { +// _, err := h.sdk.Attributes.DeleteAttribute(h.ctx, &attributes.DeleteAttributeRequest{ +// Id: int32(id), +// }) + +// return err +// } + +// func GetAttributeFqn(namespace string, name string) string { +// return fmt.Sprintf("https://%s/attr/%s", namespace, name) +// } + +// func GetAttributeRuleOptions() []string { +// return []string{ +// AttributeRuleAllOf, +// AttributeRuleAnyOf, +// AttributeRuleHierarchy, +// AttributeRuleUnspecified, +// } +// } + +// func GetAttributeRuleFromAttributeType(rule attributes.AttributeDefinition_AttributeRuleType) string { +// switch rule { +// case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ALL_OF: +// return AttributeRuleAllOf +// case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ANY_OF: +// return AttributeRuleAnyOf +// case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_HIERARCHICAL: +// return AttributeRuleHierarchy +// case attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED: +// return AttributeRuleUnspecified +// default: +// return "" +// } +// } + +// func GetAttributeRuleFromReadableString(rule string) attributes.AttributeDefinition_AttributeRuleType { +// switch rule { +// case AttributeRuleAllOf: +// return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ALL_OF +// case AttributeRuleAnyOf: +// return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_ANY_OF +// case AttributeRuleHierarchy: +// return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_HIERARCHICAL +// case AttributeRuleUnspecified: +// return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED +// } +// return attributes.AttributeDefinition_ATTRIBUTE_RULE_TYPE_UNSPECIFIED +// } diff --git a/pkg/handlers/namespaces.go b/pkg/handlers/namespaces.go new file mode 100644 index 00000000..ed882036 --- /dev/null +++ b/pkg/handlers/namespaces.go @@ -0,0 +1,58 @@ +package handlers + +import ( + "github.com/opentdf/opentdf-v2-poc/sdk/namespaces" +) + +func (h Handler) GetNamespace(id string) (*namespaces.Namespace, error) { + resp, err := h.sdk.Namespaces.GetNamespace(h.ctx, &namespaces.GetNamespaceRequest{ + Id: id, + }) + if err != nil { + return nil, err + } + + return resp.Namespace, nil +} + +func (h Handler) ListNamespaces() ([]*namespaces.Namespace, error) { + resp, err := h.sdk.Namespaces.ListNamespaces(h.ctx, &namespaces.ListNamespacesRequest{}) + if err != nil { + return nil, err + } + + return resp.Namespaces, nil +} + +func (h Handler) CreateNamespace(name string) (*namespaces.Namespace, error) { + resp, err := h.sdk.Namespaces.CreateNamespace(h.ctx, &namespaces.CreateNamespaceRequest{ + Name: name, + }) + if err != nil { + return nil, err + } + + return resp.Namespace, nil +} + +func (h Handler) UpdateNamespace(id string, name string) (*namespaces.Namespace, error) { + resp, err := h.sdk.Namespaces.UpdateNamespace(h.ctx, &namespaces.UpdateNamespaceRequest{ + Id: id, + Name: name, + }) + if err != nil { + return nil, err + } + return resp.Namespace, nil +} + +func (h Handler) DeleteNamespace(id string) error { + _, err := h.sdk.Namespaces.DeleteNamespace(h.ctx, &namespaces.DeleteNamespaceRequest{ + Id: id, + }) + if err != nil { + return err + } + + return nil +} diff --git a/tui/constants/consts.go b/tui/constants/consts.go index 91ad8101..8184f6ec 100644 --- a/tui/constants/consts.go +++ b/tui/constants/consts.go @@ -5,13 +5,6 @@ import ( "github.com/charmbracelet/lipgloss" ) -var P *tea.Program - -var WindowSize struct { - Width int - Height int -} - var ( P *tea.Program