Skip to content

Commit

Permalink
Merge pull request #38 from jtopjian/fix-proxyrequests-empty
Browse files Browse the repository at this point in the history
Don't create proxy_request when empty
  • Loading branch information
jtopjian authored Jul 14, 2020
2 parents 1c62735 + 68b7fce commit 0aef582
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
2 changes: 1 addition & 1 deletion sensu/data_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ var testAccDataSourceCheck_proxyRequests = fmt.Sprintf(`
data "sensu_check" "check_1" {
name = "${sensu_check.check_1.name}"
}
`, testAccResourceCheck_proxyRequests)
`, testAccResourceCheck_proxyRequests_1)
19 changes: 14 additions & 5 deletions sensu/resource_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func resourceCheckCreate(d *schema.ResourceData, meta interface{}) error {
runtimeAssets := expandStringList(d.Get("runtime_assets").([]interface{}))

// detailed structures
proxyRequests := expandCheckProxyRequests(d.Get("proxy_requests").([]interface{}))
envVars := expandEnvVars(d.Get("env_vars").(map[string]interface{}))
subdues := expandTimeWindows(d.Get("subdue").(*schema.Set).List())
annotations := expandStringMap(d.Get("annotations").(map[string]interface{}))
Expand All @@ -214,7 +213,6 @@ func resourceCheckCreate(d *schema.ResourceData, meta interface{}) error {
OutputMetricFormat: d.Get("output_metric_format").(string),
OutputMetricHandlers: outputMetricHandlers,
ProxyEntityName: d.Get("proxy_entity_name").(string),
ProxyRequests: &proxyRequests,
Publish: d.Get("publish").(bool),
RoundRobin: d.Get("round_robin").(bool),
RuntimeAssets: runtimeAssets,
Expand All @@ -224,6 +222,12 @@ func resourceCheckCreate(d *schema.ResourceData, meta interface{}) error {
Ttl: int64(d.Get("ttl").(int)),
}

proxyRequests := d.Get("proxy_requests").([]interface{})
if len(proxyRequests) > 0 {
v := expandCheckProxyRequests(proxyRequests)
check.ProxyRequests = &v
}

log.Printf("[DEBUG] Creating check %s: %#v", name, check)

if err := check.Validate(); err != nil {
Expand All @@ -250,7 +254,7 @@ func resourceCheckCreate(d *schema.ResourceData, meta interface{}) error {
d.SetPartial("output_metric_format")
d.SetPartial("output_metric_handlers")
d.SetPartial("proxy_entity_name")
//d.SetPartial("proxy_requests")
d.SetPartial("proxy_requests")
d.SetPartial("publish")
d.SetPartial("round_robin")
d.SetPartial("runtime_assets")
Expand Down Expand Up @@ -412,8 +416,13 @@ func resourceCheckUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("proxy_requests") {
proxyRequests := expandCheckProxyRequests(d.Get("proxy_requests").([]interface{}))
check.ProxyRequests = &proxyRequests
proxyRequests := d.Get("proxy_requests").([]interface{})
if len(proxyRequests) > 0 {
v := expandCheckProxyRequests(proxyRequests)
check.ProxyRequests = &v
} else {
check.ProxyRequests = nil
}
}

if d.HasChange("publish") {
Expand Down
73 changes: 29 additions & 44 deletions sensu/resource_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,6 @@ func TestAccResourceCheck_basic(t *testing.T) {
})
}

/*
func TestAccResourceCheck_proxyRequests(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceCheck_proxyRequests,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"sensu_check.check_1", "proxy_requests.0.entity_attributes.0", "entity.Class == \"proxy\""),
resource.TestCheckResourceAttr(
"sensu_check.check_1", "proxy_requests.0.splay", "true"),
),
},
},
})
}
*/

func TestAccResourceCheck_subdue(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -197,12 +177,19 @@ func TestAccResourceCheck_proxyRequests(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceCheck_proxyRequests,
Config: testAccResourceCheck_proxyRequests_1,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"sensu_check.check_1", "proxy_requests.0.entity_attributes.#", "2"),
),
},
resource.TestStep{
Config: testAccResourceCheck_proxyRequests_2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"sensu_check.check_1", "proxy_requests.#", "0"),
),
},
},
})
}
Expand Down Expand Up @@ -244,28 +231,6 @@ const testAccResourceCheck_update_2 = `
}
`

/*
const testAccResourceCheck_proxyRequests = `
resource "sensu_check" "check_1" {
name = "check_1"
command = "/bin/foo"
interval = 60000
subscriptions = [
"foo",
"bar",
]
proxy_requests {
entity_attributes = [
"entity.Class == \"proxy\"",
]
splay = true
splay_coverage = 90
}
}
`
*/

const testAccResourceCheck_subdue_1 = `
resource "sensu_check" "check_1" {
name = "check_1"
Expand Down Expand Up @@ -447,7 +412,7 @@ const testAccResourceCheck_annotations_3 = `
}
`

const testAccResourceCheck_proxyRequests = `
const testAccResourceCheck_proxyRequests_1 = `
resource "sensu_entity" "entities" {
count = 3
name = format("entity-%02d", count.index+1)
Expand All @@ -472,3 +437,23 @@ const testAccResourceCheck_proxyRequests = `
subscriptions = ["proxy"]
}
`

const testAccResourceCheck_proxyRequests_2 = `
resource "sensu_entity" "entities" {
count = 3
name = format("entity-%02d", count.index+1)
class = "proxy"
labels = {
"proxy_type" = "website"
"url" = format("http://example-%02d.com", count.index+1)
}
}
resource "sensu_check" "check_1" {
name = "check-http"
command = "check-http.rb -u {{ .labels url }}"
interval = 60
publish = true
subscriptions = ["proxy"]
}
`

0 comments on commit 0aef582

Please sign in to comment.