github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/azure/resource_azure_local_network.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 // resourceAzureLocalNetworkConnetion returns the schema.Resource associated to an 12 // Azure hosted service. 13 func resourceAzureLocalNetworkConnection() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAzureLocalNetworkConnectionCreate, 16 Read: resourceAzureLocalNetworkConnectionRead, 17 Update: resourceAzureLocalNetworkConnectionUpdate, 18 Exists: resourceAzureLocalNetworkConnectionExists, 19 Delete: resourceAzureLocalNetworkConnectionDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "name": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 Description: parameterDescriptions["name"], 27 }, 28 "vpn_gateway_address": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 Description: parameterDescriptions["vpn_gateway_address"], 32 }, 33 "address_space_prefixes": &schema.Schema{ 34 Type: schema.TypeList, 35 Required: true, 36 Elem: &schema.Schema{ 37 Type: schema.TypeString, 38 }, 39 Description: parameterDescriptions["address_space_prefixes"], 40 }, 41 }, 42 } 43 } 44 45 // sourceAzureLocalNetworkConnectionCreate issues all the necessary API calls 46 // to create a virtual network on Azure. 47 func resourceAzureLocalNetworkConnectionCreate(d *schema.ResourceData, meta interface{}) error { 48 azureClient := meta.(*Client) 49 mgmtClient := azureClient.mgmtClient 50 networkClient := virtualnetwork.NewClient(mgmtClient) 51 52 log.Println("[INFO] Fetching current network configuration from Azure.") 53 azureClient.mutex.Lock() 54 defer azureClient.mutex.Unlock() 55 netConf, err := networkClient.GetVirtualNetworkConfiguration() 56 if err != nil { 57 return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) 58 } 59 60 // get provided configuration: 61 name := d.Get("name").(string) 62 vpnGateway := d.Get("vpn_gateway_address").(string) 63 var prefixes []string 64 for _, prefix := range d.Get("address_space_prefixes").([]interface{}) { 65 prefixes = append(prefixes, prefix.(string)) 66 } 67 68 // add configuration to network config: 69 netConf.Configuration.LocalNetworkSites = append(netConf.Configuration.LocalNetworkSites, 70 virtualnetwork.LocalNetworkSite{ 71 Name: name, 72 VPNGatewayAddress: vpnGateway, 73 AddressSpace: virtualnetwork.AddressSpace{ 74 AddressPrefix: prefixes, 75 }, 76 }) 77 78 // send the configuration back to Azure: 79 log.Println("[INFO] Sending updated network configuration back to Azure.") 80 reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) 81 if err != nil { 82 return fmt.Errorf("Failed setting updated network configuration: %s", err) 83 } 84 err = mgmtClient.WaitForOperation(reqID, nil) 85 if err != nil { 86 return fmt.Errorf("Failed updating the network configuration: %s", err) 87 } 88 89 d.SetId(name) 90 return nil 91 } 92 93 // resourceAzureLocalNetworkConnectionRead does all the necessary API calls to 94 // read the state of our local natwork from Azure. 95 func resourceAzureLocalNetworkConnectionRead(d *schema.ResourceData, meta interface{}) error { 96 azureClient := meta.(*Client) 97 mgmtClient := azureClient.mgmtClient 98 networkClient := virtualnetwork.NewClient(mgmtClient) 99 100 log.Println("[INFO] Fetching current network configuration from Azure.") 101 netConf, err := networkClient.GetVirtualNetworkConfiguration() 102 if err != nil { 103 return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) 104 } 105 106 var found bool 107 name := d.Get("name").(string) 108 109 // browsing for our network config: 110 for _, lnet := range netConf.Configuration.LocalNetworkSites { 111 if lnet.Name == name { 112 found = true 113 d.Set("vpn_gateway_address", lnet.VPNGatewayAddress) 114 d.Set("address_space_prefixes", lnet.AddressSpace.AddressPrefix) 115 break 116 } 117 } 118 119 // remove the resource from the state of it has been deleted in the meantime: 120 if !found { 121 log.Println(fmt.Printf("[INFO] Azure local network '%s' has been deleted remotely. Removimg from Terraform.", name)) 122 d.SetId("") 123 } 124 125 return nil 126 } 127 128 // resourceAzureLocalNetworkConnectionUpdate does all the necessary API calls 129 // update the settings of our Local Network on Azure. 130 func resourceAzureLocalNetworkConnectionUpdate(d *schema.ResourceData, meta interface{}) error { 131 azureClient := meta.(*Client) 132 mgmtClient := azureClient.mgmtClient 133 networkClient := virtualnetwork.NewClient(mgmtClient) 134 135 log.Println("[INFO] Fetching current network configuration from Azure.") 136 azureClient.mutex.Lock() 137 defer azureClient.mutex.Unlock() 138 netConf, err := networkClient.GetVirtualNetworkConfiguration() 139 if err != nil { 140 return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) 141 } 142 143 name := d.Get("name").(string) 144 cvpn := d.HasChange("vpn_gateway_address") 145 cprefixes := d.HasChange("address_space_prefixes") 146 147 var found bool 148 for i, lnet := range netConf.Configuration.LocalNetworkSites { 149 if lnet.Name == name { 150 found = true 151 if cvpn { 152 netConf.Configuration.LocalNetworkSites[i].VPNGatewayAddress = d.Get("vpn_gateway_address").(string) 153 } 154 if cprefixes { 155 var prefixes []string 156 for _, prefix := range d.Get("address_space_prefixes").([]interface{}) { 157 prefixes = append(prefixes, prefix.(string)) 158 } 159 netConf.Configuration.LocalNetworkSites[i].AddressSpace.AddressPrefix = prefixes 160 } 161 break 162 } 163 } 164 165 // remove the resource from the state of it has been deleted in the meantime: 166 if !found { 167 log.Println(fmt.Printf("[INFO] Azure local network '%s' has been deleted remotely. Removimg from Terraform.", name)) 168 d.SetId("") 169 } else if cvpn || cprefixes { 170 // else, send the configuration back to Azure: 171 log.Println("[INFO] Sending updated network configuration back to Azure.") 172 reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) 173 if err != nil { 174 return fmt.Errorf("Failed setting updated network configuration: %s", err) 175 } 176 err = mgmtClient.WaitForOperation(reqID, nil) 177 if err != nil { 178 return fmt.Errorf("Failed updating the network configuration: %s", err) 179 } 180 } 181 182 return nil 183 } 184 185 // resourceAzureLocalNetworkConnectionExists does all the necessary API calls 186 // to check if the local network already exists on Azure. 187 func resourceAzureLocalNetworkConnectionExists(d *schema.ResourceData, meta interface{}) (bool, error) { 188 azureClient := meta.(*Client) 189 mgmtClient := azureClient.mgmtClient 190 networkClient := virtualnetwork.NewClient(mgmtClient) 191 192 log.Println("[INFO] Fetching current network configuration from Azure.") 193 netConf, err := networkClient.GetVirtualNetworkConfiguration() 194 if err != nil { 195 return false, fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) 196 } 197 198 name := d.Get("name") 199 200 for _, lnet := range netConf.Configuration.LocalNetworkSites { 201 if lnet.Name == name { 202 return true, nil 203 } 204 } 205 206 return false, nil 207 } 208 209 // resourceAzureLocalNetworkConnectionDelete does all the necessary API calls 210 // to delete a local network off Azure. 211 func resourceAzureLocalNetworkConnectionDelete(d *schema.ResourceData, meta interface{}) error { 212 azureClient := meta.(*Client) 213 mgmtClient := azureClient.mgmtClient 214 networkClient := virtualnetwork.NewClient(mgmtClient) 215 216 log.Println("[INFO] Fetching current network configuration from Azure.") 217 azureClient.mutex.Lock() 218 defer azureClient.mutex.Unlock() 219 netConf, err := networkClient.GetVirtualNetworkConfiguration() 220 if err != nil { 221 return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) 222 } 223 224 name := d.Get("name").(string) 225 226 // search for our local network and remove it if found: 227 for i, lnet := range netConf.Configuration.LocalNetworkSites { 228 if lnet.Name == name { 229 netConf.Configuration.LocalNetworkSites = append( 230 netConf.Configuration.LocalNetworkSites[:i], 231 netConf.Configuration.LocalNetworkSites[i+1:]..., 232 ) 233 break 234 } 235 } 236 237 // send the configuration back to Azure: 238 log.Println("[INFO] Sending updated network configuration back to Azure.") 239 reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) 240 if err != nil { 241 return fmt.Errorf("Failed setting updated network configuration: %s", err) 242 } 243 err = mgmtClient.WaitForOperation(reqID, nil) 244 if err != nil { 245 return fmt.Errorf("Failed updating the network configuration: %s", err) 246 } 247 248 d.SetId("") 249 return nil 250 }