-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathdata_source.go
67 lines (57 loc) · 2.32 KB
/
data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package searchdeployment
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)
var _ datasource.DataSource = &searchDeploymentDS{}
var _ datasource.DataSourceWithConfigure = &searchDeploymentDS{}
func DataSource() datasource.DataSource {
return &searchDeploymentDS{
DSCommon: config.DSCommon{
DataSourceName: searchDeploymentName,
},
}
}
type searchDeploymentDS struct {
config.DSCommon
}
func (d *searchDeploymentDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = conversion.DataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.DataSourceSchemaRequest{
RequiredFields: []string{"project_id", "cluster_name"},
})
}
func (d *searchDeploymentDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var searchDeploymentConfig TFSearchDeploymentDSModel
resp.Diagnostics.Append(req.Config.Get(ctx, &searchDeploymentConfig)...)
if resp.Diagnostics.HasError() {
return
}
// TODO: update before merging to master: connV2 := d.Client.AtlasV2
connV2 := d.Client.AtlasPreview
projectID := searchDeploymentConfig.ProjectID.ValueString()
clusterName := searchDeploymentConfig.ClusterName.ValueString()
deploymentResp, _, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute()
if err != nil {
resp.Diagnostics.AddError("error getting search node information", err.Error())
return
}
newSearchDeploymentModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, nil, true)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
dsModel := convertToDSModel(newSearchDeploymentModel)
resp.Diagnostics.Append(resp.State.Set(ctx, dsModel)...)
}
func convertToDSModel(inputModel *TFSearchDeploymentRSModel) TFSearchDeploymentDSModel {
return TFSearchDeploymentDSModel{
ID: inputModel.ID,
ClusterName: inputModel.ClusterName,
ProjectID: inputModel.ProjectID,
Specs: inputModel.Specs,
StateName: inputModel.StateName,
EncryptionAtRestProvider: inputModel.EncryptionAtRestProvider,
}
}