2015-12-08 23:30:12 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
2016-01-06 23:07:15 +01:00
|
|
|
"fmt"
|
2015-12-09 02:25:05 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-01-06 23:07:15 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest"
|
2015-12-08 23:30:12 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/azure"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
2016-01-08 21:06:09 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
|
2015-12-08 23:30:12 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/storage"
|
2016-01-06 23:07:15 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-12-08 23:30:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ArmClient contains the handles to all the specific Azure Resource Manager
|
|
|
|
// resource classes' respective clients.
|
|
|
|
type ArmClient struct {
|
|
|
|
availSetClient compute.AvailabilitySetsClient
|
|
|
|
usageOpsClient compute.UsageOperationsClient
|
|
|
|
vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
|
|
|
|
vmExtensionClient compute.VirtualMachineExtensionsClient
|
|
|
|
vmImageClient compute.VirtualMachineImagesClient
|
|
|
|
vmClient compute.VirtualMachinesClient
|
|
|
|
|
|
|
|
appGatewayClient network.ApplicationGatewaysClient
|
|
|
|
ifaceClient network.InterfacesClient
|
|
|
|
loadBalancerClient network.LoadBalancersClient
|
|
|
|
localNetConnClient network.LocalNetworkGatewaysClient
|
|
|
|
publicIPClient network.PublicIPAddressesClient
|
|
|
|
secGroupClient network.SecurityGroupsClient
|
|
|
|
secRuleClient network.SecurityRulesClient
|
|
|
|
subnetClient network.SubnetsClient
|
|
|
|
netUsageClient network.UsagesClient
|
|
|
|
vnetGatewayConnectionsClient network.VirtualNetworkGatewayConnectionsClient
|
|
|
|
vnetGatewayClient network.VirtualNetworkGatewaysClient
|
|
|
|
vnetClient network.VirtualNetworksClient
|
|
|
|
|
2016-01-05 22:43:52 +01:00
|
|
|
providers resources.ProvidersClient
|
2015-12-08 23:30:12 +01:00
|
|
|
resourceGroupClient resources.GroupsClient
|
|
|
|
tagsClient resources.TagsClient
|
|
|
|
|
|
|
|
jobsClient scheduler.JobsClient
|
|
|
|
jobsCollectionsClient scheduler.JobCollectionsClient
|
|
|
|
|
|
|
|
storageServiceClient storage.AccountsClient
|
|
|
|
storageUsageClient storage.UsageOperationsClient
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
func withRequestLogging() autorest.SendDecorator {
|
|
|
|
return func(s autorest.Sender) autorest.Sender {
|
|
|
|
return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
|
|
|
|
log.Printf("[DEBUG] Sending Azure RM Request %s to %s\n", r.Method, r.URL)
|
|
|
|
resp, err := s.Do(r)
|
|
|
|
log.Printf("[DEBUG] Received Azure RM Request status code %s for %s\n", resp.Status, r.URL)
|
|
|
|
return resp, err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:07:15 +01:00
|
|
|
func setUserAgent(client *autorest.Client) {
|
|
|
|
var version string
|
|
|
|
if terraform.VersionPrerelease != "" {
|
|
|
|
version = fmt.Sprintf("%s-%s", terraform.Version, terraform.VersionPrerelease)
|
|
|
|
} else {
|
|
|
|
version = terraform.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
client.UserAgent = fmt.Sprintf("HashiCorp-Terraform-v%s", version)
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
// getArmClient is a helper method which returns a fully instantiated
|
|
|
|
// *ArmClient based on the Config's current settings.
|
|
|
|
func (c *Config) getArmClient() (*ArmClient, error) {
|
|
|
|
spt, err := azure.NewServicePrincipalToken(c.ClientID, c.ClientSecret, c.TenantID, azure.AzureResourceManagerScope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// client declarations:
|
|
|
|
client := ArmClient{}
|
|
|
|
|
|
|
|
// NOTE: these declarations should be left separate for clarity should the
|
|
|
|
// clients be wished to be configured with custom Responders/PollingModess etc...
|
|
|
|
asc := compute.NewAvailabilitySetsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&asc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
asc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
asc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.availSetClient = asc
|
|
|
|
|
|
|
|
uoc := compute.NewUsageOperationsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&uoc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
uoc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
uoc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.usageOpsClient = uoc
|
|
|
|
|
|
|
|
vmeic := compute.NewVirtualMachineExtensionImagesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vmeic.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vmeic.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vmeic.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vmExtensionImageClient = vmeic
|
|
|
|
|
|
|
|
vmec := compute.NewVirtualMachineExtensionsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vmec.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vmec.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vmec.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vmExtensionClient = vmec
|
|
|
|
|
|
|
|
vmic := compute.NewVirtualMachineImagesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vmic.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vmic.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vmic.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vmImageClient = vmic
|
|
|
|
|
|
|
|
vmc := compute.NewVirtualMachinesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vmc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vmc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vmc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vmClient = vmc
|
|
|
|
|
|
|
|
agc := network.NewApplicationGatewaysClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&agc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
agc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
agc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.appGatewayClient = agc
|
|
|
|
|
|
|
|
ifc := network.NewInterfacesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&ifc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
ifc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
ifc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.ifaceClient = ifc
|
|
|
|
|
|
|
|
lbc := network.NewLoadBalancersClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&lbc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
lbc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
lbc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.loadBalancerClient = lbc
|
|
|
|
|
|
|
|
lgc := network.NewLocalNetworkGatewaysClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&lgc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
lgc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
lgc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.localNetConnClient = lgc
|
|
|
|
|
|
|
|
pipc := network.NewPublicIPAddressesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&pipc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
pipc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
pipc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.publicIPClient = pipc
|
|
|
|
|
|
|
|
sgc := network.NewSecurityGroupsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&sgc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
sgc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
sgc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.secGroupClient = sgc
|
|
|
|
|
|
|
|
src := network.NewSecurityRulesClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&src.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
src.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
src.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.secRuleClient = src
|
|
|
|
|
|
|
|
snc := network.NewSubnetsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&snc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
snc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
snc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.subnetClient = snc
|
|
|
|
|
|
|
|
vgcc := network.NewVirtualNetworkGatewayConnectionsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vgcc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vgcc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vgcc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vnetGatewayConnectionsClient = vgcc
|
|
|
|
|
|
|
|
vgc := network.NewVirtualNetworkGatewaysClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vgc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vgc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vgc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vnetGatewayClient = vgc
|
|
|
|
|
|
|
|
vnc := network.NewVirtualNetworksClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&vnc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
vnc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
vnc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.vnetClient = vnc
|
|
|
|
|
|
|
|
rgc := resources.NewGroupsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&rgc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
rgc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
rgc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.resourceGroupClient = rgc
|
|
|
|
|
2016-01-05 22:43:52 +01:00
|
|
|
pc := resources.NewProvidersClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&pc.Client)
|
2016-01-05 22:43:52 +01:00
|
|
|
pc.Authorizer = spt
|
|
|
|
pc.Sender = autorest.CreateSender(withRequestLogging())
|
|
|
|
client.providers = pc
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
tc := resources.NewTagsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&tc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
tc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
tc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.tagsClient = tc
|
|
|
|
|
|
|
|
jc := scheduler.NewJobsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&jc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
jc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
jc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.jobsClient = jc
|
|
|
|
|
|
|
|
jcc := scheduler.NewJobCollectionsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&jcc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
jcc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
jcc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.jobsCollectionsClient = jcc
|
|
|
|
|
|
|
|
ssc := storage.NewAccountsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&ssc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
ssc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
ssc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.storageServiceClient = ssc
|
|
|
|
|
|
|
|
suc := storage.NewUsageOperationsClient(c.SubscriptionID)
|
2016-01-06 23:07:15 +01:00
|
|
|
setUserAgent(&suc.Client)
|
2015-12-08 23:30:12 +01:00
|
|
|
suc.Authorizer = spt
|
2015-12-09 02:25:05 +01:00
|
|
|
suc.Sender = autorest.CreateSender(withRequestLogging())
|
2015-12-08 23:30:12 +01:00
|
|
|
client.storageUsageClient = suc
|
|
|
|
|
|
|
|
return &client, nil
|
|
|
|
}
|