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 project data source #829

Merged
merged 6 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

FEATURES:
* **New Data Source**: `r/data_source_project` is a new data source to allow users to retrieve project id and associated workspace ids, by @hs26gill ([#829](https://github.com/hashicorp/terraform-provider-tfe/pull/829))

ENHANCEMENTS:

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
github.com/hashicorp/go-slug v0.10.1
github.com/hashicorp/go-tfe v1.20.0
github.com/hashicorp/go-slug v0.11.0
github.com/hashicorp/go-tfe v1.21.0
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/hcl/v2 v2.16.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ github.com/hashicorp/go-plugin v1.4.9 h1:ESiK220/qE0aGxWdzKIvRH69iLiuN/PjoLTm69R
github.com/hashicorp/go-plugin v1.4.9/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/go-slug v0.10.1 h1:05SCRWCBpCxOeP7stQHvMgOz0raCBCekaytu8Rg/RZ4=
github.com/hashicorp/go-slug v0.10.1/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v1.20.0 h1:kDxxLbVrhbZiHZp5yN5eIsCCaY2kEU5T9eO1NI8/KRg=
github.com/hashicorp/go-tfe v1.20.0/go.mod h1:sk16Tskky9E4ywt0DJN8+qalnHuc11EW45+U8+EaZRs=
github.com/hashicorp/go-slug v0.11.0 h1:l7cHWiBk8cnnskjheloW9h8PwXhihvwXbQiiFw2KqkY=
github.com/hashicorp/go-slug v0.11.0/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v1.21.0 h1:sTZXf/MaC/iQ8HxKwYSL0xJSEVDwY+h4ngh/+na8vdk=
github.com/hashicorp/go-tfe v1.21.0/go.mod h1:jedlLiHHiDeBKKpON4aIpTdsKbc2OaVbklEPI7XEHiY=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down
95 changes: 95 additions & 0 deletions tfe/data_source_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Read context to implement cancellation
//

package tfe

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceTFEProject() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceTFEProjectRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"organization": {
Type: schema.TypeString,
Optional: true,
},

"workspace_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceTFEProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(ConfiguredClient)

// Get the project name and organization
projName := d.Get("name").(string)
orgName, err := config.schemaOrDefaultOrganizationKey(d, "organization")
if err != nil {
return diag.Errorf("Error retrieving organization name: %v", err)
}

// Create an options struct.
options := &tfe.ProjectListOptions{
Name: projName,
}

l, err := config.Client.Projects.List(ctx, orgName, options)
if err != nil {
return diag.Errorf("Error retrieving projects: %v", err)
}

for _, proj := range l.Items {
if proj.Name == projName {
// Only now include workspaces to cut down on request load.
readOptions := &tfe.WorkspaceListOptions{
ProjectID: proj.ID,
}
var workspaces []interface{}

for {
wl, err := config.Client.Workspaces.List(ctx, orgName, readOptions)
if err != nil {
return diag.Errorf("Error retrieving workspaces: %v", err)
}

for _, workspace := range wl.Items {
workspaces = append(workspaces, workspace.ID)
}

// Exit the loop when we've seen all pages.
if wl.CurrentPage >= wl.TotalPages {
break
}

// Update the page number to get the next page.
readOptions.PageNumber = wl.NextPage
}

d.Set("workspace_ids", workspaces)
d.SetId(proj.ID)
return nil
}
}
return diag.Errorf("could not find project %s/%s", orgName, projName)
}
61 changes: 61 additions & 0 deletions tfe/data_source_project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tfe

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccTFEProjectDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEProjectDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_project.foobar", "name", fmt.Sprintf("project-test-%d", rInt)),
resource.TestCheckResourceAttr(
"data.tfe_project.foobar", "organization", orgName),
resource.TestCheckResourceAttrSet("data.tfe_project.foobar", "id"),
resource.TestCheckResourceAttr(
"data.tfe_project.foobar", "workspace_ids.#", "1"),
),
},
},
})
}

func testAccTFEProjectDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}

resource "tfe_project" "foobar" {
name = "project-test-%d"
organization = tfe_organization.foobar.id
}

data "tfe_project" "foobar" {
name = tfe_project.foobar.name
organization = tfe_project.foobar.organization
}

resource "tfe_workspace" "foobar" {
name = "workspace-test-%d"
organization = tfe_organization.foobar.id
project_id = tfe_project.foobar.id
}`, rInt, rInt, rInt)
}
1 change: 1 addition & 0 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func Provider() *schema.Provider {
"tfe_organization_membership": dataSourceTFEOrganizationMembership(),
"tfe_organization_run_task": dataSourceTFEOrganizationRunTask(),
"tfe_organization_tags": dataSourceTFEOrganizationTags(),
"tfe_project": dataSourceTFEProject(),
"tfe_slug": dataSourceTFESlug(),
"tfe_ssh_key": dataSourceTFESSHKey(),
"tfe_team": dataSourceTFETeam(),
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/project.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "tfe"
page_title: "Terraform Enterprise: tfe_project"
description: |-
Get information on a Project.
---

# Data Source: tfe_project

Use this data source to get information about a project.

## Example Usage

```hcl
data "tfe_project" "foo" {
name = "my-project-name"
organization = "my-org-name"
}
```

## Argument Reference

The following arguments are supported:
* `name` - (Required) Name of the project.
* `organization` - (Required) Name of the organization.


## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The project ID.
hs26gill marked this conversation as resolved.
Show resolved Hide resolved
* `workspace_ids` - IDs of the workspaces that are associated with the project.