github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_subnet.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "github.com/Azure/azure-sdk-for-go/arm/network" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceArmSubnet() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceArmSubnetCreate, 15 Read: resourceArmSubnetRead, 16 Update: resourceArmSubnetCreate, 17 Delete: resourceArmSubnetDelete, 18 Importer: &schema.ResourceImporter{ 19 State: schema.ImportStatePassthrough, 20 }, 21 22 Schema: map[string]*schema.Schema{ 23 "name": { 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "resource_group_name": { 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "virtual_network_name": { 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "address_prefix": { 42 Type: schema.TypeString, 43 Required: true, 44 }, 45 46 "network_security_group_id": { 47 Type: schema.TypeString, 48 Optional: true, 49 Computed: true, 50 }, 51 52 "route_table_id": { 53 Type: schema.TypeString, 54 Optional: true, 55 Computed: true, 56 }, 57 58 "ip_configurations": { 59 Type: schema.TypeSet, 60 Optional: true, 61 Computed: true, 62 Elem: &schema.Schema{Type: schema.TypeString}, 63 Set: schema.HashString, 64 }, 65 }, 66 } 67 } 68 69 func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error { 70 client := meta.(*ArmClient) 71 subnetClient := client.subnetClient 72 73 log.Printf("[INFO] preparing arguments for Azure ARM Subnet creation.") 74 75 name := d.Get("name").(string) 76 vnetName := d.Get("virtual_network_name").(string) 77 resGroup := d.Get("resource_group_name").(string) 78 addressPrefix := d.Get("address_prefix").(string) 79 80 armMutexKV.Lock(vnetName) 81 defer armMutexKV.Unlock(vnetName) 82 83 properties := network.SubnetPropertiesFormat{ 84 AddressPrefix: &addressPrefix, 85 } 86 87 if v, ok := d.GetOk("network_security_group_id"); ok { 88 nsgId := v.(string) 89 properties.NetworkSecurityGroup = &network.SecurityGroup{ 90 ID: &nsgId, 91 } 92 93 networkSecurityGroupName, err := parseNetworkSecurityGroupName(nsgId) 94 if err != nil { 95 return err 96 } 97 98 armMutexKV.Lock(networkSecurityGroupName) 99 defer armMutexKV.Unlock(networkSecurityGroupName) 100 } 101 102 if v, ok := d.GetOk("route_table_id"); ok { 103 rtId := v.(string) 104 properties.RouteTable = &network.RouteTable{ 105 ID: &rtId, 106 } 107 108 routeTableName, err := parseRouteTableName(rtId) 109 if err != nil { 110 return err 111 } 112 113 armMutexKV.Lock(routeTableName) 114 defer armMutexKV.Unlock(routeTableName) 115 } 116 117 subnet := network.Subnet{ 118 Name: &name, 119 SubnetPropertiesFormat: &properties, 120 } 121 122 _, err := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{})) 123 if err != nil { 124 return err 125 } 126 127 read, err := subnetClient.Get(resGroup, vnetName, name, "") 128 if err != nil { 129 return err 130 } 131 if read.ID == nil { 132 return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup) 133 } 134 135 d.SetId(*read.ID) 136 137 return resourceArmSubnetRead(d, meta) 138 } 139 140 func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { 141 subnetClient := meta.(*ArmClient).subnetClient 142 143 id, err := parseAzureResourceID(d.Id()) 144 if err != nil { 145 return err 146 } 147 resGroup := id.ResourceGroup 148 vnetName := id.Path["virtualNetworks"] 149 name := id.Path["subnets"] 150 151 resp, err := subnetClient.Get(resGroup, vnetName, name, "") 152 153 if err != nil { 154 if resp.StatusCode == http.StatusNotFound { 155 d.SetId("") 156 return nil 157 } 158 return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err) 159 } 160 161 d.Set("name", name) 162 d.Set("resource_group_name", resGroup) 163 d.Set("virtual_network_name", vnetName) 164 d.Set("address_prefix", resp.SubnetPropertiesFormat.AddressPrefix) 165 166 if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil { 167 d.Set("network_security_group_id", resp.SubnetPropertiesFormat.NetworkSecurityGroup.ID) 168 } 169 170 if resp.SubnetPropertiesFormat.RouteTable != nil { 171 d.Set("route_table_id", resp.SubnetPropertiesFormat.RouteTable.ID) 172 } 173 174 if resp.SubnetPropertiesFormat.IPConfigurations != nil { 175 ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations)) 176 for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations { 177 ips = append(ips, *ip.ID) 178 } 179 180 if err := d.Set("ip_configurations", ips); err != nil { 181 return err 182 } 183 } else { 184 d.Set("ip_configurations", []string{}) 185 } 186 187 return nil 188 } 189 190 func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error { 191 subnetClient := meta.(*ArmClient).subnetClient 192 193 id, err := parseAzureResourceID(d.Id()) 194 if err != nil { 195 return err 196 } 197 resGroup := id.ResourceGroup 198 name := id.Path["subnets"] 199 vnetName := id.Path["virtualNetworks"] 200 201 if v, ok := d.GetOk("network_security_group_id"); ok { 202 networkSecurityGroupId := v.(string) 203 networkSecurityGroupName, err := parseNetworkSecurityGroupName(networkSecurityGroupId) 204 if err != nil { 205 return err 206 } 207 208 armMutexKV.Lock(networkSecurityGroupName) 209 defer armMutexKV.Unlock(networkSecurityGroupName) 210 } 211 212 if v, ok := d.GetOk("route_table_id"); ok { 213 rtId := v.(string) 214 routeTableName, err := parseRouteTableName(rtId) 215 if err != nil { 216 return err 217 } 218 219 armMutexKV.Lock(routeTableName) 220 defer armMutexKV.Unlock(routeTableName) 221 } 222 223 armMutexKV.Lock(vnetName) 224 defer armMutexKV.Unlock(vnetName) 225 226 _, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{})) 227 228 return err 229 }