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

GetSet Should Return an Empty List Instead of nil #73

Closed
Osazz opened this issue Feb 14, 2025 · 3 comments
Closed

GetSet Should Return an Empty List Instead of nil #73

Osazz opened this issue Feb 14, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Osazz
Copy link

Osazz commented Feb 14, 2025

*Bug Report: GetSet Should Return an Empty List Instead of nil
Currently, the GetSet() function in the Terraform Artifactory provider returns nil when no values are present for a given key. This causes Terraform to detect unwanted state drift for attributes like project_environments, which is expected to be an empty list ([]) rather than null. This behavior results in unnecessary updates, where Terraform continuously detects changes and tries to apply them.

Requirements for and issue
Terraform snippet

resource "artifactory_local_rpm_repository" "repository" {
  key                  = "deleteme-rpm-scratch-local"
  repo_layout_ref      = "simple-default"
  project_environments = [] # Explicitly setting empty list

  lifecycle {
    ignore_changes = [project_environments]
  }
}

Observed Terraform Plan Output

Terraform will perform the following actions:

  # artifactory_local_rpm_repository.repository[0] will be updated in-place
  ~ resource "artifactory_local_rpm_repository" "repository" {
      + project_environments = (known after apply)
  }

After apply statefile value

"instances": [
        {
          "index_key": 0,
          "schema_version": 1,
          "attributes": {
            "project_environments": null,
            "project_key": "",
            "yum_root_depth": 0
          },
          "sensitive_attributes": []
        }
      ]

Expected Output

If project_environments is not set, GetSet() should return [], ensuring consistency in Terraform state.

Expected Behavior
When an attribute like project_environments is not explicitly set, GetSet() should return [] instead of nil. This prevents unnecessary state drift and ensures compatibility with Terraform's expected behavior.
Affected Versions

  • Artifactory Version: "7.106.3"
  • Terraform Version: "v1.9.8"
  • Terraform Provider Version: "v12.8.4"

Suggested Fix
Modify the GetSet() function in the provider code to return an empty list instead of nil:

func (d *ResourceData) GetSet(key string) []string {
	if v, ok := d.GetOkExists(key); ok {
		arr := CastToStringArr(v.(*schema.Set).List())
		return arr
	}
	return []string{} // Return an empty list instead of nil
}

But since GetSet is been used by other resources and behaviour might be expected for those, then maybe add an a fix here

func ensureStringSlice(arr []string) []string {
	if arr == nil {
		return []string{} // Return empty slice instead of nil
	}
	return arr
}

change line 306 to

	ProjectEnvironments:    ensureStringSlice(d.GetSet("project_environments")), 
@Osazz Osazz added the bug Something isn't working label Feb 14, 2025
@alexhung
Copy link
Member

@Osazz The code you referenced is not being used by local repository since 12.8.0, and by remote repository since 12.8.3 (technically 12.8.2 but I forgot to release it).

The equivalent code is now here.

I'll be updating that to ensure it works for both undefined project_environments attribute as well as value set to [].

@Osazz
Copy link
Author

Osazz commented Feb 16, 2025

@Osazz The code you referenced is not being used by local repository since 12.8.0, and by remote repository since 12.8.3 (technically 12.8.2 but I forgot to release it).

The equivalent code is now here.

I'll be updating that to ensure it works for both undefined project_environments attribute as well as value set to [].

Okay thanks. Was trying to see where the update will be but cant. Can you please point me to it so I can keep an eye for when it has been updated.

thanks

@alexhung
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants