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

Add tfe_projects data source #1339

Conversation

tdevelioglu
Copy link
Contributor

Description

Add tfe_projects data source for retrieving organization projects.

image

Testing plan

Create and retrieve projects with the new data source.

Example configuration
resource "tfe_organization" "organization" {
  name  = "random-org"
  email = "[email protected]"
}

resource "tfe_project" "project1" {
  name         = "project1"
  description  = "Project 1"
  organization = tfe_organization.organization.name
}

resource "tfe_project" "project2" {
  name        = "project2"
  description = "Project 2"
  organization = tfe_organization.organization.name
}

resource "tfe_project" "project3" {
  name        = "project3"
  description = "Project 3"
  organization = tfe_organization.organization.name
}

data tfe_projects "all" {
  organization = tfe_organization.organization.name
}

External links

Output from acceptance tests

$ TESTARGS="-run TestAccTFEProjects" make testacc
TF_ACC=1 TF_LOG_SDK_PROTO=OFF go test $(go list ./... |grep -v 'vendor') -v -run TestAccTFEProjects -timeout 15m
?       github.com/hashicorp/terraform-provider-tfe     [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-tfe/internal/client     (cached) [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-tfe/internal/logging    (cached) [no tests to run]
=== RUN   TestAccTFEProjectsDataSource_basic
--- PASS: TestAccTFEProjectsDataSource_basic (4.09s)
=== RUN   TestAccTFEProjectsDataSource_basicNoProjects
--- PASS: TestAccTFEProjectsDataSource_basicNoProjects (3.70s)
PASS
ok      github.com/hashicorp/terraform-provider-tfe/internal/provider   (cached)
?       github.com/hashicorp/terraform-provider-tfe/internal/provider/validators        [no test files]
?       github.com/hashicorp/terraform-provider-tfe/version     [no test files]
...

@tdevelioglu tdevelioglu requested a review from a team as a code owner April 26, 2024 16:06
Copy link

hashicorp-cla-app bot commented Apr 26, 2024

CLA assistant check
All committers have signed the CLA.

@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 28d86c7 to ef3e3a0 Compare April 29, 2024 09:22
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from ef3e3a0 to 59ab0b8 Compare May 8, 2024 16:16
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 59ab0b8 to be24691 Compare May 16, 2024 07:40
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 4b3169b to cb89051 Compare May 29, 2024 13:08
@tdevelioglu
Copy link
Contributor Author

Can this one be reviewed, or does it require any additional info ?

@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from cb89051 to 30a8841 Compare May 31, 2024 15:07
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 30a8841 to fae46c4 Compare June 10, 2024 15:12
@ctrombley ctrombley force-pushed the tdevelioglu/add_data_source_projects branch from 23f978d to 331f2ca Compare October 24, 2024 19:16
@netramali netramali force-pushed the tdevelioglu/add_data_source_projects branch 2 times, most recently from fa20532 to 2939a40 Compare October 24, 2024 19:50
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 2939a40 to a5d826e Compare October 25, 2024 09:35
@netramali netramali mentioned this pull request Oct 25, 2024
2 tasks
@hs26gill
Copy link
Contributor

hs26gill commented Oct 29, 2024

@tdevelioglu could you please provide feedback so we can proceed with the changes, thanks.

@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch 3 times, most recently from 4c08483 to 540523c Compare October 29, 2024 17:08
resource.TestCheckResourceAttrSet(
"data.tfe_projects.all", "projects.0.id"),
resource.TestCheckResourceAttr(
"data.tfe_projects.all", "projects.0.name", "Default Project"),
Copy link
Contributor

@netramali netramali Oct 29, 2024

Choose a reason for hiding this comment

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

@tdevelioglu The test might fail because of the ordering of the 4 projects. Hence we need to do something like this:
Declare this array: prjNames := []string{"Default Project", prj1.Name, prj2.Name, prj3.Name}
and then

					resource.TestCheckResourceAttrWith(
						"data.tfe_projects.all", "projects.0.name", func(value string) error {
							for _, name := range prjNames {
								if name == value {
									return nil
								}
							}
							return fmt.Errorf("Excepted project name %s to be in the list %v but not found. ", value, prjNames)
						}),

instead of
resource.TestCheckResourceAttr(
"data.tfe_projects.all", "projects.0.name", "Default Project"),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but how do we check both description and name are correct ?

Related, I am unable to further testacc these changes myself due to lack of permission.

Copy link
Contributor

@netramali netramali Oct 29, 2024

Choose a reason for hiding this comment

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

@tdevelioglu For description you will also have to create something like:
description := []string{"", prj1.Description, prj2.Description, prj3.Description}
and then

resource.TestCheckResourceAttrWith(
						"data.tfe_projects.all", "projects.0.description", func(value string) error {
							for _,description := range description {
								if description == value {
									return nil
								}
							}
							return fmt.Errorf("Excepted project description %s to be in the list %v but not found. ", value, description)
						}),

But to be honest, I think we can remove and ignore description from this test entirely since the purpose of this test is to check if project is returned or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it helps, you can reference this: https://github.com/hashicorp/terraform-provider-tfe/pull/1506/files#diff-94dc573614fee0ef601ca30331d457363deb060c4ce0af0d93c51dff59f62943
Let me know if you are not able to access this.
I have confirm that the CI test passes with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've dropped the description and order-dependent checks, can you check if this passes ?

@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 540523c to 75bebba Compare October 29, 2024 17:42
Can be used to retrieve all projects in an organization.
@tdevelioglu tdevelioglu force-pushed the tdevelioglu/add_data_source_projects branch from 75bebba to 342df3d Compare October 29, 2024 18:14
@netramali
Copy link
Contributor

I have opened another PR with your commits present. I'll look into the CI tests there and get it merged as soon as possible. We can close this PR now. Thank you for addressing the feedback!

@netramali netramali closed this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants