Skip to content

Commit 5b060d3

Browse files
committed
Make headers a map
1 parent d365c57 commit 5b060d3

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

internal/clients/config/elasticsearch.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/elastic/go-elasticsearch/v8"
1313
fwdiags "github.com/hashicorp/terraform-plugin-framework/diag"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1415
sdkdiags "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -43,15 +44,10 @@ func newElasticsearchConfigFromSDK(d *schema.ResourceData, base baseConfig, key
4344
config.config.Addresses = addrs
4445
}
4546

46-
if headers, ok := esConfig["headers"]; ok && len(headers.([]interface{})) > 0 {
47-
var header_values []string
48-
for _, e := range headers.([]interface{}) {
49-
header_values = append(header_values, e.(string))
50-
}
51-
52-
for _, header := range header_values {
53-
headerParts := strings.Split(header, ":")
54-
config.config.Header.Add(strings.TrimSpace(headerParts[0]), strings.TrimSpace(headerParts[1]))
47+
if headers, ok := esConfig["headers"]; ok && len(headers.(map[string]interface{})) > 0 {
48+
headersMap := headers.(map[string]interface{})
49+
for header, value := range headersMap {
50+
config.config.Header.Add(strings.TrimSpace(header), strings.TrimSpace(value.(string)))
5551
}
5652
}
5753

@@ -157,22 +153,10 @@ func newElasticsearchConfigFromFramework(ctx context.Context, cfg ProviderConfig
157153
config.config.Addresses = endpoints
158154
}
159155

160-
var headers []string
161-
headerDiags := esConfig.Headers.ElementsAs(ctx, &headers, true)
162-
if headerDiags.HasError() {
163-
return nil, diags
164-
}
165-
166-
if len(headers) > 0 {
167-
for _, header := range headers {
168-
headerParts := strings.Split(header, ":")
169-
if len(headerParts) != 2 {
170-
diags.Append(fwdiags.NewErrorDiagnostic("Invalid header format", "Headers must be in the format 'key:value'"))
171-
return nil, diags
172-
}
173-
// trim the strings to remove any leading/trailing whitespace
174-
config.config.Header.Add(strings.TrimSpace(headerParts[0]), strings.TrimSpace(headerParts[1]))
175-
}
156+
for header, value := range esConfig.Headers.Elements() {
157+
strValue := value.(basetypes.StringValue)
158+
// trim the strings to remove any leading/trailing whitespace
159+
config.config.Header.Add(strings.TrimSpace(header), strings.TrimSpace(strValue.ValueString()))
176160
}
177161

178162
if esConfig.BearerToken.ValueString() != "" {

internal/clients/config/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ElasticsearchConnection struct {
1515
BearerToken types.String `tfsdk:"bearer_token"`
1616
ESClientAuthentication types.String `tfsdk:"es_client_authentication"`
1717
Endpoints types.List `tfsdk:"endpoints"`
18-
Headers types.List `tfsdk:"headers"`
18+
Headers types.Map `tfsdk:"headers"`
1919
Insecure types.Bool `tfsdk:"insecure"`
2020
CAFile types.String `tfsdk:"ca_file"`
2121
CAData types.String `tfsdk:"ca_data"`

internal/schema/connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func GetEsFWConnectionBlock(keyName string, isProviderConfiguration bool) fwsche
7272
Sensitive: true,
7373
ElementType: types.StringType,
7474
},
75-
"headers": fwschema.ListAttribute{
75+
"headers": fwschema.MapAttribute{
7676
MarkdownDescription: "A list of headers to be sent with each request to Elasticsearch.",
7777
Optional: true,
7878
Sensitive: true,
@@ -319,7 +319,7 @@ func GetEsConnectionSchema(keyName string, isProviderConfiguration bool) *schema
319319
},
320320
"headers": {
321321
Description: "A list of headers to be sent with each request to Elasticsearch.",
322-
Type: schema.TypeList,
322+
Type: schema.TypeMap,
323323
Optional: true,
324324
Sensitive: true,
325325
Elem: &schema.Schema{

0 commit comments

Comments
 (0)