terraform/builtin/providers/spotinst/resource_spotinst_healthche...

161 lines
4.6 KiB
Go
Raw Normal View History

New Provider: Spotinst (#5001) * providers/spotinst: Add support for Spotinst resources * providers/spotinst: Fix merge conflict - layouts/docs.erb * docs/providers/spotinst: Fix the resource description field * providers/spotinst: Fix the acceptance tests * providers/spotinst: Mark the device_index as a required field * providers/spotinst: Change the associate_public_ip_address field to TypeBool * docs/providers/spotinst: Update the description of the adjustment field * providers/spotinst: Rename IamRole to IamInstanceProfile to make it more compatible with the AWS provider * docs/providers/spotinst: Rename iam_role to iam_instance_profile * providers/spotinst: Deprecate the iam_role attribute * providers/spotinst: Fix a misspelled var (IamRole) * providers/spotinst: Fix possible null pointer exception related to "iam_instance_profile" * docs/providers/spotinst: Add "load_balancer_names" missing description * providers/spotinst: New resource "spotinst_subscription" added * providers/spotinst: Eliminate a possible null pointer exception in "spotinst_aws_group" * providers/spotinst: Eliminate a possible null pointer exception in "spotinst_subscription" * providers/spotinst: Mark spotinst_subscription as deleted in destroy * providers/spotinst: Add support for custom event format in spotinst_subscription * providers/spotinst: Disable the destroy step of spotinst_subscription * providers/spotinst: Add support for update subscriptions * providers/spotinst: Merge fixed conflict - layouts/docs.erb * providers/spotinst: Vendor dependencies * providers/spotinst: Return a detailed error message * provider/spotinst: Update the plugin list * providers/spotinst: Vendor dependencies using govendor * providers/spotinst: New resource "spotinst_healthcheck" added * providers/spotinst: Update the Spotinst SDK * providers/spotinst: Comment out unnecessary log.Printf * providers/spotinst: Fix the acceptance tests * providers/spotinst: Gofmt fixes * providers/spotinst: Use multiple functions to expand each block * providers/spotinst: Allow ondemand_count to be zero * providers/spotinst: Change security_group_ids from TypeSet to TypeList * providers/spotinst: Remove unnecessary `ForceNew` fields * providers/spotinst: Update the Spotinst SDK * providers/spotinst: Add support for capacity unit * providers/spotinst: Add support for EBS volume pool * providers/spotinst: Delete health check * providers/spotinst: Allow to set multiple availability zones * providers/spotinst: Gofmt * providers/spotinst: Omit empty strings from the load_balancer_names field * providers/spotinst: Update the Spotinst SDK to v1.1.9 * providers/spotinst: Add support for new strategy parameters * providers/spotinst: Update the Spotinst SDK to v1.2.0 * providers/spotinst: Add support for Kubernetes integration * providers/spotinst: Fix merge conflict - vendor/vendor.json * providers/spotinst: Update the Spotinst SDK to v1.2.1 * providers/spotinst: Add support for Application Load Balancers * providers/spotinst: Do not allow to set ondemand_count to 0 * providers/spotinst: Update the Spotinst SDK to v1.2.2 * providers/spotinst: Add support for scaling policy operators * providers/spotinst: Add dimensions to spotinst_aws_group tests * providers/spotinst: Allow both ARN and name for IAM instance profiles * providers/spotinst: Allow ondemand_count=0 * providers/spotinst: Split out the set funcs into flatten style funcs * providers/spotinst: Update the Spotinst SDK to v1.2.3 * providers/spotinst: Add support for EBS optimized flag * providers/spotinst: Update the Spotinst SDK to v2.0.0 * providers/spotinst: Use stringutil.Stringify for debugging * providers/spotinst: Update the Spotinst SDK to v2.0.1 * providers/spotinst: Key pair is now optional * providers/spotinst: Make sure we do not nullify signals on strategy update * providers/spotinst: Hash both Strategy and EBS Block Device * providers/spotinst: Hash AWS load balancer * providers/spotinst: Update the Spotinst SDK to v2.0.2 * providers/spotinst: Verify namespace exists before appending policy * providers/spotinst: Image ID will be in a separate block from now on, so as to allow ignoring changes only on the image ID. This change is backwards compatible. * providers/spotinst: user data decoded when returned from spotinst api, so that TF compares the two states properly, and does not update without cause.
2017-02-22 21:57:16 +01:00
package spotinst
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/spotinst/spotinst-sdk-go/spotinst"
)
func TestAccSpotinstHealthCheck_Basic(t *testing.T) {
var healthCheck spotinst.HealthCheck
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpotinstHealthCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckSpotinstHealthCheckConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
testAccCheckSpotinstHealthCheckAttributes(&healthCheck),
resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-foo"),
),
},
},
})
}
func TestAccSpotinstHealthCheck_Updated(t *testing.T) {
var healthCheck spotinst.HealthCheck
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpotinstHealthCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckSpotinstHealthCheckConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
testAccCheckSpotinstHealthCheckAttributes(&healthCheck),
resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-foo"),
),
},
{
Config: testAccCheckSpotinstHealthCheckConfigNewValue,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
testAccCheckSpotinstHealthCheckAttributesUpdated(&healthCheck),
resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-bar"),
),
},
},
})
}
func testAccCheckSpotinstHealthCheckDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*spotinst.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "spotinst_healthcheck" {
continue
}
input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(rs.Primary.ID)}
resp, err := client.HealthCheckService.Read(input)
if err == nil && resp != nil && resp.HealthCheck != nil {
return fmt.Errorf("HealthCheck still exists")
}
}
return nil
}
func testAccCheckSpotinstHealthCheckAttributes(healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
if p := spotinst.StringValue(healthCheck.Check.Protocol); p != "http" {
return fmt.Errorf("Bad content: %s", p)
}
if e := spotinst.StringValue(healthCheck.Check.Endpoint); e != "http://endpoint.com" {
return fmt.Errorf("Bad content: %s", e)
}
return nil
}
}
func testAccCheckSpotinstHealthCheckAttributesUpdated(healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
if p := spotinst.StringValue(healthCheck.Check.Protocol); p != "https" {
return fmt.Errorf("Bad content: %s", p)
}
if e := spotinst.StringValue(healthCheck.Check.Endpoint); e != "https://endpoint.com" {
return fmt.Errorf("Bad content: %s", e)
}
return nil
}
}
func testAccCheckSpotinstHealthCheckExists(n string, healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No resource ID is set")
}
client := testAccProvider.Meta().(*spotinst.Client)
input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(rs.Primary.ID)}
resp, err := client.HealthCheckService.Read(input)
if err != nil {
return err
}
if spotinst.StringValue(resp.HealthCheck.ID) != rs.Primary.Attributes["id"] {
return fmt.Errorf("HealthCheck not found: %+v,\n %+v\n", resp.HealthCheck, rs.Primary.Attributes)
}
*healthCheck = *resp.HealthCheck
return nil
}
}
const testAccCheckSpotinstHealthCheckConfigBasic = `
resource "spotinst_healthcheck" "foo" {
name = "hc-foo"
resource_id = "sig-foo"
check {
protocol = "http"
endpoint = "http://endpoint.com"
port = 1337
interval = 10
timeout = 10
}
threshold {
healthy = 1
unhealthy = 1
}
proxy {
addr = "http://proxy.com"
port = 80
}
}`
const testAccCheckSpotinstHealthCheckConfigNewValue = `
resource "spotinst_healthcheck" "foo" {
name = "hc-bar"
resource_id = "sig-foo"
check {
protocol = "https"
endpoint = "https://endpoint.com"
port = 3000
interval = 10
timeout = 10
}
threshold {
healthy = 2
unhealthy = 2
}
proxy {
addr = "http://proxy.com"
port = 8080
}
}`