@@ -5,10 +5,12 @@ import (
55 "encoding/json"
66
77 "github.com/elastic/terraform-provider-elasticstack/internal/models"
8+ "github.com/elastic/terraform-provider-elasticstack/internal/utils"
89 "github.com/elastic/terraform-provider-elasticstack/internal/utils/customtypes"
910 "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1011 "github.com/hashicorp/terraform-plugin-framework/attr"
1112 fwdiags "github.com/hashicorp/terraform-plugin-framework/diag"
13+ "github.com/hashicorp/terraform-plugin-framework/path"
1214 "github.com/hashicorp/terraform-plugin-framework/types"
1315 "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1416)
@@ -60,20 +62,11 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
6062 apiModel := & models.Datafeed {
6163 DatafeedId : m .DatafeedID .ValueString (),
6264 JobId : m .JobID .ValueString (),
63- }
64-
65- // Convert indices
66- if ! m .Indices .IsNull () && ! m .Indices .IsUnknown () {
67- var indices []string
68- diags .Append (m .Indices .ElementsAs (ctx , & indices , false )... )
69- if diags .HasError () {
70- return nil , diags
71- }
72- apiModel .Indices = indices
65+ Indices : utils .ListTypeToSlice_String (ctx , m .Indices , path .Root ("indices" ), & diags ),
7366 }
7467
7568 // Convert query
76- if ! m . Query . IsNull () && ! m .Query . IsUnknown ( ) {
69+ if utils . IsKnown ( m .Query ) {
7770 var query map [string ]interface {}
7871 diags .Append (m .Query .Unmarshal (& query )... )
7972 if diags .HasError () {
@@ -83,7 +76,7 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
8376 }
8477
8578 // Convert aggregations
86- if ! m . Aggregations . IsNull () && ! m .Aggregations . IsUnknown ( ) {
79+ if utils . IsKnown ( m .Aggregations ) {
8780 var aggregations map [string ]interface {}
8881 diags .Append (m .Aggregations .Unmarshal (& aggregations )... )
8982 if diags .HasError () {
@@ -93,7 +86,7 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
9386 }
9487
9588 // Convert script_fields
96- if ! m . ScriptFields . IsNull () && ! m .ScriptFields . IsUnknown ( ) {
89+ if utils . IsKnown ( m .ScriptFields ) {
9790 var scriptFields map [string ]interface {}
9891 err := json .Unmarshal ([]byte (m .ScriptFields .ValueString ()), & scriptFields )
9992 if err != nil {
@@ -104,7 +97,7 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
10497 }
10598
10699 // Convert runtime_mappings
107- if ! m . RuntimeMappings . IsNull () && ! m .RuntimeMappings . IsUnknown ( ) {
100+ if utils . IsKnown ( m .RuntimeMappings ) {
108101 var runtimeMappings map [string ]interface {}
109102 diags .Append (m .RuntimeMappings .Unmarshal (& runtimeMappings )... )
110103 if diags .HasError () {
@@ -114,31 +107,29 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
114107 }
115108
116109 // Convert scroll_size
117- if ! m . ScrollSize . IsNull () && ! m .ScrollSize . IsUnknown ( ) {
110+ if utils . IsKnown ( m .ScrollSize ) {
118111 scrollSize := int (m .ScrollSize .ValueInt64 ())
119112 apiModel .ScrollSize = & scrollSize
120113 }
121114
122115 // Convert frequency
123- if ! m .Frequency .IsNull () && ! m .Frequency .IsUnknown () {
124- frequency := m .Frequency .ValueString ()
125- apiModel .Frequency = & frequency
116+ if utils .IsKnown (m .Frequency ) {
117+ apiModel .Frequency = m .Frequency .ValueStringPointer ()
126118 }
127119
128120 // Convert query_delay
129- if ! m .QueryDelay .IsNull () && ! m .QueryDelay .IsUnknown () {
130- queryDelay := m .QueryDelay .ValueString ()
131- apiModel .QueryDelay = & queryDelay
121+ if utils .IsKnown (m .QueryDelay ) {
122+ apiModel .QueryDelay = m .QueryDelay .ValueStringPointer ()
132123 }
133124
134125 // Convert max_empty_searches
135- if ! m . MaxEmptySearches . IsNull () && ! m .MaxEmptySearches . IsUnknown ( ) {
126+ if utils . IsKnown ( m .MaxEmptySearches ) {
136127 maxEmptySearches := int (m .MaxEmptySearches .ValueInt64 ())
137128 apiModel .MaxEmptySearches = & maxEmptySearches
138129 }
139130
140131 // Convert chunking_config
141- if ! m . ChunkingConfig . IsNull () && ! m .ChunkingConfig . IsUnknown ( ) {
132+ if utils . IsKnown ( m .ChunkingConfig ) {
142133 var chunkingConfig ChunkingConfig
143134 diags .Append (m .ChunkingConfig .As (ctx , & chunkingConfig , basetypes.ObjectAsOptions {})... )
144135 if diags .HasError () {
@@ -156,53 +147,48 @@ func (m *Datafeed) ToAPIModel(ctx context.Context) (*models.Datafeed, fwdiags.Di
156147 }
157148
158149 // Convert delayed_data_check_config
159- if ! m . DelayedDataCheckConfig . IsNull () && ! m .DelayedDataCheckConfig . IsUnknown ( ) {
150+ if utils . IsKnown ( m .DelayedDataCheckConfig ) {
160151 var delayedDataCheckConfig DelayedDataCheckConfig
161152 diags .Append (m .DelayedDataCheckConfig .As (ctx , & delayedDataCheckConfig , basetypes.ObjectAsOptions {})... )
162153 if diags .HasError () {
163154 return nil , diags
164155 }
165156
166- apiDelayedDataCheckConfig := & models.DelayedDataCheckConfig {}
167- if ! delayedDataCheckConfig .Enabled .IsNull () && ! delayedDataCheckConfig .Enabled .IsUnknown () {
168- enabled := delayedDataCheckConfig .Enabled .ValueBool ()
169- apiDelayedDataCheckConfig .Enabled = & enabled
157+ apiDelayedDataCheckConfig := & models.DelayedDataCheckConfig {
158+ Enabled : delayedDataCheckConfig .Enabled .ValueBoolPointer (),
170159 }
171- if ! delayedDataCheckConfig . CheckWindow . IsNull () && ! delayedDataCheckConfig . CheckWindow . IsUnknown () {
172- checkWindow := delayedDataCheckConfig .CheckWindow . ValueString ()
173- apiDelayedDataCheckConfig .CheckWindow = & checkWindow
160+
161+ if utils . IsKnown ( delayedDataCheckConfig .CheckWindow ) {
162+ apiDelayedDataCheckConfig .CheckWindow = delayedDataCheckConfig . CheckWindow . ValueStringPointer ()
174163 }
175164 apiModel .DelayedDataCheckConfig = apiDelayedDataCheckConfig
176165 }
177166
178167 // Convert indices_options
179- if ! m . IndicesOptions . IsNull () && ! m .IndicesOptions . IsUnknown ( ) {
168+ if utils . IsKnown ( m .IndicesOptions ) {
180169 var indicesOptions IndicesOptions
181170 diags .Append (m .IndicesOptions .As (ctx , & indicesOptions , basetypes.ObjectAsOptions {})... )
182171 if diags .HasError () {
183172 return nil , diags
184173 }
185174
186175 apiIndicesOptions := & models.IndicesOptions {}
187- if ! indicesOptions . ExpandWildcards . IsNull () && ! indicesOptions .ExpandWildcards . IsUnknown ( ) {
176+ if utils . IsKnown ( indicesOptions .ExpandWildcards ) {
188177 var expandWildcards []string
189178 diags .Append (indicesOptions .ExpandWildcards .ElementsAs (ctx , & expandWildcards , false )... )
190179 if diags .HasError () {
191180 return nil , diags
192181 }
193182 apiIndicesOptions .ExpandWildcards = expandWildcards
194183 }
195- if ! indicesOptions .IgnoreUnavailable .IsNull () && ! indicesOptions .IgnoreUnavailable .IsUnknown () {
196- ignoreUnavailable := indicesOptions .IgnoreUnavailable .ValueBool ()
197- apiIndicesOptions .IgnoreUnavailable = & ignoreUnavailable
184+ if utils .IsKnown (indicesOptions .IgnoreUnavailable ) {
185+ apiIndicesOptions .IgnoreUnavailable = indicesOptions .IgnoreUnavailable .ValueBoolPointer ()
198186 }
199- if ! indicesOptions .AllowNoIndices .IsNull () && ! indicesOptions .AllowNoIndices .IsUnknown () {
200- allowNoIndices := indicesOptions .AllowNoIndices .ValueBool ()
201- apiIndicesOptions .AllowNoIndices = & allowNoIndices
187+ if utils .IsKnown (indicesOptions .AllowNoIndices ) {
188+ apiIndicesOptions .AllowNoIndices = indicesOptions .AllowNoIndices .ValueBoolPointer ()
202189 }
203- if ! indicesOptions .IgnoreThrottled .IsNull () && ! indicesOptions .IgnoreThrottled .IsUnknown () {
204- ignoreThrottled := indicesOptions .IgnoreThrottled .ValueBool ()
205- apiIndicesOptions .IgnoreThrottled = & ignoreThrottled
190+ if utils .IsKnown (indicesOptions .IgnoreThrottled ) {
191+ apiIndicesOptions .IgnoreThrottled = indicesOptions .IgnoreThrottled .ValueBoolPointer ()
206192 }
207193 apiModel .IndicesOptions = apiIndicesOptions
208194 }
0 commit comments