|
| 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 | +} |
0 commit comments