-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Prefer KV tags, even when tags are defined as set #35937
Open
brandonc
wants to merge
2
commits into
main
Choose a base branch
from
TF-21807-both-types-of-tags-are-duplicated-when-found-and-subsequently-updated-by-the-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−64
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"net/http" | ||
"net/url" | ||
"os" | ||
"slices" | ||
"sort" | ||
"strings" | ||
"sync" | ||
|
@@ -46,7 +47,7 @@ const ( | |
genericHostname = "localterraform.com" | ||
) | ||
|
||
var ErrCloudDoesNotSupportKVTags = errors.New("your version of Terraform Enterprise does not support key-value tags. Please upgrade to a version that supports this feature.") | ||
var ErrCloudDoesNotSupportKVTags = errors.New("your version of Terraform Enterprise does not support key-value tags. Please upgrade Terraform Enterprise to a version that supports this feature or use set type tags instead.") | ||
|
||
// Cloud is an implementation of EnhancedBackend in service of the HCP Terraform or Terraform Enterprise | ||
// integration for Terraform CLI. This backend is not intended to be surfaced at the user level and | ||
|
@@ -632,7 +633,18 @@ func (b *Cloud) Workspaces() ([]string, error) { | |
if b.WorkspaceMapping.Strategy() == WorkspaceTagsStrategy { | ||
options.Tags = strings.Join(b.WorkspaceMapping.TagsAsSet, ",") | ||
} else if b.WorkspaceMapping.Strategy() == WorkspaceKVTagsStrategy { | ||
options.TagBindings = b.WorkspaceMapping.tfeTagBindings() | ||
options.TagBindings = b.WorkspaceMapping.asTFETagBindings() | ||
|
||
// Populate keys, too, just in case backend does not support key/value tags. | ||
// The backend will end up applying both filters but that should always | ||
// be the same result set anyway. | ||
for _, tag := range options.TagBindings { | ||
if options.Tags != "" { | ||
options.Tags = options.Tags + "," | ||
} | ||
options.Tags = options.Tags + tag.Key | ||
} | ||
|
||
} | ||
log.Printf("[TRACE] cloud: Listing workspaces with tag bindings %q", b.WorkspaceMapping.DescribeTags()) | ||
|
||
|
@@ -760,7 +772,7 @@ func (b *Cloud) StateMgr(name string) (statemgr.Full, error) { | |
if b.WorkspaceMapping.Strategy() == WorkspaceTagsStrategy { | ||
workspaceCreateOptions.Tags = b.WorkspaceMapping.tfeTags() | ||
} else if b.WorkspaceMapping.Strategy() == WorkspaceKVTagsStrategy { | ||
workspaceCreateOptions.TagBindings = b.WorkspaceMapping.tfeTagBindings() | ||
workspaceCreateOptions.TagBindings = b.WorkspaceMapping.asTFETagBindings() | ||
} | ||
|
||
// Create project if not exists, otherwise use it | ||
|
@@ -813,26 +825,24 @@ func (b *Cloud) StateMgr(name string) (statemgr.Full, error) { | |
} | ||
} | ||
|
||
updateRequired, err := b.workspaceTagsRequireUpdate(context.Background(), workspace, b.WorkspaceMapping) | ||
if updateRequired { | ||
if err != nil { | ||
if errors.Is(err, tfe.ErrResourceNotFound) { | ||
return nil, fmt.Errorf("workspace %s does not exist or access to it is unauthorized", name) | ||
} else if errors.Is(err, ErrCloudDoesNotSupportKVTags) { | ||
return nil, err | ||
tagCheck, errFromTagCheck := b.workspaceTagsRequireUpdate(context.Background(), workspace, b.WorkspaceMapping) | ||
if tagCheck.requiresUpdate { | ||
if errFromTagCheck != nil { | ||
if errors.Is(errFromTagCheck, ErrCloudDoesNotSupportKVTags) { | ||
return nil, fmt.Errorf("backend does not support key/value tags. Try using key-only tags: %w", errFromTagCheck) | ||
} | ||
return nil, fmt.Errorf("error checking if workspace %s requires update: %w", name, err) | ||
} | ||
|
||
log.Printf("[TRACE] cloud: Updating tags for %s workspace %s/%s to %s", b.appName, b.Organization, name, b.WorkspaceMapping.DescribeTags()) | ||
if b.WorkspaceMapping.Strategy() == WorkspaceTagsStrategy { | ||
log.Printf("[TRACE] cloud: Updating tags for %s workspace %s/%s to %q", b.appName, b.Organization, name, b.WorkspaceMapping.DescribeTags()) | ||
// Always update using KV tags if possible | ||
if !tagCheck.supportsKVTags { | ||
options := tfe.WorkspaceAddTagsOptions{ | ||
Tags: b.WorkspaceMapping.tfeTags(), | ||
} | ||
err = b.client.Workspaces.AddTags(context.Background(), workspace.ID, options) | ||
} else if b.WorkspaceMapping.Strategy() == WorkspaceKVTagsStrategy { | ||
} else { | ||
options := tfe.WorkspaceAddTagBindingsOptions{ | ||
TagBindings: b.WorkspaceMapping.tfeTagBindings(), | ||
TagBindings: b.WorkspaceMapping.asTFETagBindings(), | ||
} | ||
_, err = b.client.Workspaces.AddTagBindings(context.Background(), workspace.ID, options) | ||
} | ||
|
@@ -1160,39 +1170,75 @@ func (b *Cloud) cliColorize() *colorstring.Colorize { | |
} | ||
} | ||
|
||
func (b *Cloud) workspaceTagsRequireUpdate(ctx context.Context, workspace *tfe.Workspace, workspaceMapping WorkspaceMapping) (bool, error) { | ||
if workspaceMapping.Strategy() == WorkspaceTagsStrategy { | ||
existingTags := map[string]struct{}{} | ||
for _, t := range workspace.TagNames { | ||
existingTags[t] = struct{}{} | ||
} | ||
type tagRequiresUpdateResult struct { | ||
requiresUpdate bool | ||
supportsKVTags bool | ||
} | ||
|
||
for _, tag := range workspaceMapping.TagsAsSet { | ||
if _, ok := existingTags[tag]; !ok { | ||
return true, nil | ||
} | ||
} | ||
} else if workspaceMapping.Strategy() == WorkspaceKVTagsStrategy { | ||
existingTags := make(map[string]string) | ||
bindings, err := b.client.Workspaces.ListTagBindings(ctx, workspace.ID) | ||
if err != nil && err == tfe.ErrResourceNotFound { | ||
return true, ErrCloudDoesNotSupportKVTags | ||
} else if err != nil { | ||
return true, err | ||
} | ||
func (b *Cloud) workspaceTagsRequireUpdate(ctx context.Context, workspace *tfe.Workspace, workspaceMapping WorkspaceMapping) (result tagRequiresUpdateResult, err error) { | ||
result = tagRequiresUpdateResult{ | ||
supportsKVTags: true, | ||
} | ||
|
||
for _, binding := range bindings { | ||
existingTags[binding.Key] = binding.Value | ||
// First, depending on the strategy, build a map of the tags defined in config | ||
// so we can compare them to the actual tags on the workspace | ||
normalizedTagMap := make(map[string]string) | ||
if workspaceMapping.IsTagsStrategy() { | ||
for _, b := range workspaceMapping.asTFETagBindings() { | ||
normalizedTagMap[b.Key] = b.Value | ||
} | ||
|
||
for tag, val := range workspaceMapping.TagsAsMap { | ||
if existingVal, ok := existingTags[tag]; !ok || existingVal != val { | ||
return true, nil | ||
} else { | ||
// Not a tag strategy | ||
return | ||
} | ||
|
||
// Fetch tag bindings and determine if they should be checked | ||
bindings, err := b.client.Workspaces.ListTagBindings(ctx, workspace.ID) | ||
if err != nil && errors.Is(err, tfe.ErrResourceNotFound) { | ||
// By this time, the workspace should have been fetched, proving that the | ||
// authenticated user has access to it. If the tag bindings are not found, | ||
// it would mean that the backened does not support tag bindings. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: s/backened/backend/ |
||
result.supportsKVTags = false | ||
} else if err != nil { | ||
return | ||
} | ||
|
||
err = nil | ||
check: | ||
// Check desired workspace tags against existing tags | ||
for k, v := range normalizedTagMap { | ||
log.Printf("[TRACE] cloud: Checking tag %q=%q", k, v) | ||
if v == "" { | ||
// Tag can exist in legacy tags or tag bindings | ||
if !slices.Contains(workspace.TagNames, k) || (result.supportsKVTags && !slices.ContainsFunc(bindings, func(b *tfe.TagBinding) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting and assigning the slice functions locally or elsewhere, for readability? |
||
return b.Key == k | ||
})) { | ||
result.requiresUpdate = true | ||
break check | ||
} | ||
} else if !result.supportsKVTags { | ||
// There is a value defined, but the backend does not support tag bindings | ||
result.requiresUpdate = true | ||
err = ErrCloudDoesNotSupportKVTags | ||
break check | ||
} else { | ||
// There is a value, so it must match a tag binding | ||
if !slices.ContainsFunc(bindings, func(b *tfe.TagBinding) bool { | ||
return b.Key == k && b.Value == v | ||
}) { | ||
result.requiresUpdate = true | ||
break check | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
doesOrDoesnot := "does " | ||
if !result.requiresUpdate { | ||
doesOrDoesnot = "does not " | ||
} | ||
log.Printf("[TRACE] cloud: Workspace %s %srequire tag update", workspace.Name, doesOrDoesnot) | ||
|
||
return | ||
} | ||
|
||
type WorkspaceMapping struct { | ||
|
@@ -1390,21 +1436,24 @@ func (wm WorkspaceMapping) tfeTags() []*tfe.Tag { | |
return tags | ||
} | ||
|
||
func (wm WorkspaceMapping) tfeTagBindings() []*tfe.TagBinding { | ||
func (wm WorkspaceMapping) asTFETagBindings() []*tfe.TagBinding { | ||
var tagBindings []*tfe.TagBinding | ||
|
||
if wm.Strategy() != WorkspaceKVTagsStrategy { | ||
return tagBindings | ||
} | ||
if wm.Strategy() == WorkspaceKVTagsStrategy { | ||
tagBindings = make([]*tfe.TagBinding, len(wm.TagsAsMap)) | ||
|
||
tagBindings = make([]*tfe.TagBinding, len(wm.TagsAsMap)) | ||
index := 0 | ||
for key, val := range wm.TagsAsMap { | ||
tagBindings[index] = &tfe.TagBinding{Key: key, Value: val} | ||
index += 1 | ||
} | ||
} else if wm.Strategy() == WorkspaceTagsStrategy { | ||
tagBindings = make([]*tfe.TagBinding, len(wm.TagsAsSet)) | ||
|
||
index := 0 | ||
for key, val := range wm.TagsAsMap { | ||
tagBindings[index] = &tfe.TagBinding{Key: key, Value: val} | ||
index += 1 | ||
for i, tag := range wm.TagsAsSet { | ||
tagBindings[i] = &tfe.TagBinding{Key: tag} | ||
} | ||
} | ||
|
||
return tagBindings | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving the else case to the top of the method as a guard clause?