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

Aidan/datasource group #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions dockerhub/datasource_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dockerhub

import (
"context"
"fmt"

dh "github.com/BarnabyShearer/dockerhub/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The provider specific id of the group.",
Type: schema.TypeString,
Computed: true,
},
"organisation": {
Type: schema.TypeString,
Required: true,
Description: "The organisation name.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Group name.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Group description.",
},
"group_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Group ID.",
},
},
}
}

func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*dh.Client)

organisation := d.Get("organisation").(string)
name := d.Get("name").(string)

group, err := client.GetGroup(ctx, organisation, name)
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%s/%s", organisation, group.Name))
d.Set("description", group.Description)
d.Set("group_id", group.Id)
return nil
}
36 changes: 36 additions & 0 deletions dockerhub/datasource_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dockerhub

import (
"fmt"
"testing"

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

func TestAccDatasourceDockerhubGroup_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckGroupResourceDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "dockerhub_group" "foo" {
organisation = "%s"
name = "terraformtest"
description = "terraform test group"
}
data "dockerhub_group" "foo" {
organisation = "%s"
name = dockerhub_group.foo.name
}
`, organisation, organisation),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dockerhub_group.foo", "description", "terraform test group"),
),
},
},
})
}
3 changes: 3 additions & 0 deletions dockerhub/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func Provider() *schema.Provider {
"dockerhub_repositorygroup": resourceRepositoryGroup(),
"dockerhub_token": resourceToken(),
},
DataSourcesMap: map[string]*schema.Resource{
"dockerhub_group": dataSourceGroup(),
},
ConfigureContextFunc: providerConfigure,
}
}
Expand Down
4 changes: 4 additions & 0 deletions dockerhub/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider
var organisation = os.Getenv("DOCKER_ORGANISATION")

func init() {
testAccProvider = Provider()
Expand All @@ -24,4 +25,7 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("DOCKER_PASSWORD"); v == "" {
t.Fatal("DOCKER_PASSWORD must be set for acceptance tests")
}
if organisation == "" {
organisation = "barnabyshearer"
}
}
61 changes: 61 additions & 0 deletions dockerhub/resource_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package dockerhub

import (
"context"
"fmt"
"strings"
"testing"

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

func TestAccDockerhubGroup_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGroupResourceDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "dockerhub_group" "foo" {
organisation = "%s"
name = "terraformtest"
description = "terraform test group"
}
`, organisation),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("dockerhub_group.foo", "description", "terraform test group"),
),
},
},
})
}

func testAccCheckGroupResourceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*dh.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "dockerhub_group" {
continue
}

split := strings.Split(rs.Primary.ID, "/")

if len(split) != 2 {
return fmt.Errorf("Unexpected Id split: %s", rs.Primary.ID)
}
organisation, name := split[0], split[1]

_, err := client.GetGroup(context.Background(), organisation, name)
if err == nil {
return fmt.Errorf("Group (%s) still exists.", rs.Primary.ID)
}
if !strings.Contains(err.Error(), "Team not found") {
return err
}
}

return nil
}
29 changes: 29 additions & 0 deletions docs/data-sources/group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dockerhub_group Data Source - terraform-provider-dockerhub"
subcategory: ""
description: |-

---

# dockerhub_group (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Group name.
- `organisation` (String) The organisation name.

### Read-Only

- `description` (String) Group description.
- `group_id` (Number) Group ID.
- `id` (String) The provider specific id of the group.