-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathdata_source_search_deployment.go
100 lines (89 loc) · 2.92 KB
/
data_source_search_deployment.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package searchdeployment
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)
var _ datasource.DataSource = &searchDeploymentDS{}
var _ datasource.DataSourceWithConfigure = &searchDeploymentDS{}
func NewSearchDeploymentDS() datasource.DataSource {
return &searchDeploymentDS{
DSCommon: config.DSCommon{
DataSourceName: searchDeploymentName,
},
}
}
type tfSearchDeploymentDSModel struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ProjectID types.String `tfsdk:"project_id"`
Specs types.List `tfsdk:"specs"`
StateName types.String `tfsdk:"state_name"`
}
type searchDeploymentDS struct {
config.DSCommon
}
func (d *searchDeploymentDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"cluster_name": schema.StringAttribute{
Required: true,
},
"project_id": schema.StringAttribute{
Required: true,
},
"specs": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_size": schema.StringAttribute{
Computed: true,
},
"node_count": schema.Int64Attribute{
Computed: true,
},
},
},
Computed: true,
},
"state_name": schema.StringAttribute{
Computed: true,
},
},
}
}
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
}
connV2 := d.Client.AtlasV2
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)
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,
}
}