Files
sailpoint-cli/cmd/transform/create.go
luke-hagar-sp 53db83cde7 Help Cleanup, Flag Corrections, Spelling, Descriptions, Whitespace formatting
Added Wait flag for SPConfig Import
Implemented Debug Logging
Adding warnings and confirm for printing or erasing environments
Corrected and Normalized Command Help Sections
Swapped GetDebug If Checks to Log.Debug() calls
Corrected Wrong command examples, added new examples
Fixed some function names that were incorrect
Adjusted Readme Download counter
2023-04-13 10:46:39 -05:00

78 lines
2.0 KiB
Go

// Copyright (c) 2021, SailPoint Technologies, Inc. All rights reserved.
package transform
import (
"context"
"encoding/json"
"fmt"
"os"
sailpointsdk "github.com/sailpoint-oss/golang-sdk/v3"
"github.com/sailpoint-oss/sailpoint-cli/internal/config"
"github.com/sailpoint-oss/sailpoint-cli/internal/sdk"
"github.com/spf13/cobra"
)
func newCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create an IdentityNow Transform from a file",
Long: "\nCreate an IdentityNow Transform from a file\n\n",
Example: "sail transform c -f /path/to/transform.json\nsail transform c < /path/to/transform.json\necho /path/to/transform.json | sail transform c",
Aliases: []string{"c"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var data map[string]interface{}
filepath := cmd.Flags().Lookup("file").Value.String()
if filepath != "" {
file, err := os.Open(filepath)
if err != nil {
return err
}
defer file.Close()
err = json.NewDecoder(file).Decode(&data)
if err != nil {
return err
}
} else {
err := json.NewDecoder(os.Stdin).Decode(&data)
if err != nil {
return err
}
}
if data["name"] == nil {
return fmt.Errorf("the transform must have a name")
}
if data["id"] != nil {
return fmt.Errorf("the transform cannot have an ID")
}
transform := sailpointsdk.NewTransform(data["name"].(string), data["type"].(string), data["attributes"].(map[string]interface{}))
apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
transformObj, resp, err := apiClient.V3.TransformsApi.CreateTransform(context.TODO()).Transform(*transform).Execute()
if err != nil {
return sdk.HandleSDKError(resp, err)
}
config.Log.Info("Transform created successfully")
cmd.Print(*transformObj.Id)
return nil
},
}
cmd.Flags().StringP("file", "f", "", "The path to the transform file")
return cmd
}