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

Data source - azurerm_data_protection_backup_vault #13062

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package dataprotection

import (
"fmt"
"log"
"regexp"
"time"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/identity"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dataprotection/legacysdk/dataprotection"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dataprotection/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceDataProtectionBackupVault() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceDataProtectionBackupVaultRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.BackupVaultID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[-a-zA-Z0-9]{2,50}$"),
"DataProtection BackupVault name must be 2 - 50 characters long, contain only letters, numbers and hyphens.).",
),
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": location.SchemaComputed(),

"datastore_type": {
Type: pluginsdk.TypeString,
Computed: true,
},

"redundancy": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

func dataSourceDataProtectionBackupVaultRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataProtection.BackupVaultClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

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

id := parse.NewBackupVaultID(subscriptionId, resourceGroup, name)

resp, err := client.Get(ctx, id.Name, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] DataProtection BackupVault %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving DataProtection BackupVault (%q): %+v", id, err)
}

d.SetId(id.ID())
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
if props := resp.Properties; props != nil {
if props.StorageSettings != nil && len(*props.StorageSettings) > 0 {
d.Set("datastore_type", (*props.StorageSettings)[0].DatastoreType)
d.Set("redundancy", (*props.StorageSettings)[0].Type)
}
}
if err := d.Set("identity", dataSourceFlattenBackupVaultDppIdentityDetails(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}
return tags.FlattenAndSet(d, resp.Tags)
}

func dataSourceFlattenBackupVaultDppIdentityDetails(input *dataprotection.DppIdentityDetails) []interface{} {
var config *identity.ExpandedConfig
if input != nil {
principalId := ""
if input.PrincipalID != nil {
principalId = *input.PrincipalID
}

tenantId := ""
if input.TenantID != nil {
tenantId = *input.TenantID
}
config = &identity.ExpandedConfig{
Type: identity.Type(*input.Type),
PrincipalId: principalId,
TenantId: tenantId,
}
}
return identity.SystemAssigned{}.Flatten(config)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package dataprotection_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/dataprotection/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type DataProtectionBackupVaultDataSource struct{}

func TestAccDataProtectionBackupVaultDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_data_protection_backup_vault", "test")
r := DataProtectionBackupVaultDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("datastore_type").HasValue("VaultStore"),
check.That(data.ResourceName).Key("redundancy").HasValue("LocallyRedundant"),
check.That(data.ResourceName).Key("location").Exists(),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"),
check.That(data.ResourceName).Key("identity.0.principal_id").Exists(),
check.That(data.ResourceName).Key("identity.0.tenant_id").Exists(),
check.That(data.ResourceName).Key("tags.ENV").HasValue("Test"),
),
},
})
}

func (r DataProtectionBackupVaultDataSource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.BackupVaultID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.DataProtection.BackupVaultClient.Get(ctx, id.Name, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving DataProtection BackupVault (%q): %+v", id, err)
}
return utils.Bool(true), nil
}

func (r DataProtectionBackupVaultDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctest-dataprotection-%d"
location = "%s"
}

resource "azurerm_data_protection_backup_vault" "test" {
name = "acctest-bv-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
datastore_type = "VaultStore"
redundancy = "LocallyRedundant"
identity {
type = "SystemAssigned"
}

tags = {
ENV = "Test"
}
}
data "azurerm_data_protection_backup_vault" "test" {
name = azurerm_data_protection_backup_vault.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
4 changes: 3 additions & 1 deletion internal/services/dataprotection/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func (r Registration) WebsiteCategories() []string {

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{}
return map[string]*pluginsdk.Resource{
"azurerm_data_protection_backup_vault": dataSourceDataProtectionBackupVault(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
64 changes: 64 additions & 0 deletions website/docs/d/data_protection_backup_vault.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
subcategory: "DataProtection"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_data_protection_backup_vault"
description: |-
Manages a Backup Vault.
---

# Data Source: azurerm_data_protection_backup_vault

Use this data source to access information about an existing Backup Vault.

## Example Usage

```hcl
data "azurerm_data_protection_backup_vault" "example" {
name = "existing-backup-vault"
resource_group_name = "existing-resource-group"
}

output "azurerm_data_protection_backup_vault_id" {
value = data.azurerm_vpn_gateway.example.id
}

output "azurerm_data_protection_backup_vault_principal_id" {
value = data.azurerm_data_protection_backup_vault.example.identity.0.principal_id
}
```

## Arguments Reference

* `name` - (Required) Specifies the name of the Backup Vault.

* `resource_group_name` - (Required) The name of the Resource Group where the Backup Vault exists.

## Attributes Reference

* `id` - The ID of the Backup Vault.

* `location` - The Azure Region where the Backup Vault exists.

* `datastore_type` - Specifies the type of the data store.

* `redundancy` - Specifies the backup storage redundancy.

* `identity` - A `identity` block as defined below.

* `tags` - A mapping of tags which are assigned to the Backup Vault.

---

`identity` exports the following:

* `type` - Specifies the identity type of the Backup Vault.

* `principal_id` - The Principal ID for the Service Principal associated with the Identity of this Backup Vault.

* `tenant_id` - The Tenant ID for the Service Principal associated with the Identity of this Backup Vault.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Backup Vault.