Skip to content

Commit 620b2c9

Browse files
committed
Create OrganizationTeamDataSource
1 parent 880f56d commit 620b2c9

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

code_generator/provider_code_spec.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,51 @@
365365
]
366366
}
367367
},
368+
{
369+
"name": "organization_team",
370+
"schema": {
371+
"attributes": [
372+
{
373+
"name": "description",
374+
"string": {
375+
"computed_optional_required": "computed",
376+
"description": "Markdown description"
377+
}
378+
},
379+
{
380+
"name": "members",
381+
"list": {
382+
"computed_optional_required": "computed",
383+
"description": "List of team members",
384+
"element_type": {
385+
"string": {}
386+
}
387+
}
388+
},
389+
{
390+
"name": "name",
391+
"string": {
392+
"computed_optional_required": "required",
393+
"description": "Team name"
394+
}
395+
},
396+
{
397+
"name": "orgname",
398+
"string": {
399+
"computed_optional_required": "required",
400+
"description": "Organization name"
401+
}
402+
},
403+
{
404+
"name": "role",
405+
"string": {
406+
"computed_optional_required": "computed",
407+
"description": "Team role"
408+
}
409+
}
410+
]
411+
}
412+
},
368413
{
369414
"name": "organization_team_permission",
370415
"schema": {

internal/datasource_organization_team/organization_team_data_source_gen.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
12+
"github.com/enthought/terraform-provider-quay/quay_api"
13+
"terraform-provider-quay/internal/datasource_organization_team"
14+
)
15+
16+
var (
17+
_ datasource.DataSource = (*organizationTeamDataSource)(nil)
18+
_ datasource.DataSourceWithConfigure = (*organizationTeamDataSource)(nil)
19+
)
20+
21+
func NewOrganizationTeamDataSource() datasource.DataSource {
22+
return &organizationTeamDataSource{}
23+
}
24+
25+
type organizationTeamDataSource struct {
26+
client *quay_api.APIClient
27+
}
28+
29+
func (d *organizationTeamDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
30+
resp.TypeName = req.ProviderTypeName + "_organization_team"
31+
}
32+
33+
func (d *organizationTeamDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
34+
resp.Schema = datasource_organization_team.OrganizationTeamDataSourceSchema(ctx)
35+
}
36+
37+
func (d *organizationTeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
38+
var data datasource_organization_team.OrganizationTeamModel
39+
var resTeamData teamModelJSON
40+
var resOrgData organizationModelJSON
41+
42+
// Read Terraform configuration data into the model
43+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
44+
45+
if resp.Diagnostics.HasError() {
46+
return
47+
}
48+
49+
// Create variables
50+
teamName := data.Name.ValueString()
51+
orgName := data.Orgname.ValueString()
52+
53+
// Get members
54+
httpRes, err := d.client.TeamAPI.GetOrganizationTeamMembers(context.Background(), orgName, teamName).Execute()
55+
if err != nil {
56+
errDetail := handleQuayAPIError(err)
57+
resp.Diagnostics.AddError("Error reading Quay team", "Could not read Quay team, unexpected error: "+errDetail)
58+
return
59+
}
60+
61+
body, err := io.ReadAll(httpRes.Body)
62+
if err != nil {
63+
resp.Diagnostics.AddError(
64+
"Error reading Quay team",
65+
"Could not read Quay team, unexpected error: "+err.Error())
66+
return
67+
}
68+
err = json.Unmarshal(body, &resTeamData)
69+
if err != nil {
70+
resp.Diagnostics.AddError(
71+
"Error reading Quay team",
72+
"Could not read Quay team, unexpected error: "+err.Error())
73+
return
74+
}
75+
76+
// Set members
77+
var memList []string
78+
for _, member := range resTeamData.Members {
79+
memList = append(memList, member.Name)
80+
}
81+
members, diags := types.ListValueFrom(ctx, types.StringType, memList)
82+
83+
resp.Diagnostics.Append(diags...)
84+
if resp.Diagnostics.HasError() {
85+
return
86+
}
87+
88+
data.Members = members
89+
90+
// Get org data
91+
httpRes, err = d.client.OrganizationAPI.GetOrganization(context.Background(), orgName).Execute()
92+
if err != nil {
93+
errDetail := handleQuayAPIError(err)
94+
resp.Diagnostics.AddError("Error reading Quay team", "Could not read Quay team, unexpected error: "+errDetail)
95+
return
96+
}
97+
98+
body, err = io.ReadAll(httpRes.Body)
99+
if err != nil {
100+
resp.Diagnostics.AddError(
101+
"Error reading Quay team",
102+
"Could not read Quay team, unexpected error: "+err.Error())
103+
return
104+
}
105+
err = json.Unmarshal(body, &resOrgData)
106+
if err != nil {
107+
resp.Diagnostics.AddError(
108+
"Error reading Quay team",
109+
"Could not read Quay team, unexpected error: "+err.Error())
110+
return
111+
}
112+
113+
// Set Role
114+
data.Role = types.StringValue(resOrgData.Teams[teamName].Role)
115+
116+
// Set Description
117+
data.Description = types.StringValue(resOrgData.Teams[teamName].Description)
118+
119+
// Save data into Terraform state
120+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
121+
}
122+
123+
func (d *organizationTeamDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
124+
if req.ProviderData == nil {
125+
return
126+
}
127+
128+
client, ok := req.ProviderData.(*quay_api.APIClient)
129+
if !ok {
130+
resp.Diagnostics.AddError(
131+
"Unexpected Data Source Configure Type",
132+
fmt.Sprintf("Expected *quay_api.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
133+
)
134+
}
135+
136+
d.client = client
137+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccOrganizationTeamDataSource(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
12+
Steps: []resource.TestStep{
13+
// Create and Read
14+
{
15+
Config: providerConfig + `
16+
resource "quay_organization" "org_team_data" {
17+
name = "org_team_data"
18+
19+
}
20+
21+
resource "quay_repository" "test" {
22+
name = "test"
23+
namespace = quay_organization.org_team_data.name
24+
}
25+
26+
resource "quay_organization_robot" "test" {
27+
name = "test"
28+
orgname = quay_organization.org_team_data.name
29+
}
30+
31+
resource "quay_organization_team" "test" {
32+
name = "test"
33+
orgname = quay_organization.org_team_data.name
34+
role = "member"
35+
description = "test"
36+
members = [
37+
quay_organization_robot.test.fullname
38+
]
39+
}
40+
41+
data "quay_organization_team" "test" {
42+
name = "test"
43+
orgname = quay_organization.org_team_data.name
44+
45+
depends_on = [
46+
quay_organization_team.test
47+
]
48+
}
49+
`,
50+
Check: resource.ComposeAggregateTestCheckFunc(
51+
resource.TestCheckResourceAttr("data.quay_organization_team.test", "name", "test"),
52+
resource.TestCheckResourceAttr("data.quay_organization_team.test", "orgname", "org_team_data"),
53+
resource.TestCheckResourceAttr("data.quay_organization_team.test", "role", "member"),
54+
resource.TestCheckResourceAttr("data.quay_organization_team.test", "description", "test"),
55+
resource.TestCheckResourceAttr("data.quay_organization_team.test", "members.0", "org_team_data+test"),
56+
),
57+
},
58+
},
59+
})
60+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func (p *quayProvider) DataSources(_ context.Context) []func() datasource.DataSo
153153
NewOrganizationDataSource,
154154
NewRepositoryDataSource,
155155
NewOrganizationTeamPermissionDataSource,
156+
NewOrganizationTeamDataSource,
156157
}
157158
}
158159

0 commit comments

Comments
 (0)