Skip to content

Commit

Permalink
Merge pull request #3 from wndhydrnt/icinga2_host_templates
Browse files Browse the repository at this point in the history
Configure "templates" via the icinga2_host resource
  • Loading branch information
grubernaut committed Jul 28, 2017
2 parents a9c508d + 94d4eab commit a620c92
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
15 changes: 14 additions & 1 deletion icinga2/resource_icinga2_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
}
Expand All @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions icinga2/resource_icinga2_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 14 additions & 2 deletions icinga2/resource_icinga2_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@ func TestAccCreateService(t *testing.T) {
name = "ssh3"
check_command = "ssh"
}`)
hostname := "docker-icinga2"
createHost := func() {
icinga2Server := testAccProvider.Meta().(*iapi.Server)
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"),
),
},
},
})

icinga2Server := testAccProvider.Meta().(*iapi.Server)
err := icinga2Server.DeleteHost(hostname)
if err != nil {
t.Errorf("Error deleting host object after test completed: %s", err)
}
}

func testAccCheckServiceExists(rn string) resource.TestCheckFunc {
Expand Down
3 changes: 2 additions & 1 deletion vendor/github.com/lrsmith/go-icinga2-api/iapi/hosts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down

0 comments on commit a620c92

Please sign in to comment.