Skip to content

Commit

Permalink
Handle conflicts b/w data protection at cluster and cluster-group level
Browse files Browse the repository at this point in the history
  • Loading branch information
asagarwal committed Jan 15, 2024
1 parent d2a42b7 commit f9b83f4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
10 changes: 8 additions & 2 deletions docs/guides/tanzu-mission-control_data_protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Description: |-
---
# Enable Data Protection

The `tanzu-mission-control_enable_data_protection` resource enables users to activate and set up data protection for a Tanzu Kubernetes Cluster.
The `tanzu-mission-control_enable_data_protection` resource enables users to activate and set up data protection for a Tanzu Kubernetes Cluster/ClusterGroup.
Once enabled, users can create instant backups or schedule backups for later.

**NOTE :** To resolve cluster and cluster group data protection conflicts use the below command
``terraform refresh``

For more information regarding data protection, see [Data Protection][data-protection].

[data-protection]: https://docs.vmware.com/en/VMware-Tanzu-Mission-Control/services/tanzumc-concepts/GUID-C16557BC-EB1B-4414-8E63-28AD92E0CAE5.html
Expand All @@ -26,7 +29,10 @@ For more information regarding target location, see [Target Location][target-loc

# Backup Schedule

The `tanzu-mission-control_backup_schedule` resource enables users to create and configure scheduled backups in a cluster.
The `tanzu-mission-control_backup_schedule` resource enables users to create and configure scheduled backups in a cluster/cluster-group.

NOTE : To resolve cluster and cluster group backup schedule conflicts use the below command
``terraform refresh``

Backups can be applied in 3 levels:

Expand Down
4 changes: 4 additions & 0 deletions docs/resources/backup_schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Description: |-
# Backup Schedule Resource

This resource enables users to create and configure scheduled backups in a cluster or cluster group level.

**NOTE :** To resolve cluster and cluster group backup schedule conflicts use the below command
``terraform refresh``

Backups can be applied in 3 levels:

* Entire Cluster
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/enable_data_protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ resource "tanzu-mission-control_enable_data_protection" "cgdemo" {
}
```

**NOTE :** To resolve cluster and cluster group data protection conflicts use the below command
``terraform refresh``

## Import Cluster Scope Data Protection
The resource ID for importing an existing data protection should be comprised of a full cluster name separated by '/'.

Expand Down
22 changes: 11 additions & 11 deletions internal/resources/backupschedule/resource_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ func resourceBackupScheduleRead(ctx context.Context, data *schema.ResourceData,

resp, err := readResourceWait(ctx, &config, backupScheduleFn)

// check if managed at cluster group level then remove from state
if resp != nil && resp.Schedule != nil && resp.Schedule.Meta != nil {
if _, ok := resp.Schedule.Meta.Annotations[commonscope.BatchUIDAnnotationKey]; ok {
_ = schema.RemoveFromState(data, m)
return diags
}
}

if err != nil {
if clienterrors.IsNotFoundError(err) {
if !helper.IsContextCallerSet(ctx) {
*data = schema.ResourceData{}

return diags
} else if helper.IsDeleteState(ctx) {
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
_ = schema.RemoveFromState(data, m)

return diags
}
// resource not found in backend then delete the state
_ = schema.RemoveFromState(data, m)
return diags
}

return diag.FromErr(errors.Wrapf(err, "Couldn't read backup schedule.\nManagement Cluster Name: %s, Provisioner Name: %s, Cluster Name: %s, Schedule Name: %s",
Expand Down
1 change: 1 addition & 0 deletions internal/resources/common/scope/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ClusterKey = "cluster"
ClusterGroupKey = "cluster_group"
ClusterGroupNameKey = "cluster_group_name"
BatchUIDAnnotationKey = "tmc.cloud.vmware.com/source-uid"
)

// Scopes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster/dataprotection"
dataprotectioncgmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/clustergroup/dataprotection"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection/scope"
)

Expand Down Expand Up @@ -64,6 +66,16 @@ func resourceEnableDataProtectionRead(ctx context.Context, data *schema.Resource
config := m.(authctx.TanzuContext)

err := populateDataFromServer(ctx, config, scopedFullnameData, data)

if scopedFullnameData.Scope == scope.ClusterScope {
metaData := data.Get(common.MetaKey).([]interface{})[0].(map[string]interface{})
annotations := metaData[common.AnnotationsKey].(map[string]interface{})

if _, ok := annotations[commonscope.BatchUIDAnnotationKey]; ok {
_ = schema.RemoveFromState(data, m)
}
}

if err != nil {
if clienterrors.IsNotFoundError(err) {
if !helper.IsContextCallerSet(ctx) {
Expand Down

0 comments on commit f9b83f4

Please sign in to comment.