@@ -2,35 +2,65 @@ package datafeed
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67
78 "github.com/elastic/terraform-provider-elasticstack/internal/asyncutils"
9+ "github.com/elastic/terraform-provider-elasticstack/internal/clients"
810 "github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
11+ "github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
12+ "github.com/hashicorp/terraform-plugin-framework/diag"
913)
1014
11- // getDatafeedState returns the current state of a datafeed
12- func ( r * datafeedResource ) getDatafeedState ( ctx context.Context , datafeedId string ) (string , error ) {
13- statsResponse , diags := elasticsearch .GetDatafeedStats (ctx , r . client , datafeedId )
15+ // GetDatafeedState returns the current state of a datafeed
16+ func GetDatafeedState ( ctx context.Context , client * clients. ApiClient , datafeedId string ) (* string , diag. Diagnostics ) {
17+ statsResponse , diags := elasticsearch .GetDatafeedStats (ctx , client , datafeedId )
1418 if diags .HasError () {
15- return "" , fmt . Errorf ( "failed to get datafeed stats: %v" , diags )
19+ return nil , diags
1620 }
1721
1822 if statsResponse == nil {
19- return "" , fmt . Errorf ( "datafeed %s not found" , datafeedId )
23+ return nil , nil
2024 }
2125
22- return statsResponse .State , nil
26+ return & statsResponse .State , nil
2327}
2428
25- // waitForDatafeedState waits for a datafeed to reach the desired state
26- func (r * datafeedResource ) waitForDatafeedState (ctx context.Context , datafeedId , desiredState string ) error {
29+ var terminalDatafeedStates = map [string ]struct {}{
30+ "stopped" : {},
31+ "started" : {},
32+ }
33+
34+ var errDatafeedInUndesiredState = errors .New ("datafeed stuck in undesired state" )
35+
36+ // 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 ) {
2738 stateChecker := func (ctx context.Context ) (bool , error ) {
28- currentState , err := r .getDatafeedState (ctx , datafeedId )
29- if err != nil {
30- return false , err
39+ currentState , diags := GetDatafeedState (ctx , client , datafeedId )
40+ if diags .HasError () {
41+ return false , diagutil .FwDiagsAsError (diags )
42+ }
43+
44+ if currentState == nil {
45+ return false , fmt .Errorf ("datafeed %s not found" , datafeedId )
46+ }
47+
48+ if * currentState == desiredState {
49+ return true , nil
50+ }
51+
52+ _ , isInTerminalState := terminalDatafeedStates [* currentState ]
53+ if isInTerminalState {
54+ return false , fmt .Errorf ("%w: datafeed is in state [%s] but desired state is [%s]" , errDatafeedInUndesiredState , * currentState , desiredState )
3155 }
32- return currentState == desiredState , nil
56+
57+ return false , nil
58+ }
59+
60+ err := asyncutils .WaitForStateTransition (ctx , "datafeed" , datafeedId , stateChecker )
61+ if errors .Is (err , errDatafeedInUndesiredState ) {
62+ return false , nil
3363 }
3464
35- return asyncutils . WaitForStateTransition ( ctx , "datafeed" , datafeedId , stateChecker )
65+ return err == nil , diagutil . FrameworkDiagFromError ( err )
3666}
0 commit comments