Skip to content

Commit

Permalink
add procedure + policy clis
Browse files Browse the repository at this point in the history
Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
  • Loading branch information
golanglemonade committed Nov 20, 2024
1 parent 96b5a18 commit 3c2680b
Show file tree
Hide file tree
Showing 21 changed files with 1,084 additions and 4 deletions.
90 changes: 90 additions & 0 deletions cmd/cli/cmd/internalpolicy/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package internalpolicy

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

var createCmd = &cobra.Command{
Use: "create",
Short: "create a new internal policy",
Run: func(cmd *cobra.Command, args []string) {
err := create(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(createCmd)

// command line flags for the create command
createCmd.Flags().StringP("name", "n", "", "name of the policy")
createCmd.Flags().StringP("description", "d", "", "description of the policy")
createCmd.Flags().StringP("status", "s", "", "status of the policy")
createCmd.Flags().StringP("type", "t", "", "type of the policy")
createCmd.Flags().StringP("version", "v", "v0.1", "version of the policy")
createCmd.Flags().StringP("purpose", "p", "", "purpose and scope of the policy")
createCmd.Flags().StringP("background", "b", "", "background information of the policy")
}

// createValidation validates the required fields for the command
func createValidation() (input openlaneclient.CreateInternalPolicyInput, err error) {
// validation of required fields for the create command
// output the input struct with the required fields and optional fields based on the command line flags
input.Name = cmd.Config.String("name")
if input.Name == "" {
return input, cmd.NewRequiredFieldMissingError("name")
}

description := cmd.Config.String("description")
if description != "" {
input.Description = &description
}

status := cmd.Config.String("status")
if status != "" {
input.Status = &status
}

policyType := cmd.Config.String("type")
if policyType != "" {
input.PolicyType = &policyType
}

version := cmd.Config.String("version")
if version != "" {
input.Version = &version
}

purpose := cmd.Config.String("purpose")
if purpose != "" {
input.PurposeAndScope = &purpose
}

background := cmd.Config.String("background")
if background != "" {
input.Background = &background
}

return input, nil
}

// create a new internal policy
func create(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

input, err := createValidation()
cobra.CheckErr(err)

o, err := client.CreateInternalPolicy(ctx, input)
cobra.CheckErr(err)

return consoleOutput(o)
}
50 changes: 50 additions & 0 deletions cmd/cli/cmd/internalpolicy/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package internalpolicy

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete an existing internalpolicy",
Run: func(cmd *cobra.Command, args []string) {
err := delete(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(deleteCmd)

deleteCmd.Flags().StringP("id", "i", "", "internalpolicy id to delete")
}

// deleteValidation validates the required fields for the command
func deleteValidation() (string, error) {
id := cmd.Config.String("id")
if id == "" {
return "", cmd.NewRequiredFieldMissingError("internalpolicy id")
}

return id, nil
}

// delete an existing internalPolicy in the platform
func delete(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

id, err := deleteValidation()
cobra.CheckErr(err)

o, err := client.DeleteInternalPolicy(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}
2 changes: 2 additions & 0 deletions cmd/cli/cmd/internalpolicy/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package internalpolicy is our cobra cli for internalPolicy endpoints
package internalpolicy
48 changes: 48 additions & 0 deletions cmd/cli/cmd/internalpolicy/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package internalpolicy

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "get an existing internalPolicy",
Run: func(cmd *cobra.Command, args []string) {
err := get(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(getCmd)
getCmd.Flags().StringP("id", "i", "", "internalPolicy id to query")
}

// get an existing internalPolicy in the platform
func get(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

// filter options
id := cmd.Config.String("id")

// if an internalPolicy ID is provided, filter on that internalPolicy, otherwise get all
if id != "" {
o, err := client.GetInternalPolicyByID(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}

// get all will be filtered for the authorized organization(s)
o, err := client.GetAllInternalPolicies(ctx)
cobra.CheckErr(err)

return consoleOutput(o)
}
107 changes: 107 additions & 0 deletions cmd/cli/cmd/internalpolicy/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package internalpolicy

import (
"encoding/json"
"strings"

"github.com/spf13/cobra"

"github.com/theopenlane/utils/cli/tables"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

// command represents the base internalPolicy command when called without any subcommands
var command = &cobra.Command{
Use: "internal-policy",
Short: "the subcommands for working with internalPolicies",
}

func init() {
cmd.RootCmd.AddCommand(command)
}

// consoleOutput prints the output in the console
func consoleOutput(e any) error {
// check if the output format is JSON and print the internalPolicies in JSON format
if strings.EqualFold(cmd.OutputFormat, cmd.JSONOutput) {
return jsonOutput(e)
}

// check the type of the internalPolicies and print them in a table format
switch v := e.(type) {
case *openlaneclient.GetAllInternalPolicies:
var nodes []*openlaneclient.GetAllInternalPolicies_InternalPolicies_Edges_Node

for _, i := range v.InternalPolicies.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetInternalPolicies:
var nodes []*openlaneclient.GetInternalPolicies_InternalPolicies_Edges_Node

for _, i := range v.InternalPolicies.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetInternalPolicyByID:
e = v.InternalPolicy
case *openlaneclient.CreateInternalPolicy:
e = v.CreateInternalPolicy.InternalPolicy
case *openlaneclient.UpdateInternalPolicy:
e = v.UpdateInternalPolicy.InternalPolicy
case *openlaneclient.DeleteInternalPolicy:
deletedTableOutput(v)
return nil
}

s, err := json.Marshal(e)
cobra.CheckErr(err)

var list []openlaneclient.InternalPolicy

err = json.Unmarshal(s, &list)
if err != nil {
var in openlaneclient.InternalPolicy
err = json.Unmarshal(s, &in)
cobra.CheckErr(err)

list = append(list, in)
}

tableOutput(list)

return nil
}

// jsonOutput prints the output in a JSON format
func jsonOutput(out any) error {
s, err := json.Marshal(out)
cobra.CheckErr(err)

return cmd.JSONPrint(s)
}

// tableOutput prints the output in a table format
func tableOutput(out []openlaneclient.InternalPolicy) {
// create a table writer
// TODO: add additional columns to the table writer
writer := tables.NewTableWriter(command.OutOrStdout(), "ID", "Name", "Description", "Status", "Type", "Version", "Purpose", "Background")
for _, i := range out {
writer.AddRow(i.ID, i.Name, *i.Description, *i.Status, *i.PolicyType, *i.Version, *i.PurposeAndScope, *i.Background)
}

writer.Render()
}

// deleteTableOutput prints the deleted id in a table format
func deletedTableOutput(e *openlaneclient.DeleteInternalPolicy) {
writer := tables.NewTableWriter(command.OutOrStdout(), "DeletedID")

writer.AddRow(e.DeleteInternalPolicy.DeletedID)

writer.Render()
}
97 changes: 97 additions & 0 deletions cmd/cli/cmd/internalpolicy/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package internalpolicy

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "update an existing internal policy",
Run: func(cmd *cobra.Command, args []string) {
err := update(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(updateCmd)

updateCmd.Flags().StringP("id", "i", "", "policy id to update")

// command line flags for the update command
updateCmd.Flags().StringP("name", "n", "", "name of the procedure")
updateCmd.Flags().StringP("description", "d", "", "description of the procedure")
updateCmd.Flags().StringP("status", "s", "", "status of the procedure")
updateCmd.Flags().StringP("type", "t", "", "type of the procedure")
updateCmd.Flags().StringP("version", "v", "v0.1", "version of the procedure")
updateCmd.Flags().StringP("purpose", "p", "", "purpose and scope of the procedure")
updateCmd.Flags().StringP("background", "b", "", "background information of the procedure")
}

// updateValidation validates the required fields for the command
func updateValidation() (id string, input openlaneclient.UpdateInternalPolicyInput, err error) {
id = cmd.Config.String("id")
if id == "" {
return id, input, cmd.NewRequiredFieldMissingError("internal policy id")
}

// validation of required fields for the update command
// output the input struct with the required fields and optional fields based on the command line flags
name := cmd.Config.String("name")
if name != "" {
input.Name = &name
}

description := cmd.Config.String("description")
if description != "" {
input.Description = &description
}

status := cmd.Config.String("status")
if status != "" {
input.Status = &status
}

policyType := cmd.Config.String("type")
if policyType != "" {
input.PolicyType = &policyType
}

version := cmd.Config.String("version")
if version != "" {
input.Version = &version
}

purpose := cmd.Config.String("purpose")
if purpose != "" {
input.PurposeAndScope = &purpose
}

background := cmd.Config.String("background")
if background != "" {
input.Background = &background
}

return id, input, nil
}

// update an existing internal policy in the platform
func update(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

id, input, err := updateValidation()
cobra.CheckErr(err)

o, err := client.UpdateInternalPolicy(ctx, id, input)
cobra.CheckErr(err)

return consoleOutput(o)
}
Loading

0 comments on commit 3c2680b

Please sign in to comment.