validate moved to initapiclient defaults restored

This commit is contained in:
luke-hagar-sp
2023-02-24 10:02:30 -06:00
parent 1256461895
commit 64a694ecaf
17 changed files with 92 additions and 21 deletions

View File

@@ -23,6 +23,11 @@ func NewEnvironmentCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := config.InitConfig()
if err != nil {
return err
}
environments := config.GetEnvironments() environments := config.GetEnvironments()
envKeys := maps.Keys(environments) envKeys := maps.Keys(environments)

View File

@@ -30,7 +30,10 @@ func newQueryCmd() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
searchQuery = args[0] searchQuery = args[0]
fmt.Println(searchQuery) fmt.Println(searchQuery)

View File

@@ -33,7 +33,10 @@ func newTemplateCmd() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
var selectedTemplate templates.SearchTemplate var selectedTemplate templates.SearchTemplate
searchTemplates, err := templates.GetSearchTemplates() searchTemplates, err := templates.GetSearchTemplates()

View File

@@ -32,10 +32,14 @@ func newAuthCommand() *cobra.Command {
Example: "sail auth pat | sail auth pat | sail auth pipeline", Example: "sail auth pat | sail auth pat | sail auth pipeline",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var selection string var selection string
var err error var err error
err = config.InitConfig()
if err != nil {
return err
}
if len(args) > 0 { if len(args) > 0 {
selection = args[0] selection = args[0]
} else { } else {

View File

@@ -3,6 +3,7 @@ package set
import ( import (
"strings" "strings"
"github.com/sailpoint-oss/sailpoint-cli/internal/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@@ -16,6 +17,11 @@ func newDebugCommand() *cobra.Command {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := config.InitConfig()
if err != nil {
return err
}
switch strings.ToLower(args[0]) { switch strings.ToLower(args[0]) {
case "enable": case "enable":
viper.Set("debug", true) viper.Set("debug", true)

View File

@@ -15,6 +15,11 @@ func newExportTemplateCommand() *cobra.Command {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := config.InitConfig()
if err != nil {
return err
}
config.SetCustomExportTemplatePath(args[0]) config.SetCustomExportTemplatePath(args[0])
return nil return nil

View File

@@ -15,6 +15,11 @@ func newSearchTemplateCommand() *cobra.Command {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := config.InitConfig()
if err != nil {
return err
}
config.SetCustomSearchTemplatePath(args[0]) config.SetCustomSearchTemplatePath(args[0])
return nil return nil

View File

@@ -32,7 +32,10 @@ func newExportCmd() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
ctx := context.TODO() ctx := context.TODO()

View File

@@ -26,7 +26,10 @@ func newImportCommand() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
ctx := context.TODO() ctx := context.TODO()

View File

@@ -26,7 +26,10 @@ func newExportStatusCmd() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
for i := 0; i < len(exportJobs); i++ { for i := 0; i < len(exportJobs); i++ {
job := exportJobs[i] job := exportJobs[i]

View File

@@ -35,7 +35,10 @@ func newTemplateCmd() *cobra.Command {
return err return err
} }
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
if folderPath == "" { if folderPath == "" {
folderPath = "search_results" folderPath = "search_results"

View File

@@ -59,7 +59,10 @@ func newCreateCmd() *cobra.Command {
transform := sailpointsdk.NewTransform(data["name"].(string), data["type"].(string), data["attributes"].(map[string]interface{})) transform := sailpointsdk.NewTransform(data["name"].(string), data["type"].(string), data["attributes"].(map[string]interface{}))
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
transformObj, resp, err := apiClient.V3.TransformsApi.CreateTransform(context.TODO()).Transform(*transform).Execute() transformObj, resp, err := apiClient.V3.TransformsApi.CreateTransform(context.TODO()).Transform(*transform).Execute()
if err != nil { if err != nil {

View File

@@ -90,8 +90,12 @@ func newDeleteCmd() *cobra.Command {
transformID := id[i] transformID := id[i]
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
_, err := apiClient.V3.TransformsApi.DeleteTransform(context.TODO(), transformID).Execute() if err != nil {
return err
}
_, err = apiClient.V3.TransformsApi.DeleteTransform(context.TODO(), transformID).Execute()
if err != nil { if err != nil {
return err return err
} }

View File

@@ -57,7 +57,11 @@ func newUpdateCmd() *cobra.Command {
transform := sailpointsdk.NewTransform(data["name"].(string), data["type"].(string), data["attributes"].(map[string]interface{})) transform := sailpointsdk.NewTransform(data["name"].(string), data["type"].(string), data["attributes"].(map[string]interface{}))
apiClient := config.InitAPIClient() apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
_, resp, err := apiClient.V3.TransformsApi.UpdateTransform(context.TODO(), id).Transform(*transform).Execute() _, resp, err := apiClient.V3.TransformsApi.UpdateTransform(context.TODO(), id).Transform(*transform).Execute()
if err != nil { if err != nil {
return sdk.HandleSDKError(resp, err) return sdk.HandleSDKError(resp, err)

View File

@@ -113,6 +113,11 @@ func InitConfig() error {
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.SetEnvPrefix("sail") viper.SetEnvPrefix("sail")
viper.SetDefault("authtype", "pat")
viper.SetDefault("customexporttemplatespath", "")
viper.SetDefault("customsearchtemplatespath", "")
viper.SetDefault("debug", false)
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
@@ -125,29 +130,31 @@ func InitConfig() error {
} }
} }
err = Validate()
if err != nil {
return err
}
return nil return nil
} }
func InitAPIClient() *sailpoint.APIClient { func InitAPIClient() (*sailpoint.APIClient, error) {
var apiClient *sailpoint.APIClient
err := Validate()
if err != nil {
return apiClient, err
}
token, err := GetAuthToken() token, err := GetAuthToken()
if err != nil && GetDebug() { if err != nil && GetDebug() {
color.Yellow("unable to retrieve accesstoken: %s ", err) color.Yellow("unable to retrieve accesstoken: %s ", err)
} }
configuration := sailpoint.NewConfiguration(sailpoint.ClientConfiguration{Token: token, BaseURL: GetBaseUrl()}) configuration := sailpoint.NewConfiguration(sailpoint.ClientConfiguration{Token: token, BaseURL: GetBaseUrl()})
apiClient := sailpoint.NewAPIClient(configuration) apiClient = sailpoint.NewAPIClient(configuration)
if !GetDebug() { if !GetDebug() {
var DevNull types.DevNull var DevNull types.DevNull
apiClient.V3.GetConfig().HTTPClient.Logger = DevNull apiClient.V3.GetConfig().HTTPClient.Logger = DevNull
apiClient.Beta.GetConfig().HTTPClient.Logger = DevNull apiClient.Beta.GetConfig().HTTPClient.Logger = DevNull
} }
return apiClient return apiClient, nil
} }
func GetAuthToken() (string, error) { func GetAuthToken() (string, error) {

View File

@@ -16,7 +16,11 @@ func PrintJob(job sailpointbetasdk.SpConfigJob) {
} }
func DownloadExport(jobId string, fileName string, folderPath string) error { func DownloadExport(jobId string, fileName string, folderPath string) error {
apiClient := config.InitAPIClient()
apiClient, err := config.InitAPIClient()
if err != nil {
return err
}
for { for {
response, _, err := apiClient.Beta.SPConfigApi.ExportSpConfigJobStatus(context.TODO(), jobId).Execute() response, _, err := apiClient.Beta.SPConfigApi.ExportSpConfigJobStatus(context.TODO(), jobId).Execute()

View File

@@ -13,7 +13,13 @@ import (
) )
func GetTransforms() ([]sailpointsdk.Transform, error) { func GetTransforms() ([]sailpointsdk.Transform, error) {
apiClient := config.InitAPIClient() var transforms []sailpointsdk.Transform
apiClient, err := config.InitAPIClient()
if err != nil {
return transforms, err
}
transforms, resp, err := sailpoint.PaginateWithDefaults[sailpointsdk.Transform](apiClient.V3.TransformsApi.ListTransforms(context.TODO())) transforms, resp, err := sailpoint.PaginateWithDefaults[sailpointsdk.Transform](apiClient.V3.TransformsApi.ListTransforms(context.TODO()))
if err != nil { if err != nil {
return transforms, sdk.HandleSDKError(resp, err) return transforms, sdk.HandleSDKError(resp, err)