diff --git a/CHANGELOG.md b/CHANGELOG.md index f303302..d290854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Issue # : Optional services based on supported version of keyhub + ## [1.3.2] - 2024-06-12 ### Changed diff --git a/keyhub.go b/keyhub.go index c61d0e1..60dd65a 100644 --- a/keyhub.go +++ b/keyhub.go @@ -18,6 +18,7 @@ package keyhub import ( "bytes" "encoding/json" + "fmt" "io" "net/http" "time" @@ -40,6 +41,7 @@ type Client struct { Vaults *VaultService ServiceAccounts *ServiceAccountService LaunchPadTile *LaunchPadTileService + VersionErrors []error } // khJsonBodyProvider encodes a JSON tagged struct value as a Body for requests. @@ -74,23 +76,35 @@ func NewClientDefault(issuer string, clientID string, clientSecret string) (*Cli func NewClient(httpClient *http.Client, issuer string, clientID string, clientSecret string) (*Client, error) { var err error + var baseVersionedSupported bool + var newerVersionedSupported bool base := sling.New().Client(httpClient).Base(issuer) versionService := newVersionService(base.New().Set("Accept", "application/json").Set("Content-Type", "application/json")) + newClient := &Client{ + ID: clientID, + Version: versionService, + VersionErrors: make([]error, 0), + } + // Create sling for contract version 60 baseVersionedSling := base.New() - _, err = versionService.CheckAndUpdateVersionedSling(60, baseVersionedSling) + baseVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(60, baseVersionedSling) if err != nil { - return nil, err + newClient.VersionErrors = append(newClient.VersionErrors, err) } // Create sling for contract version 71 newerVersionedSling := base.New() - _, err = versionService.CheckAndUpdateVersionedSling(71, newerVersionedSling) + newerVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(71, newerVersionedSling) if err != nil { - return nil, err + newClient.VersionErrors = append(newClient.VersionErrors, err) + } + + if !(baseVersionedSupported && newerVersionedSupported) { + return nil, fmt.Errorf("KeyHub %v does not support api contract versions 60 or 71", versionService.info.KeyhubVersion) } ctx := oidc.ClientContext(context.Background(), httpClient) @@ -115,15 +129,20 @@ func NewClient(httpClient *http.Client, issuer string, clientID string, clientSe Base: oauth2Client.Transport, }, } - return &Client{ - ID: clientID, - Version: versionService, - Accounts: newAccountService(oauth2Sling.New()), - ClientApplications: newClientApplicationService(oauth2Sling.New()), - Groups: newGroupService(newerVersionedSling.New().Client(oauth2Client)), - Systems: newSystemService(oauth2Sling.New()), - LaunchPadTile: newLaunchPadTileService(oauth2Sling.New()), - Vaults: newVaultService(newerVersionedSling.New().Client(vaultClient)), - ServiceAccounts: NewServiceAccountService(oauth2Sling), - }, nil + + if baseVersionedSupported { + newClient.Accounts = newAccountService(oauth2Sling.New()) + newClient.ClientApplications = newClientApplicationService(oauth2Sling.New()) + newClient.Systems = newSystemService(oauth2Sling.New()) + newClient.LaunchPadTile = newLaunchPadTileService(oauth2Sling.New()) + newClient.ServiceAccounts = NewServiceAccountService(oauth2Sling) + } + + if newerVersionedSupported { + newClient.Groups = newGroupService(newerVersionedSling.New().Client(oauth2Client)) + newClient.Vaults = newVaultService(newerVersionedSling.New().Client(vaultClient)) + } + + return newClient, nil + } diff --git a/version.go b/version.go index 6110bff..250f48e 100644 --- a/version.go +++ b/version.go @@ -59,26 +59,27 @@ func (s *VersionService) Get() (v *model.VersionInfo, err error) { return results, nil } -func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.Sling) (headerVersion string, err error) { +func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.Sling) (isSupported bool, err error) { + isSupported = false + var headerVersion string if s.info == nil { s.info, err = s.Get() if err != nil { - return "", err + return } } if version > 0 { - isContractVersionSupported := false for _, contractVersion := range s.info.ContractVersions { if version == contractVersion { - isContractVersionSupported = true + isSupported = true break } } - if !isContractVersionSupported { - return "", fmt.Errorf("KeyHub %v does not support api contract version %v", s.info.KeyhubVersion, version) + if !isSupported { + return isSupported, fmt.Errorf("KeyHub %v does not support api contract version %v", s.info.KeyhubVersion, version) } headerVersion = fmt.Sprintf("%d", version) @@ -89,5 +90,5 @@ func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.S base.Set("Accept", fmt.Sprintf("%v;version=%s", mediatype, headerVersion)) base.Set("Content-Type", fmt.Sprintf("%v;version=%s", mediatype, headerVersion)) - return + return isSupported, nil }