Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure "templates" via the icinga2_host resource #3

Merged
merged 4 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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