Skip to content

Commit

Permalink
Migrating over to the new Disks SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Jan 23, 2018
1 parent 20aadd8 commit 5bd0de7
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 142 deletions.
30 changes: 10 additions & 20 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/Azure/azure-sdk-for-go/arm/automation"
"github.com/Azure/azure-sdk-for-go/arm/cosmos-db"
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
keyVault "github.com/Azure/azure-sdk-for-go/dataplane/keyvault"
appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
Expand Down Expand Up @@ -61,8 +60,6 @@ type ArmClient struct {

StopContext context.Context

diskClient disk.DisksClient
snapshotsClient disk.SnapshotsClient
cosmosDBClient cosmosdb.DatabaseAccountsClient
automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
Expand Down Expand Up @@ -104,7 +101,9 @@ type ArmClient struct {

// Compute
availSetClient compute.AvailabilitySetsClient
diskClient compute.DisksClient
imageClient compute.ImagesClient
snapshotsClient compute.SnapshotsClient
usageOpsClient compute.UsageClient
vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
vmExtensionClient compute.VirtualMachineExtensionsClient
Expand Down Expand Up @@ -437,7 +436,6 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
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 Down Expand Up @@ -538,10 +536,18 @@ func (c *ArmClient) registerComputeClients(endpoint, subscriptionId string, auth
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
Expand Down Expand Up @@ -656,22 +662,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
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
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
16 changes: 8 additions & 8 deletions azurerm/encryption_settings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package azurerm

import (
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -63,9 +63,9 @@ func encryptionSettingsSchema() *schema.Schema {
}
}

func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk.EncryptionSettings {
func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *compute.EncryptionSettings {
enabled := settings["enabled"].(bool)
config := &disk.EncryptionSettings{
config := &compute.EncryptionSettings{
Enabled: utils.Bool(enabled),
}

Expand All @@ -74,9 +74,9 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk.

secretURL := dek["secret_url"].(string)
sourceVaultId := dek["source_vault_id"].(string)
config.DiskEncryptionKey = &disk.KeyVaultAndSecretReference{
config.DiskEncryptionKey = &compute.KeyVaultAndSecretReference{
SecretURL: utils.String(secretURL),
SourceVault: &disk.SourceVault{
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
},
}
Expand All @@ -87,9 +87,9 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk.

secretURL := kek["key_url"].(string)
sourceVaultId := kek["source_vault_id"].(string)
config.KeyEncryptionKey = &disk.KeyVaultAndKeyReference{
config.KeyEncryptionKey = &compute.KeyVaultAndKeyReference{
KeyURL: utils.String(secretURL),
SourceVault: &disk.SourceVault{
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
},
}
Expand All @@ -98,7 +98,7 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk.
return config
}

func flattenManagedDiskEncryptionSettings(encryptionSettings *disk.EncryptionSettings) []interface{} {
func flattenManagedDiskEncryptionSettings(encryptionSettings *compute.EncryptionSettings) []interface{} {
value := map[string]interface{}{
"enabled": *encryptionSettings.Enabled,
}
Expand Down
7 changes: 4 additions & 3 deletions azurerm/resource_arm_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ func testCheckAzureVMSSExists(sourceVMSS string, shouldExist bool) resource.Test
}

func testCheckAzureRMImageDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).diskClient
client := testAccProvider.Meta().(*ArmClient).diskClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_image" {
Expand All @@ -337,14 +338,14 @@ func testCheckAzureRMImageDestroy(s *terraform.State) error {
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := conn.Get(resourceGroup, name)
resp, err := client.Get(ctx, resourceGroup, name)

if err != nil {
return nil
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Managed Image still exists: \n%#v", resp.Properties)
return fmt.Errorf("Managed Image still exists: \n%#v", resp.DiskProperties)
}
}

Expand Down
Loading

0 comments on commit 5bd0de7

Please sign in to comment.