Skip to content

Commit

Permalink
Merge pull request #659 from synfinatic/fix-threads
Browse files Browse the repository at this point in the history
Fix threads
  • Loading branch information
synfinatic authored Nov 1, 2023
2 parents 4f50590 + dd07806 commit cbc8219
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Bugs

* No longer ignore the `--threads` CLI option

### New Features

* Require running `aws-sso login`. No more auto-login. #291
Expand All @@ -20,6 +24,7 @@
* Remove `--no-cache` flag
* Remove `CacheRefresh` configuration option. `aws-sso` will only update the cache
when you ask it to via `aws-sso cache`
* Remove `tags --force-update` flag

## [v1.14.2] - 2023-10-19

Expand Down
2 changes: 1 addition & 1 deletion cmd/aws-sso/cache_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (cc *CacheCmd) Run(ctx *RunContext) error {
log.Fatalf(err.Error())
}

err = ctx.Settings.Cache.Refresh(AwsSSO, s, ssoName)
err = ctx.Settings.Cache.Refresh(AwsSSO, s, ssoName, ctx.Cli.Cache.Threads)
if err != nil {
return fmt.Errorf("Unable to refresh role cache: %s", err.Error())
}
Expand Down
23 changes: 2 additions & 21 deletions cmd/aws-sso/tags_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,12 @@ import (
)

type TagsCmd struct {
AccountId int64 `kong:"name='account',short='A',help='Filter results based on AWS AccountID'"`
Role string `kong:"short='R',help='Filter results based on AWS Role Name'"`
ForceUpdate bool `kong:"help='Force account/role cache update'"`
AccountId int64 `kong:"name='account',short='A',help='Filter results based on AWS AccountID'"`
Role string `kong:"short='R',help='Filter results based on AWS Role Name'"`
}

func (cc *TagsCmd) Run(ctx *RunContext) error {
set := ctx.Settings
cache := ctx.Settings.Cache.GetSSO()
if ctx.Cli.Tags.ForceUpdate {
s := set.SSO[ctx.Cli.SSO]

ssoName, err := ctx.Settings.GetSelectedSSOName(ctx.Cli.SSO)
if err != nil {
log.Fatalf(err.Error())
}

err = set.Cache.Refresh(AwsSSO, s, ssoName)
if err != nil {
log.WithError(err).Fatalf("Unable to refresh role cache")
}
err = set.Cache.Save(true)
if err != nil {
log.WithError(err).Errorf("Unable to save cache")
}
}
roles := []*sso.AWSRoleFlat{}

// If user has specified an account (or account + role) then limit
Expand Down
16 changes: 8 additions & 8 deletions sso/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (c *Cache) deleteOldHistory() {

// Refresh updates our cached Roles based on AWS SSO & our Config
// but does not save this data!
func (c *Cache) Refresh(sso *AWSSSO, config *SSOConfig, ssoName string) error {
func (c *Cache) Refresh(sso *AWSSSO, config *SSOConfig, ssoName string, threads int) error {
// Only refresh once per execution
if c.refreshed {
return nil
Expand Down Expand Up @@ -281,7 +281,7 @@ func (c *Cache) Refresh(sso *AWSSSO, config *SSOConfig, ssoName string) error {
c.SSO[ssoName].Roles = &Roles{}

// load our AWSSSO & Config
r, err := c.NewRoles(sso, config)
r, err := c.NewRoles(sso, config, threads)
if err != nil {
return err
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func (c *Cache) GetRole(arn string) (*AWSRoleFlat, error) {

// Merges the AWS SSO and our Config file to create our Roles struct
// which is defined in cache_roles.go
func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig, threads int) (*Roles, error) {
r := Roles{
SSORegion: config.SSORegion,
StartUrl: config.StartUrl,
Expand All @@ -402,7 +402,7 @@ func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
ssoName: config.settings.DefaultSSO,
}

if err := c.addSSORoles(&r, as); err != nil {
if err := c.addSSORoles(&r, as, threads); err != nil {
return &Roles{}, err
}

Expand Down Expand Up @@ -474,7 +474,7 @@ func processSSORoles(roles []RoleInfo, cache *SSOCache, r *Roles) {
}

// addSSORoles retrieves all the SSO Roles from AWS SSO and places them in r
func (c *Cache) addSSORoles(r *Roles, as *AWSSSO) error {
func (c *Cache) addSSORoles(r *Roles, as *AWSSSO, threads int) error {
cache := c.GetSSO()

accounts, err := as.GetAccounts()
Expand All @@ -494,9 +494,9 @@ func (c *Cache) addSSORoles(r *Roles, as *AWSSSO) error {
// Per #448, doing this serially is too slow for many accounts. Hence,
// we'll use a worker pool.
if len(accounts) > 0 {
workers := 1
if c.settings.Threads > 0 {
workers = c.settings.Threads
workers := c.settings.Threads
if threads > 0 {
workers = threads
}
if workers > len(accounts) {
workers = len(accounts)
Expand Down

0 comments on commit cbc8219

Please sign in to comment.