Skip to content
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

Fix: drift issues with variable sets #889

Merged
merged 2 commits into from
Jul 11, 2024
Merged

Conversation

TomerHeber
Copy link
Collaborator

@TomerHeber TomerHeber commented Jul 8, 2024

Issue & Steps to Reproduce / Feature Request

fixes #888
fixes #894
fixes #895

Solution

  1. Fixed conflicts between environment sets and non-environment sets.
  2. Filter out scopes assigned implicitly.
  3. Updated tests.
  4. Updated documentation to warn about conflicts.

Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
AssignmentScope string `json:"assignmentScope"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added assignment scope to be able to filter implicit assignments.

@@ -433,7 +433,7 @@ func setEnvironmentSchema(ctx context.Context, d *schema.ResourceData, environme

setEnvironmentConfigurationSchema(ctx, d, configurationVariables)

if d.Get("variable_sets") != nil {
if variableSets, ok := d.GetOk("variable_sets"); variableSets != nil && ok {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as "configuration" - if this field isn't used - ignore any use-cases related to it.
this will avoid possible conflicts with explicit assignment resource.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if it's a basic question but I'm pretty new to go and terraform provider so I would like to make sure 😅 Let's say we have an environment and we assign a variable set to it (by FE or API) will we see the drift there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to apologize (:
Correct - without this check we will see a drift.
(We are doing the same check for "configuration").

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I talked about it with @GiliFaroEnv0 and it looked weird that we won't show DD but if that's how we do it for configuration it might be the way 🤔
I will raise it with the team and update you 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't hide drifts!
the set assignment might be from the FE and not from the other resource
so this is really a drift

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not hiding drifts...
you can either use the assignment resource or set the sets inline in the environment.
You cannot use both. Otherwise, you will have constant drifts/conflicts. (Same with configuration).

It just tests which one is used... if a resource assignments are used, it ignores anything inline in the environment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we just don't understand the code...
how are you checking if another resource is used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's simple (but not perfect):

if "variables-set" is set in the state, it means we use the "variables-set" inline use-case.
Otherwise, it's ignored.

@@ -630,7 +630,9 @@ func getEnvironmentVariableSetIdsFromApi(d *schema.ResourceData, apiClient clien

var environmentVariableSetIds []string
for _, variableSet := range environmentVariableSets {
environmentVariableSetIds = append(environmentVariableSetIds, variableSet.Id)
if variableSet.AssignmentScope == "ENVIRONMENT" {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter is required here... to avoid getting upper scopes variables (E.g. project or org).

@@ -181,6 +187,10 @@ func resourceVariableSetAssignmentRead(ctx context.Context, d *schema.ResourceDa
}

for _, apiConfigurationSet := range apiConfigurationSets {
if apiConfigurationSet.AssignmentScope != assignmentSchema.Scope {
continue
Copy link
Collaborator Author

@TomerHeber TomerHeber Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter to avoid getting implicit variable sets... (from other scopes)

@@ -87,6 +89,10 @@ func resourceVariableSetAssignmentUpdate(ctx context.Context, d *schema.Resource

// In API but not in Schema - delete.
for _, apiConfigurationSet := range apiConfigurationSets {
if apiConfigurationSet.AssignmentScope != assignmentSchema.Scope {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter to avoid getting implicit variable sets... (from other scopes)

Copy link
Contributor

@Yossi-kerner Yossi-kerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the general solution here, I think we can avoid code changes and just keep the docs like we do for regular variables. In the current solution, if a user uses the API or FE to assign variable sets we will ignore it and won’t show the drift

@TomerHeber
Copy link
Collaborator Author

Regarding the general solution here, I think we can avoid code changes and just keep the docs like we do for regular variables. In the current solution, if a user uses the API or FE to assign variable sets we will ignore it and won’t show the drift

@Yossi-kerner - that's not going to be enough. Fixes are required here...

@Yossi-kerner
Copy link
Contributor

Regarding the general solution here, I think we can avoid code changes and just keep the docs like we do for regular variables. In the current solution, if a user uses the API or FE to assign variable sets we will ignore it and won’t show the drift

@Yossi-kerner - that's not going to be enough. Fixes are required here...

But IIUC the fixes only hide the issue no? If there will be a drift detection we will miss that no?

@TomerHeber
Copy link
Collaborator Author

Regarding the general solution here, I think we can avoid code changes and just keep the docs like we do for regular variables. In the current solution, if a user uses the API or FE to assign variable sets we will ignore it and won’t show the drift

@Yossi-kerner - that's not going to be enough. Fixes are required here...

But IIUC the fixes only hide the issue no? If there will be a drift detection we will miss that no?

@Yossi-kerner

Not really...
One fix: The API will still return variable sets irrespective if they were set in the environment or not. This needs to be ignored if it was not set in the environment.
Second fix: The API to return variables returns both implicit and explicit assignments. Need to filter out implicit (inherited) assignments.

@github-actions github-actions bot added ready to merge PR approved - can be merged once the PR owner is ready and removed pending final review labels Jul 11, 2024
@TomerHeber TomerHeber merged commit 08b924d into main Jul 11, 2024
5 checks passed
@TomerHeber TomerHeber deleted the fix-variable-sets-drifts-#888 branch July 11, 2024 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-client fix provider ready to merge PR approved - can be merged once the PR owner is ready
Projects
None yet
3 participants