opentofu/builtin/providers/icinga2/resource_icinga2_host_test.go
Len Smith 015e96d0dd Initial check in for Icinga2 Provider/Resource (#8306)
* Initial checkin for PR request

* Added an argument to provider to allow control over whether or not TLS Certs will skip verification. Controllable via provider or env variable being set

* Initial check-in to use refactored module

* Checkin of very MVP for creating/deleting host test which works and validates basic host creation and deletion

* Check in with support for creating hosts with variables working

* Checking in work to date

* Remove code that causes travis CI to fail while I debug

* Adjust create to accept multivale

* Back on track. Working basic tests. go-icinga2-api needs more test too

* Squashing

* Back on track. Working basic tests. go-icinga2-api needs more test too

* Check in refactored hostgroup support

* Check in refactored check_command, hosts, and hsotgroup with a few test

* Checking in service code

* Add in dependency for icinga2 provider

* Add documentation. Refactor, fix and extend based on feedback from Hashicorp

* Added checking and validation around invalid URL and unavailable server
2016-12-12 15:28:26 +00:00

93 lines
2.6 KiB
Go

package icinga2
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/lrsmith/go-icinga2-api/iapi"
)
func TestAccCreateBasicHost(t *testing.T) {
var testAccCreateBasicHost = fmt.Sprintf(`
resource "icinga2_host" "tf-1" {
hostname = "terraform-host-1"
address = "10.10.10.1"
check_command = "hostalive"
}`)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCreateBasicHost,
Check: resource.ComposeTestCheckFunc(
testAccCheckHostExists("icinga2_host.tf-1"),
testAccCheckResourceState("icinga2_host.tf-1", "hostname", "terraform-host-1"),
testAccCheckResourceState("icinga2_host.tf-1", "address", "10.10.10.1"),
testAccCheckResourceState("icinga2_host.tf-1", "check_command", "hostalive"),
),
},
},
})
}
func TestAccCreateVariableHost(t *testing.T) {
var testAccCreateVariableHost = fmt.Sprintf(`
resource "icinga2_host" "tf-3" {
hostname = "terraform-host-3"
address = "10.10.10.3"
check_command = "hostalive"
vars {
os = "linux"
osver = "1"
allowance = "none" }
}`)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCreateVariableHost,
Check: resource.ComposeTestCheckFunc(
testAccCheckHostExists("icinga2_host.tf-3"),
testAccCheckResourceState("icinga2_host.tf-3", "hostname", "terraform-host-3"),
testAccCheckResourceState("icinga2_host.tf-3", "address", "10.10.10.3"),
testAccCheckResourceState("icinga2_host.tf-3", "check_command", "hostalive"),
testAccCheckResourceState("icinga2_host.tf-3", "vars.%", "3"),
testAccCheckResourceState("icinga2_host.tf-3", "vars.allowance", "none"),
testAccCheckResourceState("icinga2_host.tf-3", "vars.os", "linux"),
testAccCheckResourceState("icinga2_host.tf-3", "vars.osver", "1"),
),
},
},
})
}
func testAccCheckHostExists(rn string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resource, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("Host resource not found: %s", rn)
}
if resource.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}
client := testAccProvider.Meta().(*iapi.Server)
_, err := client.GetHost(resource.Primary.ID)
if err != nil {
return fmt.Errorf("error getting getting host: %s", err)
}
return nil
}
}