Added support for implicit and explicit transform preview

This commit is contained in:
colin-mckibben-sp
2022-09-23 10:14:56 -04:00
parent a5f8464068
commit bf7789a412
3 changed files with 75 additions and 36 deletions

View File

@@ -7,6 +7,9 @@ type transform struct {
Type string `json:"type"`
}
type operation struct {
}
func (t transform) transformToColumns() []string {
return []string{t.ID, t.Name}
}
@@ -49,10 +52,14 @@ type attributeTransformPreview struct {
Type string `json:"type"`
}
type previewBody struct {
type previewBodyImplicit struct {
AttributeTransforms []attributeTransformPreview `json:"attributeTransforms"`
}
type previewBodyExplicit struct {
AttributeTransforms []map[string]interface{} `json:"attributeTransforms"`
}
type identityAttributeConfig struct {
AttributeTransforms []attributeTransform `json:"attributeTransforms"`
}
@@ -104,7 +111,7 @@ func makeReference(data interface{}) attributesOfReference {
return reference
}
func makePreviewBody(identityAttribute string, transformName string, accountAttribute string, sourceName string) previewBody {
func makePreviewBodyImplicit(identityAttribute string, transformName string, accountAttribute string, sourceName string) previewBodyImplicit {
attributeTransform := attributeTransformPreview{}
attributeTransform.AttributeName = identityAttribute
attributeTransform.Attributes.Id = transformName
@@ -113,8 +120,17 @@ func makePreviewBody(identityAttribute string, transformName string, accountAttr
attributeTransform.Attributes.Input.Attributes.SourceName = sourceName
attributeTransform.Type = "reference"
previewBody := previewBody{}
previewBody := previewBodyImplicit{}
previewBody.AttributeTransforms = append(previewBody.AttributeTransforms, attributeTransform)
return previewBody
}
func makePreviewBodyExplicit(identityAttribute string, transformData map[string]interface{}) previewBodyExplicit {
transformData["attributeName"] = identityAttribute
previewBody := previewBodyExplicit{}
previewBody.AttributeTransforms = append(previewBody.AttributeTransforms, transformData)
return previewBody
}

View File

@@ -9,18 +9,21 @@ import (
"log"
"net/http"
"net/url"
"os"
"github.com/sailpoint-oss/sp-cli/client"
"github.com/sailpoint-oss/sp-cli/util"
"github.com/spf13/cobra"
)
var implicitInput bool
func newPreviewCmd(client client.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "preview -i <identity-profile-id> -a <attribute-name> -n <transform-name>",
Use: "preview -i <identity-profile-id> -a <attribute-name> --implicit <transform data>",
Short: "Preview transform",
Long: "Preview the final output of a transform",
Example: "sp transforms preview -i 12a199b967b64ffe992ef4ecfd076728 -a lastname -n ToLower",
Example: "sp transforms preview -i 12a199b967b64ffe992ef4ecfd076728 -a lastname < /path/to/transform.json",
Aliases: []string{"p"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -34,9 +37,13 @@ func newPreviewCmd(client client.Client) *cobra.Command {
return fmt.Errorf("attribute must be specified")
}
name := cmd.Flags().Lookup("name").Value.String()
if name == "" {
return fmt.Errorf("name must be specified")
var transform map[string]interface{}
if !implicitInput {
err := json.NewDecoder(os.Stdin).Decode(&transform)
if err != nil {
log.Fatal(err)
}
}
// Get the identity profile so we can obtain the authoritative source and
@@ -109,7 +116,10 @@ func newPreviewCmd(client client.Client) *cobra.Command {
// log.Fatal(err)
// }
// Form the request body that will be sent to the preview endpoint
var previewBodyRaw []byte
// If using implicit input, then attempt to grab the implicit
// input from the identity profile mapping.
if implicitInput {
var accountAttName string
var sourceName string
for _, t := range profile.IdentityAttributeConfig.AttributeTransforms {
@@ -130,16 +140,29 @@ func newPreviewCmd(client client.Client) *cobra.Command {
}
}
previewBody := makePreviewBody(attribute, name, accountAttName, sourceName)
name := cmd.Flags().Lookup("name").Value.String()
if name == "" {
return fmt.Errorf("The transform name must be specified when previewing with implicit input.")
}
raw, err = json.Marshal(previewBody)
previewBody := makePreviewBodyImplicit(attribute, name, accountAttName, sourceName)
previewBodyRaw, err = json.Marshal(previewBody)
if err != nil {
return err
}
} else {
previewBody := makePreviewBodyExplicit(attribute, transform)
previewBodyRaw, err = json.Marshal(previewBody)
if err != nil {
return err
}
}
// Call the preview endpoint to get the raw and transformed attribute values
endpoint = cmd.Flags().Lookup("preview-endpoint").Value.String()
resp, err = client.Post(cmd.Context(), util.ResourceUrl(endpoint, user[0].Id), "application/json", bytes.NewReader(raw))
resp, err = client.Post(cmd.Context(), util.ResourceUrl(endpoint, user[0].Id), "application/json", bytes.NewReader(previewBodyRaw))
if err != nil {
return err
}
@@ -175,12 +198,12 @@ func newPreviewCmd(client client.Client) *cobra.Command {
cmd.Flags().StringP("identity-profile", "i", "", "The GUID of an identity profile (required)")
cmd.Flags().StringP("attribute", "a", "", "Attribute name (required)")
cmd.Flags().StringP("name", "n", "", "Transform name (required)")
cmd.Flags().StringP("name", "n", "", "Transform name if using implicit input. The transform must be uploaded to IDN.")
cmd.Flags().BoolVar(&implicitInput, "implicit", false, "Use implicit input. Default is explicit input defined by the transform.")
// cmd.Flags().StringP("file", "f", "", "The path to the transform file (required)")
cmd.MarkFlagRequired("identity-profile")
cmd.MarkFlagRequired("attribute")
cmd.MarkFlagRequired("name")
// cmd.MarkFlagRequired("file")
return cmd

View File

@@ -27,9 +27,9 @@ func NewTransformCmd(client client.Client) *cobra.Command {
}
cmd.PersistentFlags().StringP("transforms-endpoint", "e", viper.GetString("baseurl")+transformsEndpoint, "Override transforms endpoint")
cmd.PersistentFlags().StringP("preview-endpoint", "", viper.GetString("baseurl")+previewEndpoint, "Override preview endpoint")
cmd.PersistentFlags().StringP("identity-profile-endpoint", "", viper.GetString("baseurl")+identityProfileEndpoint, "Override identity profile endpoint")
cmd.PersistentFlags().StringP("user-endpoint", "", viper.GetString("baseurl")+userEndpoint, "Override user endpoint")
cmd.PersistentFlags().String("preview-endpoint", viper.GetString("baseurl")+previewEndpoint, "Override preview endpoint")
cmd.PersistentFlags().String("identity-profile-endpoint", viper.GetString("baseurl")+identityProfileEndpoint, "Override identity profile endpoint")
cmd.PersistentFlags().String("user-endpoint", viper.GetString("baseurl")+userEndpoint, "Override user endpoint")
cmd.AddCommand(
newListCmd(client),