|
| 1 | +package datasources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/astronomer/astronomer-terraform-provider/internal/clients" |
| 8 | + "github.com/astronomer/astronomer-terraform-provider/internal/clients/platform" |
| 9 | + "github.com/astronomer/astronomer-terraform-provider/internal/provider/models" |
| 10 | + "github.com/astronomer/astronomer-terraform-provider/internal/provider/schemas" |
| 11 | + "github.com/astronomer/astronomer-terraform-provider/internal/utils" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 18 | +var _ datasource.DataSource = &clusterDataSource{} |
| 19 | +var _ datasource.DataSourceWithConfigure = &clusterDataSource{} |
| 20 | + |
| 21 | +func NewClusterDataSource() datasource.DataSource { |
| 22 | + return &clusterDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +// clusterDataSource defines the data source implementation. |
| 26 | +type clusterDataSource struct { |
| 27 | + PlatformClient platform.ClientWithResponsesInterface |
| 28 | + OrganizationId string |
| 29 | +} |
| 30 | + |
| 31 | +func (d *clusterDataSource) Metadata( |
| 32 | + ctx context.Context, |
| 33 | + req datasource.MetadataRequest, |
| 34 | + resp *datasource.MetadataResponse, |
| 35 | +) { |
| 36 | + resp.TypeName = req.ProviderTypeName + "_cluster" |
| 37 | +} |
| 38 | + |
| 39 | +func (d *clusterDataSource) Schema( |
| 40 | + ctx context.Context, |
| 41 | + req datasource.SchemaRequest, |
| 42 | + resp *datasource.SchemaResponse, |
| 43 | +) { |
| 44 | + resp.Schema = schema.Schema{ |
| 45 | + // This description is used by the documentation generator and the language server. |
| 46 | + MarkdownDescription: "Cluster data source", |
| 47 | + Attributes: schemas.ClusterDataSourceSchemaAttributes(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (d *clusterDataSource) Configure( |
| 52 | + ctx context.Context, |
| 53 | + req datasource.ConfigureRequest, |
| 54 | + resp *datasource.ConfigureResponse, |
| 55 | +) { |
| 56 | + // Prevent panic if the provider has not been configured. |
| 57 | + if req.ProviderData == nil { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + apiClients, ok := req.ProviderData.(models.ApiClientsModel) |
| 62 | + if !ok { |
| 63 | + utils.DataSourceApiClientConfigureError(ctx, req, resp) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + d.PlatformClient = apiClients.PlatformClient |
| 68 | + d.OrganizationId = apiClients.OrganizationId |
| 69 | +} |
| 70 | + |
| 71 | +func (d *clusterDataSource) Read( |
| 72 | + ctx context.Context, |
| 73 | + req datasource.ReadRequest, |
| 74 | + resp *datasource.ReadResponse, |
| 75 | +) { |
| 76 | + var data models.ClusterDataSource |
| 77 | + |
| 78 | + // Read Terraform configuration data into the model |
| 79 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 80 | + if resp.Diagnostics.HasError() { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + cluster, err := d.PlatformClient.GetClusterWithResponse( |
| 85 | + ctx, |
| 86 | + d.OrganizationId, |
| 87 | + data.Id.ValueString(), |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + tflog.Error(ctx, "failed to get cluster", map[string]interface{}{"error": err}) |
| 91 | + resp.Diagnostics.AddError( |
| 92 | + "Client Error", |
| 93 | + fmt.Sprintf("Unable to read cluster, got error: %s", err), |
| 94 | + ) |
| 95 | + return |
| 96 | + } |
| 97 | + _, diagnostic := clients.NormalizeAPIError(ctx, cluster.HTTPResponse, cluster.Body) |
| 98 | + if diagnostic != nil { |
| 99 | + resp.Diagnostics.Append(diagnostic) |
| 100 | + return |
| 101 | + } |
| 102 | + if cluster.JSON200 == nil { |
| 103 | + tflog.Error(ctx, "failed to get cluster", map[string]interface{}{"error": "nil response"}) |
| 104 | + resp.Diagnostics.AddError("Client Error", "Unable to read cluster, got nil response") |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + // Populate the model with the response data |
| 109 | + diags := data.ReadFromResponse(ctx, cluster.JSON200) |
| 110 | + if diags.HasError() { |
| 111 | + resp.Diagnostics.Append(diags...) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + // Save data into Terraform state |
| 116 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 117 | +} |
0 commit comments