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