Skip to content

Commit

Permalink
Merge pull request #420 from terraform-providers/snapshot
Browse files Browse the repository at this point in the history
New Resource & Data Source: `azurerm_snapshot`
  • Loading branch information
tombuildsstuff authored Oct 12, 2017
2 parents d23f21a + cfb0f05 commit a8d12b8
Show file tree
Hide file tree
Showing 16 changed files with 1,328 additions and 47 deletions.
22 changes: 16 additions & 6 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ArmClient struct {
imageClient compute.ImagesClient

diskClient disk.DisksClient
snapshotsClient disk.SnapshotsClient
cosmosDBClient cosmosdb.DatabaseAccountsClient
automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
Expand Down Expand Up @@ -354,12 +355,6 @@ func (c *Config) getArmClient() (*ArmClient, error) {
cdb.Sender = sender
client.cosmosDBClient = cdb

dkc := disk.NewDisksClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&dkc.Client)
dkc.Authorizer = auth
dkc.Sender = sender
client.diskClient = dkc

img := compute.NewImagesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&img.Client)
img.Authorizer = auth
Expand Down Expand Up @@ -656,6 +651,7 @@ func (c *Config) getArmClient() (*ArmClient, error) {

client.registerAuthentication(endpoint, graphEndpoint, c.SubscriptionID, c.TenantID, auth, graphAuth, sender)
client.registerDatabases(endpoint, c.SubscriptionID, auth, sender)
client.registerDisks(endpoint, c.SubscriptionID, auth, sender)
client.registerKeyVaultClients(endpoint, c.SubscriptionID, auth, keyVaultAuth, sender)

return &client, nil
Expand Down Expand Up @@ -758,6 +754,20 @@ 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
c.diskClient = diskClient

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

func (c *ArmClient) registerKeyVaultClients(endpoint, subscriptionId string, auth autorest.Authorizer, keyVaultAuth autorest.Authorizer, sender autorest.Sender) {
keyVaultClient := keyvault.NewVaultsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&keyVaultClient.Client)
Expand Down
142 changes: 142 additions & 0 deletions azurerm/data_source_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceArmSnapshot() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSnapshotRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
},

// Computed
"os_type": {
Type: schema.TypeString,
Computed: true,
},
"disk_size_gb": {
Type: schema.TypeString,
Computed: true,
},
"time_created": {
Type: schema.TypeString,
Computed: true,
},
"creation_option": {
Type: schema.TypeString,
Computed: true,
},
"source_uri": {
Type: schema.TypeString,
Computed: true,
},
"source_resource_id": {
Type: schema.TypeString,
Computed: true,
},
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
"encryption_settings": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
},

"disk_encryption_key": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"secret_url": {
Type: schema.TypeString,
Computed: true,
},

"source_vault_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"key_encryption_key": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_url": {
Type: schema.TypeString,
Computed: true,
},

"source_vault_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}

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

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

resp, err := client.Get(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 {
d.Set("os_type", string(props.OsType))
d.Set("time_created", props.TimeCreated.String())

if props.DiskSizeGB != nil {
d.Set("disk_size_gb", int(*props.DiskSizeGB))
}

if props.EncryptionSettings != nil {
d.Set("encryption_settings", flattenManagedDiskEncryptionSettings(props.EncryptionSettings))
}
}

if data := resp.CreationData; data != nil {
d.Set("creation_option", string(data.CreateOption))
if data.SourceURI != nil {
d.Set("source_uri", *data.SourceURI)
}
if data.SourceResourceID != nil {
d.Set("source_resource_id", *data.SourceResourceID)
}
if data.StorageAccountID != nil {
d.Set("storage_account_id", *data.StorageAccountID)
}
}

return nil
}
176 changes: 176 additions & 0 deletions azurerm/data_source_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMSnapshot_importBasic(t *testing.T) {
dataSourceName := "data.azurerm_snapshot.snapshot"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSnapshot_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
),
},
},
})
}

func TestAccDataSourceAzureRMSnapshot_importEncryption(t *testing.T) {
dataSourceName := "data.azurerm_snapshot.snapshot"
ri := acctest.RandInt()
rs := acctest.RandString(4)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMSnapshot_encryption(ri, rs, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttr(dataSourceName, "encryption_settings.0.enabled", "true"),
),
},
},
})
}

func testAccDataSourceAzureRMSnapshot_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "%s"
}
resource "azurerm_managed_disk" "test" {
name = "acctestmd-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
}
resource "azurerm_snapshot" "test" {
name = "acctestss_%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
create_option = "Copy"
source_uri = "${azurerm_managed_disk.test.id}"
}
data "azurerm_snapshot" "snapshot" {
name = "${azurerm_snapshot.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, rInt)
}

func testAccDataSourceAzureRMSnapshot_encryption(rInt int, rString string, location string) string {
return fmt.Sprintf(`
data "azurerm_client_config" "current" {}
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "%s"
}
resource "azurerm_managed_disk" "test" {
name = "acctestmd-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "10"
}
resource "azurerm_key_vault" "test" {
name = "acctestkv%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
sku {
name = "premium"
}
access_policy {
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
object_id = "${data.azurerm_client_config.current.service_principal_object_id}"
key_permissions = [
"all",
]
secret_permissions = [
"all",
]
}
enabled_for_disk_encryption = true
}
resource "azurerm_key_vault_key" "test" {
name = "generated-certificate"
vault_uri = "${azurerm_key_vault.test.vault_uri}"
key_type = "RSA"
key_size = 2048
key_opts = [
"decrypt",
"encrypt",
"sign",
"unwrapKey",
"verify",
"wrapKey",
]
}
resource "azurerm_key_vault_secret" "test" {
name = "secret-sauce"
value = "szechuan"
vault_uri = "${azurerm_key_vault.test.vault_uri}"
}
resource "azurerm_snapshot" "test" {
name = "acctestss_%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
create_option = "Copy"
source_uri = "${azurerm_managed_disk.test.id}"
disk_size_gb = "20"
encryption_settings {
enabled = true
disk_encryption_key {
secret_url = "${azurerm_key_vault_secret.test.id}"
source_vault_id = "${azurerm_key_vault.test.id}"
}
key_encryption_key {
key_url = "${azurerm_key_vault_key.test.id}"
source_vault_id = "${azurerm_key_vault.test.id}"
}
}
}
data "azurerm_snapshot" "snapshot" {
name = "${azurerm_snapshot.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, rString, rInt)
}
Loading

0 comments on commit a8d12b8

Please sign in to comment.