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