Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDKv12: Migrating over to the new Compute SDK #744

Merged
merged 9 commits into from
Jan 24, 2018
148 changes: 58 additions & 90 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/disk"
appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/authorization/mgmt/2015-07-01/authorization"
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2017-04-02/cdn"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2017-08-01-preview/containerinstance"
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2017-09-30/containerservice"
Expand Down Expand Up @@ -62,18 +61,8 @@ type ArmClient struct {

StopContext context.Context

availSetClient compute.AvailabilitySetsClient
usageOpsClient compute.UsageClient
vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
vmExtensionClient compute.VirtualMachineExtensionsClient
vmScaleSetClient compute.VirtualMachineScaleSetsClient
vmImageClient compute.VirtualMachineImagesClient
vmClient compute.VirtualMachinesClient
imageClient compute.ImagesClient
cosmosDBClient documentdb.DatabaseAccountsClient

diskClient disk.DisksClient
snapshotsClient disk.SnapshotsClient
cosmosDBClient documentdb.DatabaseAccountsClient
automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
automationCredentialClient automation.CredentialClient
Expand Down Expand Up @@ -109,6 +98,18 @@ type ArmClient struct {
cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient

// Compute
availSetClient compute.AvailabilitySetsClient
diskClient compute.DisksClient
imageClient compute.ImagesClient
snapshotsClient compute.SnapshotsClient
usageOpsClient compute.UsageClient
vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
vmExtensionClient compute.VirtualMachineExtensionsClient
vmScaleSetClient compute.VirtualMachineScaleSetsClient
vmImageClient compute.VirtualMachineImagesClient
vmClient compute.VirtualMachinesClient

// Databases
mysqlConfigurationsClient mysql.ConfigurationsClient
mysqlDatabasesClient mysql.DatabasesClient
Expand Down Expand Up @@ -321,80 +322,22 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
return keyVaultSpt, nil
})

// NOTE: these declarations should be left separate for clarity should the
// clients be wished to be configured with custom Responders/PollingModes etc...
asc := compute.NewAvailabilitySetsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&asc.Client)
asc.Authorizer = auth
asc.Sender = sender
asc.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.availSetClient = asc

uoc := compute.NewUsageClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&uoc.Client)
uoc.Authorizer = auth
uoc.Sender = sender
uoc.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.usageOpsClient = uoc

vmeic := compute.NewVirtualMachineExtensionImagesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&vmeic.Client)
vmeic.Authorizer = auth
vmeic.Sender = sender
vmeic.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.vmExtensionImageClient = vmeic

vmec := compute.NewVirtualMachineExtensionsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&vmec.Client)
vmec.Authorizer = auth
vmec.Sender = sender
vmec.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.vmExtensionClient = vmec

vmic := compute.NewVirtualMachineImagesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&vmic.Client)
vmic.Authorizer = auth
vmic.Sender = sender
vmic.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.vmImageClient = vmic

vmssc := compute.NewVirtualMachineScaleSetsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&vmssc.Client)
vmssc.Authorizer = auth
vmssc.Sender = sender
vmssc.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.vmScaleSetClient = vmssc

vmc := compute.NewVirtualMachinesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&vmc.Client)
vmc.Authorizer = auth
vmc.Sender = sender
vmc.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.vmClient = vmc

csc := containerservice.NewContainerServicesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&csc.Client)
csc.Authorizer = auth
csc.Sender = sender
csc.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.containerServicesClient = csc

img := compute.NewImagesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&img.Client)
img.Authorizer = auth
img.Sender = sender
img.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.imageClient = img

client.registerAppInsightsClient(endpoint, c.SubscriptionID, auth, sender)
client.registerAppInsightsClients(endpoint, c.SubscriptionID, auth, sender)
client.registerAutomationClients(endpoint, c.SubscriptionID, auth, sender)
client.registerAuthentication(endpoint, graphEndpoint, c.SubscriptionID, c.TenantID, auth, graphAuth, sender)
client.registerCDNClients(endpoint, c.SubscriptionID, auth, sender)
client.registerComputeClients(endpoint, c.SubscriptionID, auth, sender)
client.registerCosmosDBClients(endpoint, c.SubscriptionID, auth, sender)
client.registerContainerInstanceClients(endpoint, c.SubscriptionID, auth, sender)
client.registerContainerRegistryClients(endpoint, c.SubscriptionID, auth, sender)
client.registerDatabases(endpoint, c.SubscriptionID, auth, sender)
client.registerDisks(endpoint, c.SubscriptionID, auth, sender)
client.registerDNSClients(endpoint, c.SubscriptionID, auth, sender)
client.registerEventGridClients(endpoint, c.SubscriptionID, auth, sender)
client.registerEventHubClients(endpoint, c.SubscriptionID, auth, sender)
Expand All @@ -413,7 +356,7 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
return &client, nil
}

