Skip to content

Commit

Permalink
Merge pull request #122 from hashicorp/f-org-detect
Browse files Browse the repository at this point in the history
Improved Organization detection
  • Loading branch information
dadgar authored Jun 24, 2024
2 parents 50168de + 5ca2b0f commit 35825cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/122.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
Run auto-detection of organization ID even if project ID is configured in the profile.
```
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ func loadProfile(ctx context.Context, iam iam_service.ClientService, tokenSource
}
}

// If the profile has an org/project, or we don't have a valid access
// If the profile has an org, or we don't have a valid access
// token, skip trying to infer the organization and project.
tkn, err := tokenSource.Token()
if p.OrganizationID != "" || p.ProjectID != "" || err != nil || !tkn.Expiry.After(time.Now()) {
if p.OrganizationID != "" || err != nil || !tkn.Expiry.After(time.Now()) {
return p, nil
}

Expand All @@ -210,9 +210,13 @@ func loadProfile(ctx context.Context, iam iam_service.ClientService, tokenSource
return p, nil
}

// Set the organization and project. Project may be empty.
// Set the organization.
p.OrganizationID = ident.Payload.Principal.Service.OrganizationID
p.ProjectID = ident.Payload.Principal.Service.ProjectID

// Only set the project if it is not already set.
if p.ProjectID == "" {
p.ProjectID = ident.Payload.Principal.Service.ProjectID
}

// Save the profile.
if err := p.Write(); err != nil {
Expand Down

0 comments on commit 35825cd

Please sign in to comment.