Skip to content

Conversation

RJiraya
Copy link
Contributor

@RJiraya RJiraya commented Oct 13, 2025

Pull Request: Add Labels Field to Kibana Synthetics Monitor Resource

Summary

This PR adds support for the labels field to the Kibana synthetics monitor terraform resource, allowing users to associate key-value pairs with monitors for filtering and grouping purposes.

Changes Made

API Layer Updates

  • File: libs/go-kibana-rest/kbapi/api.kibana_synthetics.go
  • Added Labels map[string]string field to SyntheticsMonitorConfig struct with JSON tag "labels,omitempty"
  • Added Labels map[string]string field to SyntheticsMonitor struct with JSON tag "labels,omitempty"

Terraform Schema Updates

  • File: internal/kibana/synthetics/schema.go
  • Added Labels types.Map field to tfModelV0 struct with tfsdk tag "labels"
  • Added labels field as schema.MapAttribute with types.StringType elements to monitorConfigSchema
  • Added helper functions:
    • MapStringValue(): Converts map[string]string to types.Map
    • ValueStringMap(): Converts types.Map to map[string]string
  • Updated conversion functions:
    • Modified toModelV0() to handle labels from API response
    • Modified toSyntheticsMonitorConfig() to include labels in API requests

Documentation Updates

  • File: docs/resources/kibana_synthetics_monitor.md
  • Added labels field to the optional parameters list with description
  • Added example usage showing labels with environment and team values

Usage Example

resource "elasticstack_kibana_synthetics_monitor" "my_monitor" {
  name      = "Example http monitor"
  space_id  = "default"
  schedule  = 10
  locations = ["us_west"]
  enabled   = false
  tags      = ["tag"]
  labels = {
    environment = "production"
    team        = "platform"
    service     = "web-app"
  }
  alert = {
    status = {
      enabled = true
    }
    tls = {
      enabled = false
    }
  }
  service_name = "example apm service"
  timeout      = 30
  http = {
    url                     = "http://localhost:8080"
    ssl_verification_mode   = "full"
    ssl_supported_protocols = ["TLSv1.2"]
    max_redirects           = "10"
    mode                    = "all"
    ipv4                    = true
    ipv6                    = true
  }
}

API Compatibility

  • Follows the official Kibana Synthetics API specification
  • Labels field is optional and backward compatible
  • Existing configurations continue to work without modification
  • Labels are defined as key-value pairs that can be used for filtering and grouping monitors

Technical Details

Field Definition

  • Type: Map of String in Terraform schema
  • API Type: map[string]string in Go structs
  • JSON Tag: "labels,omitempty" - field is omitted when empty
  • Description: "Key-value pairs of labels to associate with the monitor. Labels can be used for filtering and grouping monitors."

Implementation Notes

  • The implementation maintains consistency with existing field patterns in the codebase
  • Proper null/unknown value handling for Terraform types
  • Conversion functions handle empty/null maps gracefully
  • No breaking changes to existing functionality

Unit Tests Added

  • TestMapStringValue: Tests conversion from map[string]string to types.Map
    • Covers nil maps, empty maps, and populated maps
  • TestValueStringMap: Tests conversion from types.Map to map[string]string
    • Covers null maps, unknown maps, empty maps, and populated maps
  • TestLabelsFieldConversion: Tests labels field conversion in API response processing
    • Tests nil labels, empty labels, and populated labels scenarios
  • TestToSyntheticsMonitorConfig: Tests labels field conversion in API request creation
    • Verifies proper handling of labels in both directions
  • Updated existing test cases: All existing monitor type tests updated to include Labels field

Acceptance Tests Added

  • File: internal/kibana/synthetics/acc_test.go
  • Test Function: TestSyntheticMonitorLabelsResource
  • Test Configurations:
    • httpMonitorLabelsConfig: Creates monitor with 3 labels (environment=production, team=platform, service=web-app)
    • httpMonitorLabelsUpdated: Updates label values (environment=staging, team=platform-updated, service=web-app-v2)
    • httpMonitorLabelsRemoved: Removes all labels to test round-trip consistency

Testing Checklist

  • Unit tests added and pass
  • Unit tests cover all conversion functions (MapStringValue, ValueStringMap)
  • Unit tests cover labels field conversion in both directions
  • Unit tests cover null, empty, and populated label scenarios
  • All existing unit tests updated to include Labels field
  • Acceptance tests added and pass
  • Backward compatibility verified (Labels field is optional)
  • Documentation generation works correctly

Files Modified

  1. libs/go-kibana-rest/kbapi/api.kibana_synthetics.go - Added Labels field to API structs
  2. internal/kibana/synthetics/schema.go - Added Labels field to Terraform schema and conversion functions
  3. internal/kibana/synthetics/schema_test.go - Added comprehensive unit tests for Labels field
  4. internal/kibana/synthetics/acc_test.go - Added acceptance tests for Labels field (TestSyntheticMonitorLabelsResource)
  5. docs/resources/kibana_synthetics_monitor.md - Updated documentation with Labels field

Pre-submission Checklist

  • Code follows project conventions
  • Documentation updated with field description
  • Usage examples provided
  • Backward compatibility maintained
  • API specification compliance verified
  • Tests added/updated (comprehensive unit tests added)
  • Changelog entry

Additional Notes

  • The labels field supports any string key-value pairs
  • Labels are optional and can be omitted from configuration
  • Implementation follows the existing patterns for similar fields like tags
  • The feature aligns with Kibana's native labeling capabilities for synthetics monitors

Changelog Entry

### Added
- Added `labels` field to `elasticstack_kibana_synthetics_monitor` resource for associating key-value pairs with monitors ([#1360](https://github.com/elastic/terraform-provider-elasticstack/pull/1360))

Copy link

cla-checker-service bot commented Oct 13, 2025

💚 CLA has been signed

@RJiraya RJiraya force-pushed the feature/add-labels-field-to-synthetics-monitor branch from 7b7c7b5 to 219216f Compare October 13, 2025 14:00
Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @RJiraya are you able to add an acceptance test step covering the labels attribute and update CHANGELOG.md as well please?

@RJiraya RJiraya force-pushed the feature/add-labels-field-to-synthetics-monitor branch from 219216f to 4f911ed Compare October 14, 2025 08:10
- Add labels field to SyntheticsMonitorConfig and SyntheticsMonitor structs
- Add labels field to terraform schema as MapAttribute
- Add conversion functions for labels between API and terraform types
- Update documentation with labels field description and example
- Labels support key-value pairs for filtering and grouping monitors
@RJiraya RJiraya force-pushed the feature/add-labels-field-to-synthetics-monitor branch from 4f911ed to 86f59e9 Compare October 14, 2025 08:20
@RJiraya
Copy link
Contributor Author

RJiraya commented Oct 14, 2025

@tobio it's updated :)

Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it, ty!

@tobio tobio merged commit 1a96a43 into elastic:main Oct 14, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants