-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
internal/datasource_organization_robot/organization_robot_data_source_gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/enthought/terraform-provider-quay/quay_api" | ||
"terraform-provider-quay/internal/datasource_organization_robot" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = (*organizationRobotDataSource)(nil) | ||
_ datasource.DataSourceWithConfigure = (*organizationRobotDataSource)(nil) | ||
) | ||
|
||
func NewOrganizationRobotDataSource() datasource.DataSource { | ||
return &organizationRobotDataSource{} | ||
} | ||
|
||
type organizationRobotDataSource struct { | ||
client *quay_api.APIClient | ||
} | ||
|
||
func (d *organizationRobotDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_organization_robot" | ||
} | ||
|
||
func (d *organizationRobotDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = datasource_organization_robot.OrganizationRobotDataSourceSchema(ctx) | ||
} | ||
|
||
func (d *organizationRobotDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data datasource_organization_robot.OrganizationRobotModel | ||
var resRobotData organizationRobotModelJSON | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Create variables | ||
orgName := data.Orgname.ValueString() | ||
robotName := data.Name.ValueString() | ||
|
||
// Get robot | ||
httpRes, err := d.client.RobotAPI.GetOrgRobot(context.Background(), orgName, robotName).Execute() | ||
if err != nil { | ||
errDetail := handleQuayAPIError(err) | ||
resp.Diagnostics.AddError( | ||
"Error reading Quay org robot", | ||
"Could not read Quay org robot, unexpected error: "+errDetail) | ||
return | ||
} | ||
body, err := io.ReadAll(httpRes.Body) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Error reading Quay team", | ||
"Could not read Quay team, unexpected error: "+err.Error()) | ||
return | ||
} | ||
err = json.Unmarshal(body, &resRobotData) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Error reading Quay team", | ||
"Could not read Quay team, unexpected error: "+err.Error()) | ||
return | ||
} | ||
|
||
// Set Description | ||
data.Description = types.StringValue(resRobotData.Description) | ||
|
||
// Set robot full name | ||
data.Fullname = types.StringValue(orgName + "+" + robotName) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (d *organizationRobotDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*quay_api.APIClient) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *quay_api.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
} | ||
|
||
d.client = client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccOrganizationRobotDataSource(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: providerConfig + ` | ||
resource "quay_organization" "org_robot_data" { | ||
name = "org_robot_data" | ||
email = "[email protected]" | ||
} | ||
resource "quay_organization_robot" "test" { | ||
name = "test" | ||
orgname = quay_organization.org_robot_data.name | ||
description = "test" | ||
} | ||
data "quay_organization_robot" "test" { | ||
name = "test" | ||
orgname = quay_organization.org_robot_data.name | ||
depends_on = [ | ||
quay_organization_robot.test | ||
] | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("quay_organization_robot.test", "name", "test"), | ||
resource.TestCheckResourceAttr("quay_organization_robot.test", "orgname", "org_robot_data"), | ||
resource.TestCheckResourceAttr("quay_organization_robot.test", "description", "test"), | ||
resource.TestCheckResourceAttr("quay_organization_robot.test", "fullname", "org_robot_data+test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters