github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/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(name) 81 defer armMutexKV.Unlock(name) 82 83 armMutexKV.Lock(vnetName) 84 defer armMutexKV.Unlock(vnetName) 85 86 properties := network.SubnetPropertiesFormat{ 87 AddressPrefix: &addressPrefix, 88 } 89 90 if v, ok := d.GetOk("network_security_group_id"); ok { 91 nsgId := v.(string) 92 properties.NetworkSecurityGroup = &network.SecurityGroup{ 93 ID: &nsgId, 94 } 95 96 networkSecurityGroupName, err := parseNetworkSecurityGroupName(nsgId) 97 if err != nil { 98 return err 99 } 100 101 armMutexKV.Lock(networkSecurityGroupName) 102 defer armMutexKV.Unlock(networkSecurityGroupName) 103 } 104 105 if v, ok := d.GetOk("route_table_id"); ok { 106 rtId := v.(string) 107 properties.RouteTable = &network.RouteTable{ 108 ID: &rtId, 109 } 110 111 routeTableName, err := parseRouteTableName(rtId) 112 if err != nil { 113 return err 114 } 115 116 armMutexKV.Lock(routeTableName) 117 defer armMutexKV.Unlock(routeTableName) 118 } 119 120 subnet := network.Subnet{ 121 Name: &name, 122 SubnetPropertiesFormat: &properties, 123 } 124 125 _, err := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{})) 126 if err != nil { 127 return err 128 } 129 130 read, err := subnetClient.Get(resGroup, vnetName, name, "") 131 if err != nil { 132 return err 133 } 134 if read.ID == nil { 135 return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup) 136 } 137 138 d.SetId(*read.ID) 139 140 return resourceArmSubnetRead(d, meta) 141 } 142 143 func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { 144 subnetClient := meta.(*ArmClient).subnetClient 145 146 id, err := parseAzureResourceID(d.Id()) 147 if err != nil { 148 return err 149 } 150 resGroup := id.ResourceGroup 151 vnetName := id.Path["virtualNetworks"] 152 name := id.Path["subnets"] 153 154 resp, err := subnetClient.Get(resGroup, vnetName, name, "") 155 156 if err != nil { 157 if resp.StatusCode == http.StatusNotFound { 158 d.SetId("") 159 return nil 160 } 161 return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err) 162 } 163 164 d.Set("name", name) 165 d.Set("resource_group_name", resGroup) 166 d.Set("virtual_network_name", vnetName) 167 d.Set("address_prefix", resp.SubnetPropertiesFormat.AddressPrefix) 168 169 if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil { 170 d.Set("network_security_group_id", resp.SubnetPropertiesFormat.NetworkSecurityGroup.ID) 171 } 172 173 if resp.SubnetPropertiesFormat.RouteTable != nil { 174 d.Set("route_table_id", resp.SubnetPropertiesFormat.RouteTable.ID) 175 } 176 177 if resp.SubnetPropertiesFormat.IPConfigurations != nil { 178 ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations)) 179 for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations { 180 ips = append(ips, *ip.ID) 181 } 182 183 if err := d.Set("ip_configurations", ips); err != nil { 184 return err 185 } 186 } else { 187 d.Set("ip_configurations", []string{}) 188 } 189 190 return nil 191 } 192 193 func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error { 194 subnetClient := meta.(*ArmClient).subnetClient 195 196 id, err := parseAzureResourceID(d.Id()) 197 if err != nil { 198 return err 199 } 200 resGroup := id.ResourceGroup 201 name := id.Path["subnets"] 202 vnetName := id.Path["virtualNetworks"] 203 204 if v, ok := d.GetOk("network_security_group_id"); ok { 205 networkSecurityGroupId := v.(string) 206 networkSecurityGroupName, err := parseNetworkSecurityGroupName(networkSecurityGroupId) 207 if err != nil { 208 return err 209 } 210 211 armMutexKV.Lock(networkSecurityGroupName) 212 defer armMutexKV.Unlock(networkSecurityGroupName) 213 } 214 215 if v, ok := d.GetOk("route_table_id"); ok { 216 rtId := v.(string) 217 routeTableName, err := parseRouteTableName(rtId) 218 if err != nil { 219 return err 220 } 221 222 armMutexKV.Lock(routeTableName) 223 defer armMutexKV.Unlock(routeTableName) 224 } 225 226 armMutexKV.Lock(vnetName) 227 defer armMutexKV.Unlock(vnetName) 228 229 armMutexKV.Lock(name) 230 defer armMutexKV.Unlock(name) 231 232 _, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{})) 233 234 return err 235 }