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_key_vault_secrets, azurerm_key_vault_certificates - expose certificates block #20498

Merged
merged 2 commits into from
Feb 16, 2023
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
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.1/keyvault"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/parse"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
Expand Down Expand Up @@ -40,6 +42,29 @@ func dataSourceKeyVaultCertificates() *pluginsdk.Resource {
Optional: true,
Default: true,
},

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

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

"enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -70,6 +95,7 @@ func dataSourceKeyVaultCertificatesRead(d *pluginsdk.ResourceData, meta interfac
d.SetId(keyVaultId.ID())

var names []string
var certs []map[string]interface{}
if certificateList.Response().Value != nil {
for certificateList.NotDone() {
for _, v := range *certificateList.Response().Value {
Expand All @@ -78,6 +104,7 @@ func dataSourceKeyVaultCertificatesRead(d *pluginsdk.ResourceData, meta interfac
return err
}
names = append(names, nestedItem.Name)
certs = append(certs, expandCertificate(nestedItem.Name, v))
err = certificateList.NextWithContext(ctx)
if err != nil {
return fmt.Errorf("retrieving next page of Certificates from %s: %+v", *keyVaultId, err)
Expand All @@ -87,7 +114,19 @@ func dataSourceKeyVaultCertificatesRead(d *pluginsdk.ResourceData, meta interfac
}

d.Set("names", names)
d.Set("certificates", certs)
d.Set("key_vault_id", keyVaultId.ID())

return nil
}

func expandCertificate(name string, item keyvault.CertificateItem) map[string]interface{} {
var cert = map[string]interface{}{
"name": name,
"id": *item.ID,
}
if item.Attributes != nil && item.Attributes.Enabled != nil {
cert["enabled"] = *item.Attributes.Enabled
}
return cert
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestAccDataSourceKeyVaultCertificates_basic(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("names.#").HasValue("31"),
check.That(data.ResourceName).Key("certificates.#").HasValue("31"),
),
},
})
Expand Down
39 changes: 39 additions & 0 deletions internal/services/keyvault/key_vault_secrets_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.1/keyvault"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/parse"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
Expand Down Expand Up @@ -36,6 +38,29 @@ func dataSourceKeyVaultSecrets() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
},
},

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

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

"enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -64,6 +89,7 @@ func dataSourceKeyVaultSecretsRead(d *pluginsdk.ResourceData, meta interface{})
d.SetId(keyVaultId.ID())

var names []string
var secrets []map[string]interface{}

if secretList.Response().Value != nil {
for secretList.NotDone() {
Expand All @@ -73,6 +99,7 @@ func dataSourceKeyVaultSecretsRead(d *pluginsdk.ResourceData, meta interface{})
return err
}
names = append(names, *name)
secrets = append(secrets, expandSecrets(*name, v))
err = secretList.NextWithContext(ctx)
if err != nil {
return fmt.Errorf("listing secrets on Azure KeyVault %q: %+v", *keyVaultId, err)
Expand All @@ -82,6 +109,7 @@ func dataSourceKeyVaultSecretsRead(d *pluginsdk.ResourceData, meta interface{})
}

d.Set("names", names)
d.Set("secrets", secrets)
d.Set("key_vault_id", keyVaultId.ID())

return nil
Expand All @@ -99,3 +127,14 @@ func parseNameFromSecretUrl(input string) (*string, error) {
}
return &segments[2], nil
}

func expandSecrets(name string, item keyvault.SecretItem) map[string]interface{} {
res := map[string]interface{}{
"id": *item.ID,
"name": name,
}
if item.Attributes != nil && item.Attributes.Enabled != nil {
res["enabled"] = *item.Attributes.Enabled
}
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestAccDataSourceKeyVaultSecrets_basic(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("names.#").HasValue("31"),
check.That(data.ResourceName).Key("secrets.#").HasValue("31"),
),
},
})
Expand Down
10 changes: 10 additions & 0 deletions website/docs/d/key_vault_certificates.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ In addition to the arguments above, the following attributes are exported:
* `names` - List containing names of certificates that exist in this Key Vault.

* `key_vault_id` - The Key Vault ID.

* `certificates` - One or more `certificates` blocks as defined below.

---

A `certificates` block supports following:

* `name` - The name of secret.

* `enabled` - Whether this secret is enabled.

## Timeouts

Expand Down
12 changes: 12 additions & 0 deletions website/docs/d/key_vault_secrets.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ In addition to the Argument listed above - the following Attributes are exported

* `names` - List containing names of secrets that exist in this Key Vault.

* `secrets` - One or more `secrets` blocks as defined below.

---

A `secrets` block supports following:

* `name` - The name of secret.

* `enabled` - Whether this secret is enabled.

* `id` - The ID of this secret.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down