-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathresource.go
206 lines (179 loc) · 7.61 KB
/
resource.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package searchdeployment
import (
"context"
"errors"
"regexp"
"time"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)
var _ resource.ResourceWithConfigure = &searchDeploymentRS{}
var _ resource.ResourceWithImportState = &searchDeploymentRS{}
const searchDeploymentName = "search_deployment"
func Resource() resource.Resource {
return &searchDeploymentRS{
RSCommon: config.RSCommon{
ResourceName: searchDeploymentName,
},
}
}
type searchDeploymentRS struct {
config.RSCommon
}
func (r *searchDeploymentRS) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = ResourceSchema(ctx)
conversion.UpdateSchemaDescription(&resp.Schema)
}
const defaultSearchNodeTimeout time.Duration = 3 * time.Hour
const minTimeoutCreateUpdate time.Duration = 1 * time.Minute
const minTimeoutDelete time.Duration = 30 * time.Second
func retryTimeConfig(configuredTimeout, minTimeout time.Duration) retrystrategy.TimeConfig {
return retrystrategy.TimeConfig{
Timeout: configuredTimeout,
MinTimeout: minTimeout,
Delay: 1 * time.Minute,
}
}
func (r *searchDeploymentRS) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var searchDeploymentPlan TFSearchDeploymentRSModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &searchDeploymentPlan)...)
if resp.Diagnostics.HasError() {
return
}
// TODO: update before merging to master: connV2 := d.Client.AtlasV2
connV2 := r.Client.AtlasPreview
projectID := searchDeploymentPlan.ProjectID.ValueString()
clusterName := searchDeploymentPlan.ClusterName.ValueString()
searchDeploymentReq := NewSearchDeploymentReq(ctx, &searchDeploymentPlan)
if _, _, err := connV2.AtlasSearchApi.CreateAtlasSearchDeployment(ctx, projectID, clusterName, &searchDeploymentReq).Execute(); err != nil {
resp.Diagnostics.AddError("error during search deployment creation", err.Error())
return
}
createTimeout, diags := searchDeploymentPlan.Timeouts.Create(ctx, defaultSearchNodeTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
retryTimeConfig(createTimeout, minTimeoutCreateUpdate))
if err != nil {
resp.Diagnostics.AddError("error during search deployment creation", err.Error())
return
}
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, newSearchNodeModel)...)
}
func (r *searchDeploymentRS) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var searchDeploymentPlan TFSearchDeploymentRSModel
resp.Diagnostics.Append(req.State.Get(ctx, &searchDeploymentPlan)...)
if resp.Diagnostics.HasError() {
return
}
// TODO: update before merging to master: connV2 := d.Client.AtlasV2
connV2 := r.Client.AtlasPreview
projectID := searchDeploymentPlan.ProjectID.ValueString()
clusterName := searchDeploymentPlan.ClusterName.ValueString()
deploymentResp, getResp, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute()
if err != nil {
if validate.StatusNotFound(getResp) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("error getting search deployment information", err.Error())
return
}
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, newSearchNodeModel)...)
}
func (r *searchDeploymentRS) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var searchDeploymentPlan TFSearchDeploymentRSModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &searchDeploymentPlan)...)
if resp.Diagnostics.HasError() {
return
}
// TODO: update before merging to master: connV2 := d.Client.AtlasV2
connV2 := r.Client.AtlasPreview
projectID := searchDeploymentPlan.ProjectID.ValueString()
clusterName := searchDeploymentPlan.ClusterName.ValueString()
searchDeploymentReq := NewSearchDeploymentReq(ctx, &searchDeploymentPlan)
if _, _, err := connV2.AtlasSearchApi.UpdateAtlasSearchDeployment(ctx, projectID, clusterName, &searchDeploymentReq).Execute(); err != nil {
resp.Diagnostics.AddError("error during search deployment update", err.Error())
return
}
updateTimeout, diags := searchDeploymentPlan.Timeouts.Update(ctx, defaultSearchNodeTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
retryTimeConfig(updateTimeout, minTimeoutCreateUpdate))
if err != nil {
resp.Diagnostics.AddError("error during search deployment update", err.Error())
return
}
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, newSearchNodeModel)...)
}
func (r *searchDeploymentRS) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var searchDeploymentState *TFSearchDeploymentRSModel
resp.Diagnostics.Append(req.State.Get(ctx, &searchDeploymentState)...)
if resp.Diagnostics.HasError() {
return
}
// TODO: update before merging to master: connV2 := d.Client.AtlasV2
connV2 := r.Client.AtlasPreview
projectID := searchDeploymentState.ProjectID.ValueString()
clusterName := searchDeploymentState.ClusterName.ValueString()
if _, _, err := connV2.AtlasSearchApi.DeleteAtlasSearchDeployment(ctx, projectID, clusterName).Execute(); err != nil {
resp.Diagnostics.AddError("error during search deployment delete", err.Error())
return
}
deleteTimeout, diags := searchDeploymentState.Timeouts.Delete(ctx, defaultSearchNodeTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if err := WaitSearchNodeDelete(ctx, projectID, clusterName, connV2.AtlasSearchApi, retryTimeConfig(deleteTimeout, minTimeoutDelete)); err != nil {
resp.Diagnostics.AddError("error during search deployment delete", err.Error())
return
}
}
func (r *searchDeploymentRS) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
projectID, clusterName, err := splitSearchNodeImportID(req.ID)
if err != nil {
resp.Diagnostics.AddError("error splitting search deployment import ID", err.Error())
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cluster_name"), clusterName)...)
if resp.Diagnostics.HasError() {
return
}
}
func splitSearchNodeImportID(id string) (projectID, clusterName string, err error) {
var re = regexp.MustCompile(`(?s)^([0-9a-fA-F]{24})-(.*)$`)
parts := re.FindStringSubmatch(id)
if len(parts) != 3 {
err = errors.New("use the format {project_id}-{cluster_name}")
return
}
projectID = parts[1]
clusterName = parts[2]
return
}