github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_contact_test.go (about)

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/circonus-labs/circonus-gometrics/api"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccCirconusContactGroup_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckDestroyCirconusContactGroup,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccCirconusContactGroupConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					// testAccContactGroupExists("circonus_contact_group.staging-sev3", "foo"),
    23  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "name", "ops-staging-sev3"),
    24  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.#", "3"),
    25  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.1119127802.address", ""),
    26  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.1119127802.user", "/user/5469"),
    27  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.1456570992.address", ""),
    28  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.1456570992.user", "/user/6331"),
    29  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.343263208.address", "user@example.com"),
    30  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "email.343263208.user", ""),
    31  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "http.#", "1"),
    32  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "http.1287846151.address", "https://www.example.org/post/endpoint"),
    33  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "http.1287846151.format", "json"),
    34  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "http.1287846151.method", "POST"),
    35  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "irc.#", "0"),
    36  					// resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "irc.918937268.user", "/user/6331"),
    37  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "slack.#", "1"),
    38  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "slack.274933206.channel", "#ops-staging"),
    39  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "slack.274933206.team", "T123UT98F"),
    40  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "slack.274933206.username", "Circonus"),
    41  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "slack.274933206.buttons", "true"),
    42  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "sms.#", "1"),
    43  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "sms.1119127802.user", "/user/5469"),
    44  
    45  					// xmpp.# will be 0 for user faux user accounts that don't have an
    46  					// XMPP address setup.
    47  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "xmpp.#", "0"),
    48  					// resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "xmpp.1119127802.user", "/user/5469"),
    49  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.#", "1"),
    50  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.2029434450.api_key", "123"),
    51  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.2029434450.critical", "2"),
    52  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.2029434450.info", "5"),
    53  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.2029434450.team", "bender"),
    54  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "victorops.2029434450.warning", "3"),
    55  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "aggregation_window", "60s"),
    56  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.#", "5"),
    57  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.689365425.severity", "1"),
    58  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.689365425.reminder", "60s"),
    59  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.689365425.escalate_after", "3600s"),
    60  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.689365425.escalate_to", "/contact_group/2913"),
    61  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.551050940.severity", "2"),
    62  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.551050940.reminder", "120s"),
    63  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.551050940.escalate_after", "7200s"),
    64  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.551050940.escalate_to", "/contact_group/2913"),
    65  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1292974544.severity", "3"),
    66  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1292974544.reminder", "180s"),
    67  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1292974544.escalate_after", "10800s"),
    68  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1292974544.escalate_to", "/contact_group/2913"),
    69  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1183354841.severity", "4"),
    70  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1183354841.reminder", "240s"),
    71  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1183354841.escalate_after", "14400s"),
    72  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.1183354841.escalate_to", "/contact_group/2913"),
    73  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.2942620849.severity", "5"),
    74  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.2942620849.reminder", "300s"),
    75  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.2942620849.escalate_after", "18000s"),
    76  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "alert_option.2942620849.escalate_to", "/contact_group/2913"),
    77  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "long_message", "a long message"),
    78  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "long_subject", "long subject"),
    79  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "long_summary", "long summary"),
    80  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "short_message", "short message"),
    81  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "short_summary", "short summary"),
    82  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "tags.#", "2"),
    83  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "tags.2087084518", "author:terraform"),
    84  					resource.TestCheckResourceAttr("circonus_contact_group.staging-sev3", "tags.393923453", "other:foo"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func testAccCheckDestroyCirconusContactGroup(s *terraform.State) error {
    92  	c := testAccProvider.Meta().(*providerContext)
    93  
    94  	for _, rs := range s.RootModule().Resources {
    95  		if rs.Type != "circonus_contact_group" {
    96  			continue
    97  		}
    98  
    99  		cid := rs.Primary.ID
   100  		exists, err := checkContactGroupExists(c, api.CIDType(&cid))
   101  		switch {
   102  		case !exists:
   103  			// noop
   104  		case exists:
   105  			return fmt.Errorf("contact group still exists after destroy")
   106  		case err != nil:
   107  			return fmt.Errorf("Error checking contact group %s", err)
   108  		}
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  func checkContactGroupExists(c *providerContext, contactGroupCID api.CIDType) (bool, error) {
   115  	cb, err := c.client.FetchContactGroup(contactGroupCID)
   116  	if err != nil {
   117  		if strings.Contains(err.Error(), defaultCirconus404ErrorString) {
   118  			return false, nil
   119  		}
   120  
   121  		return false, err
   122  	}
   123  
   124  	if api.CIDType(&cb.CID) == contactGroupCID {
   125  		return true, nil
   126  	}
   127  
   128  	return false, nil
   129  }
   130  
   131  const testAccCirconusContactGroupConfig = `
   132  resource "circonus_contact_group" "staging-sev3" {
   133    name = "ops-staging-sev3"
   134  
   135    email {
   136      user = "/user/5469"
   137    }
   138  
   139    email {
   140      address = "user@example.com"
   141    }
   142  
   143    email {
   144      user = "/user/6331"
   145    }
   146  
   147    http {
   148      address = "https://www.example.org/post/endpoint"
   149      format = "json"
   150      method = "POST"
   151    }
   152  
   153  /*
   154    // Account needs to be setup with IRC before this can work.
   155    irc {
   156      user = "/user/6331"
   157    }
   158  */
   159  
   160  /*
   161    pager_duty {
   162      // NOTE(sean@): needs to be filled in
   163    }
   164  */
   165  
   166    slack {
   167      channel = "#ops-staging"
   168      team = "T123UT98F"
   169      username = "Circonus"
   170      buttons = true
   171    }
   172  
   173    sms {
   174      user = "/user/5469"
   175    }
   176  
   177    victorops {
   178      api_key = "123"
   179      critical = 2
   180      info = 5
   181      team = "bender"
   182      warning = 3
   183    }
   184  
   185  	// Faux user accounts that don't have an XMPP address setup will not return a
   186  	// valid response in the future.
   187    //
   188    // xmpp {
   189    //   user = "/user/5469"
   190    // }
   191  
   192    aggregation_window = "1m"
   193  
   194    alert_option {
   195      severity = 1
   196      reminder = "60s"
   197      escalate_after = "3600s"
   198      escalate_to = "/contact_group/2913"
   199    }
   200  
   201    alert_option {
   202      severity = 2
   203      reminder = "2m"
   204      escalate_after = "2h"
   205      escalate_to = "/contact_group/2913"
   206    }
   207  
   208    alert_option {
   209      severity = 3
   210      reminder = "3m"
   211      escalate_after = "3h"
   212      escalate_to = "/contact_group/2913"
   213    }
   214  
   215    alert_option {
   216      severity = 4
   217      reminder = "4m"
   218      escalate_after = "4h"
   219      escalate_to = "/contact_group/2913"
   220    }
   221  
   222    alert_option {
   223      severity = 5
   224      reminder = "5m"
   225      escalate_after = "5h"
   226      escalate_to = "/contact_group/2913"
   227    }
   228  
   229    // alert_formats: omit to use defaults
   230    long_message = "a long message"
   231    long_subject = "long subject"
   232    long_summary = "long summary"
   233    short_message = "short message"
   234    short_summary = "short summary"
   235  
   236    tags = [
   237      "author:terraform",
   238      "other:foo",
   239    ]
   240  }
   241  `