github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/spotinst/resource_spotinst_subscription.go (about) 1 package spotinst 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/spotinst/spotinst-sdk-go/spotinst" 10 "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" 11 ) 12 13 func resourceSpotinstSubscription() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceSpotinstSubscriptionCreate, 16 Update: resourceSpotinstSubscriptionUpdate, 17 Read: resourceSpotinstSubscriptionRead, 18 Delete: resourceSpotinstSubscriptionDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "resource_id": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 26 "event_type": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 StateFunc: func(v interface{}) string { 30 value := v.(string) 31 return strings.ToUpper(value) 32 }, 33 }, 34 35 "protocol": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 40 "endpoint": &schema.Schema{ 41 Type: schema.TypeString, 42 Required: true, 43 }, 44 45 "format": &schema.Schema{ 46 Type: schema.TypeMap, 47 Optional: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceSpotinstSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { 54 client := meta.(*spotinst.Client) 55 newSubscription, err := buildSubscriptionOpts(d, meta) 56 if err != nil { 57 return err 58 } 59 log.Printf("[DEBUG] Subscription create configuration: %s\n", stringutil.Stringify(newSubscription)) 60 input := &spotinst.CreateSubscriptionInput{Subscription: newSubscription} 61 resp, err := client.SubscriptionService.Create(input) 62 if err != nil { 63 return fmt.Errorf("Error creating subscription: %s", err) 64 } 65 d.SetId(spotinst.StringValue(resp.Subscription.ID)) 66 log.Printf("[INFO] Subscription created successfully: %s\n", d.Id()) 67 return resourceSpotinstSubscriptionRead(d, meta) 68 } 69 70 func resourceSpotinstSubscriptionRead(d *schema.ResourceData, meta interface{}) error { 71 client := meta.(*spotinst.Client) 72 input := &spotinst.ReadSubscriptionInput{ID: spotinst.String(d.Id())} 73 resp, err := client.SubscriptionService.Read(input) 74 if err != nil { 75 return fmt.Errorf("Error retrieving subscription: %s", err) 76 } 77 if s := resp.Subscription; s != nil { 78 d.Set("resource_id", s.ResourceID) 79 d.Set("event_type", s.EventType) 80 d.Set("protocol", s.Protocol) 81 d.Set("endpoint", s.Endpoint) 82 d.Set("format", s.Format) 83 } else { 84 d.SetId("") 85 } 86 return nil 87 } 88 89 func resourceSpotinstSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error { 90 client := meta.(*spotinst.Client) 91 subscription := &spotinst.Subscription{ID: spotinst.String(d.Id())} 92 update := false 93 94 if d.HasChange("resource_id") { 95 subscription.ResourceID = spotinst.String(d.Get("resource_id").(string)) 96 update = true 97 } 98 99 if d.HasChange("event_type") { 100 subscription.EventType = spotinst.String(d.Get("event_type").(string)) 101 update = true 102 } 103 104 if d.HasChange("protocol") { 105 subscription.Protocol = spotinst.String(d.Get("protocol").(string)) 106 update = true 107 } 108 109 if d.HasChange("endpoint") { 110 subscription.Endpoint = spotinst.String(d.Get("endpoint").(string)) 111 update = true 112 } 113 114 if d.HasChange("format") { 115 subscription.Format = d.Get("format").(map[string]interface{}) 116 update = true 117 } 118 119 if update { 120 log.Printf("[DEBUG] Subscription update configuration: %s\n", stringutil.Stringify(subscription)) 121 input := &spotinst.UpdateSubscriptionInput{Subscription: subscription} 122 if _, err := client.SubscriptionService.Update(input); err != nil { 123 return fmt.Errorf("Error updating subscription %s: %s", d.Id(), err) 124 } 125 } 126 127 return resourceSpotinstSubscriptionRead(d, meta) 128 } 129 130 func resourceSpotinstSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { 131 d.SetId("") 132 return nil 133 } 134 135 // buildSubscriptionOpts builds the Spotinst Subscription options. 136 func buildSubscriptionOpts(d *schema.ResourceData, meta interface{}) (*spotinst.Subscription, error) { 137 subscription := &spotinst.Subscription{ 138 ResourceID: spotinst.String(d.Get("resource_id").(string)), 139 EventType: spotinst.String(strings.ToUpper(d.Get("event_type").(string))), 140 Protocol: spotinst.String(d.Get("protocol").(string)), 141 Endpoint: spotinst.String(d.Get("endpoint").(string)), 142 Format: d.Get("format").(map[string]interface{}), 143 } 144 return subscription, nil 145 }