github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/google/resource_compute_forwarding_rule.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "google.golang.org/api/compute/v1" 10 "google.golang.org/api/googleapi" 11 ) 12 13 func resourceComputeForwardingRule() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeForwardingRuleCreate, 16 Read: resourceComputeForwardingRuleRead, 17 Delete: resourceComputeForwardingRuleDelete, 18 Update: resourceComputeForwardingRuleUpdate, 19 20 Schema: map[string]*schema.Schema{ 21 "ip_address": &schema.Schema{ 22 Type: schema.TypeString, 23 Optional: true, 24 ForceNew: true, 25 Computed: true, 26 }, 27 28 "ip_protocol": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 ForceNew: true, 32 Computed: true, 33 }, 34 35 "description": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "name": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 }, 46 47 "port_range": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 ForceNew: true, 51 }, 52 53 "self_link": &schema.Schema{ 54 Type: schema.TypeString, 55 Computed: true, 56 }, 57 58 "target": &schema.Schema{ 59 Type: schema.TypeString, 60 Required: true, 61 ForceNew: false, 62 }, 63 }, 64 } 65 } 66 67 func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { 68 config := meta.(*Config) 69 70 frule := &compute.ForwardingRule{ 71 IPAddress: d.Get("ip_address").(string), 72 IPProtocol: d.Get("ip_protocol").(string), 73 Description: d.Get("description").(string), 74 Name: d.Get("name").(string), 75 PortRange: d.Get("port_range").(string), 76 Target: d.Get("target").(string), 77 } 78 79 log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule) 80 op, err := config.clientCompute.ForwardingRules.Insert( 81 config.Project, config.Region, frule).Do() 82 if err != nil { 83 return fmt.Errorf("Error creating ForwardingRule: %s", err) 84 } 85 86 // It probably maybe worked, so store the ID now 87 d.SetId(frule.Name) 88 89 // Wait for the operation to complete 90 w := &OperationWaiter{ 91 Service: config.clientCompute, 92 Op: op, 93 Region: config.Region, 94 Project: config.Project, 95 Type: OperationWaitRegion, 96 } 97 state := w.Conf() 98 state.Timeout = 2 * time.Minute 99 state.MinTimeout = 1 * time.Second 100 opRaw, err := state.WaitForState() 101 if err != nil { 102 return fmt.Errorf("Error waiting for ForwardingRule to create: %s", err) 103 } 104 op = opRaw.(*compute.Operation) 105 if op.Error != nil { 106 // The resource didn't actually create 107 d.SetId("") 108 109 // Return the error 110 return OperationError(*op.Error) 111 } 112 113 return resourceComputeForwardingRuleRead(d, meta) 114 } 115 116 func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { 117 config := meta.(*Config) 118 119 d.Partial(true) 120 121 if d.HasChange("target") { 122 target_name := d.Get("target").(string) 123 target_ref := &compute.TargetReference{Target: target_name} 124 op, err := config.clientCompute.ForwardingRules.SetTarget( 125 config.Project, config.Region, d.Id(), target_ref).Do() 126 if err != nil { 127 return fmt.Errorf("Error updating target: %s", err) 128 } 129 130 // Wait for the operation to complete 131 w := &OperationWaiter{ 132 Service: config.clientCompute, 133 Op: op, 134 Region: config.Region, 135 Project: config.Project, 136 Type: OperationWaitRegion, 137 } 138 state := w.Conf() 139 state.Timeout = 2 * time.Minute 140 state.MinTimeout = 1 * time.Second 141 opRaw, err := state.WaitForState() 142 if err != nil { 143 return fmt.Errorf("Error waiting for ForwardingRule to update target: %s", err) 144 } 145 op = opRaw.(*compute.Operation) 146 if op.Error != nil { 147 // The resource didn't actually create 148 d.SetId("") 149 150 // Return the error 151 return OperationError(*op.Error) 152 } 153 d.SetPartial("target") 154 } 155 156 d.Partial(false) 157 158 return resourceComputeForwardingRuleRead(d, meta) 159 } 160 161 func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { 162 config := meta.(*Config) 163 164 frule, err := config.clientCompute.ForwardingRules.Get( 165 config.Project, config.Region, d.Id()).Do() 166 if err != nil { 167 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 168 // The resource doesn't exist anymore 169 d.SetId("") 170 171 return nil 172 } 173 174 return fmt.Errorf("Error reading ForwardingRule: %s", err) 175 } 176 177 d.Set("ip_address", frule.IPAddress) 178 d.Set("ip_protocol", frule.IPProtocol) 179 d.Set("self_link", frule.SelfLink) 180 181 return nil 182 } 183 184 func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { 185 config := meta.(*Config) 186 187 // Delete the ForwardingRule 188 log.Printf("[DEBUG] ForwardingRule delete request") 189 op, err := config.clientCompute.ForwardingRules.Delete( 190 config.Project, config.Region, d.Id()).Do() 191 if err != nil { 192 return fmt.Errorf("Error deleting ForwardingRule: %s", err) 193 } 194 195 // Wait for the operation to complete 196 w := &OperationWaiter{ 197 Service: config.clientCompute, 198 Op: op, 199 Region: config.Region, 200 Project: config.Project, 201 Type: OperationWaitRegion, 202 } 203 state := w.Conf() 204 state.Timeout = 2 * time.Minute 205 state.MinTimeout = 1 * time.Second 206 opRaw, err := state.WaitForState() 207 if err != nil { 208 return fmt.Errorf("Error waiting for ForwardingRule to delete: %s", err) 209 } 210 op = opRaw.(*compute.Operation) 211 if op.Error != nil { 212 // Return the error 213 return OperationError(*op.Error) 214 } 215 216 d.SetId("") 217 return nil 218 }