github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/config.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "net/http/httputil" 8 9 "github.com/Azure/azure-sdk-for-go/arm/cdn" 10 "github.com/Azure/azure-sdk-for-go/arm/compute" 11 "github.com/Azure/azure-sdk-for-go/arm/network" 12 "github.com/Azure/azure-sdk-for-go/arm/resources/resources" 13 "github.com/Azure/azure-sdk-for-go/arm/scheduler" 14 "github.com/Azure/azure-sdk-for-go/arm/servicebus" 15 "github.com/Azure/azure-sdk-for-go/arm/storage" 16 "github.com/Azure/azure-sdk-for-go/arm/trafficmanager" 17 mainStorage "github.com/Azure/azure-sdk-for-go/storage" 18 "github.com/Azure/go-autorest/autorest" 19 "github.com/Azure/go-autorest/autorest/azure" 20 "github.com/hashicorp/terraform/terraform" 21 riviera "github.com/jen20/riviera/azure" 22 ) 23 24 // ArmClient contains the handles to all the specific Azure Resource Manager 25 // resource classes' respective clients. 26 type ArmClient struct { 27 rivieraClient *riviera.Client 28 29 availSetClient compute.AvailabilitySetsClient 30 usageOpsClient compute.UsageOperationsClient 31 vmExtensionImageClient compute.VirtualMachineExtensionImagesClient 32 vmExtensionClient compute.VirtualMachineExtensionsClient 33 vmScaleSetClient compute.VirtualMachineScaleSetsClient 34 vmImageClient compute.VirtualMachineImagesClient 35 vmClient compute.VirtualMachinesClient 36 37 appGatewayClient network.ApplicationGatewaysClient 38 ifaceClient network.InterfacesClient 39 loadBalancerClient network.LoadBalancersClient 40 localNetConnClient network.LocalNetworkGatewaysClient 41 publicIPClient network.PublicIPAddressesClient 42 secGroupClient network.SecurityGroupsClient 43 secRuleClient network.SecurityRulesClient 44 subnetClient network.SubnetsClient 45 netUsageClient network.UsagesClient 46 vnetGatewayConnectionsClient network.VirtualNetworkGatewayConnectionsClient 47 vnetGatewayClient network.VirtualNetworkGatewaysClient 48 vnetClient network.VirtualNetworksClient 49 vnetPeeringsClient network.VirtualNetworkPeeringsClient 50 routeTablesClient network.RouteTablesClient 51 routesClient network.RoutesClient 52 53 cdnProfilesClient cdn.ProfilesClient 54 cdnEndpointsClient cdn.EndpointsClient 55 56 providers resources.ProvidersClient 57 resourceGroupClient resources.GroupsClient 58 tagsClient resources.TagsClient 59 60 jobsClient scheduler.JobsClient 61 jobsCollectionsClient scheduler.JobCollectionsClient 62 63 storageServiceClient storage.AccountsClient 64 storageUsageClient storage.UsageOperationsClient 65 66 deploymentsClient resources.DeploymentsClient 67 68 trafficManagerProfilesClient trafficmanager.ProfilesClient 69 trafficManagerEndpointsClient trafficmanager.EndpointsClient 70 71 serviceBusNamespacesClient servicebus.NamespacesClient 72 } 73 74 func withRequestLogging() autorest.SendDecorator { 75 return func(s autorest.Sender) autorest.Sender { 76 return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) { 77 // dump request to wire format 78 if dump, err := httputil.DumpRequestOut(r, true); err == nil { 79 log.Printf("[DEBUG] AzureRM Request: \n%s\n", dump) 80 } else { 81 // fallback to basic message 82 log.Printf("[DEBUG] AzureRM Request: %s to %s\n", r.Method, r.URL) 83 } 84 85 resp, err := s.Do(r) 86 if resp != nil { 87 // dump response to wire format 88 if dump, err := httputil.DumpResponse(resp, true); err == nil { 89 log.Printf("[DEBUG] AzureRM Response for %s: \n%s\n", r.URL, dump) 90 } else { 91 // fallback to basic message 92 log.Printf("[DEBUG] AzureRM Response: %s for %s\n", resp.Status, r.URL) 93 } 94 } else { 95 log.Printf("[DEBUG] Request to %s completed with no response", r.URL) 96 } 97 return resp, err 98 }) 99 } 100 } 101 102 func setUserAgent(client *autorest.Client) { 103 version := terraform.VersionString() 104 client.UserAgent = fmt.Sprintf("HashiCorp-Terraform-v%s", version) 105 } 106 107 // getArmClient is a helper method which returns a fully instantiated 108 // *ArmClient based on the Config's current settings. 109 func (c *Config) getArmClient() (*ArmClient, error) { 110 // client declarations: 111 client := ArmClient{} 112 113 rivieraClient, err := riviera.NewClient(&riviera.AzureResourceManagerCredentials{ 114 ClientID: c.ClientID, 115 ClientSecret: c.ClientSecret, 116 TenantID: c.TenantID, 117 SubscriptionID: c.SubscriptionID, 118 }) 119 if err != nil { 120 return nil, fmt.Errorf("Error creating Riviera client: %s", err) 121 } 122 123 // validate that the credentials are correct using Riviera. Note that this must be 124 // done _before_ using the Microsoft SDK, because Riviera handles errors. Using a 125 // namespace registration instead of a simple OAuth token refresh guarantees that 126 // service delegation is correct. This has the effect of registering Microsoft.Compute 127 // which is neccessary anyway. 128 if err := registerProviderWithSubscription("Microsoft.Compute", rivieraClient); err != nil { 129 return nil, err 130 } 131 client.rivieraClient = rivieraClient 132 133 oauthConfig, err := azure.PublicCloud.OAuthConfigForTenant(c.TenantID) 134 if err != nil { 135 return nil, err 136 } 137 138 // OAuthConfigForTenant returns a pointer, which can be nil. 139 if oauthConfig == nil { 140 return nil, fmt.Errorf("Unable to configure OAuthConfig for tenant %s", c.TenantID) 141 } 142 143 spt, err := azure.NewServicePrincipalToken(*oauthConfig, c.ClientID, c.ClientSecret, 144 azure.PublicCloud.ResourceManagerEndpoint) 145 if err != nil { 146 return nil, err 147 } 148 149 // NOTE: these declarations should be left separate for clarity should the 150 // clients be wished to be configured with custom Responders/PollingModess etc... 151 asc := compute.NewAvailabilitySetsClient(c.SubscriptionID) 152 setUserAgent(&asc.Client) 153 asc.Authorizer = spt 154 asc.Sender = autorest.CreateSender(withRequestLogging()) 155 client.availSetClient = asc 156 157 uoc := compute.NewUsageOperationsClient(c.SubscriptionID) 158 setUserAgent(&uoc.Client) 159 uoc.Authorizer = spt 160 uoc.Sender = autorest.CreateSender(withRequestLogging()) 161 client.usageOpsClient = uoc 162 163 vmeic := compute.NewVirtualMachineExtensionImagesClient(c.SubscriptionID) 164 setUserAgent(&vmeic.Client) 165 vmeic.Authorizer = spt 166 vmeic.Sender = autorest.CreateSender(withRequestLogging()) 167 client.vmExtensionImageClient = vmeic 168 169 vmec := compute.NewVirtualMachineExtensionsClient(c.SubscriptionID) 170 setUserAgent(&vmec.Client) 171 vmec.Authorizer = spt 172 vmec.Sender = autorest.CreateSender(withRequestLogging()) 173 client.vmExtensionClient = vmec 174 175 vmic := compute.NewVirtualMachineImagesClient(c.SubscriptionID) 176 setUserAgent(&vmic.Client) 177 vmic.Authorizer = spt 178 vmic.Sender = autorest.CreateSender(withRequestLogging()) 179 client.vmImageClient = vmic 180 181 vmssc := compute.NewVirtualMachineScaleSetsClient(c.SubscriptionID) 182 setUserAgent(&vmssc.Client) 183 vmssc.Authorizer = spt 184 vmssc.Sender = autorest.CreateSender(withRequestLogging()) 185 client.vmScaleSetClient = vmssc 186 187 vmc := compute.NewVirtualMachinesClient(c.SubscriptionID) 188 setUserAgent(&vmc.Client) 189 vmc.Authorizer = spt 190 vmc.Sender = autorest.CreateSender(withRequestLogging()) 191 client.vmClient = vmc 192 193 agc := network.NewApplicationGatewaysClient(c.SubscriptionID) 194 setUserAgent(&agc.Client) 195 agc.Authorizer = spt 196 agc.Sender = autorest.CreateSender(withRequestLogging()) 197 client.appGatewayClient = agc 198 199 ifc := network.NewInterfacesClient(c.SubscriptionID) 200 setUserAgent(&ifc.Client) 201 ifc.Authorizer = spt 202 ifc.Sender = autorest.CreateSender(withRequestLogging()) 203 client.ifaceClient = ifc 204 205 lbc := network.NewLoadBalancersClient(c.SubscriptionID) 206 setUserAgent(&lbc.Client) 207 lbc.Authorizer = spt 208 lbc.Sender = autorest.CreateSender(withRequestLogging()) 209 client.loadBalancerClient = lbc 210 211 lgc := network.NewLocalNetworkGatewaysClient(c.SubscriptionID) 212 setUserAgent(&lgc.Client) 213 lgc.Authorizer = spt 214 lgc.Sender = autorest.CreateSender(withRequestLogging()) 215 client.localNetConnClient = lgc 216 217 pipc := network.NewPublicIPAddressesClient(c.SubscriptionID) 218 setUserAgent(&pipc.Client) 219 pipc.Authorizer = spt 220 pipc.Sender = autorest.CreateSender(withRequestLogging()) 221 client.publicIPClient = pipc 222 223 sgc := network.NewSecurityGroupsClient(c.SubscriptionID) 224 setUserAgent(&sgc.Client) 225 sgc.Authorizer = spt 226 sgc.Sender = autorest.CreateSender(withRequestLogging()) 227 client.secGroupClient = sgc 228 229 src := network.NewSecurityRulesClient(c.SubscriptionID) 230 setUserAgent(&src.Client) 231 src.Authorizer = spt 232 src.Sender = autorest.CreateSender(withRequestLogging()) 233 client.secRuleClient = src 234 235 snc := network.NewSubnetsClient(c.SubscriptionID) 236 setUserAgent(&snc.Client) 237 snc.Authorizer = spt 238 snc.Sender = autorest.CreateSender(withRequestLogging()) 239 client.subnetClient = snc 240 241 vgcc := network.NewVirtualNetworkGatewayConnectionsClient(c.SubscriptionID) 242 setUserAgent(&vgcc.Client) 243 vgcc.Authorizer = spt 244 vgcc.Sender = autorest.CreateSender(withRequestLogging()) 245 client.vnetGatewayConnectionsClient = vgcc 246 247 vgc := network.NewVirtualNetworkGatewaysClient(c.SubscriptionID) 248 setUserAgent(&vgc.Client) 249 vgc.Authorizer = spt 250 vgc.Sender = autorest.CreateSender(withRequestLogging()) 251 client.vnetGatewayClient = vgc 252 253 vnc := network.NewVirtualNetworksClient(c.SubscriptionID) 254 setUserAgent(&vnc.Client) 255 vnc.Authorizer = spt 256 vnc.Sender = autorest.CreateSender(withRequestLogging()) 257 client.vnetClient = vnc 258 259 vnpc := network.NewVirtualNetworkPeeringsClient(c.SubscriptionID) 260 setUserAgent(&vnpc.Client) 261 vnpc.Authorizer = spt 262 vnpc.Sender = autorest.CreateSender(withRequestLogging()) 263 client.vnetPeeringsClient = vnpc 264 265 rtc := network.NewRouteTablesClient(c.SubscriptionID) 266 setUserAgent(&rtc.Client) 267 rtc.Authorizer = spt 268 rtc.Sender = autorest.CreateSender(withRequestLogging()) 269 client.routeTablesClient = rtc 270 271 rc := network.NewRoutesClient(c.SubscriptionID) 272 setUserAgent(&rc.Client) 273 rc.Authorizer = spt 274 rc.Sender = autorest.CreateSender(withRequestLogging()) 275 client.routesClient = rc 276 277 rgc := resources.NewGroupsClient(c.SubscriptionID) 278 setUserAgent(&rgc.Client) 279 rgc.Authorizer = spt 280 rgc.Sender = autorest.CreateSender(withRequestLogging()) 281 client.resourceGroupClient = rgc 282 283 pc := resources.NewProvidersClient(c.SubscriptionID) 284 setUserAgent(&pc.Client) 285 pc.Authorizer = spt 286 pc.Sender = autorest.CreateSender(withRequestLogging()) 287 client.providers = pc 288 289 tc := resources.NewTagsClient(c.SubscriptionID) 290 setUserAgent(&tc.Client) 291 tc.Authorizer = spt 292 tc.Sender = autorest.CreateSender(withRequestLogging()) 293 client.tagsClient = tc 294 295 jc := scheduler.NewJobsClient(c.SubscriptionID) 296 setUserAgent(&jc.Client) 297 jc.Authorizer = spt 298 jc.Sender = autorest.CreateSender(withRequestLogging()) 299 client.jobsClient = jc 300 301 jcc := scheduler.NewJobCollectionsClient(c.SubscriptionID) 302 setUserAgent(&jcc.Client) 303 jcc.Authorizer = spt 304 jcc.Sender = autorest.CreateSender(withRequestLogging()) 305 client.jobsCollectionsClient = jcc 306 307 ssc := storage.NewAccountsClient(c.SubscriptionID) 308 setUserAgent(&ssc.Client) 309 ssc.Authorizer = spt 310 ssc.Sender = autorest.CreateSender(withRequestLogging()) 311 client.storageServiceClient = ssc 312 313 suc := storage.NewUsageOperationsClient(c.SubscriptionID) 314 setUserAgent(&suc.Client) 315 suc.Authorizer = spt 316 suc.Sender = autorest.CreateSender(withRequestLogging()) 317 client.storageUsageClient = suc 318 319 cpc := cdn.NewProfilesClient(c.SubscriptionID) 320 setUserAgent(&cpc.Client) 321 cpc.Authorizer = spt 322 cpc.Sender = autorest.CreateSender(withRequestLogging()) 323 client.cdnProfilesClient = cpc 324 325 cec := cdn.NewEndpointsClient(c.SubscriptionID) 326 setUserAgent(&cec.Client) 327 cec.Authorizer = spt 328 cec.Sender = autorest.CreateSender(withRequestLogging()) 329 client.cdnEndpointsClient = cec 330 331 dc := resources.NewDeploymentsClient(c.SubscriptionID) 332 setUserAgent(&dc.Client) 333 dc.Authorizer = spt 334 dc.Sender = autorest.CreateSender(withRequestLogging()) 335 client.deploymentsClient = dc 336 337 tmpc := trafficmanager.NewProfilesClient(c.SubscriptionID) 338 setUserAgent(&tmpc.Client) 339 tmpc.Authorizer = spt 340 tmpc.Sender = autorest.CreateSender(withRequestLogging()) 341 client.trafficManagerProfilesClient = tmpc 342 343 tmec := trafficmanager.NewEndpointsClient(c.SubscriptionID) 344 setUserAgent(&tmec.Client) 345 tmec.Authorizer = spt 346 tmec.Sender = autorest.CreateSender(withRequestLogging()) 347 client.trafficManagerEndpointsClient = tmec 348 349 sbnc := servicebus.NewNamespacesClient(c.SubscriptionID) 350 setUserAgent(&sbnc.Client) 351 sbnc.Authorizer = spt 352 sbnc.Sender = autorest.CreateSender(withRequestLogging()) 353 client.serviceBusNamespacesClient = sbnc 354 355 return &client, nil 356 } 357 358 func (armClient *ArmClient) getKeyForStorageAccount(resourceGroupName, storageAccountName string) (string, bool, error) { 359 accountKeys, err := armClient.storageServiceClient.ListKeys(resourceGroupName, storageAccountName) 360 if accountKeys.StatusCode == http.StatusNotFound { 361 return "", false, nil 362 } 363 if err != nil { 364 // We assume this is a transient error rather than a 404 (which is caught above), so assume the 365 // account still exists. 366 return "", true, fmt.Errorf("Error retrieving keys for storage account %q: %s", storageAccountName, err) 367 } 368 369 if accountKeys.Keys == nil { 370 return "", false, fmt.Errorf("Nil key returned for storage account %q", storageAccountName) 371 } 372 373 keys := *accountKeys.Keys 374 return *keys[0].Value, true, nil 375 } 376 377 func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.BlobStorageClient, bool, error) { 378 key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName) 379 if err != nil { 380 return nil, accountExists, err 381 } 382 if accountExists == false { 383 return nil, false, nil 384 } 385 386 storageClient, err := mainStorage.NewBasicClient(storageAccountName, key) 387 if err != nil { 388 return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err) 389 } 390 391 blobClient := storageClient.GetBlobService() 392 return &blobClient, true, nil 393 } 394 func (armClient *ArmClient) getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.TableServiceClient, bool, error) { 395 key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName) 396 if err != nil { 397 return nil, accountExists, err 398 } 399 if accountExists == false { 400 return nil, false, nil 401 } 402 403 storageClient, err := mainStorage.NewBasicClient(storageAccountName, key) 404 if err != nil { 405 return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err) 406 } 407 408 tableClient := storageClient.GetTableService() 409 return &tableClient, true, nil 410 } 411 412 func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, bool, error) { 413 key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName) 414 if err != nil { 415 return nil, accountExists, err 416 } 417 if accountExists == false { 418 return nil, false, nil 419 } 420 421 storageClient, err := mainStorage.NewBasicClient(storageAccountName, key) 422 if err != nil { 423 return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err) 424 } 425 426 queueClient := storageClient.GetQueueService() 427 return &queueClient, true, nil 428 }