github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_compute_global_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 resourceComputeGlobalForwardingRule() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeGlobalForwardingRuleCreate, 15 Read: resourceComputeGlobalForwardingRuleRead, 16 Update: resourceComputeGlobalForwardingRuleUpdate, 17 Delete: resourceComputeGlobalForwardingRuleDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "target": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 31 "description": &schema.Schema{ 32 Type: schema.TypeString, 33 Optional: true, 34 ForceNew: true, 35 }, 36 37 "ip_address": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 ForceNew: true, 41 Computed: true, 42 }, 43 44 "ip_protocol": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 Computed: true, 49 }, 50 51 "port_range": &schema.Schema{ 52 Type: schema.TypeString, 53 Optional: true, 54 ForceNew: true, 55 }, 56 57 "project": &schema.Schema{ 58 Type: schema.TypeString, 59 Optional: true, 60 ForceNew: true, 61 }, 62 63 "region": &schema.Schema{ 64 Type: schema.TypeString, 65 Optional: true, 66 ForceNew: true, 67 Deprecated: "Please remove this attribute (it was never used)", 68 }, 69 70 "self_link": &schema.Schema{ 71 Type: schema.TypeString, 72 Computed: true, 73 }, 74 }, 75 } 76 } 77 78 func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { 79 config := meta.(*Config) 80 81 project, err := getProject(d, config) 82 if err != nil { 83 return err 84 } 85 86 frule := &compute.ForwardingRule{ 87 IPAddress: d.Get("ip_address").(string), 88 IPProtocol: d.Get("ip_protocol").(string), 89 Description: d.Get("description").(string), 90 Name: d.Get("name").(string), 91 PortRange: d.Get("port_range").(string), 92 Target: d.Get("target").(string), 93 } 94 95 op, err := config.clientCompute.GlobalForwardingRules.Insert( 96 project, frule).Do() 97 if err != nil { 98 return fmt.Errorf("Error creating Global Forwarding Rule: %s", err) 99 } 100 101 // It probably maybe worked, so store the ID now 102 d.SetId(frule.Name) 103 104 err = computeOperationWaitGlobal(config, op, project, "Creating Global Fowarding Rule") 105 if err != nil { 106 return err 107 } 108 109 return resourceComputeGlobalForwardingRuleRead(d, meta) 110 } 111 112 func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { 113 config := meta.(*Config) 114 115 project, err := getProject(d, config) 116 if err != nil { 117 return err 118 } 119 120 d.Partial(true) 121 122 if d.HasChange("target") { 123 target_name := d.Get("target").(string) 124 target_ref := &compute.TargetReference{Target: target_name} 125 op, err := config.clientCompute.GlobalForwardingRules.SetTarget( 126 project, d.Id(), target_ref).Do() 127 if err != nil { 128 return fmt.Errorf("Error updating target: %s", err) 129 } 130 131 err = computeOperationWaitGlobal(config, op, project, "Updating Global Forwarding Rule") 132 if err != nil { 133 return err 134 } 135 136 d.SetPartial("target") 137 } 138 139 d.Partial(false) 140 141 return resourceComputeGlobalForwardingRuleRead(d, meta) 142 } 143 144 func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { 145 config := meta.(*Config) 146 147 project, err := getProject(d, config) 148 if err != nil { 149 return err 150 } 151 152 frule, err := config.clientCompute.GlobalForwardingRules.Get( 153 project, d.Id()).Do() 154 if err != nil { 155 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 156 log.Printf("[WARN] Removing Global Forwarding Rule %q because it's gone", d.Get("name").(string)) 157 // The resource doesn't exist anymore 158 d.SetId("") 159 160 return nil 161 } 162 163 return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) 164 } 165 166 d.Set("ip_address", frule.IPAddress) 167 d.Set("ip_protocol", frule.IPProtocol) 168 d.Set("self_link", frule.SelfLink) 169 170 return nil 171 } 172 173 func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { 174 config := meta.(*Config) 175 176 project, err := getProject(d, config) 177 if err != nil { 178 return err 179 } 180 181 // Delete the GlobalForwardingRule 182 log.Printf("[DEBUG] GlobalForwardingRule delete request") 183 op, err := config.clientCompute.GlobalForwardingRules.Delete( 184 project, d.Id()).Do() 185 if err != nil { 186 return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err) 187 } 188 189 err = computeOperationWaitGlobal(config, op, project, "Deleting GlobalForwarding Rule") 190 if err != nil { 191 return err 192 } 193 194 d.SetId("") 195 return nil 196 }