-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
New Resource: azurerm_data_protection_backup_vault_customer_managed_key
#28679
Merged
+667
−0
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c36f6eb
add new resource azurerm_data_protection_backup_vault_customer_manage…
catriona-m 2d1696d
fix delete
catriona-m 1d07896
fix test
catriona-m 454628e
fix delete
catriona-m 704e006
fix errs and nil check
catriona-m 0f0461e
fix update
catriona-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
223 changes: 223 additions & 0 deletions
223
...nal/services/dataprotection/data_protection_backup_vault_customer_managed_key_resource.go
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,223 @@ | ||||||||||||||||||||||||
// Copyright (c) HashiCorp, Inc. | ||||||||||||||||||||||||
// SPDX-License-Identifier: MPL-2.0 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
package dataprotection | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"context" | ||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||
"log" | ||||||||||||||||||||||||
"time" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||||||||||||||||||||||||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||||||||||||||||||||||||
"github.com/hashicorp/go-azure-sdk/resource-manager/dataprotection/2024-04-01/backupvaults" | ||||||||||||||||||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/locks" | ||||||||||||||||||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||||||||||||||||||||||||
keyVaultParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/parse" | ||||||||||||||||||||||||
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate" | ||||||||||||||||||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type DataProtectionBackupVaultCustomerManagedKeyResource struct{} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type DataProtectionBackupVaultCustomerManagedKeyModel struct { | ||||||||||||||||||||||||
DataProtectionBackupVaultID string `tfschema:"data_protection_backup_vault_id"` | ||||||||||||||||||||||||
KeyVaultKeyID string `tfschema:"key_vault_key_id"` | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var _ sdk.ResourceWithUpdate = DataProtectionBackupVaultCustomerManagedKeyResource{} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) ModelObject() interface{} { | ||||||||||||||||||||||||
return &DataProtectionBackupVaultCustomerManagedKeyResource{} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) ResourceType() string { | ||||||||||||||||||||||||
return "azurerm_data_protection_backup_vault_customer_managed_key" | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||||||||||||||||||||||||
return backupvaults.ValidateBackupVaultID | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Arguments() map[string]*pluginsdk.Schema { | ||||||||||||||||||||||||
return map[string]*pluginsdk.Schema{ | ||||||||||||||||||||||||
"data_protection_backup_vault_id": { | ||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||
Required: true, | ||||||||||||||||||||||||
ForceNew: true, | ||||||||||||||||||||||||
ValidateFunc: backupvaults.ValidateBackupVaultID, | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
"key_vault_key_id": { | ||||||||||||||||||||||||
Type: pluginsdk.TypeString, | ||||||||||||||||||||||||
Required: true, | ||||||||||||||||||||||||
ValidateFunc: keyVaultValidate.NestedItemIdWithOptionalVersion, | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Attributes() map[string]*pluginsdk.Schema { | ||||||||||||||||||||||||
return map[string]*pluginsdk.Schema{} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Create() sdk.ResourceFunc { | ||||||||||||||||||||||||
return sdk.ResourceFunc{ | ||||||||||||||||||||||||
Timeout: 30 * time.Minute, | ||||||||||||||||||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||||||||||||||||||
client := metadata.Client.DataProtection.BackupVaultClient | ||||||||||||||||||||||||
var cmk DataProtectionBackupVaultCustomerManagedKeyModel | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if err := metadata.Decode(&cmk); err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
id, err := backupvaults.ParseBackupVaultID(cmk.DataProtectionBackupVaultID) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
locks.ByID(id.ID()) | ||||||||||||||||||||||||
defer locks.UnlockByID(id.ID()) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
resp, err := client.Get(ctx, *id) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
if response.WasNotFound(resp.HttpResponse) { | ||||||||||||||||||||||||
return fmt.Errorf("%s was not found", *id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return fmt.Errorf("retrieving %s: %+v", *id, err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
model := resp.Model | ||||||||||||||||||||||||
if model == nil { | ||||||||||||||||||||||||
return fmt.Errorf("retrieving %s: `model` is nil", *id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if model.Properties.SecuritySettings != nil && model.Properties.SecuritySettings.EncryptionSettings != nil { | ||||||||||||||||||||||||
return metadata.ResourceRequiresImport(r.ResourceType(), *id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
keyId, err := keyVaultParse.ParseOptionallyVersionedNestedItemID(cmk.KeyVaultKeyID) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
model.Properties.SecuritySettings.EncryptionSettings = &backupvaults.EncryptionSettings{ | ||||||||||||||||||||||||
State: pointer.To(backupvaults.EncryptionStateEnabled), | ||||||||||||||||||||||||
KeyVaultProperties: &backupvaults.CmkKeyVaultProperties{ | ||||||||||||||||||||||||
KeyUri: pointer.To(keyId.ID()), | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
model.Properties.SecuritySettings.EncryptionSettings.KekIdentity = &backupvaults.CmkKekIdentity{ | ||||||||||||||||||||||||
IdentityType: pointer.To(backupvaults.IdentityTypeSystemAssigned), | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
err = client.CreateOrUpdateThenPoll(ctx, *id, *model, backupvaults.DefaultCreateOrUpdateOperationOptions()) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return fmt.Errorf("creating Data Protection Backup Vault Customer Managed Key %s: %+v", *id, err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
metadata.SetID(id) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Read() sdk.ResourceFunc { | ||||||||||||||||||||||||
return sdk.ResourceFunc{ | ||||||||||||||||||||||||
Timeout: 5 * time.Minute, | ||||||||||||||||||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||||||||||||||||||
client := metadata.Client.DataProtection.BackupVaultClient | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
id, err := backupvaults.ParseBackupVaultID(metadata.ResourceData.Id()) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
existing, err := client.Get(ctx, *id) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
if response.WasNotFound(existing.HttpResponse) { | ||||||||||||||||||||||||
return metadata.MarkAsGone(id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return fmt.Errorf("retrieving %s: %+v", *id, err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var state DataProtectionBackupVaultCustomerManagedKeyModel | ||||||||||||||||||||||||
state.DataProtectionBackupVaultID = id.ID() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if model := existing.Model; model != nil { | ||||||||||||||||||||||||
props := model.Properties | ||||||||||||||||||||||||
if props.SecuritySettings != nil && props.SecuritySettings.EncryptionSettings != nil { | ||||||||||||||||||||||||
if props.SecuritySettings.EncryptionSettings.KeyVaultProperties != nil { | ||||||||||||||||||||||||
state.KeyVaultKeyID = pointer.From(props.SecuritySettings.EncryptionSettings.KeyVaultProperties.KeyUri) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return metadata.Encode(&state) | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Delete() sdk.ResourceFunc { | ||||||||||||||||||||||||
return sdk.ResourceFunc{ | ||||||||||||||||||||||||
Timeout: 5 * time.Minute, | ||||||||||||||||||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||||||||||||||||||
log.Printf(`[INFO] Customer Managed Keys cannot be removed from Data Protection Backup Vaults once added. To remove the Customer Managed Key delete and recreate the parent Data Protection Backup Vault`) | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (r DataProtectionBackupVaultCustomerManagedKeyResource) Update() sdk.ResourceFunc { | ||||||||||||||||||||||||
return sdk.ResourceFunc{ | ||||||||||||||||||||||||
Timeout: 30 * time.Minute, | ||||||||||||||||||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||||||||||||||||||
client := metadata.Client.DataProtection.BackupVaultClient | ||||||||||||||||||||||||
var cmk DataProtectionBackupVaultCustomerManagedKeyModel | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if err := metadata.Decode(&cmk); err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
id, err := backupvaults.ParseBackupVaultID(metadata.ResourceData.Id()) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
locks.ByID(id.ID()) | ||||||||||||||||||||||||
defer locks.UnlockByID(id.ID()) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
resp, err := client.Get(ctx, *id) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
if response.WasNotFound(resp.HttpResponse) { | ||||||||||||||||||||||||
return fmt.Errorf("%s was not found", id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
model := resp.Model | ||||||||||||||||||||||||
if model == nil { | ||||||||||||||||||||||||
return fmt.Errorf("retrieving %s: `model` is nil", *id) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please update this so it's consistent with how we do these nil checks in other update methods
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
if metadata.ResourceData.HasChange("key_vault_key_id") { | ||||||||||||||||||||||||
keyId, err := keyVaultParse.ParseOptionallyVersionedNestedItemID(cmk.KeyVaultKeyID) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
model.Properties.SecuritySettings.EncryptionSettings.KeyVaultProperties = &backupvaults.CmkKeyVaultProperties{ | ||||||||||||||||||||||||
KeyUri: pointer.To(keyId.ID()), | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
err = client.CreateOrUpdateThenPoll(ctx, *id, *model, backupvaults.DefaultCreateOrUpdateOperationOptions()) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return fmt.Errorf("updating Data Protection Backup Vault Customer Managed Key for %s: %+v", *id, err) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Printing the ID will already contain the resource name, rephrasing this will remove the redundant information