Skip to content

Commit f281fd7

Browse files
committed
State constants
1 parent 4d0a347 commit f281fd7

File tree

7 files changed

+28
-17
lines changed

7 files changed

+28
-17
lines changed

internal/elasticsearch/ml/datafeed/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (r *datafeedResource) maybeStopDatafeed(ctx context.Context, datafeedId str
5454
}
5555

5656
// If the datafeed is not running, nothing to stop
57-
if *currentState != "started" && *currentState != "starting" {
57+
if *currentState != StateStarted && *currentState != StateStarting {
5858
return false, diags
5959
}
6060

@@ -66,7 +66,7 @@ func (r *datafeedResource) maybeStopDatafeed(ctx context.Context, datafeedId str
6666
}
6767

6868
// Wait for the datafeed to reach stopped state
69-
_, waitDiags := WaitForDatafeedState(ctx, r.client, datafeedId, "stopped")
69+
_, waitDiags := WaitForDatafeedState(ctx, r.client, datafeedId, StateStopped)
7070
diags.Append(waitDiags...)
7171
if diags.HasError() {
7272
return true, diags

internal/elasticsearch/ml/datafeed/state_utils.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
)
1414

15+
type State string
16+
17+
const (
18+
StateStopped State = "stopped"
19+
StateStarted State = "started"
20+
StateStarting State = "starting"
21+
)
22+
1523
// GetDatafeedState returns the current state of a datafeed
16-
func GetDatafeedState(ctx context.Context, client *clients.ApiClient, datafeedId string) (*string, diag.Diagnostics) {
24+
func GetDatafeedState(ctx context.Context, client *clients.ApiClient, datafeedId string) (*State, diag.Diagnostics) {
1725
statsResponse, diags := elasticsearch.GetDatafeedStats(ctx, client, datafeedId)
1826
if diags.HasError() {
1927
return nil, diags
@@ -23,18 +31,19 @@ func GetDatafeedState(ctx context.Context, client *clients.ApiClient, datafeedId
2331
return nil, nil
2432
}
2533

26-
return &statsResponse.State, nil
34+
state := State(statsResponse.State)
35+
return &state, nil
2736
}
2837

29-
var terminalDatafeedStates = map[string]struct{}{
30-
"stopped": {},
31-
"started": {},
38+
var terminalDatafeedStates = map[State]struct{}{
39+
StateStopped: {},
40+
StateStarted: {},
3241
}
3342

3443
var errDatafeedInUndesiredState = errors.New("datafeed stuck in undesired state")
3544

3645
// WaitForDatafeedState waits for a datafeed to reach the desired state
37-
func WaitForDatafeedState(ctx context.Context, client *clients.ApiClient, datafeedId, desiredState string) (bool, diag.Diagnostics) {
46+
func WaitForDatafeedState(ctx context.Context, client *clients.ApiClient, datafeedId string, desiredState State) (bool, diag.Diagnostics) {
3847
stateChecker := func(ctx context.Context) (bool, error) {
3948
currentState, diags := GetDatafeedState(ctx, client, datafeedId)
4049
if diags.HasError() {

internal/elasticsearch/ml/datafeed/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (r *datafeedResource) update(ctx context.Context, req resource.UpdateReques
6161
}
6262

6363
// Wait for the datafeed to reach started state
64-
_, waitDiags := WaitForDatafeedState(ctx, r.client, datafeedId, "started")
64+
_, waitDiags := WaitForDatafeedState(ctx, r.client, datafeedId, StateStarted)
6565
resp.Diagnostics.Append(waitDiags...)
6666
if resp.Diagnostics.HasError() {
6767
return

internal/elasticsearch/ml/datafeed_state/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *mlDatafeedStateResource) Delete(ctx context.Context, req resource.Delet
3939
}
4040

4141
// If the datafeed is started, stop it when deleting the resource
42-
if *currentState == "started" {
42+
if *currentState == datafeed.StateStarted {
4343
tflog.Info(ctx, fmt.Sprintf("Stopping ML datafeed %s during delete", datafeedId))
4444

4545
// Parse timeout duration
@@ -57,7 +57,7 @@ func (r *mlDatafeedStateResource) Delete(ctx context.Context, req resource.Delet
5757
}
5858

5959
// Wait for the datafeed to stop
60-
_, diags := datafeed.WaitForDatafeedState(ctx, client, datafeedId, "stopped")
60+
_, diags := datafeed.WaitForDatafeedState(ctx, client, datafeedId, datafeed.StateStopped)
6161
resp.Diagnostics.Append(diags...)
6262
if resp.Diagnostics.HasError() {
6363
return

internal/elasticsearch/ml/datafeed_state/read.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
88
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed"
910
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -55,7 +56,7 @@ func (r *mlDatafeedStateResource) read(ctx context.Context, data MLDatafeedState
5556
// Update the data with current information
5657
data.State = types.StringValue(datafeedStats.State)
5758

58-
if datafeedStats.State == "started" {
59+
if datafeed.State(datafeedStats.State) == datafeed.StateStarted {
5960
if datafeedStats.RunningState == nil {
6061
diags.AddWarning("Running state was empty for a started datafeed", "The Elasticsearch API returned an empty running state for a Datafeed which was successfully started. Ignoring start and end response values.")
6162
}

internal/elasticsearch/ml/datafeed_state/schema.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "embed"
66
"regexp"
77

8+
"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/ml/datafeed"
89
"github.com/elastic/terraform-provider-elasticstack/internal/utils/customtypes"
910
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
1011
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
@@ -56,7 +57,7 @@ func GetSchema() schema.Schema {
5657
MarkdownDescription: "The desired state for the ML datafeed. Valid values are `started` and `stopped`.",
5758
Required: true,
5859
Validators: []validator.String{
59-
stringvalidator.OneOf("started", "stopped"),
60+
stringvalidator.OneOf(string(datafeed.StateStarted), string(datafeed.StateStopped)),
6061
},
6162
},
6263
"force": schema.BoolAttribute{

internal/elasticsearch/ml/datafeed_state/update.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ func (r *mlDatafeedStateResource) update(ctx context.Context, plan tfsdk.Plan, s
140140
}
141141

142142
// performStateTransition handles the ML datafeed state transition process
143-
func (r *mlDatafeedStateResource) performStateTransition(ctx context.Context, client *clients.ApiClient, data MLDatafeedStateData, currentState string) (bool, diag.Diagnostics) {
143+
func (r *mlDatafeedStateResource) performStateTransition(ctx context.Context, client *clients.ApiClient, data MLDatafeedStateData, currentState datafeed.State) (bool, diag.Diagnostics) {
144144
datafeedId := data.DatafeedId.ValueString()
145-
desiredState := data.State.ValueString()
145+
desiredState := datafeed.State(data.State.ValueString())
146146
force := data.Force.ValueBool()
147147

148148
// Parse timeout duration
@@ -159,7 +159,7 @@ func (r *mlDatafeedStateResource) performStateTransition(ctx context.Context, cl
159159

160160
// Initiate the state change
161161
switch desiredState {
162-
case "started":
162+
case datafeed.StateStarted:
163163
start, diags := data.GetStartAsString()
164164
if diags.HasError() {
165165
return false, diags
@@ -175,7 +175,7 @@ func (r *mlDatafeedStateResource) performStateTransition(ctx context.Context, cl
175175
if diags.HasError() {
176176
return false, diags
177177
}
178-
case "stopped":
178+
case datafeed.StateStopped:
179179
if diags := elasticsearch.StopDatafeed(ctx, client, datafeedId, force, timeout); diags.HasError() {
180180
return false, diags
181181
}

0 commit comments

Comments
 (0)