|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | +) |
| 10 | + |
| 11 | +var _ datasource.DataSource = &SearchDeploymentDS{} |
| 12 | +var _ datasource.DataSourceWithConfigure = &SearchDeploymentDS{} |
| 13 | + |
| 14 | +func NewSearchDeploymentDS() datasource.DataSource { |
| 15 | + return &SearchDeploymentDS{ |
| 16 | + DSCommon: DSCommon{ |
| 17 | + dataSourceName: searchDeploymentName, |
| 18 | + }, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +type tfSearchDeploymentDSModel struct { |
| 23 | + ID types.String `tfsdk:"id"` |
| 24 | + ClusterName types.String `tfsdk:"cluster_name"` |
| 25 | + ProjectID types.String `tfsdk:"project_id"` |
| 26 | + Specs types.List `tfsdk:"specs"` |
| 27 | + StateName types.String `tfsdk:"state_name"` |
| 28 | +} |
| 29 | + |
| 30 | +type SearchDeploymentDS struct { |
| 31 | + DSCommon |
| 32 | +} |
| 33 | + |
| 34 | +func (d *SearchDeploymentDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 35 | + resp.Schema = schema.Schema{ |
| 36 | + Attributes: map[string]schema.Attribute{ |
| 37 | + "id": schema.StringAttribute{ |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "cluster_name": schema.StringAttribute{ |
| 41 | + Required: true, |
| 42 | + }, |
| 43 | + "project_id": schema.StringAttribute{ |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + "specs": schema.ListNestedAttribute{ |
| 47 | + NestedObject: schema.NestedAttributeObject{ |
| 48 | + Attributes: map[string]schema.Attribute{ |
| 49 | + "instance_size": schema.StringAttribute{ |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "node_count": schema.Int64Attribute{ |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "state_name": schema.StringAttribute{ |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (d *SearchDeploymentDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 67 | + var searchDeploymentConfig tfSearchDeploymentDSModel |
| 68 | + resp.Diagnostics.Append(req.Config.Get(ctx, &searchDeploymentConfig)...) |
| 69 | + if resp.Diagnostics.HasError() { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + connV2 := d.client.AtlasV2 |
| 74 | + projectID := searchDeploymentConfig.ProjectID.ValueString() |
| 75 | + clusterName := searchDeploymentConfig.ClusterName.ValueString() |
| 76 | + deploymentResp, _, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute() |
| 77 | + if err != nil { |
| 78 | + resp.Diagnostics.AddError("error getting search node information", err.Error()) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + newSearchDeploymentModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, nil) |
| 83 | + resp.Diagnostics.Append(diagnostics...) |
| 84 | + if resp.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + dsModel := convertToDSModel(newSearchDeploymentModel) |
| 88 | + resp.Diagnostics.Append(resp.State.Set(ctx, dsModel)...) |
| 89 | +} |
| 90 | + |
| 91 | +func convertToDSModel(inputModel *tfSearchDeploymentRSModel) tfSearchDeploymentDSModel { |
| 92 | + return tfSearchDeploymentDSModel{ |
| 93 | + ID: inputModel.ID, |
| 94 | + ClusterName: inputModel.ClusterName, |
| 95 | + ProjectID: inputModel.ProjectID, |
| 96 | + Specs: inputModel.Specs, |
| 97 | + StateName: inputModel.StateName, |
| 98 | + } |
| 99 | +} |
0 commit comments