Skip to content

Commit

Permalink
Add new resource and data source DiskEncryptionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Dec 25, 2019
1 parent 82d660c commit 2653139
Show file tree
Hide file tree
Showing 10 changed files with 1,068 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Client struct {
AvailabilitySetsClient *compute.AvailabilitySetsClient
DisksClient *compute.DisksClient
DiskEncryptionSetsClient *compute.DiskEncryptionSetsClient
GalleriesClient *compute.GalleriesClient
GalleryImagesClient *compute.GalleryImagesClient
GalleryImageVersionsClient *compute.GalleryImageVersionsClient
Expand All @@ -33,6 +34,9 @@ func NewClient(o *common.ClientOptions) *Client {
disksClient := compute.NewDisksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&disksClient.Client, o.ResourceManagerAuthorizer)

diskEncryptionSetsClient := compute.NewDiskEncryptionSetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&diskEncryptionSetsClient.Client, o.ResourceManagerAuthorizer)

galleriesClient := compute.NewGalleriesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&galleriesClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -81,6 +85,7 @@ func NewClient(o *common.ClientOptions) *Client {
return &Client{
AvailabilitySetsClient: &availabilitySetsClient,
DisksClient: &disksClient,
DiskEncryptionSetsClient: &diskEncryptionSetsClient,
GalleriesClient: &galleriesClient,
GalleryImagesClient: &galleryImagesClient,
GalleryImageVersionsClient: &galleryImageVersionsClient,
Expand Down
137 changes: 137 additions & 0 deletions azurerm/internal/services/compute/data_source_disk_encryption_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package compute

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmDiskEncryptionSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDiskEncryptionSetRead,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"active_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,
},
},
},
},

"identity": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"previous_keys": {
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,
},
},
},
},

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmDiskEncryptionSetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DiskEncryptionSetsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Disk Encryption Set %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading Disk Encryption Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if encryptionSetProperties := resp.EncryptionSetProperties; encryptionSetProperties != nil {
if err := d.Set("active_key", flattenArmDiskEncryptionSetKeyVaultAndKeyReference(encryptionSetProperties.ActiveKey)); err != nil {
return fmt.Errorf("Error setting `active_key`: %+v", err)
}
if err := d.Set("previous_keys", flattenArmDiskEncryptionSetKeyVaultAndKeyReferenceArray(encryptionSetProperties.PreviousKeys)); err != nil {
return fmt.Errorf("Error setting `previous_keys`: %+v", err)
}
}
if identity := resp.Identity; identity != nil {
if err := d.Set("identity", flattenArmDiskEncryptionSetIdentity(identity)); err != nil {
return fmt.Errorf("Error setting `identity`: %+v", err)
}
}

return nil
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/compute/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (r Registration) Name() string {
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_availability_set": dataSourceArmAvailabilitySet(),
"azurerm_disk_encryption_set": dataSourceArmDiskEncryptionSet(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_image": dataSourceArmImage(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
Expand All @@ -31,6 +32,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
func (r Registration) SupportedResources() map[string]*schema.Resource {
resources := map[string]*schema.Resource{
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_disk_encryption_set": resourceArmDiskEncryptionSet(),
"azurerm_image": resourceArmImage(),
"azurerm_managed_disk": resourceArmManagedDisk(),
"azurerm_marketplace_agreement": resourceArmMarketplaceAgreement(),
Expand Down
Loading

0 comments on commit 2653139

Please sign in to comment.