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

azurerm_key_vault_certificate_contacts - in v4.0 make the contact field optional to allow for deletion of contacts from the key vault. / azurerm_key_vault - deprecate the contact field from v3.x provider and update properties to Computed Optional. #25552

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/parse"
Expand Down Expand Up @@ -39,10 +40,38 @@ type Contact struct {
}

func (r KeyVaultCertificateContactsResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
schema := map[string]*pluginsdk.Schema{
"key_vault_id": commonschema.ResourceIDReferenceRequiredForceNew(&commonids.KeyVaultId{}),
}

"contact": {
if features.FourPointOhBeta() {
schema["contact"] = &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"email": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"phone": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
} else {
schema["contact"] = &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Required: true,
MinItems: 1,
Expand All @@ -67,8 +96,10 @@ func (r KeyVaultCertificateContactsResource) Arguments() map[string]*pluginsdk.S
},
},
},
},
}
}

return schema
}

func (r KeyVaultCertificateContactsResource) Attributes() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -132,8 +163,20 @@ func (r KeyVaultCertificateContactsResource) Create() sdk.ResourceFunc {
ContactList: expandKeyVaultCertificateContactsContact(state.Contact),
}

if _, err := client.SetCertificateContacts(ctx, *keyVaultBaseUri, contacts); err != nil {
return fmt.Errorf("creating Key Vault Certificate Contacts %s: %+v", id, err)
if features.FourPointOhBeta() {
if len(*contacts.ContactList) == 0 {
if _, err := client.DeleteCertificateContacts(ctx, id.KeyVaultBaseUrl); err != nil {
return fmt.Errorf("removing Key Vault Certificate Contacts %s: %+v", id, err)
}
} else {
if _, err := client.SetCertificateContacts(ctx, *keyVaultBaseUri, contacts); err != nil {
return fmt.Errorf("creating Key Vault Certificate Contacts %s: %+v", id, err)
}
}
} else {
if _, err := client.SetCertificateContacts(ctx, *keyVaultBaseUri, contacts); err != nil {
return fmt.Errorf("creating Key Vault Certificate Contacts %s: %+v", id, err)
}
}

metadata.SetID(id)
Expand Down Expand Up @@ -216,8 +259,20 @@ func (r KeyVaultCertificateContactsResource) Update() sdk.ResourceFunc {
existing.ContactList = expandKeyVaultCertificateContactsContact(state.Contact)
}

if _, err := client.SetCertificateContacts(ctx, id.KeyVaultBaseUrl, existing); err != nil {
return fmt.Errorf("updating Key Vault Certificate Contacts %s: %+v", id, err)
if features.FourPointOhBeta() {
if len(*existing.ContactList) == 0 {
if _, err := client.DeleteCertificateContacts(ctx, id.KeyVaultBaseUrl); err != nil {
return fmt.Errorf("removing Key Vault Certificate Contacts %s: %+v", id, err)
}
} else {
if _, err := client.SetCertificateContacts(ctx, id.KeyVaultBaseUrl, existing); err != nil {
return fmt.Errorf("updating Key Vault Certificate Contacts %s: %+v", id, err)
}
}
} else {
if _, err := client.SetCertificateContacts(ctx, id.KeyVaultBaseUrl, existing); err != nil {
return fmt.Errorf("updating Key Vault Certificate Contacts %s: %+v", id, err)
}
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand Down Expand Up @@ -102,6 +103,41 @@ func TestAccKeyVaultCertificateContacts_nonExistentVault(t *testing.T) {
})
}

func TestAccKeyVaultCertificateContacts_remove(t *testing.T) {
if !features.FourPointOhBeta() {
t.Skipf("Skippped as test is not valid in 3.x")
}

data := acceptance.BuildTestData(t, "azurerm_key_vault_certificate_contacts", "test")
r := KeyVaultCertificateContactsResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.remove(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("contact").IsEmpty(),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("contact").IsNotEmpty(),
),
},
data.ImportStep(),
})
}

func (r KeyVaultCertificateContactsResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.CertificateContactsID(state.ID)
if err != nil {
Expand Down Expand Up @@ -255,3 +291,18 @@ resource "azurerm_key_vault_access_policy" "test" {
}
`, data.Locations.Primary, data.RandomInteger, data.RandomString)
}

func (r KeyVaultCertificateContactsResource) remove(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_key_vault_certificate_contacts" "test" {
key_vault_id = azurerm_key_vault.test.id

depends_on = [
azurerm_key_vault_access_policy.test
]
}
`, template)
}
Loading
Loading