Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom url flag #18

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cmd/claim/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCmdAddClaim(authServiceFactory *cmdutil.AuthServiceFactory, serviceFacto
if err != nil {
return err
}
service, err := serviceFactory.CreateService("claim", env, authService)
service, err := serviceFactory.CreateService("claim", env, url, authService)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/claim/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"snd-cli/pkg/cmdutil"
)

var env, authProvider, userId, claimProvider string
const boxerClaimURL = "https://boxer-claim.%s.sneaksanddata.com"

var env, url, authProvider, userId, claimProvider string

type Service interface {
AddClaim(user string, provider string, claims []string) (string, error)
Expand Down Expand Up @@ -38,6 +40,7 @@ func NewCmdClaim(serviceFactory cmdutil.ServiceFactory, authServiceFactory *cmdu
cmd.PersistentFlags().StringVarP(&authProvider, "auth-provider", "a", "azuread", "Specify the OAuth provider name")
cmd.PersistentFlags().StringVarP(&userId, "user", "u", "", "Specify the user ID")
cmd.PersistentFlags().StringVarP(&claimProvider, "claims-provider", "", "", "Specify the claim provider")
cmd.PersistentFlags().StringVarP(&url, "custom-service-url", "", boxerClaimURL, "Specify the service url")

cmd.AddCommand(NewCmdUser(authServiceFactory, serviceFactory))
cmd.AddCommand(NewCmdRemoveClaim(authServiceFactory, serviceFactory))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/claim/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewCmdGetClaim(authServiceFactory *cmdutil.AuthServiceFactory, serviceFacto
if err != nil {
return err
}
service, err := serviceFactory.CreateService("claim", env, authService)
service, err := serviceFactory.CreateService("claim", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/claim/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewCmdRemoveClaim(authServiceFactory *cmdutil.AuthServiceFactory, serviceFa
if err != nil {
return err
}
service, err := serviceFactory.CreateService("claim", env, authService)
service, err := serviceFactory.CreateService("claim", env, url, authService)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/claim/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewCmdAddUser(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactor
if err != nil {
return err
}
service, err := serviceFactory.CreateService("claim", env, authService)
service, err := serviceFactory.CreateService("claim", env, url, authService)
if err != nil {
return err
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func NewCmdRemoveUser(authServiceFactory *cmdutil.AuthServiceFactory, serviceFac
if err != nil {
return err
}
service, err := serviceFactory.CreateService("claim", env, authService)
service, err := serviceFactory.CreateService("claim", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/ml/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCmdGet(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactory cm
if err != nil {
return err
}
service, err := serviceFactory.CreateService("algorithm", env, authService)
service, err := serviceFactory.CreateService("algorithm", env, url, authService)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/ml/ml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"snd-cli/pkg/cmdutil"
)

var env, authProvider, algorithm string
const crystalURL = "https://crystal.%s.sneaksanddata.com"

var env, url, authProvider, algorithm string

type Service interface {
RetrieveRun(runID string, algorithmName string) (string, error)
Expand All @@ -34,6 +36,7 @@ func NewCmdAlgorithm(serviceFactory cmdutil.ServiceFactory, authServiceFactory *
cmd.PersistentFlags().StringVarP(&env, "env", "e", "test", "Target environment")
cmd.PersistentFlags().StringVarP(&authProvider, "auth-provider", "a", "azuread", "Specify the OAuth provider name")
cmd.PersistentFlags().StringVarP(&algorithm, "algorithm", "", "", "Specify the algorithm name")
cmd.PersistentFlags().StringVarP(&url, "custom-service-url", "", crystalURL, "Specify the service url")

cmd.AddCommand(NewCmdGet(authServiceFactory, serviceFactory))
cmd.AddCommand(NewCmdRun(authServiceFactory, serviceFactory))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/ml/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCmdRun(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactory cm
if err != nil {
return err
}
service, err := serviceFactory.CreateService("algorithm", env, authService)
service, err := serviceFactory.CreateService("algorithm", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCmdConfiguration(authServiceFactory *cmdutil.AuthServiceFactory, service
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCmdEncrypt(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactor
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewCmdLogs(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactory c
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/requestStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewCmdRequestStatus(authServiceFactory *cmdutil.AuthServiceFactory, service
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/runtimeInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCmdRuntimeInfo(authServiceFactory *cmdutil.AuthServiceFactory, serviceFa
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/spark/spark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"snd-cli/pkg/cmdutil"
)

var env, authProvider, id string
const beastURL = "https://beast-v3.%s.sneaksanddata.com"

var env, url, authProvider, id string

type Service interface {
GetConfiguration(name string) (spark.SubmissionConfiguration, error)
Expand All @@ -34,6 +36,7 @@ func NewCmdSpark(serviceFactory cmdutil.ServiceFactory, authServiceFactory *cmdu
cmd.PersistentFlags().StringVarP(&env, "env", "e", "test", "Target environment")
cmd.PersistentFlags().StringVarP(&authProvider, "auth-provider", "a", "azuread", "Specify the OAuth provider name")
cmd.PersistentFlags().StringVarP(&id, "id", "i", "", "Specify the Job ID")
cmd.PersistentFlags().StringVarP(&url, "custom-service-url", "", beastURL, "Specify the service url")

cmd.AddCommand(NewCmdSubmit(authServiceFactory, serviceFactory))
cmd.AddCommand(NewCmdRuntimeInfo(authServiceFactory, serviceFactory))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/spark/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewCmdSubmit(authServiceFactory *cmdutil.AuthServiceFactory, serviceFactory
if err != nil {
return err
}
service, err := serviceFactory.CreateService("spark", env, authService)
service, err := serviceFactory.CreateService("spark", env, url, authService)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/util/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"snd-cli/pkg/cmd/util/file"
"time"
Expand Down Expand Up @@ -97,13 +96,13 @@ func (p *Provider) getTokenFromCache() error {
// updates the token and its TTL, caches the new token, and returns it.
func (p *Provider) GetToken() (string, error) {
if p.token == "" || time.Now().After(p.ttl) {
log.Println("Reading token from cache")
// log.Println("Reading token from cache")
if err := p.getTokenFromCache(); err == nil {
return p.token, nil
}
}
// Either cache is empty, or token is expired, fetch a new one.
log.Println("Cached token not existent or expired, retrieving new token")
// log.Println("Cached token not existent or expired, retrieving new token")
token, err := p.authService.GetBoxerToken()
if err != nil {
return "", err
Expand All @@ -115,5 +114,4 @@ func (p *Provider) GetToken() (string, error) {
}

return p.token, nil

}
4 changes: 2 additions & 2 deletions pkg/cmd/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func CheckIfNewVersionIsAvailable() error {
}
result := semver.Compare(lastTag, currentVersion)
if result > 0 {
fmt.Printf("New version available. Please upgrade.\nCurrent version: %s\nAvailable version: %s\nPlease run `snd upgrade` command to update the CLI to the latest version.", currentVersion, lastTag)
fmt.Printf("New version available. Please upgrade.\nCurrent version: %s\nLast available version: %s\nPlease run `snd upgrade` command to update the CLI to the latest version.\n", currentVersion, lastTag)
} else if result < 0 {
fmt.Printf("Your version is newer than the one present in GitHub release.\nCurrent version: %s\nLast available version in GitHub release: %s", currentVersion, lastTag)
fmt.Printf("Your version is newer than the one present in GitHub release.\nCurrent version: %s\nLast available version in GitHub release: %s\n", currentVersion, lastTag)
} else {
fmt.Printf("The snd version is up to date. %s", currentVersion)
}
Expand Down
37 changes: 23 additions & 14 deletions pkg/cmdutil/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
)

const boxerURL = "https://boxer.%s.sneaksanddata.com"
const boxerClaimURL = "https://boxer-claim.%s.sneaksanddata.com"
const crystalURL = "https://crystal.%s.sneaksanddata.com"
const beastURL = "https://beast-v3.%s.sneaksanddata.com"

// AuthServiceFactory is responsible for creating instances of AuthService.
// It encapsulates the logic required to configure and instantiate an AuthService.
Expand Down Expand Up @@ -42,7 +39,7 @@ func (f *AuthServiceFactory) CreateAuthService(env, provider string) (*auth.Serv

// ServiceFactory defines an interface for factories capable of creating different types of services.
type ServiceFactory interface {
CreateService(serviceType string, env string, authService token.AuthService) (interface{}, error)
CreateService(serviceType, env, serviceUrl string, authService token.AuthService) (interface{}, error)
}

// ConcreteServiceFactory implements the ServiceFactory interface, providing concrete logic to create specific
Expand All @@ -69,26 +66,27 @@ func NewConcreteServiceFactory() *ConcreteServiceFactory {
//
// An interface{} representing the created service, which should be type-asserted to the appropriate service type.
// An error if the service creation fails or if an unknown service type is specified.
func (f *ConcreteServiceFactory) CreateService(serviceType, env string, authService token.AuthService) (interface{}, error) {
func (f *ConcreteServiceFactory) CreateService(serviceType, env, serviceUrl string, authService token.AuthService) (interface{}, error) {
switch serviceType {
case "claim":
return initClaimService(env, authService)
return initClaimService(env, serviceUrl, authService)
case "algorithm":
return initAlgorithmService(env, authService)
return initAlgorithmService(env, serviceUrl, authService)
case "spark":
return initSparkService(env, authService)
return initSparkService(env, serviceUrl, authService)
default:
return nil, fmt.Errorf("unknown service type: %s", serviceType)
}
}

func initClaimService(env string, authService token.AuthService) (*claim.Service, error) {
func initClaimService(env, boxerClaimURL string, authService token.AuthService) (*claim.Service, error) {
tp, err := token.NewProvider(authService)
if err != nil {
return nil, fmt.Errorf("unable to create token provider: %w", err)
}
url := processURL(boxerClaimURL, env)
config := claim.Config{
ClaimURL: fmt.Sprintf(boxerClaimURL, env),
ClaimURL: url,
GetTokenFunc: tp.GetToken,
}
claimService, err := claim.New(config)
Expand All @@ -98,13 +96,14 @@ func initClaimService(env string, authService token.AuthService) (*claim.Service
return claimService, nil
}

func initAlgorithmService(env string, authService token.AuthService) (*algorithm.Service, error) {
func initAlgorithmService(env, crystalURL string, authService token.AuthService) (*algorithm.Service, error) {
tp, err := token.NewProvider(authService)
if err != nil {
return nil, fmt.Errorf("unable to create token provider: %w", err)
}
url := processURL(crystalURL, env)
config := algorithm.Config{
SchedulerURL: fmt.Sprintf(crystalURL, env),
SchedulerURL: url,
APIVersion: "v1.2",
GetTokenFunc: tp.GetToken,
}
Expand All @@ -116,13 +115,14 @@ func initAlgorithmService(env string, authService token.AuthService) (*algorithm
return algorithmService, nil
}

func initSparkService(env string, authService token.AuthService) (*spark.Service, error) {
func initSparkService(env, beastURL string, authService token.AuthService) (*spark.Service, error) {
tp, err := token.NewProvider(authService)
if err != nil {
return nil, fmt.Errorf("unable to create token provider: %w", err)
}
url := processURL(beastURL, env)
config := spark.Config{
BaseURL: fmt.Sprintf(beastURL, env),
BaseURL: url,
GetTokenFunc: tp.GetToken,
}

Expand All @@ -131,5 +131,14 @@ func initSparkService(env string, authService token.AuthService) (*spark.Service
return nil, fmt.Errorf("failed to create spark service: %w", err)
}
return sparkService, nil
}

func processURL(url, env string) string {
var s1, s2 string
_, err := fmt.Sscanf(url, "%s%s", &s1, &s2)
if err == nil {
url = fmt.Sprintf(url, env)
return url
}
return url
}
Loading