Skip to content

Commit

Permalink
feat: Optional services (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
maikelpoot authored Jun 25, 2024
1 parent b90883d commit d4e2087
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 34 additions & 15 deletions keyhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package keyhub
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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

}
15 changes: 8 additions & 7 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit d4e2087

Please sign in to comment.