github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_monitoring_policy_test.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/1and1/oneandone-cloudserver-sdk-go" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "os" 11 "time" 12 ) 13 14 func TestAccOneandoneMonitoringPolicy_Basic(t *testing.T) { 15 var mp oneandone.MonitoringPolicy 16 17 name := "test" 18 name_updated := "test1" 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { 22 testAccPreCheck(t) 23 }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDOneandoneMonitoringPolicyDestroyCheck, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: fmt.Sprintf(testAccCheckOneandoneMonitoringPolicy_basic, name), 29 Check: resource.ComposeTestCheckFunc( 30 func(*terraform.State) error { 31 time.Sleep(10 * time.Second) 32 return nil 33 }, 34 testAccCheckOneandoneMonitoringPolicyExists("oneandone_monitoring_policy.mp", &mp), 35 testAccCheckOneandoneMonitoringPolicyAttributes("oneandone_monitoring_policy.mp", name), 36 resource.TestCheckResourceAttr("oneandone_monitoring_policy.mp", "name", name), 37 ), 38 }, 39 resource.TestStep{ 40 Config: fmt.Sprintf(testAccCheckOneandoneMonitoringPolicy_basic, name_updated), 41 Check: resource.ComposeTestCheckFunc( 42 func(*terraform.State) error { 43 time.Sleep(10 * time.Second) 44 return nil 45 }, 46 testAccCheckOneandoneMonitoringPolicyExists("oneandone_monitoring_policy.mp", &mp), 47 testAccCheckOneandoneMonitoringPolicyAttributes("oneandone_monitoring_policy.mp", name_updated), 48 resource.TestCheckResourceAttr("oneandone_monitoring_policy.mp", "name", name_updated), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func testAccCheckDOneandoneMonitoringPolicyDestroyCheck(s *terraform.State) error { 56 for _, rs := range s.RootModule().Resources { 57 if rs.Type != "oneandone_monitoring_policy.mp" { 58 continue 59 } 60 61 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 62 63 _, err := api.GetMonitoringPolicy(rs.Primary.ID) 64 65 if err == nil { 66 return fmt.Errorf("MonitoringPolicy still exists %s %s", rs.Primary.ID, err.Error()) 67 } 68 } 69 70 return nil 71 } 72 func testAccCheckOneandoneMonitoringPolicyAttributes(n string, reverse_dns string) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 if !ok { 76 return fmt.Errorf("Not found: %s", n) 77 } 78 if rs.Primary.Attributes["name"] != reverse_dns { 79 return fmt.Errorf("Bad name: expected %s : found %s ", reverse_dns, rs.Primary.Attributes["name"]) 80 } 81 82 return nil 83 } 84 } 85 86 func testAccCheckOneandoneMonitoringPolicyExists(n string, fw_p *oneandone.MonitoringPolicy) resource.TestCheckFunc { 87 return func(s *terraform.State) error { 88 rs, ok := s.RootModule().Resources[n] 89 90 if !ok { 91 return fmt.Errorf("Not found: %s", n) 92 } 93 94 if rs.Primary.ID == "" { 95 return fmt.Errorf("No Record ID is set") 96 } 97 98 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 99 100 found_fw, err := api.GetMonitoringPolicy(rs.Primary.ID) 101 102 if err != nil { 103 return fmt.Errorf("Error occured while fetching MonitoringPolicy: %s", rs.Primary.ID) 104 } 105 if found_fw.Id != rs.Primary.ID { 106 return fmt.Errorf("Record not found") 107 } 108 fw_p = found_fw 109 110 return nil 111 } 112 } 113 114 const testAccCheckOneandoneMonitoringPolicy_basic = ` 115 resource "oneandone_monitoring_policy" "mp" { 116 name = "%s" 117 agent = true 118 email = "email@address.com" 119 thresholds = { 120 cpu = { 121 warning = { 122 value = 50, 123 alert = false 124 } 125 critical = { 126 value = 66, 127 alert = false 128 } 129 } 130 ram = { 131 warning = { 132 value = 70, 133 alert = true 134 } 135 critical = { 136 value = 80, 137 alert = true 138 } 139 }, 140 ram = { 141 warning = { 142 value = 85, 143 alert = true 144 } 145 critical = { 146 value = 95, 147 alert = true 148 } 149 }, 150 disk = { 151 warning = { 152 value = 84, 153 alert = true 154 } 155 critical = { 156 value = 94, 157 alert = true 158 } 159 }, 160 transfer = { 161 warning = { 162 value = 1000, 163 alert = true 164 } 165 critical = { 166 value = 2000, 167 alert = true 168 } 169 }, 170 internal_ping = { 171 warning = { 172 value = 3000, 173 alert = true 174 } 175 critical = { 176 value = 4000, 177 alert = true 178 } 179 } 180 } 181 ports = [ 182 { 183 email_notification = true 184 port = 443 185 protocol = "TCP" 186 alert_if = "NOT_RESPONDING" 187 }, 188 { 189 email_notification = false 190 port = 80 191 protocol = "TCP" 192 alert_if = "NOT_RESPONDING" 193 }, 194 { 195 email_notification = true 196 port = 21 197 protocol = "TCP" 198 alert_if = "NOT_RESPONDING" 199 } 200 ] 201 processes = [ 202 { 203 email_notification = false 204 process = "httpdeamon" 205 alert_if = "RUNNING" 206 }, 207 { 208 process = "iexplorer", 209 alert_if = "NOT_RUNNING" 210 email_notification = true 211 }] 212 }`