-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from terraform-providers/snapshot
New Resource & Data Source: `azurerm_snapshot`
- Loading branch information
Showing
16 changed files
with
1,328 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.