github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 8 "github.com/hashicorp/terraform/helper/hashcode" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/rackspace/gophercloud/openstack/networking/v2/subnets" 11 ) 12 13 func resourceNetworkingSubnetV2() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceNetworkingSubnetV2Create, 16 Read: resourceNetworkingSubnetV2Read, 17 Update: resourceNetworkingSubnetV2Update, 18 Delete: resourceNetworkingSubnetV2Delete, 19 20 Schema: map[string]*schema.Schema{ 21 "region": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 DefaultFunc: envDefaultFunc("OS_REGION_NAME"), 26 }, 27 "network_id": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "cidr": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 "name": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 ForceNew: false, 41 }, 42 "tenant_id": &schema.Schema{ 43 Type: schema.TypeString, 44 Optional: true, 45 ForceNew: true, 46 }, 47 "allocation_pools": &schema.Schema{ 48 Type: schema.TypeList, 49 Optional: true, 50 ForceNew: true, 51 Elem: &schema.Resource{ 52 Schema: map[string]*schema.Schema{ 53 "start": &schema.Schema{ 54 Type: schema.TypeString, 55 Required: true, 56 }, 57 "end": &schema.Schema{ 58 Type: schema.TypeString, 59 Required: true, 60 }, 61 }, 62 }, 63 }, 64 "gateway_ip": &schema.Schema{ 65 Type: schema.TypeString, 66 Optional: true, 67 ForceNew: false, 68 }, 69 "ip_version": &schema.Schema{ 70 Type: schema.TypeInt, 71 Required: true, 72 ForceNew: true, 73 }, 74 "enable_dhcp": &schema.Schema{ 75 Type: schema.TypeString, 76 Optional: true, 77 ForceNew: false, 78 }, 79 "dns_nameservers": &schema.Schema{ 80 Type: schema.TypeSet, 81 Optional: true, 82 ForceNew: false, 83 Elem: &schema.Schema{Type: schema.TypeString}, 84 Set: func(v interface{}) int { 85 return hashcode.String(v.(string)) 86 }, 87 }, 88 "host_routes": &schema.Schema{ 89 Type: schema.TypeList, 90 Optional: true, 91 ForceNew: false, 92 Elem: &schema.Resource{ 93 Schema: map[string]*schema.Schema{ 94 "destination_cidr": &schema.Schema{ 95 Type: schema.TypeString, 96 Required: true, 97 }, 98 "next_hop": &schema.Schema{ 99 Type: schema.TypeString, 100 Required: true, 101 }, 102 }, 103 }, 104 }, 105 }, 106 } 107 } 108 109 func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{}) error { 110 config := meta.(*Config) 111 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 112 if err != nil { 113 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 114 } 115 116 createOpts := subnets.CreateOpts{ 117 NetworkID: d.Get("network_id").(string), 118 CIDR: d.Get("cidr").(string), 119 Name: d.Get("name").(string), 120 TenantID: d.Get("tenant_id").(string), 121 AllocationPools: resourceSubnetAllocationPoolsV2(d), 122 GatewayIP: d.Get("gateway_ip").(string), 123 IPVersion: d.Get("ip_version").(int), 124 DNSNameservers: resourceSubnetDNSNameserversV2(d), 125 HostRoutes: resourceSubnetHostRoutesV2(d), 126 } 127 128 edRaw := d.Get("enable_dhcp").(string) 129 if edRaw != "" { 130 ed, err := strconv.ParseBool(edRaw) 131 if err != nil { 132 return fmt.Errorf("enable_dhcp, if provided, must be either 'true' or 'false'") 133 } 134 createOpts.EnableDHCP = &ed 135 } 136 137 log.Printf("[DEBUG] Create Options: %#v", createOpts) 138 s, err := subnets.Create(networkingClient, createOpts).Extract() 139 if err != nil { 140 return fmt.Errorf("Error creating OpenStack Neutron subnet: %s", err) 141 } 142 log.Printf("[INFO] Subnet ID: %s", s.ID) 143 144 d.SetId(s.ID) 145 146 return resourceNetworkingSubnetV2Read(d, meta) 147 } 148 149 func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) error { 150 config := meta.(*Config) 151 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 152 if err != nil { 153 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 154 } 155 156 s, err := subnets.Get(networkingClient, d.Id()).Extract() 157 if err != nil { 158 return CheckDeleted(d, err, "subnet") 159 } 160 161 log.Printf("[DEBUG] Retreived Subnet %s: %+v", d.Id(), s) 162 163 d.Set("newtork_id", s.NetworkID) 164 d.Set("cidr", s.CIDR) 165 d.Set("ip_version", s.IPVersion) 166 d.Set("name", s.Name) 167 d.Set("tenant_id", s.TenantID) 168 d.Set("allocation_pools", s.AllocationPools) 169 d.Set("gateway_ip", s.GatewayIP) 170 d.Set("enable_dhcp", strconv.FormatBool(s.EnableDHCP)) 171 d.Set("dns_nameservers", s.DNSNameservers) 172 d.Set("host_routes", s.HostRoutes) 173 174 return nil 175 } 176 177 func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{}) error { 178 config := meta.(*Config) 179 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 180 if err != nil { 181 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 182 } 183 184 var updateOpts subnets.UpdateOpts 185 186 if d.HasChange("name") { 187 updateOpts.Name = d.Get("name").(string) 188 } 189 190 if d.HasChange("gateway_ip") { 191 updateOpts.GatewayIP = d.Get("gateway_ip").(string) 192 } 193 194 if d.HasChange("dns_nameservers") { 195 updateOpts.DNSNameservers = resourceSubnetDNSNameserversV2(d) 196 } 197 198 if d.HasChange("host_routes") { 199 updateOpts.HostRoutes = resourceSubnetHostRoutesV2(d) 200 } 201 202 if d.HasChange("enable_dhcp") { 203 edRaw := d.Get("enable_dhcp").(string) 204 if edRaw != "" { 205 ed, err := strconv.ParseBool(edRaw) 206 if err != nil { 207 return fmt.Errorf("enable_dhcp, if provided, must be either 'true' or 'false'") 208 } 209 updateOpts.EnableDHCP = &ed 210 } 211 } 212 213 log.Printf("[DEBUG] Updating Subnet %s with options: %+v", d.Id(), updateOpts) 214 215 _, err = subnets.Update(networkingClient, d.Id(), updateOpts).Extract() 216 if err != nil { 217 return fmt.Errorf("Error updating OpenStack Neutron Subnet: %s", err) 218 } 219 220 return resourceNetworkingSubnetV2Read(d, meta) 221 } 222 223 func resourceNetworkingSubnetV2Delete(d *schema.ResourceData, meta interface{}) error { 224 config := meta.(*Config) 225 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 226 if err != nil { 227 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 228 } 229 230 err = subnets.Delete(networkingClient, d.Id()).ExtractErr() 231 if err != nil { 232 return fmt.Errorf("Error deleting OpenStack Neutron Subnet: %s", err) 233 } 234 235 d.SetId("") 236 return nil 237 } 238 239 func resourceSubnetAllocationPoolsV2(d *schema.ResourceData) []subnets.AllocationPool { 240 rawAPs := d.Get("allocation_pools").([]interface{}) 241 aps := make([]subnets.AllocationPool, len(rawAPs)) 242 for i, raw := range rawAPs { 243 rawMap := raw.(map[string]interface{}) 244 aps[i] = subnets.AllocationPool{ 245 Start: rawMap["start"].(string), 246 End: rawMap["end"].(string), 247 } 248 } 249 return aps 250 } 251 252 func resourceSubnetDNSNameserversV2(d *schema.ResourceData) []string { 253 rawDNSN := d.Get("dns_nameservers").(*schema.Set) 254 dnsn := make([]string, rawDNSN.Len()) 255 for i, raw := range rawDNSN.List() { 256 dnsn[i] = raw.(string) 257 } 258 return dnsn 259 } 260 261 func resourceSubnetHostRoutesV2(d *schema.ResourceData) []subnets.HostRoute { 262 rawHR := d.Get("host_routes").([]interface{}) 263 hr := make([]subnets.HostRoute, len(rawHR)) 264 for i, raw := range rawHR { 265 rawMap := raw.(map[string]interface{}) 266 hr[i] = subnets.HostRoute{ 267 DestinationCIDR: rawMap["destination_cidr"].(string), 268 NextHop: rawMap["next_hop"].(string), 269 } 270 } 271 return hr 272 }