|
| 1 | +package datasources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 7 | + "github.com/samber/lo" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/astronomer/astronomer-terraform-provider/internal/clients" |
| 11 | + "github.com/astronomer/astronomer-terraform-provider/internal/clients/platform" |
| 12 | + "github.com/astronomer/astronomer-terraform-provider/internal/provider/models" |
| 13 | + "github.com/astronomer/astronomer-terraform-provider/internal/provider/schemas" |
| 14 | + "github.com/astronomer/astronomer-terraform-provider/internal/utils" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 18 | +) |
| 19 | + |
| 20 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 21 | +var _ datasource.DataSource = &workspacesDataSource{} |
| 22 | +var _ datasource.DataSourceWithConfigure = &workspacesDataSource{} |
| 23 | + |
| 24 | +func NewWorkspacesDataSource() datasource.DataSource { |
| 25 | + return &workspacesDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +// workspacesDataSource defines the data source implementation. |
| 29 | +type workspacesDataSource struct { |
| 30 | + PlatformClient platform.ClientWithResponsesInterface |
| 31 | + OrganizationId string |
| 32 | +} |
| 33 | + |
| 34 | +func (d *workspacesDataSource) Metadata( |
| 35 | + ctx context.Context, |
| 36 | + req datasource.MetadataRequest, |
| 37 | + resp *datasource.MetadataResponse, |
| 38 | +) { |
| 39 | + resp.TypeName = req.ProviderTypeName + "_workspaces" |
| 40 | +} |
| 41 | + |
| 42 | +func (d *workspacesDataSource) Schema( |
| 43 | + ctx context.Context, |
| 44 | + req datasource.SchemaRequest, |
| 45 | + resp *datasource.SchemaResponse, |
| 46 | +) { |
| 47 | + resp.Schema = schema.Schema{ |
| 48 | + // This description is used by the documentation generator and the language server. |
| 49 | + MarkdownDescription: "Workspaces data source", |
| 50 | + Attributes: schemas.WorkspacesDataSourceSchemaAttributes(), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (d *workspacesDataSource) Configure( |
| 55 | + ctx context.Context, |
| 56 | + req datasource.ConfigureRequest, |
| 57 | + resp *datasource.ConfigureResponse, |
| 58 | +) { |
| 59 | + // Prevent panic if the provider has not been configured. |
| 60 | + if req.ProviderData == nil { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + apiClients, ok := req.ProviderData.(models.ApiClientsModel) |
| 65 | + if !ok { |
| 66 | + utils.DataSourceApiClientConfigureError(ctx, req, resp) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + d.PlatformClient = apiClients.PlatformClient |
| 71 | + d.OrganizationId = apiClients.OrganizationId |
| 72 | +} |
| 73 | + |
| 74 | +func (d *workspacesDataSource) Read( |
| 75 | + ctx context.Context, |
| 76 | + req datasource.ReadRequest, |
| 77 | + resp *datasource.ReadResponse, |
| 78 | +) { |
| 79 | + var data models.WorkspacesDataSource |
| 80 | + |
| 81 | + // Read Terraform configuration data into the model |
| 82 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 83 | + |
| 84 | + params := &platform.ListWorkspacesParams{ |
| 85 | + Limit: lo.ToPtr(1000), |
| 86 | + } |
| 87 | + workspaceIds := data.WorkspaceIds.Elements() |
| 88 | + if len(workspaceIds) > 0 { |
| 89 | + workspaceIdsParam := lo.Map(workspaceIds, func(id attr.Value, _ int) string { |
| 90 | + // Terraform includes quotes around the string, so we need to remove them |
| 91 | + return strings.ReplaceAll(id.String(), `"`, "") |
| 92 | + }) |
| 93 | + params.WorkspaceIds = &workspaceIdsParam |
| 94 | + } |
| 95 | + names := data.Names.Elements() |
| 96 | + if len(names) > 0 { |
| 97 | + namesParam := lo.Map(names, func(name attr.Value, _ int) string { |
| 98 | + // Terraform includes quotes around the string, so we need to remove them |
| 99 | + return strings.ReplaceAll(name.String(), `"`, "") |
| 100 | + }) |
| 101 | + params.Names = &namesParam |
| 102 | + } |
| 103 | + |
| 104 | + if resp.Diagnostics.HasError() { |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + var workspaces []platform.Workspace |
| 109 | + offset := 0 |
| 110 | + for { |
| 111 | + params.Offset = &offset |
| 112 | + workspacesResp, err := d.PlatformClient.ListWorkspacesWithResponse( |
| 113 | + ctx, |
| 114 | + d.OrganizationId, |
| 115 | + params, |
| 116 | + ) |
| 117 | + if err != nil { |
| 118 | + tflog.Error(ctx, "failed to get workspace", map[string]interface{}{"error": err}) |
| 119 | + resp.Diagnostics.AddError( |
| 120 | + "Client Error", |
| 121 | + fmt.Sprintf("Unable to read workspace, got error: %s", err), |
| 122 | + ) |
| 123 | + return |
| 124 | + } |
| 125 | + _, diagnostic := clients.NormalizeAPIError(ctx, workspacesResp.HTTPResponse, workspacesResp.Body) |
| 126 | + if diagnostic != nil { |
| 127 | + resp.Diagnostics.Append(diagnostic) |
| 128 | + return |
| 129 | + } |
| 130 | + if workspacesResp.JSON200 == nil { |
| 131 | + tflog.Error(ctx, "failed to get workspace", map[string]interface{}{"error": "nil response"}) |
| 132 | + resp.Diagnostics.AddError("Client Error", "Unable to read workspace, got nil response") |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + workspaces = append(workspaces, workspacesResp.JSON200.Workspaces...) |
| 137 | + |
| 138 | + if workspacesResp.JSON200.TotalCount <= offset { |
| 139 | + break |
| 140 | + } |
| 141 | + |
| 142 | + offset += 1000 |
| 143 | + } |
| 144 | + |
| 145 | + // Populate the model with the response data |
| 146 | + diags := data.ReadFromResponse(ctx, workspaces) |
| 147 | + if diags.HasError() { |
| 148 | + resp.Diagnostics.Append(diags...) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + // Save data into Terraform state |
| 153 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 154 | +} |
0 commit comments