Files
sailpoint-cli/internal/types/types.go
2023-01-28 13:55:49 -06:00

130 lines
3.3 KiB
Go

package types
import (
"errors"
"fmt"
"strings"
"time"
"github.com/fatih/color"
sailpointsdk "github.com/sailpoint-oss/golang-sdk/sdk-output/v3"
"github.com/spf13/viper"
)
type Variable struct {
Name string `json:"name"`
Prompt string `json:"prompt"`
}
type SearchTemplate struct {
Name string `json:"name"`
Description string `json:"description"`
Variables []Variable `json:"variables"`
SearchQuery sailpointsdk.Search1 `json:"searchQuery"`
Raw []byte
}
type Templates struct {
Templates []SearchTemplate `json:"templates"`
}
type Choice struct {
Title string
Description string
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
type Redirect struct {
Port int `mapstructure:"port"`
Path string `mapstructure:"path"`
}
type Token struct {
AccessToken string `mapstructure:"accesstoken"`
Expiry time.Time `mapstructure:"expiry"`
}
type OAuthConfig struct {
Tenant string `mapstructure:"tenant"`
AuthUrl string `mapstructure:"authurl"`
BaseUrl string `mapstructure:"baseurl"`
TokenUrl string `mapstructure:"tokenurl"`
Redirect Redirect `mapstructure:"redirect"`
ClientSecret string `mapstructure:"clientSecret"`
ClientID string `mapstructure:"clientid"`
Token Token `mapstructure:"token"`
}
type PatConfig struct {
Tenant string `mapstructure:"tenant"`
BaseUrl string `mapstructure:"baseurl"`
TokenUrl string `mapstructure:"tokenurl"`
ClientSecret string `mapstructure:"clientSecret"`
ClientID string `mapstructure:"clientid"`
Token Token `mapstructure:"token"`
}
type OrgConfig struct {
Pat PatConfig `mapstructure:"pat"`
OAuth OAuthConfig `mapstructure:"oauth"`
AuthType string `mapstructure:"authtype"`
CustomSearchTemplatesPath string `mapstructure:"customSearchTemplatesPath"`
Debug bool `mapstructure:"debug"`
}
func (c OrgConfig) Validate() error {
debug := viper.GetBool("debug")
switch strings.ToLower(c.AuthType) {
case "pat":
if c.Pat.TokenUrl == "" {
return fmt.Errorf("missing PAT TokenURL configuration value")
}
if c.Pat.ClientID == "" {
return fmt.Errorf("missing PAT ClientID configuration value")
}
if c.Pat.ClientSecret == "" {
return fmt.Errorf("missing PAT ClientSecret configuration value")
}
return nil
case "oauth":
if c.OAuth.AuthUrl == "" {
return fmt.Errorf("missing OAuth URL configuration value")
}
if c.OAuth.ClientID == "" {
return fmt.Errorf("missing OAuth ClientID configuration value")
}
if c.OAuth.ClientSecret == "" && debug {
color.Yellow("missing OAuth ClientSecret configuration value")
}
if c.OAuth.Redirect.Path == "" {
return fmt.Errorf("missing OAuth Redirect Path configuration value")
}
if c.OAuth.Redirect.Port == 0 {
return fmt.Errorf("missing OAuth Redirect Port configuration value")
}
if c.OAuth.TokenUrl == "" {
return fmt.Errorf("missing OAuth TokenUrl configuration value")
}
return nil
default:
return errors.New("configured authtype is invalid or missing")
}
}
type DevNull interface {
}
func Filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}