From 060697bc6cbaea16cf05ad5f3463f25e0dd4ebe1 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Thu, 27 Jul 2017 10:12:05 +0200 Subject: [PATCH 1/4] Update lrsmith/go-icinga2-api --- vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go | 3 ++- vendor/vendor.json | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go b/vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go index db0629b6..26c2a9b2 100644 --- a/vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go +++ b/vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go @@ -33,12 +33,13 @@ func (server *Server) GetHost(hostname string) ([]HostStruct, error) { } // CreateHost ... -func (server *Server) CreateHost(hostname, address, checkCommand string, variables map[string]string) ([]HostStruct, error) { +func (server *Server) CreateHost(hostname, address, checkCommand string, variables map[string]string, templates []string) ([]HostStruct, error) { var newAttrs HostAttrs newAttrs.Address = address newAttrs.CheckCommand = "hostalive" newAttrs.Vars = variables + newAttrs.Templates = templates var newHost HostStruct newHost.Name = hostname diff --git a/vendor/vendor.json b/vendor/vendor.json index cc1e51d9..f03ec72b 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -489,10 +489,10 @@ "revisionTime": "2016-08-03T19:07:31Z" }, { - "checksumSHA1": "8fIVetxvijD+9nPT5TEl2OeDHxg=", + "checksumSHA1": "5vT6l22kAQVXWOT1YI5bhLQ9kBA=", "path": "github.com/lrsmith/go-icinga2-api/iapi", - "revision": "ba9eccb088d652b05154765828ad78345bc36f14", - "revisionTime": "2016-12-10T04:55:21Z" + "revision": "7b46d425cc71344e4357bf0bcc873e93723d1dc7", + "revisionTime": "2017-07-26T19:48:34Z" }, { "checksumSHA1": "guxbLo8KHHBeM0rzou4OTzzpDNs=", From 45641956b9a0c93a56f75995736683cf76c50005 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Thu, 27 Jul 2017 10:42:42 +0200 Subject: [PATCH 2/4] Fix failing test --- icinga2/resource_icinga2_service_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/icinga2/resource_icinga2_service_test.go b/icinga2/resource_icinga2_service_test.go index a9c9a63f..cf65a1d3 100644 --- a/icinga2/resource_icinga2_service_test.go +++ b/icinga2/resource_icinga2_service_test.go @@ -18,22 +18,33 @@ func TestAccCreateService(t *testing.T) { name = "ssh3" check_command = "ssh" }`) + hostname := "docker-icinga2" + icinga2Server := testAccProvider.Meta().(*iapi.Server) + createHost := func() { + icinga2Server.CreateHost(hostname, "10.0.0.1", "hostalive", nil, nil) + } resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccCreateService, + PreConfig: createHost, + Config: testAccCreateService, Check: resource.ComposeTestCheckFunc( testAccCheckServiceExists("icinga2_service.tf-service-1"), - testAccCheckResourceState("icinga2_service.tf-service-1", "hostname", "docker-icinga2"), + testAccCheckResourceState("icinga2_service.tf-service-1", "hostname", hostname), testAccCheckResourceState("icinga2_service.tf-service-1", "name", "ssh3"), testAccCheckResourceState("icinga2_service.tf-service-1", "check_command", "ssh"), ), }, }, }) + + err := icinga2Server.DeleteHost(hostname) + if err != nil { + t.Errorf("Error deleting host object after test completed: %s", err) + } } func testAccCheckServiceExists(rn string) resource.TestCheckFunc { From 13c7bdd59f9ba3f0cc2cae3b40ddfb64dba1ac40 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Thu, 27 Jul 2017 11:06:21 +0200 Subject: [PATCH 3/4] Configure "templates" via the icinga2_host resource --- icinga2/resource_icinga2_host.go | 15 ++++++++++++++- icinga2/resource_icinga2_host_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/icinga2/resource_icinga2_host.go b/icinga2/resource_icinga2_host.go index 454537e8..e2fa8d13 100644 --- a/icinga2/resource_icinga2_host.go +++ b/icinga2/resource_icinga2_host.go @@ -35,6 +35,14 @@ func resourceIcinga2Host() *schema.Resource { Optional: true, ForceNew: true, }, + "templates": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, } } @@ -55,8 +63,13 @@ func resourceIcinga2HostCreate(d *schema.ResourceData, meta interface{}) error { vars[key] = value.(string) } + templates := make([]string, len(d.Get("templates").([]interface{}))) + for i, v := range d.Get("templates").([]interface{}) { + templates[i] = v.(string) + } + // Call CreateHost with normalized data - hosts, err := client.CreateHost(hostname, address, checkCommand, vars) + hosts, err := client.CreateHost(hostname, address, checkCommand, vars, templates) if err != nil { return err } diff --git a/icinga2/resource_icinga2_host_test.go b/icinga2/resource_icinga2_host_test.go index c33a2e9e..b5006971 100644 --- a/icinga2/resource_icinga2_host_test.go +++ b/icinga2/resource_icinga2_host_test.go @@ -69,6 +69,33 @@ func TestAccCreateVariableHost(t *testing.T) { }) } +func TestAccCreateTemplateHost(t *testing.T) { + testAccCreateTemplateHost := `resource "icinga2_host" "tf-4" { + hostname = "terraform-host-4" + address = "10.10.10.4" + check_command = "hostalive" + templates = ["generic", "az1"] +}` + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCreateTemplateHost, + Check: resource.ComposeTestCheckFunc( + testAccCheckHostExists("icinga2_host.tf-4"), + testAccCheckResourceState("icinga2_host.tf-4", "hostname", "terraform-host-4"), + testAccCheckResourceState("icinga2_host.tf-4", "address", "10.10.10.4"), + testAccCheckResourceState("icinga2_host.tf-4", "check_command", "hostalive"), + testAccCheckResourceState("icinga2_host.tf-4", "templates.#", "2"), + testAccCheckResourceState("icinga2_host.tf-4", "templates.0", "generic"), + testAccCheckResourceState("icinga2_host.tf-4", "templates.1", "az1"), + ), + }, + }, + }) +} + func testAccCheckHostExists(rn string) resource.TestCheckFunc { return func(s *terraform.State) error { resource, ok := s.RootModule().Resources[rn] From 94d4eabeecc819095e97023c04d257aa68047d0b Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Thu, 27 Jul 2017 11:20:45 +0200 Subject: [PATCH 4/4] Fix "make test" failing --- icinga2/resource_icinga2_service_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/icinga2/resource_icinga2_service_test.go b/icinga2/resource_icinga2_service_test.go index cf65a1d3..b8aaf7f5 100644 --- a/icinga2/resource_icinga2_service_test.go +++ b/icinga2/resource_icinga2_service_test.go @@ -19,8 +19,8 @@ func TestAccCreateService(t *testing.T) { check_command = "ssh" }`) hostname := "docker-icinga2" - icinga2Server := testAccProvider.Meta().(*iapi.Server) createHost := func() { + icinga2Server := testAccProvider.Meta().(*iapi.Server) icinga2Server.CreateHost(hostname, "10.0.0.1", "hostalive", nil, nil) } @@ -41,6 +41,7 @@ func TestAccCreateService(t *testing.T) { }, }) + icinga2Server := testAccProvider.Meta().(*iapi.Server) err := icinga2Server.DeleteHost(hostname) if err != nil { t.Errorf("Error deleting host object after test completed: %s", err)