Skip to content

Commit eb91e34

Browse files
committed
Support imports on plugin framework index
1 parent b38ec22 commit eb91e34

File tree

6 files changed

+180
-6
lines changed

6 files changed

+180
-6
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package index
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/attr"
7+
"github.com/hashicorp/terraform-plugin-framework/path"
8+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
9+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
10+
)
11+
12+
type rawSettingsPlanModifier struct{}
13+
14+
func (p rawSettingsPlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
15+
for _, key := range dynamicSettingsKeys {
16+
attrPath := path.Root(convertSettingsKeyToTFFieldKey(key))
17+
var cfgValue, stateValue attr.Value
18+
19+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, attrPath, &cfgValue)...)
20+
if resp.Diagnostics.HasError() {
21+
return
22+
}
23+
24+
resp.Diagnostics.Append(req.State.GetAttribute(ctx, attrPath, &stateValue)...)
25+
if resp.Diagnostics.HasError() {
26+
return
27+
}
28+
29+
if cfgValue.IsUnknown() {
30+
continue
31+
}
32+
33+
if cfgValue.Equal(stateValue) {
34+
continue
35+
}
36+
37+
resp.PlanValue = basetypes.NewStringUnknown()
38+
return
39+
}
40+
}
41+
42+
func (p rawSettingsPlanModifier) Description(_ context.Context) string {
43+
return "Sets the value to Unknown only if any dynamic settings have been changed"
44+
}
45+
46+
func (p rawSettingsPlanModifier) MarkdownDescription(ctx context.Context) string {
47+
return p.Description(ctx)
48+
}

internal/elasticsearch/index/index/resource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import (
55

66
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
77
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/path"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
)
1011

12+
// Ensure provider defined types fully satisfy framework interfaces
13+
var _ resource.Resource = &Resource{}
14+
var _ resource.ResourceWithConfigure = &Resource{}
15+
var _ resource.ResourceWithImportState = &Resource{}
16+
1117
type Resource struct {
1218
client *clients.ApiClient
1319
}
@@ -33,3 +39,8 @@ func (r *Resource) Configure(ctx context.Context, request resource.ConfigureRequ
3339
func (r *Resource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
3440
response.TypeName = request.ProviderTypeName + "_elasticsearch_index"
3541
}
42+
43+
func (r *Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
44+
// Retrieve import ID and save to id attribute
45+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
46+
}

internal/elasticsearch/index/index/schema.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ import (
2626
"github.com/hashicorp/terraform-plugin-framework/types"
2727
)
2828

29-
// Ensure provider defined types fully satisfy framework interfaces
30-
var _ resource.Resource = &Resource{}
31-
var _ resource.ResourceWithConfigure = &Resource{}
32-
33-
// var _ resource.ResourceWithImportState = &Resource{}
34-
3529
func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
3630
resp.Schema = getSchema()
3731
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package planmodifiers
19+
20+
import (
21+
"context"
22+
23+
"github.com/hashicorp/terraform-plugin-framework/attr"
24+
"github.com/hashicorp/terraform-plugin-framework/diag"
25+
"github.com/hashicorp/terraform-plugin-framework/path"
26+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
27+
)
28+
29+
func AttributeChanged(ctx context.Context, p path.Path, plan tfsdk.Plan, state tfsdk.State) (bool, diag.Diagnostics) {
30+
var planValue attr.Value
31+
32+
if diags := plan.GetAttribute(ctx, p, &planValue); diags.HasError() {
33+
return false, diags
34+
}
35+
36+
var stateValue attr.Value
37+
38+
if diags := state.GetAttribute(ctx, p, &stateValue); diags.HasError() {
39+
return false, diags
40+
}
41+
42+
return !planValue.Equal(stateValue), nil
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package planmodifiers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
8+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
9+
)
10+
11+
func BoolUseDefaultIfUnknown(defaultValue bool) boolDefault {
12+
return boolDefault{defaultValue: defaultValue}
13+
}
14+
15+
type boolDefault struct {
16+
defaultValue bool
17+
}
18+
19+
func (bd boolDefault) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
20+
// Do nothing if there is a known planned value.
21+
if !req.PlanValue.IsUnknown() {
22+
return
23+
}
24+
25+
// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
26+
if req.ConfigValue.IsUnknown() {
27+
return
28+
}
29+
30+
resp.PlanValue = basetypes.NewBoolValue(bd.defaultValue)
31+
}
32+
33+
func (bd boolDefault) Description(context.Context) string {
34+
return fmt.Sprintf("Sets the value to [%t] if unknown", bd.defaultValue)
35+
}
36+
37+
func (bd boolDefault) MarkdownDescription(ctx context.Context) string {
38+
return bd.Description(ctx)
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package planmodifiers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
8+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
9+
)
10+
11+
func StringUseDefaultIfUnknown(defaultValue string) stringDefault {
12+
return stringDefault{defaultValue: defaultValue}
13+
}
14+
15+
type stringDefault struct {
16+
defaultValue string
17+
}
18+
19+
func (bd stringDefault) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
20+
// Do nothing if there is a known planned value.
21+
if !req.PlanValue.IsUnknown() {
22+
return
23+
}
24+
25+
// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
26+
if req.ConfigValue.IsUnknown() {
27+
return
28+
}
29+
30+
resp.PlanValue = basetypes.NewStringValue(bd.defaultValue)
31+
}
32+
33+
func (bd stringDefault) Description(context.Context) string {
34+
return fmt.Sprintf("Sets the value to [%s] if unknown", bd.defaultValue)
35+
}
36+
37+
func (bd stringDefault) MarkdownDescription(ctx context.Context) string {
38+
return bd.Description(ctx)
39+
}

0 commit comments

Comments
 (0)