github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/config.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"net/http/httputil"
     9  
    10  	"github.com/Azure/azure-sdk-for-go/arm/cdn"
    11  	"github.com/Azure/azure-sdk-for-go/arm/compute"
    12  	"github.com/Azure/azure-sdk-for-go/arm/containerregistry"
    13  	"github.com/Azure/azure-sdk-for-go/arm/containerservice"
    14  	"github.com/Azure/azure-sdk-for-go/arm/disk"
    15  	"github.com/Azure/azure-sdk-for-go/arm/eventhub"
    16  	"github.com/Azure/azure-sdk-for-go/arm/keyvault"
    17  	"github.com/Azure/azure-sdk-for-go/arm/network"
    18  	"github.com/Azure/azure-sdk-for-go/arm/redis"
    19  	"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
    20  	"github.com/Azure/azure-sdk-for-go/arm/scheduler"
    21  	"github.com/Azure/azure-sdk-for-go/arm/servicebus"
    22  	"github.com/Azure/azure-sdk-for-go/arm/storage"
    23  	"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
    24  	mainStorage "github.com/Azure/azure-sdk-for-go/storage"
    25  	"github.com/Azure/go-autorest/autorest"
    26  	"github.com/Azure/go-autorest/autorest/azure"
    27  	"github.com/hashicorp/terraform/terraform"
    28  	riviera "github.com/jen20/riviera/azure"
    29  )
    30  
    31  // ArmClient contains the handles to all the specific Azure Resource Manager
    32  // resource classes' respective clients.
    33  type ArmClient struct {
    34  	clientId       string
    35  	tenantId       string
    36  	subscriptionId string
    37  	environment    azure.Environment
    38  
    39  	StopContext context.Context
    40  
    41  	rivieraClient *riviera.Client
    42  
    43  	availSetClient         compute.AvailabilitySetsClient
    44  	usageOpsClient         compute.UsageClient
    45  	vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
    46  	vmExtensionClient      compute.VirtualMachineExtensionsClient
    47  	vmScaleSetClient       compute.VirtualMachineScaleSetsClient
    48  	vmImageClient          compute.VirtualMachineImagesClient
    49  	vmClient               compute.VirtualMachinesClient
    50  
    51  	diskClient disk.DisksClient
    52  
    53  	appGatewayClient             network.ApplicationGatewaysClient
    54  	ifaceClient                  network.InterfacesClient
    55  	loadBalancerClient           network.LoadBalancersClient
    56  	localNetConnClient           network.LocalNetworkGatewaysClient
    57  	publicIPClient               network.PublicIPAddressesClient
    58  	secGroupClient               network.SecurityGroupsClient
    59  	secRuleClient                network.SecurityRulesClient
    60  	subnetClient                 network.SubnetsClient
    61  	netUsageClient               network.UsagesClient
    62  	vnetGatewayConnectionsClient network.VirtualNetworkGatewayConnectionsClient
    63  	vnetGatewayClient            network.VirtualNetworkGatewaysClient
    64  	vnetClient                   network.VirtualNetworksClient
    65  	vnetPeeringsClient           network.VirtualNetworkPeeringsClient
    66  	routeTablesClient            network.RouteTablesClient
    67  	routesClient                 network.RoutesClient
    68  
    69  	cdnProfilesClient  cdn.ProfilesClient
    70  	cdnEndpointsClient cdn.EndpointsClient
    71  
    72  	containerRegistryClient containerregistry.RegistriesClient
    73  	containerServicesClient containerservice.ContainerServicesClient
    74  
    75  	eventHubClient              eventhub.EventHubsClient
    76  	eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
    77  	eventHubNamespacesClient    eventhub.NamespacesClient
    78  
    79  	providers           resources.ProvidersClient
    80  	resourceGroupClient resources.GroupsClient
    81  	tagsClient          resources.TagsClient
    82  	resourceFindClient  resources.GroupClient
    83  
    84  	jobsClient            scheduler.JobsClient
    85  	jobsCollectionsClient scheduler.JobCollectionsClient
    86  
    87  	storageServiceClient storage.AccountsClient
    88  	storageUsageClient   storage.UsageOperationsClient
    89  
    90  	deploymentsClient resources.DeploymentsClient
    91  
    92  	redisClient redis.GroupClient
    93  
    94  	trafficManagerProfilesClient  trafficmanager.ProfilesClient
    95  	trafficManagerEndpointsClient trafficmanager.EndpointsClient
    96  
    97  	serviceBusNamespacesClient    servicebus.NamespacesClient
    98  	serviceBusTopicsClient        servicebus.TopicsClient
    99  	serviceBusSubscriptionsClient servicebus.SubscriptionsClient
   100  
   101  	keyVaultClient keyvault.VaultsClient
   102  }
   103  
   104  func withRequestLogging() autorest.SendDecorator {
   105  	return func(s autorest.Sender) autorest.Sender {
   106  		return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
   107  			// dump request to wire format
   108  			if dump, err := httputil.DumpRequestOut(r, true); err == nil {
   109  				log.Printf("[DEBUG] AzureRM Request: \n%s\n", dump)
   110  			} else {
   111  				// fallback to basic message
   112  				log.Printf("[DEBUG] AzureRM Request: %s to %s\n", r.Method, r.URL)
   113  			}
   114  
   115  			resp, err := s.Do(r)
   116  			if resp != nil {
   117  				// dump response to wire format
   118  				if dump, err := httputil.DumpResponse(resp, true); err == nil {
   119  					log.Printf("[DEBUG] AzureRM Response for %s: \n%s\n", r.URL, dump)
   120  				} else {
   121  					// fallback to basic message
   122  					log.Printf("[DEBUG] AzureRM Response: %s for %s\n", resp.Status, r.URL)
   123  				}
   124  			} else {
   125  				log.Printf("[DEBUG] Request to %s completed with no response", r.URL)
   126  			}
   127  			return resp, err
   128  		})
   129  	}
   130  }
   131  
   132  func setUserAgent(client *autorest.Client) {
   133  	version := terraform.VersionString()
   134  	client.UserAgent = fmt.Sprintf("HashiCorp-Terraform-v%s", version)
   135  }
   136  
   137  // getArmClient is a helper method which returns a fully instantiated
   138  // *ArmClient based on the Config's current settings.
   139  func (c *Config) getArmClient() (*ArmClient, error) {
   140  	// detect cloud from environment
   141  	env, envErr := azure.EnvironmentFromName(c.Environment)
   142  	if envErr != nil {
   143  		// try again with wrapped value to support readable values like german instead of AZUREGERMANCLOUD
   144  		wrapped := fmt.Sprintf("AZURE%sCLOUD", c.Environment)
   145  		var innerErr error
   146  		if env, innerErr = azure.EnvironmentFromName(wrapped); innerErr != nil {
   147  			return nil, envErr
   148  		}
   149  	}
   150  
   151  	// client declarations:
   152  	client := ArmClient{
   153  		clientId:       c.ClientID,
   154  		tenantId:       c.TenantID,
   155  		subscriptionId: c.SubscriptionID,
   156  		environment:    env,
   157  	}
   158  
   159  	rivieraClient, err := riviera.NewClient(&riviera.AzureResourceManagerCredentials{
   160  		ClientID:                c.ClientID,
   161  		ClientSecret:            c.ClientSecret,
   162  		TenantID:                c.TenantID,
   163  		SubscriptionID:          c.SubscriptionID,
   164  		ResourceManagerEndpoint: env.ResourceManagerEndpoint,
   165  		ActiveDirectoryEndpoint: env.ActiveDirectoryEndpoint,
   166  	})
   167  	if err != nil {
   168  		return nil, fmt.Errorf("Error creating Riviera client: %s", err)
   169  	}
   170  	client.rivieraClient = rivieraClient
   171  
   172  	oauthConfig, err := env.OAuthConfigForTenant(c.TenantID)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	// OAuthConfigForTenant returns a pointer, which can be nil.
   178  	if oauthConfig == nil {
   179  		return nil, fmt.Errorf("Unable to configure OAuthConfig for tenant %s", c.TenantID)
   180  	}
   181  
   182  	spt, err := azure.NewServicePrincipalToken(*oauthConfig, c.ClientID, c.ClientSecret, env.ResourceManagerEndpoint)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	endpoint := env.ResourceManagerEndpoint
   188  
   189  	// NOTE: these declarations should be left separate for clarity should the
   190  	// clients be wished to be configured with custom Responders/PollingModess etc...
   191  	asc := compute.NewAvailabilitySetsClientWithBaseURI(endpoint, c.SubscriptionID)
   192  	setUserAgent(&asc.Client)
   193  	asc.Authorizer = spt
   194  	asc.Sender = autorest.CreateSender(withRequestLogging())
   195  	client.availSetClient = asc
   196  
   197  	uoc := compute.NewUsageClientWithBaseURI(endpoint, c.SubscriptionID)
   198  	setUserAgent(&uoc.Client)
   199  	uoc.Authorizer = spt
   200  	uoc.Sender = autorest.CreateSender(withRequestLogging())
   201  	client.usageOpsClient = uoc
   202  
   203  	vmeic := compute.NewVirtualMachineExtensionImagesClientWithBaseURI(endpoint, c.SubscriptionID)
   204  	setUserAgent(&vmeic.Client)
   205  	vmeic.Authorizer = spt
   206  	vmeic.Sender = autorest.CreateSender(withRequestLogging())
   207  	client.vmExtensionImageClient = vmeic
   208  
   209  	vmec := compute.NewVirtualMachineExtensionsClientWithBaseURI(endpoint, c.SubscriptionID)
   210  	setUserAgent(&vmec.Client)
   211  	vmec.Authorizer = spt
   212  	vmec.Sender = autorest.CreateSender(withRequestLogging())
   213  	client.vmExtensionClient = vmec
   214  
   215  	vmic := compute.NewVirtualMachineImagesClientWithBaseURI(endpoint, c.SubscriptionID)
   216  	setUserAgent(&vmic.Client)
   217  	vmic.Authorizer = spt
   218  	vmic.Sender = autorest.CreateSender(withRequestLogging())
   219  	client.vmImageClient = vmic
   220  
   221  	vmssc := compute.NewVirtualMachineScaleSetsClientWithBaseURI(endpoint, c.SubscriptionID)
   222  	setUserAgent(&vmssc.Client)
   223  	vmssc.Authorizer = spt
   224  	vmssc.Sender = autorest.CreateSender(withRequestLogging())
   225  	client.vmScaleSetClient = vmssc
   226  
   227  	vmc := compute.NewVirtualMachinesClientWithBaseURI(endpoint, c.SubscriptionID)
   228  	setUserAgent(&vmc.Client)
   229  	vmc.Authorizer = spt
   230  	vmc.Sender = autorest.CreateSender(withRequestLogging())
   231  	client.vmClient = vmc
   232  
   233  	agc := network.NewApplicationGatewaysClientWithBaseURI(endpoint, c.SubscriptionID)
   234  	setUserAgent(&agc.Client)
   235  	agc.Authorizer = spt
   236  	agc.Sender = autorest.CreateSender(withRequestLogging())
   237  	client.appGatewayClient = agc
   238  
   239  	crc := containerregistry.NewRegistriesClientWithBaseURI(endpoint, c.SubscriptionID)
   240  	setUserAgent(&crc.Client)
   241  	crc.Authorizer = spt
   242  	crc.Sender = autorest.CreateSender(withRequestLogging())
   243  	client.containerRegistryClient = crc
   244  
   245  	csc := containerservice.NewContainerServicesClientWithBaseURI(endpoint, c.SubscriptionID)
   246  	setUserAgent(&csc.Client)
   247  	csc.Authorizer = spt
   248  	csc.Sender = autorest.CreateSender(withRequestLogging())
   249  	client.containerServicesClient = csc
   250  
   251  	dkc := disk.NewDisksClientWithBaseURI(endpoint, c.SubscriptionID)
   252  	setUserAgent(&dkc.Client)
   253  	dkc.Authorizer = spt
   254  	dkc.Sender = autorest.CreateSender(withRequestLogging())
   255  	client.diskClient = dkc
   256  
   257  	ehc := eventhub.NewEventHubsClientWithBaseURI(endpoint, c.SubscriptionID)
   258  	setUserAgent(&ehc.Client)
   259  	ehc.Authorizer = spt
   260  	ehc.Sender = autorest.CreateSender(withRequestLogging())
   261  	client.eventHubClient = ehc
   262  
   263  	chcgc := eventhub.NewConsumerGroupsClientWithBaseURI(endpoint, c.SubscriptionID)
   264  	setUserAgent(&chcgc.Client)
   265  	chcgc.Authorizer = spt
   266  	chcgc.Sender = autorest.CreateSender(withRequestLogging())
   267  	client.eventHubConsumerGroupClient = chcgc
   268  
   269  	ehnc := eventhub.NewNamespacesClientWithBaseURI(endpoint, c.SubscriptionID)
   270  	setUserAgent(&ehnc.Client)
   271  	ehnc.Authorizer = spt
   272  	ehnc.Sender = autorest.CreateSender(withRequestLogging())
   273  	client.eventHubNamespacesClient = ehnc
   274  
   275  	ifc := network.NewInterfacesClientWithBaseURI(endpoint, c.SubscriptionID)
   276  	setUserAgent(&ifc.Client)
   277  	ifc.Authorizer = spt
   278  	ifc.Sender = autorest.CreateSender(withRequestLogging())
   279  	client.ifaceClient = ifc
   280  
   281  	lbc := network.NewLoadBalancersClientWithBaseURI(endpoint, c.SubscriptionID)
   282  	setUserAgent(&lbc.Client)
   283  	lbc.Authorizer = spt
   284  	lbc.Sender = autorest.CreateSender(withRequestLogging())
   285  	client.loadBalancerClient = lbc
   286  
   287  	lgc := network.NewLocalNetworkGatewaysClientWithBaseURI(endpoint, c.SubscriptionID)
   288  	setUserAgent(&lgc.Client)
   289  	lgc.Authorizer = spt
   290  	lgc.Sender = autorest.CreateSender(withRequestLogging())
   291  	client.localNetConnClient = lgc
   292  
   293  	pipc := network.NewPublicIPAddressesClientWithBaseURI(endpoint, c.SubscriptionID)
   294  	setUserAgent(&pipc.Client)
   295  	pipc.Authorizer = spt
   296  	pipc.Sender = autorest.CreateSender(withRequestLogging())
   297  	client.publicIPClient = pipc
   298  
   299  	sgc := network.NewSecurityGroupsClientWithBaseURI(endpoint, c.SubscriptionID)
   300  	setUserAgent(&sgc.Client)
   301  	sgc.Authorizer = spt
   302  	sgc.Sender = autorest.CreateSender(withRequestLogging())
   303  	client.secGroupClient = sgc
   304  
   305  	src := network.NewSecurityRulesClientWithBaseURI(endpoint, c.SubscriptionID)
   306  	setUserAgent(&src.Client)
   307  	src.Authorizer = spt
   308  	src.Sender = autorest.CreateSender(withRequestLogging())
   309  	client.secRuleClient = src
   310  
   311  	snc := network.NewSubnetsClientWithBaseURI(endpoint, c.SubscriptionID)
   312  	setUserAgent(&snc.Client)
   313  	snc.Authorizer = spt
   314  	snc.Sender = autorest.CreateSender(withRequestLogging())
   315  	client.subnetClient = snc
   316  
   317  	vgcc := network.NewVirtualNetworkGatewayConnectionsClientWithBaseURI(endpoint, c.SubscriptionID)
   318  	setUserAgent(&vgcc.Client)
   319  	vgcc.Authorizer = spt
   320  	vgcc.Sender = autorest.CreateSender(withRequestLogging())
   321  	client.vnetGatewayConnectionsClient = vgcc
   322  
   323  	vgc := network.NewVirtualNetworkGatewaysClientWithBaseURI(endpoint, c.SubscriptionID)
   324  	setUserAgent(&vgc.Client)
   325  	vgc.Authorizer = spt
   326  	vgc.Sender = autorest.CreateSender(withRequestLogging())
   327  	client.vnetGatewayClient = vgc
   328  
   329  	vnc := network.NewVirtualNetworksClientWithBaseURI(endpoint, c.SubscriptionID)
   330  	setUserAgent(&vnc.Client)
   331  	vnc.Authorizer = spt
   332  	vnc.Sender = autorest.CreateSender(withRequestLogging())
   333  	client.vnetClient = vnc
   334  
   335  	vnpc := network.NewVirtualNetworkPeeringsClientWithBaseURI(endpoint, c.SubscriptionID)
   336  	setUserAgent(&vnpc.Client)
   337  	vnpc.Authorizer = spt
   338  	vnpc.Sender = autorest.CreateSender(withRequestLogging())
   339  	client.vnetPeeringsClient = vnpc
   340  
   341  	rtc := network.NewRouteTablesClientWithBaseURI(endpoint, c.SubscriptionID)
   342  	setUserAgent(&rtc.Client)
   343  	rtc.Authorizer = spt
   344  	rtc.Sender = autorest.CreateSender(withRequestLogging())
   345  	client.routeTablesClient = rtc
   346  
   347  	rc := network.NewRoutesClientWithBaseURI(endpoint, c.SubscriptionID)
   348  	setUserAgent(&rc.Client)
   349  	rc.Authorizer = spt
   350  	rc.Sender = autorest.CreateSender(withRequestLogging())
   351  	client.routesClient = rc
   352  
   353  	rgc := resources.NewGroupsClientWithBaseURI(endpoint, c.SubscriptionID)
   354  	setUserAgent(&rgc.Client)
   355  	rgc.Authorizer = spt
   356  	rgc.Sender = autorest.CreateSender(withRequestLogging())
   357  	client.resourceGroupClient = rgc
   358  
   359  	pc := resources.NewProvidersClientWithBaseURI(endpoint, c.SubscriptionID)
   360  	setUserAgent(&pc.Client)
   361  	pc.Authorizer = spt
   362  	pc.Sender = autorest.CreateSender(withRequestLogging())
   363  	client.providers = pc
   364  
   365  	tc := resources.NewTagsClientWithBaseURI(endpoint, c.SubscriptionID)
   366  	setUserAgent(&tc.Client)
   367  	tc.Authorizer = spt
   368  	tc.Sender = autorest.CreateSender(withRequestLogging())
   369  	client.tagsClient = tc
   370  
   371  	rf := resources.NewGroupClientWithBaseURI(endpoint, c.SubscriptionID)
   372  	setUserAgent(&rf.Client)
   373  	rf.Authorizer = spt
   374  	rf.Sender = autorest.CreateSender(withRequestLogging())
   375  	client.resourceFindClient = rf
   376  
   377  	jc := scheduler.NewJobsClientWithBaseURI(endpoint, c.SubscriptionID)
   378  	setUserAgent(&jc.Client)
   379  	jc.Authorizer = spt
   380  	jc.Sender = autorest.CreateSender(withRequestLogging())
   381  	client.jobsClient = jc
   382  
   383  	jcc := scheduler.NewJobCollectionsClientWithBaseURI(endpoint, c.SubscriptionID)
   384  	setUserAgent(&jcc.Client)
   385  	jcc.Authorizer = spt
   386  	jcc.Sender = autorest.CreateSender(withRequestLogging())
   387  	client.jobsCollectionsClient = jcc
   388  
   389  	ssc := storage.NewAccountsClientWithBaseURI(endpoint, c.SubscriptionID)
   390  	setUserAgent(&ssc.Client)
   391  	ssc.Authorizer = spt
   392  	ssc.Sender = autorest.CreateSender(withRequestLogging())
   393  	client.storageServiceClient = ssc
   394  
   395  	suc := storage.NewUsageOperationsClientWithBaseURI(endpoint, c.SubscriptionID)
   396  	setUserAgent(&suc.Client)
   397  	suc.Authorizer = spt
   398  	suc.Sender = autorest.CreateSender(withRequestLogging())
   399  	client.storageUsageClient = suc
   400  
   401  	cpc := cdn.NewProfilesClientWithBaseURI(endpoint, c.SubscriptionID)
   402  	setUserAgent(&cpc.Client)
   403  	cpc.Authorizer = spt
   404  	cpc.Sender = autorest.CreateSender(withRequestLogging())
   405  	client.cdnProfilesClient = cpc
   406  
   407  	cec := cdn.NewEndpointsClientWithBaseURI(endpoint, c.SubscriptionID)
   408  	setUserAgent(&cec.Client)
   409  	cec.Authorizer = spt
   410  	cec.Sender = autorest.CreateSender(withRequestLogging())
   411  	client.cdnEndpointsClient = cec
   412  
   413  	dc := resources.NewDeploymentsClientWithBaseURI(endpoint, c.SubscriptionID)
   414  	setUserAgent(&dc.Client)
   415  	dc.Authorizer = spt
   416  	dc.Sender = autorest.CreateSender(withRequestLogging())
   417  	client.deploymentsClient = dc
   418  
   419  	tmpc := trafficmanager.NewProfilesClientWithBaseURI(endpoint, c.SubscriptionID)
   420  	setUserAgent(&tmpc.Client)
   421  	tmpc.Authorizer = spt
   422  	tmpc.Sender = autorest.CreateSender(withRequestLogging())
   423  	client.trafficManagerProfilesClient = tmpc
   424  
   425  	tmec := trafficmanager.NewEndpointsClientWithBaseURI(endpoint, c.SubscriptionID)
   426  	setUserAgent(&tmec.Client)
   427  	tmec.Authorizer = spt
   428  	tmec.Sender = autorest.CreateSender(withRequestLogging())
   429  	client.trafficManagerEndpointsClient = tmec
   430  
   431  	rdc := redis.NewGroupClientWithBaseURI(endpoint, c.SubscriptionID)
   432  	setUserAgent(&rdc.Client)
   433  	rdc.Authorizer = spt
   434  	rdc.Sender = autorest.CreateSender(withRequestLogging())
   435  	client.redisClient = rdc
   436  
   437  	sbnc := servicebus.NewNamespacesClientWithBaseURI(endpoint, c.SubscriptionID)
   438  	setUserAgent(&sbnc.Client)
   439  	sbnc.Authorizer = spt
   440  	sbnc.Sender = autorest.CreateSender(withRequestLogging())
   441  	client.serviceBusNamespacesClient = sbnc
   442  
   443  	sbtc := servicebus.NewTopicsClientWithBaseURI(endpoint, c.SubscriptionID)
   444  	setUserAgent(&sbtc.Client)
   445  	sbtc.Authorizer = spt
   446  	sbtc.Sender = autorest.CreateSender(withRequestLogging())
   447  	client.serviceBusTopicsClient = sbtc
   448  
   449  	sbsc := servicebus.NewSubscriptionsClientWithBaseURI(endpoint, c.SubscriptionID)
   450  	setUserAgent(&sbsc.Client)
   451  	sbsc.Authorizer = spt
   452  	sbsc.Sender = autorest.CreateSender(withRequestLogging())
   453  	client.serviceBusSubscriptionsClient = sbsc
   454  
   455  	kvc := keyvault.NewVaultsClientWithBaseURI(endpoint, c.SubscriptionID)
   456  	setUserAgent(&kvc.Client)
   457  	kvc.Authorizer = spt
   458  	kvc.Sender = autorest.CreateSender(withRequestLogging())
   459  	client.keyVaultClient = kvc
   460  
   461  	return &client, nil
   462  }
   463  
   464  func (armClient *ArmClient) getKeyForStorageAccount(resourceGroupName, storageAccountName string) (string, bool, error) {
   465  	accountKeys, err := armClient.storageServiceClient.ListKeys(resourceGroupName, storageAccountName)
   466  	if accountKeys.StatusCode == http.StatusNotFound {
   467  		return "", false, nil
   468  	}
   469  	if err != nil {
   470  		// We assume this is a transient error rather than a 404 (which is caught above),  so assume the
   471  		// account still exists.
   472  		return "", true, fmt.Errorf("Error retrieving keys for storage account %q: %s", storageAccountName, err)
   473  	}
   474  
   475  	if accountKeys.Keys == nil {
   476  		return "", false, fmt.Errorf("Nil key returned for storage account %q", storageAccountName)
   477  	}
   478  
   479  	keys := *accountKeys.Keys
   480  	return *keys[0].Value, true, nil
   481  }
   482  
   483  func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.BlobStorageClient, bool, error) {
   484  	key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
   485  	if err != nil {
   486  		return nil, accountExists, err
   487  	}
   488  	if accountExists == false {
   489  		return nil, false, nil
   490  	}
   491  
   492  	storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
   493  		mainStorage.DefaultAPIVersion, true)
   494  	if err != nil {
   495  		return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
   496  	}
   497  
   498  	blobClient := storageClient.GetBlobService()
   499  	return &blobClient, true, nil
   500  }
   501  
   502  func (armClient *ArmClient) getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.FileServiceClient, bool, error) {
   503  	key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
   504  	if err != nil {
   505  		return nil, accountExists, err
   506  	}
   507  	if accountExists == false {
   508  		return nil, false, nil
   509  	}
   510  
   511  	storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
   512  		mainStorage.DefaultAPIVersion, true)
   513  	if err != nil {
   514  		return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
   515  	}
   516  
   517  	fileClient := storageClient.GetFileService()
   518  	return &fileClient, true, nil
   519  }
   520  
   521  func (armClient *ArmClient) getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.TableServiceClient, bool, error) {
   522  	key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
   523  	if err != nil {
   524  		return nil, accountExists, err
   525  	}
   526  	if accountExists == false {
   527  		return nil, false, nil
   528  	}
   529  
   530  	storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
   531  		mainStorage.DefaultAPIVersion, true)
   532  	if err != nil {
   533  		return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
   534  	}
   535  
   536  	tableClient := storageClient.GetTableService()
   537  	return &tableClient, true, nil
   538  }
   539  
   540  func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, bool, error) {
   541  	key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
   542  	if err != nil {
   543  		return nil, accountExists, err
   544  	}
   545  	if accountExists == false {
   546  		return nil, false, nil
   547  	}
   548  
   549  	storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
   550  		mainStorage.DefaultAPIVersion, true)
   551  	if err != nil {
   552  		return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
   553  	}
   554  
   555  	queueClient := storageClient.GetQueueService()
   556  	return &queueClient, true, nil
   557  }