From 97beefe54c905207fb0f65030d17ff67b42e7785 Mon Sep 17 00:00:00 2001 From: Keisuke Nitta Date: Thu, 9 May 2024 19:51:21 +0900 Subject: [PATCH 1/4] chore(deps): update github.com/mackerelio/mackerel-client-go from v0.26.0 to v0.31.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8f72952..44a17ff 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/golangci/golangci-lint v1.50.1 github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0 - github.com/mackerelio/mackerel-client-go v0.26.0 + github.com/mackerelio/mackerel-client-go v0.31.0 ) require ( diff --git a/go.sum b/go.sum index 47f255f..a2a25bb 100644 --- a/go.sum +++ b/go.sum @@ -514,6 +514,8 @@ github.com/lufeee/execinquery v1.2.1 h1:hf0Ems4SHcUGBxpGN7Jz78z1ppVkP/837ZlETPCE github.com/lufeee/execinquery v1.2.1/go.mod h1:EC7DrEKView09ocscGHC+apXMIaorh4xqSxS/dy8SbM= github.com/mackerelio/mackerel-client-go v0.26.0 h1:72hj2zw/rqTUNMNokenRxs6KfJ1aVxRHpZT8X4eOUuA= github.com/mackerelio/mackerel-client-go v0.26.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo= +github.com/mackerelio/mackerel-client-go v0.31.0 h1:oUBwuJzZkvhSFf7C7zpOBej8jM0kITUz7eVMAINIfO4= +github.com/mackerelio/mackerel-client-go v0.31.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= From 9b1b18711e4f8470421cb90708c479edeb67ca0b Mon Sep 17 00:00:00 2001 From: Keisuke Nitta Date: Fri, 10 May 2024 20:42:25 +0900 Subject: [PATCH 2/4] feat: added support for query monitor --- mackerel/data_source_mackerel_monitor.go | 28 +++++ mackerel/data_source_mackerel_monitor_test.go | 65 ++++++++++ mackerel/resource_mackerel_monitor.go | 88 +++++++++++++- mackerel/resource_mackerel_monitor_test.go | 114 ++++++++++++++++++ mackerel/structure_flattens.go | 22 ++++ 5 files changed, 311 insertions(+), 6 deletions(-) diff --git a/mackerel/data_source_mackerel_monitor.go b/mackerel/data_source_mackerel_monitor.go index d384420..5716de5 100644 --- a/mackerel/data_source_mackerel_monitor.go +++ b/mackerel/data_source_mackerel_monitor.go @@ -257,6 +257,34 @@ func dataSourceMackerelMonitor() *schema.Resource { }, }, }, + "query": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query": { + Type: schema.TypeString, + Computed: true, + }, + "legend": { + Type: schema.TypeString, + Computed: true, + }, + "operator": { + Type: schema.TypeString, + Computed: true, + }, + "warning": { + Type: schema.TypeString, + Computed: true, + }, + "critical": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, }, } } diff --git a/mackerel/data_source_mackerel_monitor_test.go b/mackerel/data_source_mackerel_monitor_test.go index ea6b4f6..5d42838 100644 --- a/mackerel/data_source_mackerel_monitor_test.go +++ b/mackerel/data_source_mackerel_monitor_test.go @@ -41,6 +41,7 @@ func TestAccDataSourceMackerelMonitorHostMetric(t *testing.T) { resource.TestCheckResourceAttr(dsName, "external.#", "0"), resource.TestCheckResourceAttr(dsName, "expression.#", "0"), resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), ), }, }, @@ -74,6 +75,7 @@ func TestAccDataSourceMackerelMonitorConnectivity(t *testing.T) { resource.TestCheckResourceAttr(dsName, "external.#", "0"), resource.TestCheckResourceAttr(dsName, "expression.#", "0"), resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), ), }, }, @@ -115,6 +117,7 @@ func TestAccDataSourceMackerelMonitorServiceMetric(t *testing.T) { resource.TestCheckResourceAttr(dsName, "external.#", "0"), resource.TestCheckResourceAttr(dsName, "expression.#", "0"), resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), ), }, }, @@ -162,6 +165,7 @@ func TestAccDataSourceMackerelMonitorExternal(t *testing.T) { ), resource.TestCheckResourceAttr(dsName, "expression.#", "0"), resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), ), }, }, @@ -197,6 +201,7 @@ func TestAccDataSourceMackerelMonitorExpression(t *testing.T) { resource.TestCheckResourceAttr(dsName, "expression.0.critical", "0.9"), ), resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), ), }, }, @@ -233,6 +238,44 @@ func TestAccDataSourceMackerelMonitorAnomalyDetection(t *testing.T) { resource.TestCheckResourceAttr(dsName, "anomaly_detection.0.training_period_from", "1577836800"), resource.TestCheckResourceAttr(dsName, "anomaly_detection.0.scopes.#", "1"), ), + resource.TestCheckResourceAttr(dsName, "query.#", "0"), + ), + }, + }, + }) +} + +func TestAccDataSourceMackerelMonitorQuery(t *testing.T) { + dsName := "data.mackerel_monitor.foo" + rand := acctest.RandString(5) + name := fmt.Sprintf("tf-monitor-%s", rand) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceMackerelMonitorConfigQuery(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dsName, "id"), + resource.TestCheckResourceAttr(dsName, "name", name), + resource.TestCheckResourceAttr(dsName, "memo", "This monitor is managed by Terraform."), + resource.TestCheckResourceAttr(dsName, "is_mute", "true"), + resource.TestCheckResourceAttr(dsName, "notification_interval", "30"), + resource.TestCheckResourceAttr(dsName, "host_metric.#", "0"), + resource.TestCheckResourceAttr(dsName, "connectivity.#", "0"), + resource.TestCheckResourceAttr(dsName, "service_metric.#", "0"), + resource.TestCheckResourceAttr(dsName, "external.#", "0"), + resource.TestCheckResourceAttr(dsName, "expression.#", "0"), + resource.TestCheckResourceAttr(dsName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(dsName, "query.#", "1"), + resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dsName, "query.0.query", "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"), + resource.TestCheckResourceAttr(dsName, "query.0.legend", "cpu.utilization {{k8s.node.name}}"), + resource.TestCheckResourceAttr(dsName, "query.0.operator", ">"), + resource.TestCheckResourceAttr(dsName, "query.0.warning", "70"), + resource.TestCheckResourceAttr(dsName, "query.0.critical", "90"), + ), ), }, }, @@ -445,3 +488,25 @@ data "mackerel_monitor" "foo" { } `, rand, rand, name) } + +func testAccDataSourceMackerelMonitorConfigQuery(name string) string { + return fmt.Sprintf(` +resource "mackerel_monitor" "foo" { + name = "%s" + memo = "This monitor is managed by Terraform." + is_mute = true + notification_interval = 30 + query { + query = "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}" + legend = "cpu.utilization {{k8s.node.name}}" + operator = ">" + warning = "70" + critical = "90" + } +} + +data "mackerel_monitor" "foo" { + id = mackerel_monitor.foo.id +} +`, name) +} diff --git a/mackerel/resource_mackerel_monitor.go b/mackerel/resource_mackerel_monitor.go index 0850422..142e2ba 100644 --- a/mackerel/resource_mackerel_monitor.go +++ b/mackerel/resource_mackerel_monitor.go @@ -10,6 +10,18 @@ import ( "github.com/mackerelio/mackerel-client-go" ) +var ( + monitorTypes = []string{ + "host_metric", + "connectivity", + "service_metric", + "external", + "expression", + "anomaly_detection", + "query", + } +) + func resourceMackerelMonitor() *schema.Resource { return &schema.Resource{ CreateContext: resourceMackerelMonitorCreate, @@ -42,7 +54,7 @@ func resourceMackerelMonitor() *schema.Resource { "host_metric": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -95,7 +107,7 @@ func resourceMackerelMonitor() *schema.Resource { "connectivity": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -115,7 +127,7 @@ func resourceMackerelMonitor() *schema.Resource { "service_metric": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -177,7 +189,7 @@ func resourceMackerelMonitor() *schema.Resource { "external": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -254,7 +266,7 @@ func resourceMackerelMonitor() *schema.Resource { "expression": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -285,7 +297,7 @@ func resourceMackerelMonitor() *schema.Resource { "anomaly_detection": { Type: schema.TypeList, Optional: true, - ExactlyOneOf: []string{"host_metric", "connectivity", "service_metric", "external", "expression", "anomaly_detection"}, + ExactlyOneOf: monitorTypes, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -319,6 +331,41 @@ func resourceMackerelMonitor() *schema.Resource { }, }, }, + "query": { + Type: schema.TypeList, + Optional: true, + ExactlyOneOf: monitorTypes, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query": { + Type: schema.TypeString, + Required: true, + }, + "legend": { + Type: schema.TypeString, + Required: true, + }, + "operator": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{">", "<"}, false), + }, + "warning": { + Type: schema.TypeString, + Optional: true, + AtLeastOneOf: []string{"query.0.warning", "query.0.critical"}, + ValidateFunc: ValidateFloatString, + }, + "critical": { + Type: schema.TypeString, + Optional: true, + AtLeastOneOf: []string{"query.0.warning", "query.0.critical"}, + ValidateFunc: ValidateFloatString, + }, + }, + }, + }, }, } } @@ -382,6 +429,9 @@ func expandMonitor(d *schema.ResourceData) mackerel.Monitor { if _, ok := d.GetOk("anomaly_detection"); ok { monitor = expandMonitorAnomalyDetection(d) } + if _, ok := d.GetOk("query"); ok { + monitor = expandMonitorQuery(d) + } return monitor } @@ -559,3 +609,29 @@ func expandMonitorAnomalyDetection(d *schema.ResourceData) *mackerel.MonitorAnom } return monitor } + +func expandMonitorQuery(d *schema.ResourceData) *mackerel.MonitorQuery { + monitor := &mackerel.MonitorQuery{ + Name: d.Get("name").(string), + Memo: d.Get("memo").(string), + Type: "query", + IsMute: d.Get("is_mute").(bool), + NotificationInterval: uint64(d.Get("notification_interval").(int)), + Query: d.Get("query.0.query").(string), + Legend: d.Get("query.0.legend").(string), + Operator: d.Get("query.0.operator").(string), + Warning: nil, + Critical: nil, + } + if warning, ok := d.GetOkExists("query.0.warning"); ok { + if w, err := strconv.ParseFloat(warning.(string), 64); err == nil { + monitor.Warning = &w + } + } + if critical, ok := d.GetOkExists("query.0.critical"); ok { + if c, err := strconv.ParseFloat(critical.(string), 64); err == nil { + monitor.Critical = &c + } + } + return monitor +} diff --git a/mackerel/resource_mackerel_monitor_test.go b/mackerel/resource_mackerel_monitor_test.go index 388f3d0..20dab68 100644 --- a/mackerel/resource_mackerel_monitor_test.go +++ b/mackerel/resource_mackerel_monitor_test.go @@ -46,6 +46,7 @@ func TestAccMackerelMonitor_HostMetric(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -73,6 +74,7 @@ func TestAccMackerelMonitor_HostMetric(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Import @@ -115,6 +117,7 @@ func TestAccMackerelMonitor_Connectivity(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -136,6 +139,7 @@ func TestAccMackerelMonitor_Connectivity(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Import @@ -186,6 +190,7 @@ func TestAccMackerelMonitor_ServiceMetric(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -214,6 +219,7 @@ func TestAccMackerelMonitor_ServiceMetric(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "external.#", "0"), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Import @@ -269,6 +275,7 @@ func TestAccMackerelMonitor_External(t *testing.T) { ), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -303,6 +310,7 @@ func TestAccMackerelMonitor_External(t *testing.T) { ), resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Import @@ -346,6 +354,7 @@ func TestAccMackerelMonitor_Expression(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "expression.0.warning", "0.7"), ), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -369,6 +378,7 @@ func TestAccMackerelMonitor_Expression(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "expression.0.critical", "0.9"), ), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Import @@ -414,6 +424,7 @@ func TestAccMackerelMonitor_AnomalyDetection(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "anomaly_detection.0.training_period_from", "0"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.0.scopes.#", "1"), ), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), ), }, // Test: Update @@ -438,6 +449,77 @@ func TestAccMackerelMonitor_AnomalyDetection(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "anomaly_detection.0.training_period_from", "1577836800"), resource.TestCheckResourceAttr(resourceName, "anomaly_detection.0.scopes.#", "1"), ), + resource.TestCheckResourceAttr(resourceName, "query.#", "0"), + ), + }, + // Test: Import + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccMackerelMonitor_Query(t *testing.T) { + resourceName := "mackerel_monitor.foo" + rand := acctest.RandString(5) + name := fmt.Sprintf("tf-monitor query %s", rand) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckMackerelMonitorDestroy, + Steps: []resource.TestStep{ + // Test: Create + { + Config: testAccMackerelMonitorConfigQuery(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckMackerelMonitorExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "memo", ""), + resource.TestCheckResourceAttr(resourceName, "is_mute", "false"), + resource.TestCheckResourceAttr(resourceName, "notification_interval", "0"), + resource.TestCheckResourceAttr(resourceName, "host_metric.#", "0"), + resource.TestCheckResourceAttr(resourceName, "connectivity.#", "0"), + resource.TestCheckResourceAttr(resourceName, "service_metric.#", "0"), + resource.TestCheckResourceAttr(resourceName, "external.#", "0"), + resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), + resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "1"), + resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "query.0.query", "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"), + resource.TestCheckResourceAttr(resourceName, "query.0.legend", "cpu.utilization {{k8s.node.name}}"), + resource.TestCheckResourceAttr(resourceName, "query.0.operator", ">"), + resource.TestCheckResourceAttr(resourceName, "query.0.warning", "70"), + resource.TestCheckResourceAttr(resourceName, "query.0.critical", ""), + ), + ), + }, + // Test: Update + { + Config: testAccMackerelMonitorConfigQueryUpdated(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckMackerelMonitorExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "memo", "This monitor is managed by Terraform."), + resource.TestCheckResourceAttr(resourceName, "is_mute", "true"), + resource.TestCheckResourceAttr(resourceName, "notification_interval", "30"), + resource.TestCheckResourceAttr(resourceName, "host_metric.#", "0"), + resource.TestCheckResourceAttr(resourceName, "connectivity.#", "0"), + resource.TestCheckResourceAttr(resourceName, "service_metric.#", "0"), + resource.TestCheckResourceAttr(resourceName, "external.#", "0"), + resource.TestCheckResourceAttr(resourceName, "expression.#", "0"), + resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), + resource.TestCheckResourceAttr(resourceName, "query.#", "1"), + resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "query.0.query", "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"), + resource.TestCheckResourceAttr(resourceName, "query.0.legend", "cpu.utilization {{k8s.node.name}}"), + resource.TestCheckResourceAttr(resourceName, "query.0.operator", ">"), + resource.TestCheckResourceAttr(resourceName, "query.0.warning", "70"), + resource.TestCheckResourceAttr(resourceName, "query.0.critical", "90"), + ), ), }, // Test: Import @@ -755,3 +837,35 @@ resource "mackerel_monitor" "foo" { } `, rand, rand, name) } + +func testAccMackerelMonitorConfigQuery(name string) string { + return fmt.Sprintf(` +resource "mackerel_monitor" "foo" { + name = "%s" + query { + query = "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}" + legend = "cpu.utilization {{k8s.node.name}}" + operator = ">" + warning = "70" + } +} +`, name) +} + +func testAccMackerelMonitorConfigQueryUpdated(name string) string { + return fmt.Sprintf(` +resource "mackerel_monitor" "foo" { + name = "%s" + memo = "This monitor is managed by Terraform." + is_mute = true + notification_interval = 30 + query { + query = "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}" + legend = "cpu.utilization {{k8s.node.name}}" + operator = ">" + warning = "70" + critical = "90" + } +} +`, name) +} diff --git a/mackerel/structure_flattens.go b/mackerel/structure_flattens.go index 7fb64cb..162e6d1 100644 --- a/mackerel/structure_flattens.go +++ b/mackerel/structure_flattens.go @@ -64,6 +64,9 @@ func flattenMonitor(monitor mackerel.Monitor, d *schema.ResourceData) (diags dia if v, ok := monitor.(*mackerel.MonitorAnomalyDetection); ok { diags = flattenMonitorAnomalyDetection(v, d) } + if v, ok := monitor.(*mackerel.MonitorQuery); ok { + diags = flatternMonitorQuery(v, d) + } return diags } @@ -204,6 +207,25 @@ func flattenMonitorAnomalyDetection(monitor *mackerel.MonitorAnomalyDetection, d return diags } +func flatternMonitorQuery(monitor *mackerel.MonitorQuery, d *schema.ResourceData) (diags diag.Diagnostics) { + d.Set("name", monitor.Name) + d.Set("memo", monitor.Memo) + d.Set("is_mute", monitor.IsMute) + d.Set("notification_interval", monitor.NotificationInterval) + + d.Set("query", []map[string]any{ + { + "query": monitor.Query, + "operator": monitor.Operator, + "legend": monitor.Legend, + "warning": parseFloat64ToString(monitor.Warning), + "critical": parseFloat64ToString(monitor.Critical), + }, + }) + + return diags +} + func flattenDowntime(downtime *mackerel.Downtime, d *schema.ResourceData) (diags diag.Diagnostics) { d.Set("name", downtime.Name) d.Set("memo", downtime.Memo) From 77657c554753f5afcc6899813b911ebe27738f5e Mon Sep 17 00:00:00 2001 From: Keisuke Nitta Date: Wed, 15 May 2024 18:10:24 +0900 Subject: [PATCH 3/4] fix: recreate a monitor when a query is updated --- mackerel/resource_mackerel_monitor.go | 1 + mackerel/resource_mackerel_monitor_test.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mackerel/resource_mackerel_monitor.go b/mackerel/resource_mackerel_monitor.go index 142e2ba..b842b11 100644 --- a/mackerel/resource_mackerel_monitor.go +++ b/mackerel/resource_mackerel_monitor.go @@ -341,6 +341,7 @@ func resourceMackerelMonitor() *schema.Resource { "query": { Type: schema.TypeString, Required: true, + ForceNew: true, }, "legend": { Type: schema.TypeString, diff --git a/mackerel/resource_mackerel_monitor_test.go b/mackerel/resource_mackerel_monitor_test.go index 20dab68..9545e3c 100644 --- a/mackerel/resource_mackerel_monitor_test.go +++ b/mackerel/resource_mackerel_monitor_test.go @@ -514,7 +514,7 @@ func TestAccMackerelMonitor_Query(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "anomaly_detection.#", "0"), resource.TestCheckResourceAttr(resourceName, "query.#", "1"), resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "query.0.query", "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}"), + resource.TestCheckResourceAttr(resourceName, "query.0.query", "container.cpu.utilization{k8s.deployment.name=\"nginx\"}"), resource.TestCheckResourceAttr(resourceName, "query.0.legend", "cpu.utilization {{k8s.node.name}}"), resource.TestCheckResourceAttr(resourceName, "query.0.operator", ">"), resource.TestCheckResourceAttr(resourceName, "query.0.warning", "70"), @@ -860,7 +860,7 @@ resource "mackerel_monitor" "foo" { is_mute = true notification_interval = 30 query { - query = "container.cpu.utilization{k8s.deployment.name=\"httpbin\"}" + query = "container.cpu.utilization{k8s.deployment.name=\"nginx\"}" legend = "cpu.utilization {{k8s.node.name}}" operator = ">" warning = "70" From 585e8ed6c825d8fbb0af48378d1fde337bd04f3a Mon Sep 17 00:00:00 2001 From: Keisuke Nitta Date: Thu, 16 May 2024 14:14:15 +0900 Subject: [PATCH 4/4] fix: gofmt --- mackerel/data_source_mackerel_monitor.go | 56 ++++++++++++------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/mackerel/data_source_mackerel_monitor.go b/mackerel/data_source_mackerel_monitor.go index 5716de5..b1fac32 100644 --- a/mackerel/data_source_mackerel_monitor.go +++ b/mackerel/data_source_mackerel_monitor.go @@ -257,34 +257,34 @@ func dataSourceMackerelMonitor() *schema.Resource { }, }, }, - "query": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "query": { - Type: schema.TypeString, - Computed: true, - }, - "legend": { - Type: schema.TypeString, - Computed: true, - }, - "operator": { - Type: schema.TypeString, - Computed: true, - }, - "warning": { - Type: schema.TypeString, - Computed: true, - }, - "critical": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, + "query": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query": { + Type: schema.TypeString, + Computed: true, + }, + "legend": { + Type: schema.TypeString, + Computed: true, + }, + "operator": { + Type: schema.TypeString, + Computed: true, + }, + "warning": { + Type: schema.TypeString, + Computed: true, + }, + "critical": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, }, } }