|
| 1 | +package export_saved_objects |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/generated/kbapi" |
| 9 | + "github.com/elastic/terraform-provider-elasticstack/internal/clients" |
| 10 | + "github.com/elastic/terraform-provider-elasticstack/internal/utils" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | +) |
| 15 | + |
| 16 | +// Read refreshes the Terraform state with the latest data. |
| 17 | +func (d *dataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 18 | + var config dataSourceModel |
| 19 | + |
| 20 | + // Read configuration |
| 21 | + diags := req.Config.Get(ctx, &config) |
| 22 | + resp.Diagnostics.Append(diags...) |
| 23 | + if resp.Diagnostics.HasError() { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + // Get Kibana client |
| 28 | + oapiClient, err := d.client.GetKibanaOapiClient() |
| 29 | + if err != nil { |
| 30 | + resp.Diagnostics.AddError("unable to get Kibana client", err.Error()) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Set default space_id if not provided |
| 35 | + spaceId := "default" |
| 36 | + if !config.SpaceID.IsNull() && !config.SpaceID.IsUnknown() { |
| 37 | + spaceId = config.SpaceID.ValueString() |
| 38 | + } |
| 39 | + |
| 40 | + objectsList := utils.ListTypeToSlice(ctx, config.Objects, path.Root("objects"), &resp.Diagnostics, func(item objectModel, meta utils.ListMeta) struct { |
| 41 | + Id string `json:"id"` |
| 42 | + Type string `json:"type"` |
| 43 | + } { |
| 44 | + return struct { |
| 45 | + Id string `json:"id"` |
| 46 | + Type string `json:"type"` |
| 47 | + }{ |
| 48 | + Id: item.ID.ValueString(), |
| 49 | + Type: item.Type.ValueString(), |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + // Set default values for boolean options |
| 54 | + excludeExportDetails := true |
| 55 | + if !config.ExcludeExportDetails.IsNull() && !config.ExcludeExportDetails.IsUnknown() { |
| 56 | + excludeExportDetails = config.ExcludeExportDetails.ValueBool() |
| 57 | + } |
| 58 | + |
| 59 | + includeReferencesDeep := true |
| 60 | + if !config.IncludeReferencesDeep.IsNull() && !config.IncludeReferencesDeep.IsUnknown() { |
| 61 | + includeReferencesDeep = config.IncludeReferencesDeep.ValueBool() |
| 62 | + } |
| 63 | + |
| 64 | + // Create request body |
| 65 | + body := kbapi.PostSavedObjectsExportJSONRequestBody{ |
| 66 | + ExcludeExportDetails: &excludeExportDetails, |
| 67 | + IncludeReferencesDeep: &includeReferencesDeep, |
| 68 | + Objects: &objectsList, |
| 69 | + } |
| 70 | + |
| 71 | + // Make the API call |
| 72 | + apiResp, err := oapiClient.API.PostSavedObjectsExportWithResponse(ctx, body) |
| 73 | + if err != nil { |
| 74 | + resp.Diagnostics.AddError("API call failed", fmt.Sprintf("Unable to export saved objects: %v", err)) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if apiResp.StatusCode() != http.StatusOK { |
| 79 | + resp.Diagnostics.AddError( |
| 80 | + "Unexpected API response", |
| 81 | + fmt.Sprintf("Unexpected status code from server: got HTTP %d, response: %s", apiResp.StatusCode(), string(apiResp.Body)), |
| 82 | + ) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Create composite ID for state tracking |
| 87 | + compositeID := &clients.CompositeId{ClusterId: spaceId, ResourceId: "export"} |
| 88 | + |
| 89 | + // Set the state |
| 90 | + var state dataSourceModel |
| 91 | + state.ID = types.StringValue(compositeID.String()) |
| 92 | + state.SpaceID = types.StringValue(spaceId) |
| 93 | + state.Objects = config.Objects |
| 94 | + state.ExcludeExportDetails = types.BoolValue(excludeExportDetails) |
| 95 | + state.IncludeReferencesDeep = types.BoolValue(includeReferencesDeep) |
| 96 | + state.ExportedObjects = types.StringValue(string(apiResp.Body)) |
| 97 | + |
| 98 | + // Set state |
| 99 | + diags = resp.State.Set(ctx, &state) |
| 100 | + resp.Diagnostics.Append(diags...) |
| 101 | +} |
0 commit comments