Skip to content

Commit 5a0bef1

Browse files
committed
Create OrganizationRobotDataSource
1 parent 620b2c9 commit 5a0bef1

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

code_generator/provider_code_spec.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,41 @@
365365
]
366366
}
367367
},
368+
{
369+
"name": "organization_robot",
370+
"schema": {
371+
"attributes": [
372+
{
373+
"name": "description",
374+
"string": {
375+
"computed_optional_required": "computed",
376+
"description": "Text description"
377+
}
378+
},
379+
{
380+
"name": "fullname",
381+
"string": {
382+
"computed_optional_required": "computed",
383+
"description": "Robot full name"
384+
}
385+
},
386+
{
387+
"name": "name",
388+
"string": {
389+
"computed_optional_required": "required",
390+
"description": "Robot short name"
391+
}
392+
},
393+
{
394+
"name": "orgname",
395+
"string": {
396+
"computed_optional_required": "required",
397+
"description": "Organization name"
398+
}
399+
}
400+
]
401+
}
402+
},
368403
{
369404
"name": "organization_team",
370405
"schema": {

internal/datasource_organization_robot/organization_robot_data_source_gen.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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_robot"
14+
)
15+
16+
var (
17+
_ datasource.DataSource = (*organizationRobotDataSource)(nil)
18+
_ datasource.DataSourceWithConfigure = (*organizationRobotDataSource)(nil)
19+
)
20+
21+
func NewOrganizationRobotDataSource() datasource.DataSource {
22+
return &organizationRobotDataSource{}
23+
}
24+
25+
type organizationRobotDataSource struct {
26+
client *quay_api.APIClient
27+
}
28+
29+
func (d *organizationRobotDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
30+
resp.TypeName = req.ProviderTypeName + "_organization_robot"
31+
}
32+
33+
func (d *organizationRobotDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
34+
resp.Schema = datasource_organization_robot.OrganizationRobotDataSourceSchema(ctx)
35+
}
36+
37+
func (d *organizationRobotDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
38+
var data datasource_organization_robot.OrganizationRobotModel
39+
var resRobotData organizationRobotModelJSON
40+
41+
// Read Terraform configuration data into the model
42+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
43+
44+
if resp.Diagnostics.HasError() {
45+
return
46+
}
47+
48+
// Create variables
49+
orgName := data.Orgname.ValueString()
50+
robotName := data.Name.ValueString()
51+
52+
// Get robot
53+
httpRes, err := d.client.RobotAPI.GetOrgRobot(context.Background(), orgName, robotName).Execute()
54+
if err != nil {
55+
errDetail := handleQuayAPIError(err)
56+
resp.Diagnostics.AddError(
57+
"Error reading Quay org robot",
58+
"Could not read Quay org robot, unexpected error: "+errDetail)
59+
return
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, &resRobotData)
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 Description
77+
data.Description = types.StringValue(resRobotData.Description)
78+
79+
// Set robot full name
80+
data.Fullname = types.StringValue(orgName + "+" + robotName)
81+
82+
// Save data into Terraform state
83+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
84+
}
85+
86+
func (d *organizationRobotDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
87+
if req.ProviderData == nil {
88+
return
89+
}
90+
91+
client, ok := req.ProviderData.(*quay_api.APIClient)
92+
if !ok {
93+
resp.Diagnostics.AddError(
94+
"Unexpected Data Source Configure Type",
95+
fmt.Sprintf("Expected *quay_api.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
96+
)
97+
}
98+
99+
d.client = client
100+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccOrganizationRobotDataSource(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
12+
Steps: []resource.TestStep{
13+
{
14+
Config: providerConfig + `
15+
resource "quay_organization" "org_robot_data" {
16+
name = "org_robot_data"
17+
18+
}
19+
20+
resource "quay_organization_robot" "test" {
21+
name = "test"
22+
orgname = quay_organization.org_robot_data.name
23+
description = "test"
24+
}
25+
26+
data "quay_organization_robot" "test" {
27+
name = "test"
28+
orgname = quay_organization.org_robot_data.name
29+
30+
depends_on = [
31+
quay_organization_robot.test
32+
]
33+
}
34+
`,
35+
Check: resource.ComposeAggregateTestCheckFunc(
36+
resource.TestCheckResourceAttr("quay_organization_robot.test", "name", "test"),
37+
resource.TestCheckResourceAttr("quay_organization_robot.test", "orgname", "org_robot_data"),
38+
resource.TestCheckResourceAttr("quay_organization_robot.test", "description", "test"),
39+
resource.TestCheckResourceAttr("quay_organization_robot.test", "fullname", "org_robot_data+test"),
40+
),
41+
},
42+
},
43+
})
44+
}

internal/provider/provider.go

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

0 commit comments

Comments
 (0)