-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add kibana_saved_object resource #948
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "elasticstack_kibana_saved_object Resource - terraform-provider-elasticstack" | ||
subcategory: "" | ||
description: |- | ||
Import a Kibana saved object | ||
--- | ||
|
||
# elasticstack_kibana_saved_object (Resource) | ||
|
||
Import a Kibana saved object | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `object` (String) Kibana object to import in JSON format | ||
|
||
### Optional | ||
|
||
- `space_id` (String) An identifier for the space. If space_id is not provided, the default space is used. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) Extracted ID from the object | ||
- `imported` (String) Kibana object imported. | ||
- `type` (String) Extracted type from the object |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package saved_object | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var model ksoModelV0 | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
err := model.UpdateModelWithObject() | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to update model from object", err.Error()) | ||
return | ||
} | ||
|
||
kibanaClient, err := r.client.GetKibanaClient() | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to get kibana client", err.Error()) | ||
return | ||
} | ||
|
||
result, err := kibanaClient.KibanaSavedObject.Import([]byte(model.Imported.ValueString()), false, model.SpaceID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to import saved object", err.Error()) | ||
return | ||
} | ||
|
||
var success any | ||
var ok bool | ||
if success, ok = result["success"]; !ok { | ||
resp.Diagnostics.AddError("failed to import saved object", "success key not found in response") | ||
return | ||
} | ||
if success != true { | ||
resp.Diagnostics.AddError("failed to import saved object", fmt.Sprintf("%v\n", result["errors"])) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package saved_object | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var model ksoModelV0 | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
kibanaClient, err := r.client.GetKibanaClient() | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to get kibana client", err.Error()) | ||
return | ||
} | ||
|
||
if err := kibanaClient.KibanaSavedObject.Delete(model.Type.ValueString(), model.ID.ValueString(), model.SpaceID.ValueString()); err != nil { | ||
resp.Diagnostics.AddError("failed to delete saved object", err.Error()) | ||
return | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package saved_object | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type ksoModelV0 struct { | ||
ID types.String `tfsdk:"id"` | ||
SpaceID types.String `tfsdk:"space_id"` | ||
Object types.String `tfsdk:"object"` | ||
Imported types.String `tfsdk:"imported"` | ||
Type types.String `tfsdk:"type"` | ||
} | ||
|
||
func (m *ksoModelV0) UpdateModelWithObject() error { | ||
var object map[string]any | ||
|
||
err := json.Unmarshal([]byte(m.Object.ValueString()), &object) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if objType, ok := object["type"]; ok { | ||
m.Type = types.StringValue(objType.(string)) | ||
} else { | ||
return errors.New("missing 'type' field in JSON object") | ||
} | ||
|
||
if objId, ok := object["id"]; ok { | ||
m.ID = types.StringValue(objId.(string)) | ||
} else { | ||
return errors.New("missing 'id' field in JSON object") | ||
} | ||
|
||
ksoRemoveUnwantedFields(object) | ||
|
||
imported, err := json.Marshal(object) | ||
if err != nil { | ||
return err | ||
} | ||
m.Imported = types.StringValue(string(imported)) | ||
return nil | ||
} | ||
|
||
func ksoRemoveUnwantedFields(object map[string]any) { | ||
// remove fields carrying state | ||
delete(object, "created_at") | ||
delete(object, "created_by") | ||
delete(object, "updated_at") | ||
delete(object, "updated_by") | ||
delete(object, "version") | ||
delete(object, "migrationVersion") | ||
delete(object, "namespaces") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package saved_object | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func (r *Resource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { | ||
var configData ksoModelV0 | ||
|
||
if req.Plan.Raw.IsNull() || req.State.Raw.IsNull() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &configData)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
err := configData.UpdateModelWithObject() | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to update model from object", err.Error()) | ||
return | ||
} | ||
|
||
resp.Plan.SetAttribute(ctx, path.Root("id"), configData.ID) | ||
resp.Plan.SetAttribute(ctx, path.Root("type"), configData.Type) | ||
resp.Plan.SetAttribute(ctx, path.Root("imported"), configData.Imported) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package saved_object | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func (r *Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var model ksoModelV0 | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
kibanaClient, err := r.client.GetKibanaClient() | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to get kibana client", err.Error()) | ||
return | ||
} | ||
|
||
result, err := kibanaClient.KibanaSavedObject.Get(model.Type.ValueString(), model.ID.ValueString(), model.SpaceID.ValueString()) | ||
wandergeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
resp.Diagnostics.AddError("failed to get saved object", err.Error()) | ||
return | ||
} | ||
|
||
ksoRemoveUnwantedFields(result) | ||
|
||
object, err := json.Marshal(result) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to marshal saved object", err.Error()) | ||
return | ||
} | ||
|
||
model.Imported = types.StringValue(string(object)) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,80 @@ | ||||||||
package saved_object | ||||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
|
||||||||
"github.com/elastic/terraform-provider-elasticstack/internal/clients" | ||||||||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||||||||
) | ||||||||
|
||||||||
// Ensure provider defined types fully satisfy framework interfaces | ||||||||
var _ resource.Resource = &Resource{} | ||||||||
var _ resource.ResourceWithConfigure = &Resource{} | ||||||||
var _ resource.ResourceWithModifyPlan = &Resource{} | ||||||||
var _ resource.ResourceWithConfigValidators = &Resource{} | ||||||||
|
||||||||
func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||||||||
resp.Schema = schema.Schema{ | ||||||||
Description: "Import a Kibana saved object", | ||||||||
Attributes: map[string]schema.Attribute{ | ||||||||
"id": schema.StringAttribute{ | ||||||||
Computed: true, | ||||||||
MarkdownDescription: "Extracted ID from the object", | ||||||||
PlanModifiers: []planmodifier.String{ | ||||||||
stringplanmodifier.RequiresReplace(), | ||||||||
}, | ||||||||
}, | ||||||||
// This is needed as the user provided object cannot be cleaned up | ||||||||
// see https://discuss.hashicorp.com/t/using-modifyplan-with-a-custom-provider/47690 | ||||||||
// We thus store a copy of the object with some fields removed | ||||||||
"imported": schema.StringAttribute{ | ||||||||
Computed: true, | ||||||||
MarkdownDescription: "Kibana object imported.", | ||||||||
PlanModifiers: []planmodifier.String{ | ||||||||
stringplanmodifier.UseStateForUnknown(), | ||||||||
}, | ||||||||
}, | ||||||||
"space_id": schema.StringAttribute{ | ||||||||
Description: "An identifier for the space. If space_id is not provided, the default space is used.", | ||||||||
Optional: true, | ||||||||
PlanModifiers: []planmodifier.String{ | ||||||||
stringplanmodifier.RequiresReplace(), | ||||||||
}, | ||||||||
}, | ||||||||
"object": schema.StringAttribute{ | ||||||||
Description: "Kibana object to import in JSON format", | ||||||||
Required: true, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The index resource has an example of using this. It means simply restructuring the JSON won't be seen as a diff. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, thanks for pointing that out |
||||||||
}, | ||||||||
"type": schema.StringAttribute{ | ||||||||
Computed: true, | ||||||||
MarkdownDescription: "Extracted type from the object", | ||||||||
PlanModifiers: []planmodifier.String{ | ||||||||
stringplanmodifier.RequiresReplace(), | ||||||||
}, | ||||||||
}, | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
type Resource struct { | ||||||||
client *clients.ApiClient | ||||||||
} | ||||||||
|
||||||||
func (r *Resource) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { | ||||||||
client, diags := clients.ConvertProviderData(request.ProviderData) | ||||||||
response.Diagnostics.Append(diags...) | ||||||||
r.client = client | ||||||||
} | ||||||||
|
||||||||
func (r *Resource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { | ||||||||
response.TypeName = request.ProviderTypeName + "_kibana_saved_object" | ||||||||
} | ||||||||
|
||||||||
func (r *Resource) ConfigValidators(context.Context) []resource.ConfigValidator { | ||||||||
return []resource.ConfigValidator{ | ||||||||
&KibanaSavedObjectValidator{}, | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package saved_object | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func (r *Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var model ksoModelV0 | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
kibanaClient, err := r.client.GetKibanaClient() | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to get kibana client", err.Error()) | ||
return | ||
} | ||
|
||
result, err := kibanaClient.KibanaSavedObject.Import([]byte(model.Imported.ValueString()), true, model.SpaceID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("failed to import saved object", err.Error()) | ||
return | ||
} | ||
|
||
var success any | ||
var ok bool | ||
if success, ok = result["success"]; !ok { | ||
resp.Diagnostics.AddError("failed to import saved object", "success key not found in response") | ||
return | ||
} | ||
if success != true { | ||
resp.Diagnostics.AddError("failed to import saved object", fmt.Sprintf("%v\n", result["errors"])) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the use case here, I wonder if we should just always overwrite existing objects. It would simplify the workflow for bringing existing objects into TF, especially in a resource where a proper
import
handler is kind of tricky given the API constraints.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered about that and initially had overwrite set to
true
. I agree it would simplify importing resources into TF to have it set totrue
.