github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_subnetwork.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "google.golang.org/api/compute/v1" 11 "google.golang.org/api/googleapi" 12 ) 13 14 func resourceComputeSubnetwork() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceComputeSubnetworkCreate, 17 Read: resourceComputeSubnetworkRead, 18 Delete: resourceComputeSubnetworkDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "ip_cidr_range": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 33 "network": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 }, 38 39 "description": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 ForceNew: true, 43 }, 44 45 "gateway_address": &schema.Schema{ 46 Type: schema.TypeString, 47 Computed: true, 48 }, 49 50 "project": &schema.Schema{ 51 Type: schema.TypeString, 52 Optional: true, 53 ForceNew: true, 54 }, 55 56 "region": &schema.Schema{ 57 Type: schema.TypeString, 58 Optional: true, 59 ForceNew: true, 60 }, 61 62 "self_link": &schema.Schema{ 63 Type: schema.TypeString, 64 Computed: true, 65 }, 66 }, 67 } 68 } 69 70 func createSubnetID(s *compute.Subnetwork) string { 71 return fmt.Sprintf("%s/%s", s.Region, s.Name) 72 } 73 74 func splitSubnetID(id string) (region string, name string) { 75 parts := strings.Split(id, "/") 76 region = parts[0] 77 name = parts[1] 78 return 79 } 80 81 func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error { 82 config := meta.(*Config) 83 84 region, err := getRegion(d, config) 85 if err != nil { 86 return err 87 } 88 89 project, err := getProject(d, config) 90 if err != nil { 91 return err 92 } 93 94 network, err := getNetworkLink(d, config, "network") 95 if err != nil { 96 return err 97 } 98 99 // Build the subnetwork parameters 100 subnetwork := &compute.Subnetwork{ 101 Name: d.Get("name").(string), 102 Description: d.Get("description").(string), 103 IpCidrRange: d.Get("ip_cidr_range").(string), 104 Network: network, 105 } 106 107 log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork) 108 op, err := config.clientCompute.Subnetworks.Insert( 109 project, region, subnetwork).Do() 110 111 if err != nil { 112 return fmt.Errorf("Error creating subnetwork: %s", err) 113 } 114 115 // It probably maybe worked, so store the ID now. ID is a combination of region + subnetwork 116 // name because subnetwork names are not unique in a project, per the Google docs: 117 // "When creating a new subnetwork, its name has to be unique in that project for that region, even across networks. 118 // The same name can appear twice in a project, as long as each one is in a different region." 119 // https://cloud.google.com/compute/docs/subnetworks 120 subnetwork.Region = region 121 d.SetId(createSubnetID(subnetwork)) 122 123 err = computeOperationWaitRegion(config, op, project, region, "Creating Subnetwork") 124 if err != nil { 125 return err 126 } 127 128 return resourceComputeSubnetworkRead(d, meta) 129 } 130 131 func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error { 132 config := meta.(*Config) 133 134 region, err := getRegion(d, config) 135 if err != nil { 136 return err 137 } 138 139 project, err := getProject(d, config) 140 if err != nil { 141 return err 142 } 143 144 name := d.Get("name").(string) 145 146 subnetwork, err := config.clientCompute.Subnetworks.Get( 147 project, region, name).Do() 148 if err != nil { 149 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 150 log.Printf("[WARN] Removing Subnetwork %q because it's gone", name) 151 // The resource doesn't exist anymore 152 d.SetId("") 153 154 return nil 155 } 156 157 return fmt.Errorf("Error reading subnetwork: %s", err) 158 } 159 160 d.Set("gateway_address", subnetwork.GatewayAddress) 161 d.Set("self_link", subnetwork.SelfLink) 162 163 return nil 164 } 165 166 func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error { 167 config := meta.(*Config) 168 169 region, err := getRegion(d, config) 170 if err != nil { 171 return err 172 } 173 174 project, err := getProject(d, config) 175 if err != nil { 176 return err 177 } 178 179 // Delete the subnetwork 180 op, err := config.clientCompute.Subnetworks.Delete( 181 project, region, d.Get("name").(string)).Do() 182 if err != nil { 183 return fmt.Errorf("Error deleting subnetwork: %s", err) 184 } 185 186 err = computeOperationWaitRegion(config, op, project, region, "Deleting Subnetwork") 187 if err != nil { 188 return err 189 } 190 191 d.SetId("") 192 return nil 193 }