func (c *ArmClient) registerAppInsightsClient(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
func (c *ArmClient) registerAppInsightsClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
ai := appinsights.NewComponentsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&ai.Client)
ai.Authorizer = auth
Expand Down Expand Up @@ -495,7 +438,48 @@ func (c *ArmClient) registerCosmosDBClients(endpoint, subscriptionId string, aut
cdb := documentdb.NewDatabaseAccountsClientWithBaseURI(endpoint, subscriptionId, "", "", "", "", "")
c.configureClient(&cdb.Client, auth)
c.cosmosDBClient = cdb
}

func (c *ArmClient) registerComputeClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
availabilitySetsClient := compute.NewAvailabilitySetsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&availabilitySetsClient.Client, auth)
c.availSetClient = availabilitySetsClient

diskClient := compute.NewDisksClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&diskClient.Client, auth)
c.diskClient = diskClient

imagesClient := compute.NewImagesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&imagesClient.Client, auth)
c.imageClient = imagesClient

snapshotsClient := compute.NewSnapshotsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&snapshotsClient.Client, auth)
c.snapshotsClient = snapshotsClient

usageClient := compute.NewUsageClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&usageClient.Client, auth)
c.usageOpsClient = usageClient

extensionImagesClient := compute.NewVirtualMachineExtensionImagesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&extensionImagesClient.Client, auth)
c.vmExtensionImageClient = extensionImagesClient

extensionsClient := compute.NewVirtualMachineExtensionsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&extensionsClient.Client, auth)
c.vmExtensionClient = extensionsClient

virtualMachineImagesClient := compute.NewVirtualMachineImagesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&virtualMachineImagesClient.Client, auth)
c.vmImageClient = virtualMachineImagesClient

scaleSetsClient := compute.NewVirtualMachineScaleSetsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&scaleSetsClient.Client, auth)
c.vmScaleSetClient = scaleSetsClient

virtualMachinesClient := compute.NewVirtualMachinesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&virtualMachinesClient.Client, auth)
c.vmClient = virtualMachinesClient
}

func (c *ArmClient) registerContainerInstanceClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
Expand Down Expand Up @@ -587,22 +571,6 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto
c.sqlServersClient = sqlSrvClient
}

func (c *ArmClient) registerDisks(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
diskClient := disk.NewDisksClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&diskClient.Client)
diskClient.Authorizer = auth
diskClient.Sender = sender
diskClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.diskClient = diskClient

snapshotsClient := disk.NewSnapshotsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&snapshotsClient.Client)
snapshotsClient.Authorizer = auth
snapshotsClient.Sender = sender
snapshotsClient.SkipResourceProviderRegistration = c.skipProviderRegistration
c.snapshotsClient = snapshotsClient
}

func (c *ArmClient) registerDNSClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
dn := dns.NewRecordSetsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&dn.Client, auth)
Expand Down
40 changes: 20 additions & 20 deletions azurerm/data_source_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"sort"

"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -109,6 +109,7 @@ func dataSourceArmImage() *schema.Resource {

func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).imageClient
ctx := meta.(*ArmClient).StopContext

resGroup := d.Get("resource_group_name").(string)

