Skip to content

Commit

Permalink
Merge pull request #50 from ryanhristovski/issue-48
Browse files Browse the repository at this point in the history
setup a docker_org_team data source
  • Loading branch information
ryanhristovski authored Aug 28, 2024
2 parents 6eab26c + 9d599cf commit c6bad59
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 16 deletions.
8 changes: 5 additions & 3 deletions internal/hubclient/client_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ type OrgSettings struct {
}

type OrgTeam struct {
ID int64 `json:"id"`
TeamName string `json:"name"`
TeamDescription string `json:"description"`
ID int64 `json:"id"`
UUID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
MemberCount int `json:"member_count"`
}

type OrgTeamMember struct {
Expand Down
116 changes: 116 additions & 0 deletions internal/provider/data_source_org_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package provider

import (
"context"
"fmt"

"github.com/docker/terraform-provider-docker/internal/hubclient"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &OrgTeamDataSource{}
_ datasource.DataSourceWithConfigure = &OrgTeamDataSource{}
)

func NewOrgTeamDataSource() datasource.DataSource {
return &OrgTeamDataSource{}
}

type OrgTeamDataSource struct {
client *hubclient.Client
}

type OrgTeamDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
OrgName types.String `tfsdk:"org_name"`
TeamName types.String `tfsdk:"team_name"`
UUID types.String `tfsdk:"uuid"`
Description types.String `tfsdk:"description"`
MemberCount types.Int64 `tfsdk:"member_count"`
}

func (d *OrgTeamDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_org_team"
}

func (d *OrgTeamDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Docker Hub Organization Team",

Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "The ID of the team",
Computed: true,
},
"org_name": schema.StringAttribute{
MarkdownDescription: "Organization namespace",
Required: true,
},
"team_name": schema.StringAttribute{
MarkdownDescription: "Team name within the organization",
Required: true,
},
"uuid": schema.StringAttribute{
MarkdownDescription: "UUID of the team",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the team",
Computed: true,
},
"member_count": schema.Int64Attribute{
MarkdownDescription: "Number of members in the team",
Computed: true,
},
},
}
}

func (d *OrgTeamDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*hubclient.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hubclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *OrgTeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrgTeamDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

orgTeam, err := d.client.GetOrgTeam(ctx, data.OrgName.ValueString(), data.TeamName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Docker Hub API error reading organization team", fmt.Sprintf("%v", err))
return
}

data.ID = types.Int64Value(int64(orgTeam.ID))
data.UUID = types.StringValue(orgTeam.UUID)
data.Description = types.StringValue(orgTeam.Description)
data.MemberCount = types.Int64Value(int64(orgTeam.MemberCount))

diags := resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
49 changes: 49 additions & 0 deletions internal/provider/data_source_org_team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package provider

import (
"fmt"
"testing"

"github.com/docker/terraform-provider-docker/internal/envvar"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccOrgTeamDataSource(t *testing.T) {
orgName := envvar.GetWithDefault(envvar.AccTestOrganization)
teamName := "teamname"
description := "Test org team description"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOrgTeamDataSourceConfig(orgName, teamName, description),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_org_team.test", "org_name", orgName),
resource.TestCheckResourceAttr("data.docker_org_team.test", "team_name", teamName),
resource.TestCheckResourceAttrSet("data.docker_org_team.test", "id"),
resource.TestCheckResourceAttrSet("data.docker_org_team.test", "uuid"),
resource.TestCheckResourceAttr("data.docker_org_team.test", "description", description),
resource.TestCheckResourceAttr("data.docker_org_team.test", "member_count", "0"),
),
},
},
})
}

func testAccOrgTeamDataSourceConfig(orgName string, teamName string, description string) string {
return fmt.Sprintf(`
resource "docker_org_team" "terraform-team" {
org_name = "%s"
team_name = "%s"
team_description = "%s"
}
data "docker_org_team" "test" {
org_name = docker_org_team.terraform-team.org_name
team_name = docker_org_team.terraform-team.team_name
}
`, orgName, teamName, description)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func (p *DockerProvider) DataSources(ctx context.Context) []func() datasource.Da
NewRepositoriesDataSource,
NewAccessTokenDataSource,
NewAccessTokensDataSource,
NewOrgTeamDataSource,
}
}

Expand Down
26 changes: 13 additions & 13 deletions internal/provider/resource_org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (r *OrgTeamResource) Create(ctx context.Context, req resource.CreateRequest
}

createReq := hubclient.OrgTeam{
TeamName: data.TeamName.ValueString(),
TeamDescription: data.TeamDesc.ValueString(),
Name: data.TeamName.ValueString(),
Description: data.TeamDesc.ValueString(),
}

orgTeam, err := r.client.CreateOrgTeam(ctx, data.OrgName.ValueString(), createReq)
Expand All @@ -113,9 +113,9 @@ func (r *OrgTeamResource) Create(ctx context.Context, req resource.CreateRequest
}

data.ID = types.Int64Value(orgTeam.ID)
data.TeamName = types.StringValue(orgTeam.TeamName)
if len(orgTeam.TeamDescription) > 0 {
data.TeamDesc = types.StringValue(orgTeam.TeamDescription)
data.TeamName = types.StringValue(orgTeam.Name)
if len(orgTeam.Description) > 0 {
data.TeamDesc = types.StringValue(orgTeam.Description)
} else {
data.TeamDesc = types.StringNull()
}
Expand All @@ -141,9 +141,9 @@ func (r *OrgTeamResource) Read(ctx context.Context, req resource.ReadRequest, re
}

data.ID = types.Int64Value(orgTeam.ID)
data.TeamName = types.StringValue(orgTeam.TeamName)
if len(orgTeam.TeamDescription) > 0 {
data.TeamDesc = types.StringValue(orgTeam.TeamDescription)
data.TeamName = types.StringValue(orgTeam.Name)
if len(orgTeam.Description) > 0 {
data.TeamDesc = types.StringValue(orgTeam.Description)
} else {
data.TeamDesc = types.StringNull()
}
Expand All @@ -159,8 +159,8 @@ func (r *OrgTeamResource) Update(ctx context.Context, req resource.UpdateRequest
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

updateReq := hubclient.OrgTeam{
TeamName: data.TeamName.ValueString(),
TeamDescription: data.TeamDesc.ValueString(),
Name: data.TeamName.ValueString(),
Description: data.TeamDesc.ValueString(),
}

// Updates to Team Names are a bit awkward.
Expand All @@ -177,9 +177,9 @@ func (r *OrgTeamResource) Update(ctx context.Context, req resource.UpdateRequest
}

data.ID = types.Int64Value(orgTeam.ID)
data.TeamName = types.StringValue(orgTeam.TeamName)
if len(orgTeam.TeamDescription) > 0 {
data.TeamDesc = types.StringValue(orgTeam.TeamDescription)
data.TeamName = types.StringValue(orgTeam.Name)
if len(orgTeam.Description) > 0 {
data.TeamDesc = types.StringValue(orgTeam.Description)
} else {
data.TeamDesc = types.StringNull()
}
Expand Down

0 comments on commit c6bad59

Please sign in to comment.