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 a couple of data sources #43

Merged
merged 2 commits into from
Jan 8, 2019
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
61 changes: 61 additions & 0 deletions tfe/data_source_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tfe

import (
"fmt"

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

func dataSourceTFESSHKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFESSHKeyRead,

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

"organization": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceTFESSHKeyRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get the name and organization.
name := d.Get("name").(string)
organization := d.Get("organization").(string)

// Create an options struct.
options := tfe.SSHKeyListOptions{}

for {
l, err := tfeClient.SSHKeys.List(ctx, organization, options)
if err != nil {
return fmt.Errorf("Error retrieving SSH keys: %v", err)
}

for _, k := range l.Items {
if k.Name == name {
d.SetId(k.ID)
return nil
}
}

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

// Update the page number to get the next page.
options.PageNumber = l.NextPage
}

return fmt.Errorf("Could not find SSH key %s/%s", organization, name)
}
50 changes: 50 additions & 0 deletions tfe/data_source_ssh_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tfe

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

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccTFESSHKeyDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccTFESSHKeyDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_ssh_key.foobar", "name", fmt.Sprintf("ssh-key-test-%d", rInt)),
resource.TestCheckResourceAttr(
"data.tfe_ssh_key.foobar", "organization", fmt.Sprintf("terraform-test-%d", rInt)),
resource.TestCheckResourceAttrSet("data.tfe_ssh_key.foobar", "id"),
),
},
},
})
}

func testAccTFESSHKeyDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
svanharmelen marked this conversation as resolved.
Show resolved Hide resolved
name = "terraform-test-%d"
email = "admin@company.com"
}

resource "tfe_ssh_key" "foobar" {
name = "ssh-key-test-%d"
organization = "${tfe_organization.foobar.id}"
key = "SSH-KEY-CONTENT"
}

data "tfe_ssh_key" "foobar" {
name = "${tfe_ssh_key.foobar.name}"
organization = "${tfe_ssh_key.foobar.organization}"
}`, rInt, rInt)
}
61 changes: 61 additions & 0 deletions tfe/data_source_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tfe

import (
"fmt"

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

func dataSourceTFETeam() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFETeamRead,

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

"organization": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceTFETeamRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get the name and organization.
name := d.Get("name").(string)
organization := d.Get("organization").(string)

// Create an options struct.
options := tfe.TeamListOptions{}

for {
l, err := tfeClient.Teams.List(ctx, organization, options)
if err != nil {
return fmt.Errorf("Error retrieving teams: %v", err)
}

for _, tm := range l.Items {
if tm.Name == name {
d.SetId(tm.ID)
return nil
}
}

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

// Update the page number to get the next page.
options.PageNumber = l.NextPage
}

return fmt.Errorf("Could not find team %s/%s", organization, name)
}
80 changes: 80 additions & 0 deletions tfe/data_source_team_access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tfe

import (
"fmt"

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

func dataSourceTFETeamAccess() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFETeamAccessRead,

Schema: map[string]*schema.Schema{
"access": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"team_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"workspace_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceTFETeamAccessRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get the team ID.
teamID := d.Get("team_id").(string)

// Get organization and workspace.
organization, workspace, err := unpackWorkspaceID(d.Get("workspace_id").(string))
if err != nil {
return fmt.Errorf("Error unpacking workspace ID: %v", err)
}

// Get the workspace.
ws, err := tfeClient.Workspaces.Read(ctx, organization, workspace)
if err != nil {
return fmt.Errorf(
"Error retrieving workspace %s from organization %s: %v", workspace, organization, err)
}

// Create an options struct.
options := tfe.TeamAccessListOptions{
WorkspaceID: tfe.String(ws.ID),
}

for {
l, err := tfeClient.TeamAccess.List(ctx, options)
if err != nil {
return fmt.Errorf("Error retrieving team access list: %v", err)
}

for _, ta := range l.Items {
if ta.Team.ID == teamID {
d.SetId(ta.ID)
return resourceTFETeamAccessRead(d, meta)
}
}

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

// Update the page number to get the next page.
options.PageNumber = l.NextPage
}

return fmt.Errorf("Could not find team access for %s and workspace %s", teamID, ws.Name)
}
60 changes: 60 additions & 0 deletions tfe/data_source_team_access_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tfe

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

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccTFETeamAccessDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccTFETeamAccessDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_team_access.foobar", "access", "write"),
resource.TestCheckResourceAttrSet("data.tfe_team_access.foobar", "id"),
resource.TestCheckResourceAttrSet("data.tfe_team_access.foobar", "team_id"),
resource.TestCheckResourceAttrSet("data.tfe_team_access.foobar", "workspace_id"),
),
},
},
})
}

func testAccTFETeamAccessDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "terraform-test-%d"
email = "admin@company.com"
}

resource "tfe_team" "foobar" {
name = "team-test-%d"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_workspace" "foobar" {
name = "workspace-test-%d"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_team_access" "foobar" {
access = "write"
team_id = "${tfe_team.foobar.id}"
workspace_id = "${tfe_workspace.foobar.id}"
}

data "tfe_team_access" "foobar" {
team_id = "${tfe_team.foobar.id}"
workspace_id = "${tfe_team_access.foobar.workspace_id}"
}`, rInt, rInt, rInt)
}
49 changes: 49 additions & 0 deletions tfe/data_source_team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tfe

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

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccTFETeamDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccTFETeamDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_team.foobar", "name", fmt.Sprintf("team-test-%d", rInt)),
resource.TestCheckResourceAttr(
"data.tfe_team.foobar", "organization", fmt.Sprintf("terraform-test-%d", rInt)),
resource.TestCheckResourceAttrSet("data.tfe_team.foobar", "id"),
),
},
},
})
}

func testAccTFETeamDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "terraform-test-%d"
email = "admin@company.com"
}

resource "tfe_team" "foobar" {
name = "team-test-%d"
organization = "${tfe_organization.foobar.id}"
}

data "tfe_team" "foobar" {
name = "${tfe_team.foobar.name}"
organization = "${tfe_team.foobar.organization}"
}`, rInt, rInt)
}
Loading