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