Added create transform command

This commit is contained in:
colin-mckibben-sp
2022-09-21 10:58:01 -04:00
parent 2907f297d4
commit 0b80b41c4b
15 changed files with 119 additions and 32 deletions

View File

@@ -5,9 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"path"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/spf13/cobra"
@@ -20,15 +18,6 @@ const (
connectorsEndpoint = "/beta/platform-connectors"
)
func connResourceUrl(endpoint string, resourceParts ...string) string {
u, err := url.Parse(endpoint)
if err != nil {
log.Fatalf("invalid endpoint: %s (%q)", err, endpoint)
}
u.Path = path.Join(append([]string{u.Path}, resourceParts...)...)
return u.String()
}
func NewConnCmd(client client.Client) *cobra.Command {
conn := &cobra.Command{
Use: "connectors",

View File

@@ -10,6 +10,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -39,7 +40,7 @@ func newConnCreateCmd(client client.Client) *cobra.Command {
}
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Post(cmd.Context(), connResourceUrl(endpoint), "application/json", bytes.NewReader(raw))
resp, err := client.Post(cmd.Context(), util.ResourceUrl(endpoint), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -47,7 +48,7 @@ func newConnCreateVersionCmd(client client.Client) *cobra.Command {
}
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Post(cmd.Context(), connResourceUrl(endpoint, connectorRef, "versions"), "application/zip", f)
resp, err := client.Post(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "versions"), "application/zip", f)
if err != nil {
return err
}
@@ -76,7 +77,7 @@ func newConnCreateVersionCmd(client client.Client) *cobra.Command {
table.Render()
if tagName != "" {
resp, err := client.Get(cmd.Context(), connResourceUrl(endpoint, connectorRef, "tags", tagName))
resp, err := client.Get(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "tags", tagName))
if err != nil {
return err
}
@@ -121,7 +122,7 @@ func updateTagWithVersion(cmd *cobra.Command, client client.Client, endpoint str
return err
}
resp, err := client.Put(cmd.Context(), connResourceUrl(endpoint, connectorID, "tags", tagName), "application/json", bytes.NewReader(raw))
resp, err := client.Put(cmd.Context(), util.ResourceUrl(endpoint, connectorID, "tags", tagName), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}
@@ -159,7 +160,7 @@ func createTagWithVersion(cmd *cobra.Command, client client.Client, endpoint str
return err
}
resp, err := client.Post(cmd.Context(), connResourceUrl(endpoint, connectorID, "tags"), "application/json", bytes.NewReader(raw))
resp, err := client.Post(cmd.Context(), util.ResourceUrl(endpoint, connectorID, "tags"), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -22,7 +23,7 @@ func newConnGetCmd(client client.Client) *cobra.Command {
connectorRef := cmd.Flags().Lookup("id").Value.String()
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Get(cmd.Context(), connResourceUrl(endpoint, connectorRef))
resp, err := client.Get(cmd.Context(), util.ResourceUrl(endpoint, connectorRef))
if err != nil {
return err
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -35,7 +36,7 @@ func newConnTagCreateCmd(client client.Client) *cobra.Command {
}
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Post(cmd.Context(), connResourceUrl(endpoint, connectorRef, "tags"), "application/json", bytes.NewReader(raw))
resp, err := client.Post(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "tags"), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -22,7 +23,7 @@ func newConnTagListCmd(client client.Client) *cobra.Command {
connectorRef := cmd.Flags().Lookup("id").Value.String()
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Get(cmd.Context(), connResourceUrl(endpoint, connectorRef, "tags"))
resp, err := client.Get(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "tags"))
if err != nil {
return err
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -36,7 +37,7 @@ func newConnTagUpdateCmd(client client.Client) *cobra.Command {
}
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Put(cmd.Context(), connResourceUrl(endpoint, connectorRef, "tags", tagName), "application/json", bytes.NewReader(raw))
resp, err := client.Put(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "tags", tagName), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/sailpoint-oss/sp-cli/mocks"
"github.com/sailpoint-oss/sp-cli/util"
)
// Unit tests for conn.go
@@ -21,7 +22,7 @@ func TestConnResourceUrl(t *testing.T) {
testResource := "123"
expected := "http://localhost:7100/resources/123"
actual := connResourceUrl(testEndpoint, testResource)
actual := util.ResourceUrl(testEndpoint, testResource)
if expected != actual {
t.Errorf("expected: %s, actual: %s", expected, actual)

View File

@@ -10,6 +10,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -38,7 +39,7 @@ func newConnUpdateCmd(client client.Client) *cobra.Command {
}
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Put(cmd.Context(), connResourceUrl(endpoint, id), "application/json", bytes.NewReader(raw))
resp, err := client.Put(cmd.Context(), util.ResourceUrl(endpoint, id), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
@@ -22,7 +23,7 @@ func newConnVersionsCmd(client client.Client) *cobra.Command {
connectorRef := cmd.Flags().Lookup("id").Value.String()
endpoint := cmd.Flags().Lookup("conn-endpoint").Value.String()
resp, err := client.Get(cmd.Context(), connResourceUrl(endpoint, connectorRef, "versions"))
resp, err := client.Get(cmd.Context(), util.ResourceUrl(endpoint, connectorRef, "versions"))
if err != nil {
return err
}

72
cmd/transform/create.go Normal file
View File

@@ -0,0 +1,72 @@
// Copyright (c) 2021, SailPoint Technologies, Inc. All rights reserved.
package transform
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
func newCreateCmd(client client.Client) *cobra.Command {
// TODO: Clean up and not send display name
type create struct {
DisplayName string `json:"displayName"`
Alias string `json:"alias"`
}
cmd := &cobra.Command{
Use: "create <transform data>",
Short: "Create transform",
Long: "Create a transform",
Example: "sp transforms create < transform.json",
Aliases: []string{"c"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var data map[string]interface{}
err := json.NewDecoder(os.Stdin).Decode(&data)
if err != nil {
log.Fatal(err)
}
if data["name"] == nil {
log.Fatal("The transform must have a name.")
return nil
}
raw, err := json.Marshal(data)
if err != nil {
return err
}
endpoint := cmd.Flags().Lookup("transforms-endpoint").Value.String()
resp, err := client.Post(cmd.Context(), util.ResourceUrl(endpoint), "application/json", bytes.NewReader(raw))
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("create transform failed. status: %s\nbody: %s", resp.Status, body)
}
return nil
},
}
return cmd
}

View File

@@ -13,11 +13,11 @@ import (
"github.com/spf13/cobra"
)
func newTransformDownloadCmd(client client.Client) *cobra.Command {
func newDownloadCmd(client client.Client) *cobra.Command {
return &cobra.Command{
Use: "download",
Short: "Download Transforms",
Long: "Download Transforms To Local Storage",
Short: "Download transforms",
Long: "Download transforms to local storage",
Aliases: []string{"dl"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -12,11 +12,11 @@ import (
"github.com/spf13/cobra"
)
func newTransformListCmd(client client.Client) *cobra.Command {
func newListCmd(client client.Client) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List Transforms",
Long: "List Transforms For Tenant",
Short: "List transforms",
Long: "List transforms for tenant",
Aliases: []string{"ls"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -16,7 +16,7 @@ const (
func NewTransformCmd(client client.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "transforms",
Short: "Manage Transforms",
Short: "Manage transforms",
Aliases: []string{"trans"},
Run: func(cmd *cobra.Command, args []string) {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), cmd.UsageString())
@@ -26,8 +26,9 @@ func NewTransformCmd(client client.Client) *cobra.Command {
cmd.PersistentFlags().StringP("transforms-endpoint", "e", viper.GetString("baseurl")+transformsEndpoint, "Override transforms endpoint")
cmd.AddCommand(
newTransformListCmd(client),
newTransformDownloadCmd(client),
newListCmd(client),
newDownloadCmd(client),
newCreateCmd(client),
)
return cmd

16
util/url.go Normal file
View File

@@ -0,0 +1,16 @@
package util
import (
"log"
"net/url"
"path"
)
func ResourceUrl(endpoint string, resourceParts ...string) string {
u, err := url.Parse(endpoint)
if err != nil {
log.Fatalf("invalid endpoint: %s (%q)", err, endpoint)
}
u.Path = path.Join(append([]string{u.Path}, resourceParts...)...)
return u.String()
}