|
| 1 | +package agent_configuration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/generated/kbapi" |
| 9 | + "github.com/elastic/terraform-provider-elasticstack/internal/utils" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | +) |
| 13 | + |
| 14 | +func (r *resourceAgentConfiguration) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 15 | + var state AgentConfiguration |
| 16 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 17 | + if resp.Diagnostics.HasError() { |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + kibana, err := r.client.GetKibanaOapiClient() |
| 22 | + if err != nil { |
| 23 | + resp.Diagnostics.AddError("Unable to get Kibana client", err.Error()) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + apiResp, err := kibana.API.GetAgentConfigurationsWithResponse(ctx, &kbapi.GetAgentConfigurationsParams{}) |
| 28 | + if err != nil { |
| 29 | + resp.Diagnostics.AddError("Failed to get APM agent configurations", err.Error()) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + if diags := utils.CheckHttpErrorFromFW(apiResp.HTTPResponse, "Failed to get APM agent configurations"); diags.HasError() { |
| 34 | + resp.Diagnostics.Append(diags...) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if apiResp.JSON200 == nil { |
| 39 | + resp.Diagnostics.AddError("Failed to get APM agent configurations from body", "Expected 200 response body to not be nil") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + idFromState := state.ID.ValueString() |
| 44 | + var foundConfig *kbapi.APMUIAgentConfigurationObject |
| 45 | + for _, config := range *apiResp.JSON200.Configurations { |
| 46 | + if config.Service.Name == nil { |
| 47 | + continue |
| 48 | + } |
| 49 | + idFromAPI := createAgentConfigIDfromAPI(config) |
| 50 | + if idFromAPI == idFromState { |
| 51 | + foundConfig = &config |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if foundConfig == nil { |
| 57 | + resp.State.RemoveResource(ctx) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + state.ID = types.StringValue(idFromState) |
| 62 | + state.ServiceName = types.StringPointerValue(foundConfig.Service.Name) |
| 63 | + state.ServiceEnvironment = types.StringPointerValue(foundConfig.Service.Environment) |
| 64 | + state.AgentName = types.StringPointerValue(foundConfig.AgentName) |
| 65 | + |
| 66 | + stringSettings := make(map[string]interface{}) |
| 67 | + if foundConfig.Settings != nil { |
| 68 | + for k, v := range foundConfig.Settings { |
| 69 | + stringSettings[k] = fmt.Sprintf("%v", v) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + settings, diags := types.MapValueFrom(ctx, types.StringType, stringSettings) |
| 74 | + resp.Diagnostics.Append(diags...) |
| 75 | + if resp.Diagnostics.HasError() { |
| 76 | + return |
| 77 | + } |
| 78 | + state.Settings = settings |
| 79 | + |
| 80 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 81 | +} |
| 82 | + |
| 83 | +func createAgentConfigIDfromAPI(config kbapi.APMUIAgentConfigurationObject) string { |
| 84 | + parts := []string{*config.Service.Name} |
| 85 | + if config.Service.Environment != nil && *config.Service.Environment != "" { |
| 86 | + parts = append(parts, *config.Service.Environment) |
| 87 | + } |
| 88 | + return strings.Join(parts, ":") |
| 89 | +} |
0 commit comments