-
Notifications
You must be signed in to change notification settings - Fork 123
Fix state consistency issues with ML Datafeed start and end times. #1563
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
Changes from 4 commits
edd7ee8
aec85c7
ecacda9
88a5c1c
b1d6f3d
75ef7d3
a7503b9
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 |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ func GetSchema() schema.Schema { | |
| Computed: true, | ||
| PlanModifiers: []planmodifier.String{ | ||
| stringplanmodifier.UseStateForUnknown(), | ||
| SetUnknownIfStateHasChanges(), | ||
| }, | ||
| }, | ||
| "end": schema.StringAttribute{ | ||
|
Contributor
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. Shall we treat it similarly to “start”? (e.g. use
Member
Author
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.
Contributor
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. Yeah, I know, but I meant making it Computed as well :) |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package datafeed_state | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/utils" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| ) | ||
|
|
||
| // SetUnknownIfStateHasChanges returns a plan modifier that sets the current attribute to unknown | ||
| // if the state attribute has changed between state and config. | ||
| func SetUnknownIfStateHasChanges() planmodifier.String { | ||
| return setUnknownIfStateHasChanges{} | ||
| } | ||
|
|
||
| type setUnknownIfStateHasChanges struct{} | ||
|
|
||
| func (s setUnknownIfStateHasChanges) Description(ctx context.Context) string { | ||
| return "Sets the attribute value to unknown if the state attribute has changed" | ||
| } | ||
|
|
||
| func (s setUnknownIfStateHasChanges) MarkdownDescription(ctx context.Context) string { | ||
| return s.Description(ctx) | ||
| } | ||
|
|
||
| func (s setUnknownIfStateHasChanges) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { | ||
| // Only apply this modifier if we have both state and config | ||
| if req.State.Raw.IsNull() || req.Config.Raw.IsNull() { | ||
| return | ||
| } | ||
|
|
||
| // Continue using the config value if it's explicitly set | ||
| if utils.IsKnown(req.ConfigValue) { | ||
| return | ||
| } | ||
|
|
||
| // Get the state attribute from state and config to check if it has changed | ||
| var stateValue, configValue types.String | ||
| resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("state"), &stateValue)...) | ||
| resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("state"), &configValue)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // If the state attribute has changed between state and config, set the current attribute to Unknown | ||
| if !stateValue.Equal(configValue) { | ||
| resp.PlanValue = types.StringUnknown() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| variable "job_id" { | ||
| description = "The ML job ID" | ||
| type = string | ||
| } | ||
|
|
||
| variable "datafeed_id" { | ||
| description = "The ML datafeed ID" | ||
| type = string | ||
| } | ||
|
|
||
| variable "index_name" { | ||
| description = "The index name" | ||
| type = string | ||
| } | ||
|
|
||
| provider "elasticstack" { | ||
| elasticsearch {} | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_index" "test" { | ||
| name = var.index_name | ||
| deletion_protection = false | ||
| mappings = jsonencode({ | ||
| properties = { | ||
| "@timestamp" = { | ||
| type = "date" | ||
| } | ||
| nginx = { | ||
| properties = { | ||
| access = { | ||
| properties = { | ||
| body_sent = { | ||
| properties = { | ||
| bytes = { | ||
| type = "long" | ||
| } | ||
| } | ||
| } | ||
| geoip = { | ||
| properties = { | ||
| city_name = { | ||
| type = "keyword" | ||
| } | ||
| } | ||
| } | ||
| user_agent = { | ||
| properties = { | ||
| build = { | ||
| type = "keyword" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_anomaly_detection_job" "nginx" { | ||
| job_id = var.job_id | ||
| description = "Anomaly detection for network traffic" | ||
| analysis_config = { | ||
| bucket_span = "15m" | ||
| detectors = [ | ||
| { | ||
| function = "count" | ||
| detector_description = "count" | ||
| }, | ||
| { | ||
| function = "mean" | ||
| field_name = "nginx.access.body_sent.bytes" | ||
| detector_description = "mean(\"nginx.access.body_sent.bytes\")" | ||
| } | ||
| ] | ||
| influencers = ["nginx.access.geoip.city_name", "nginx.access.user_agent.build"] | ||
| model_prune_window = "30d" | ||
| } | ||
| analysis_limits = { | ||
| model_memory_limit = "10MB" | ||
| categorization_examples_limit = 4 | ||
| } | ||
| data_description = { | ||
| time_field = "@timestamp" | ||
| time_format = "epoch_ms" | ||
| } | ||
| model_snapshot_retention_days = 10 | ||
| daily_model_snapshot_retention_after_days = 1 | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_datafeed" "datafeed_nginx" { | ||
| datafeed_id = var.datafeed_id | ||
| job_id = elasticstack_elasticsearch_ml_anomaly_detection_job.nginx.job_id | ||
| query = jsonencode({ | ||
| bool = { | ||
| must = [ | ||
| { | ||
| match_all = {} | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
| indices = [elasticstack_elasticsearch_index.test.name] | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_job_state" "nginx" { | ||
| job_id = elasticstack_elasticsearch_ml_anomaly_detection_job.nginx.job_id | ||
| state = "closed" | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_ml_datafeed_state" "nginx" { | ||
| datafeed_id = elasticstack_elasticsearch_ml_datafeed.datafeed_nginx.datafeed_id | ||
| state = "stopped" | ||
| force = true | ||
|
|
||
| depends_on = [ | ||
| elasticstack_elasticsearch_ml_datafeed.datafeed_nginx, | ||
| elasticstack_elasticsearch_ml_job_state.nginx | ||
| ] | ||
| } |
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.
The variable
diagsis reused for both start and end conversions, shadowing the outerdiagsvariable declared at line 44. This makes it unclear which diagnostics are being accumulated. Use distinct variable names likestartDiagsandendDiagsto avoid confusion.