github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ultradns/resource_ultradns_probe_ping.go (about) 1 package ultradns 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/Ensighten/udnssdk" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceUltradnsProbePing() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceUltradnsProbePingCreate, 14 Read: resourceUltradnsProbePingRead, 15 Update: resourceUltradnsProbePingUpdate, 16 Delete: resourceUltradnsProbePingDelete, 17 18 Schema: map[string]*schema.Schema{ 19 // Key 20 "zone": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 "name": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 "pool_record": &schema.Schema{ 31 Type: schema.TypeString, 32 Optional: true, 33 ForceNew: true, 34 }, 35 // Required 36 "agents": &schema.Schema{ 37 Type: schema.TypeList, 38 Required: true, 39 Elem: &schema.Schema{Type: schema.TypeString}, 40 }, 41 "threshold": &schema.Schema{ 42 Type: schema.TypeInt, 43 Required: true, 44 }, 45 // Optional 46 "interval": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 Default: "FIVE_MINUTES", 50 }, 51 "ping_probe": &schema.Schema{ 52 Type: schema.TypeList, 53 Optional: true, 54 Elem: schemaPingProbe(), 55 }, 56 // Computed 57 "id": &schema.Schema{ 58 Type: schema.TypeString, 59 Computed: true, 60 }, 61 }, 62 } 63 } 64 65 func resourceUltradnsProbePingCreate(d *schema.ResourceData, meta interface{}) error { 66 client := meta.(*udnssdk.Client) 67 68 r, err := makePingProbeResource(d) 69 if err != nil { 70 return fmt.Errorf("Could not load ultradns_probe_ping configuration: %v", err) 71 } 72 73 log.Printf("[INFO] ultradns_probe_ping create: %#v, detail: %#v", r, r.Details.Detail) 74 resp, err := client.Probes.Create(r.Key().RRSetKey(), r.ProbeInfoDTO()) 75 if err != nil { 76 return fmt.Errorf("create failed: %v", err) 77 } 78 79 uri := resp.Header.Get("Location") 80 d.Set("uri", uri) 81 d.SetId(uri) 82 log.Printf("[INFO] ultradns_probe_ping.id: %v", d.Id()) 83 84 return resourceUltradnsProbePingRead(d, meta) 85 } 86 87 func resourceUltradnsProbePingRead(d *schema.ResourceData, meta interface{}) error { 88 client := meta.(*udnssdk.Client) 89 90 r, err := makePingProbeResource(d) 91 if err != nil { 92 return fmt.Errorf("Could not load ultradns_probe_ping configuration: %v", err) 93 } 94 95 log.Printf("[DEBUG] ultradns_probe_ping read: %#v", r) 96 probe, _, err := client.Probes.Find(r.Key()) 97 log.Printf("[DEBUG] ultradns_probe_ping response: %#v", probe) 98 99 if err != nil { 100 uderr, ok := err.(*udnssdk.ErrorResponseList) 101 if ok { 102 for _, r := range uderr.Responses { 103 // 70002 means Probes Not Found 104 if r.ErrorCode == 70002 { 105 d.SetId("") 106 return nil 107 } 108 return fmt.Errorf("not found: %s", err) 109 } 110 } 111 return fmt.Errorf("not found: %s", err) 112 } 113 114 return populateResourceDataFromPingProbe(probe, d) 115 } 116 117 func resourceUltradnsProbePingUpdate(d *schema.ResourceData, meta interface{}) error { 118 client := meta.(*udnssdk.Client) 119 120 r, err := makePingProbeResource(d) 121 if err != nil { 122 return fmt.Errorf("Could not load ultradns_probe_ping configuration: %v", err) 123 } 124 125 log.Printf("[INFO] ultradns_probe_ping update: %+v", r) 126 _, err = client.Probes.Update(r.Key(), r.ProbeInfoDTO()) 127 if err != nil { 128 return fmt.Errorf("update failed: %s", err) 129 } 130 131 return resourceUltradnsProbePingRead(d, meta) 132 } 133 134 func resourceUltradnsProbePingDelete(d *schema.ResourceData, meta interface{}) error { 135 client := meta.(*udnssdk.Client) 136 137 r, err := makePingProbeResource(d) 138 if err != nil { 139 return fmt.Errorf("Could not load ultradns_probe_ping configuration: %s", err) 140 } 141 142 log.Printf("[INFO] ultradns_probe_ping delete: %+v", r) 143 _, err = client.Probes.Delete(r.Key()) 144 if err != nil { 145 return fmt.Errorf("delete failed: %s", err) 146 } 147 148 return nil 149 } 150 151 // Resource Helpers 152 153 func makePingProbeResource(d *schema.ResourceData) (probeResource, error) { 154 p := probeResource{} 155 p.Zone = d.Get("zone").(string) 156 p.Name = d.Get("name").(string) 157 p.ID = d.Id() 158 p.Interval = d.Get("interval").(string) 159 p.PoolRecord = d.Get("pool_record").(string) 160 p.Threshold = d.Get("threshold").(int) 161 for _, a := range d.Get("agents").([]interface{}) { 162 p.Agents = append(p.Agents, a.(string)) 163 } 164 165 p.Type = udnssdk.PingProbeType 166 pps := d.Get("ping_probe").([]interface{}) 167 if len(pps) >= 1 { 168 if len(pps) > 1 { 169 return p, fmt.Errorf("ping_probe: only 0 or 1 blocks alowed, got: %#v", len(pps)) 170 } 171 p.Details = makePingProbeDetails(pps[0]) 172 } 173 174 return p, nil 175 } 176 177 func makePingProbeDetails(configured interface{}) *udnssdk.ProbeDetailsDTO { 178 data := configured.(map[string]interface{}) 179 // Convert limits from flattened set format to mapping. 180 ls := make(map[string]udnssdk.ProbeDetailsLimitDTO) 181 for _, limit := range data["limit"].(*schema.Set).List() { 182 l := limit.(map[string]interface{}) 183 name := l["name"].(string) 184 ls[name] = *makeProbeDetailsLimit(l) 185 } 186 res := udnssdk.ProbeDetailsDTO{ 187 Detail: udnssdk.PingProbeDetailsDTO{ 188 Limits: ls, 189 PacketSize: data["packet_size"].(int), 190 Packets: data["packets"].(int), 191 }, 192 } 193 return &res 194 } 195 196 func populateResourceDataFromPingProbe(p udnssdk.ProbeInfoDTO, d *schema.ResourceData) error { 197 d.SetId(p.ID) 198 d.Set("pool_record", p.PoolRecord) 199 d.Set("interval", p.Interval) 200 d.Set("agents", p.Agents) 201 d.Set("threshold", p.Threshold) 202 203 pd, err := p.Details.PingProbeDetails() 204 if err != nil { 205 return fmt.Errorf("ProbeInfo.details could not be unmarshalled: %v, Details: %#v", err, p.Details) 206 } 207 pp := map[string]interface{}{ 208 "packets": pd.Packets, 209 "packet_size": pd.PacketSize, 210 "limit": makeSetFromLimits(pd.Limits), 211 } 212 213 err = d.Set("ping_probe", []map[string]interface{}{pp}) 214 if err != nil { 215 return fmt.Errorf("ping_probe set failed: %v, from %#v", err, pp) 216 } 217 return nil 218 }