github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_compute_forwarding_rule.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/compute/v1" 9 "google.golang.org/api/googleapi" 10 ) 11 12 func resourceComputeForwardingRule() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeForwardingRuleCreate, 15 Read: resourceComputeForwardingRuleRead, 16 Delete: resourceComputeForwardingRuleDelete, 17 Update: resourceComputeForwardingRuleUpdate, 18 Importer: &schema.ResourceImporter{ 19 State: schema.ImportStatePassthrough, 20 }, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "target": &schema.Schema{ 30 Type: schema.TypeString, 31 Optional: true, 32 ForceNew: false, 33 }, 34 35 "backend_service": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "description": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 ForceNew: true, 45 }, 46 47 "ip_address": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 ForceNew: true, 51 Computed: true, 52 }, 53 54 "ip_protocol": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 ForceNew: true, 58 Computed: true, 59 }, 60 61 "load_balancing_scheme": &schema.Schema{ 62 Type: schema.TypeString, 63 Optional: true, 64 ForceNew: true, 65 Default: "EXTERNAL", 66 }, 67 68 "network": &schema.Schema{ 69 Type: schema.TypeString, 70 Optional: true, 71 ForceNew: true, 72 Computed: true, 73 }, 74 75 "port_range": &schema.Schema{ 76 Type: schema.TypeString, 77 Optional: true, 78 ForceNew: true, 79 }, 80 81 "ports": &schema.Schema{ 82 Type: schema.TypeSet, 83 Elem: &schema.Schema{Type: schema.TypeString}, 84 Optional: true, 85 Set: schema.HashString, 86 }, 87 88 "project": &schema.Schema{ 89 Type: schema.TypeString, 90 Optional: true, 91 ForceNew: true, 92 Computed: true, 93 }, 94 95 "region": &schema.Schema{ 96 Type: schema.TypeString, 97 Optional: true, 98 ForceNew: true, 99 Computed: true, 100 }, 101 102 "self_link": &schema.Schema{ 103 Type: schema.TypeString, 104 Computed: true, 105 }, 106 107 "subnetwork": &schema.Schema{ 108 Type: schema.TypeString, 109 Optional: true, 110 ForceNew: true, 111 Computed: true, 112 }, 113 }, 114 } 115 } 116 117 func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { 118 config := meta.(*Config) 119 120 region, err := getRegion(d, config) 121 if err != nil { 122 return err 123 } 124 125 project, err := getProject(d, config) 126 if err != nil { 127 return err 128 } 129 130 ps := d.Get("ports").(*schema.Set).List() 131 ports := make([]string, 0, len(ps)) 132 for _, v := range ps { 133 ports = append(ports, v.(string)) 134 } 135 136 frule := &compute.ForwardingRule{ 137 BackendService: d.Get("backend_service").(string), 138 IPAddress: d.Get("ip_address").(string), 139 IPProtocol: d.Get("ip_protocol").(string), 140 Description: d.Get("description").(string), 141 LoadBalancingScheme: d.Get("load_balancing_scheme").(string), 142 Name: d.Get("name").(string), 143 Network: d.Get("network").(string), 144 PortRange: d.Get("port_range").(string), 145 Ports: ports, 146 Subnetwork: d.Get("subnetwork").(string), 147 Target: d.Get("target").(string), 148 } 149 150 log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule) 151 op, err := config.clientCompute.ForwardingRules.Insert( 152 project, region, frule).Do() 153 if err != nil { 154 return fmt.Errorf("Error creating ForwardingRule: %s", err) 155 } 156 157 // It probably maybe worked, so store the ID now 158 d.SetId(frule.Name) 159 160 err = computeOperationWaitRegion(config, op, project, region, "Creating Fowarding Rule") 161 if err != nil { 162 return err 163 } 164 165 return resourceComputeForwardingRuleRead(d, meta) 166 } 167 168 func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { 169 config := meta.(*Config) 170 171 region, err := getRegion(d, config) 172 if err != nil { 173 return err 174 } 175 176 project, err := getProject(d, config) 177 if err != nil { 178 return err 179 } 180 181 d.Partial(true) 182 183 if d.HasChange("target") { 184 target_name := d.Get("target").(string) 185 target_ref := &compute.TargetReference{Target: target_name} 186 op, err := config.clientCompute.ForwardingRules.SetTarget( 187 project, region, d.Id(), target_ref).Do() 188 if err != nil { 189 return fmt.Errorf("Error updating target: %s", err) 190 } 191 192 err = computeOperationWaitRegion(config, op, project, region, "Updating Forwarding Rule") 193 if err != nil { 194 return err 195 } 196 197 d.SetPartial("target") 198 } 199 200 d.Partial(false) 201 202 return resourceComputeForwardingRuleRead(d, meta) 203 } 204 205 func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { 206 config := meta.(*Config) 207 208 region, err := getRegion(d, config) 209 if err != nil { 210 return err 211 } 212 213 project, err := getProject(d, config) 214 if err != nil { 215 return err 216 } 217 218 frule, err := config.clientCompute.ForwardingRules.Get( 219 project, region, d.Id()).Do() 220 if err != nil { 221 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 222 log.Printf("[WARN] Removing Forwarding Rule %q because it's gone", d.Get("name").(string)) 223 // The resource doesn't exist anymore 224 d.SetId("") 225 226 return nil 227 } 228 229 return fmt.Errorf("Error reading ForwardingRule: %s", err) 230 } 231 232 d.Set("name", frule.Name) 233 d.Set("target", frule.Target) 234 d.Set("backend_service", frule.BackendService) 235 d.Set("description", frule.Description) 236 d.Set("load_balancing_scheme", frule.LoadBalancingScheme) 237 d.Set("network", frule.Network) 238 d.Set("port_range", frule.PortRange) 239 d.Set("ports", frule.Ports) 240 d.Set("project", project) 241 d.Set("region", region) 242 d.Set("subnetwork", frule.Subnetwork) 243 d.Set("ip_address", frule.IPAddress) 244 d.Set("ip_protocol", frule.IPProtocol) 245 d.Set("self_link", frule.SelfLink) 246 return nil 247 } 248 249 func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { 250 config := meta.(*Config) 251 252 region, err := getRegion(d, config) 253 if err != nil { 254 return err 255 } 256 257 project, err := getProject(d, config) 258 if err != nil { 259 return err 260 } 261 262 // Delete the ForwardingRule 263 log.Printf("[DEBUG] ForwardingRule delete request") 264 op, err := config.clientCompute.ForwardingRules.Delete( 265 project, region, d.Id()).Do() 266 if err != nil { 267 return fmt.Errorf("Error deleting ForwardingRule: %s", err) 268 } 269 270 err = computeOperationWaitRegion(config, op, project, region, "Deleting Forwarding Rule") 271 if err != nil { 272 return err 273 } 274 275 d.SetId("") 276 return nil 277 }