Expand All @@ -123,7 +124,7 @@ func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {

if !nameRegexOk {
var err error
if img, err = client.Get(resGroup, name, ""); err != nil {
if img, err = client.Get(ctx, resGroup, name, ""); err != nil {
if utils.ResponseWasNotFound(img.Response) {
d.SetId("")
return nil
Expand All @@ -134,28 +135,24 @@ func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
r := regexp.MustCompile(nameRegex.(string))

list := []compute.Image{}
resp, err := client.ListByResourceGroup(resGroup)
resp, err := client.ListByResourceGroupComplete(ctx, resGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if utils.ResponseWasNotFound(resp.Response().Response) {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error getting list of images (resource group %q): %+v", resGroup, err)
}
for _, ri := range *resp.Value {
if r.Match(([]byte)(*ri.Name)) {
list = append(list, ri)

for resp.NotDone() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if the resp object will abort if the cancel on the StopContext is invoked? If not, we may want a select in here on the StopContext.Done, but not a big deal, I don't think it should hold up this pr as its an edge case to have enough images where it would be an issue I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not from what I can see, whilst it's a valid concern it's such an edge case (and minor issue in terms of iterations) I think we can safely ignore it for the moment

img := resp.Value()
if r.Match(([]byte)(*img.Name)) {
list = append(list, img)
}
}
for resp.NextLink != nil && *resp.NextLink != "" {
resp, err = client.ListByResourceGroupNextResults(resp)
err = resp.Next()

if err != nil {
return fmt.Errorf("[ERROR] Unable to query next results for image list (resource group %q): %+v", resGroup, err)
}
for _, ri := range *resp.Value {
if r.Match(([]byte)(*ri.Name)) {
list = append(list, ri)
}
return err
}
}

Expand All @@ -180,17 +177,20 @@ func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(*img.ID)
d.Set("name", img.Name)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(*img.Location))

if location := img.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if profile := img.StorageProfile; profile != nil {
if disk := profile.OsDisk; disk != nil {
if err := d.Set("os_disk", flattenAzureRmImageOSDisk(d, disk)); err != nil {
if err := d.Set("os_disk", flattenAzureRmImageOSDisk(disk)); err != nil {
return fmt.Errorf("[DEBUG] Error setting AzureRM Image OS Disk error: %+v", err)
}
}

if disks := img.StorageProfile.DataDisks; disks != nil {
if err := d.Set("data_disk", flattenAzureRmImageDataDisks(d, disks)); err != nil {
if disks := profile.DataDisks; disks != nil {
if err := d.Set("data_disk", flattenAzureRmImageDataDisks(disks)); err != nil {
return fmt.Errorf("[DEBUG] Error setting AzureRM Image Data Disks error: %+v", err)
}
}
Expand Down
19 changes: 15 additions & 4 deletions azurerm/data_source_managed_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ func dataSourceArmManagedDisk() *schema.Resource {
}

func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error {
diskClient := meta.(*ArmClient).diskClient
client := meta.(*ArmClient).diskClient
ctx := meta.(*ArmClient).StopContext

resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := diskClient.Get(resGroup, name)
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
Expand All @@ -65,8 +66,18 @@ func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) erro
}

d.SetId(*resp.ID)
if resp.Properties != nil {
flattenAzureRmManagedDiskProperties(d, resp.Properties)

if sku := resp.Sku; sku != nil {
d.Set("storage_account_type", string(sku.Name))
}

if props := resp.DiskProperties; props != nil {
if diskSize := props.DiskSizeGB; diskSize != nil {
d.Set("disk_size_gb", *diskSize)
}
if osType := props.OsType; osType != "" {
d.Set("os_type", string(osType))
}
}

if resp.CreationData != nil {
Expand Down
3 changes: 2 additions & 1 deletion azurerm/data_source_platform_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ func dataSourceArmPlatformImage() *schema.Resource {

func dataSourceArmPlatformImageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vmImageClient
ctx := meta.(*ArmClient).StopContext

location := azureRMNormalizeLocation(d.Get("location").(string))
publisher := d.Get("publisher").(string)
offer := d.Get("offer").(string)
sku := d.Get("sku").(string)

result, err := client.List(location, publisher, offer, sku, "", utils.Int32(int32(1000)), "name")
result, err := client.List(ctx, location, publisher, offer, sku, "", utils.Int32(int32(1000)), "name")
if err != nil {
return fmt.Errorf("Error reading Platform Images: %+v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions azurerm/data_source_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ func dataSourceArmSnapshot() *schema.Resource {

func dataSourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).snapshotsClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(resourceGroup, name)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error loading Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

if props := resp.Properties; props != nil {
if props := resp.DiskProperties; props != nil {
d.Set("os_type", string(props.OsType))
d.Set("time_created", props.TimeCreated.String())

Expand Down
Loading