diff --git a/internal/client/cluster/dataprotection/dataprotection_resource.go b/internal/client/cluster/dataprotection/dataprotection_resource.go index e2859229e..7b1b7c066 100644 --- a/internal/client/cluster/dataprotection/dataprotection_resource.go +++ b/internal/client/cluster/dataprotection/dataprotection_resource.go @@ -11,7 +11,7 @@ import ( "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/transport" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" - dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster/dataprotection" + dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" ) const ( diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 260b3e19e..1297e43ad 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -7,12 +7,12 @@ package provider import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + dataprotection2 "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/akscluster" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/backupschedule" - "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/integration" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/nodepools" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/clustergroup" @@ -78,7 +78,7 @@ func Provider() *schema.Provider { helmrelease.ResourceName: helmrelease.ResourceHelmRelease(), helmfeature.ResourceName: helmfeature.ResourceHelm(), backupschedule.ResourceName: backupschedule.ResourceBackupSchedule(), - dataprotection.ResourceName: dataprotection.ResourceEnableDataProtection(), + dataprotection2.ResourceName: dataprotection2.ResourceEnableDataProtection(), targetlocation.ResourceName: targetlocation.ResourceTargetLocation(), }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/internal/resources/cluster/backupschedule/tests/helper_test.go b/internal/resources/cluster/backupschedule/tests/helper_test.go index ad10cb468..02fd70a3e 100644 --- a/internal/resources/cluster/backupschedule/tests/helper_test.go +++ b/internal/resources/cluster/backupschedule/tests/helper_test.go @@ -9,6 +9,7 @@ SPDX-License-Identifier: MPL-2.0 package backupscheduletests import ( + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -17,7 +18,6 @@ import ( "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" backupscheduleres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/backupschedule" - dataprotectionres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" targetlocationres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/targetlocation" ) @@ -36,7 +36,7 @@ func initTestProvider(t *testing.T) *schema.Provider { ResourcesMap: map[string]*schema.Resource{ cluster.ResourceName: cluster.ResourceTMCCluster(), targetlocationres.ResourceName: targetlocationres.ResourceTargetLocation(), - dataprotectionres.ResourceName: dataprotectionres.ResourceEnableDataProtection(), + dataprotection.ResourceName: dataprotection.ResourceEnableDataProtection(), backupscheduleres.ResourceName: backupscheduleres.ResourceBackupSchedule(), }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/internal/resources/cluster/backupschedule/tests/resource_tf_configs.go b/internal/resources/cluster/backupschedule/tests/resource_tf_configs.go index e82c6dd60..3066abce1 100644 --- a/internal/resources/cluster/backupschedule/tests/resource_tf_configs.go +++ b/internal/resources/cluster/backupschedule/tests/resource_tf_configs.go @@ -7,11 +7,11 @@ package backupscheduletests import ( "fmt" + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection/tests" "strings" clusterres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" backupscheduleres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/backupschedule" - dataprotectiontests "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection/tests" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" targetlocationres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/targetlocation" targetlocationtests "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/targetlocation/tests" diff --git a/internal/resources/cluster/dataprotection/converter_mapping.go b/internal/resources/dataprotection/converter_mapping.go similarity index 95% rename from internal/resources/cluster/dataprotection/converter_mapping.go rename to internal/resources/dataprotection/converter_mapping.go index b88d10272..b5bad80c3 100644 --- a/internal/resources/cluster/dataprotection/converter_mapping.go +++ b/internal/resources/dataprotection/converter_mapping.go @@ -7,7 +7,7 @@ package dataprotection import ( tfModelConverterHelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper/converter" - dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster/dataprotection" + dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common" ) diff --git a/internal/resources/cluster/dataprotection/resource_enable_data_protection.go b/internal/resources/dataprotection/resource_enable_data_protection.go similarity index 91% rename from internal/resources/cluster/dataprotection/resource_enable_data_protection.go rename to internal/resources/dataprotection/resource_enable_data_protection.go index 00cc1d896..36f2c380e 100644 --- a/internal/resources/cluster/dataprotection/resource_enable_data_protection.go +++ b/internal/resources/dataprotection/resource_enable_data_protection.go @@ -7,6 +7,9 @@ package dataprotection import ( "context" + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common" + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" + dataprotectionscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection/scope" "strings" "time" @@ -17,7 +20,7 @@ import ( "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" clienterrors "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/errors" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" - dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster/dataprotection" + dataprotectionmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" ) func ResourceEnableDataProtection() *schema.Resource { @@ -35,10 +38,28 @@ func ResourceEnableDataProtection() *schema.Resource { func resourceEnableDataProtectionCreate(ctx context.Context, data *schema.ResourceData, m interface{}) (diags diag.Diagnostics) { config := m.(authctx.TanzuContext) - model, err := tfModelConverter.ConvertTFSchemaToAPIModel(data, []string{}) + /*model, err := tfModelConverter.ConvertTFSchemaToAPIModel(data, []string{}) if err != nil { return diag.FromErr(errors.Wrapf(err, "Couldn't create Tanzu Mission Control data protection configurations.")) + }*/ + + scopedFullNameData := dataprotectionscope.ConstructScope(data) + + if scopedFullNameData == nil { + return diag.Errorf("Unable to enable Tanzu Mission Control Data Protection; Scope full name is empty") + } + + var ( + UID string + meta = common.ConstructMeta(data) + ) + + switch scopedFullNameData.Scope { + case commonscope.ClusterScope: + if scopedFullNameData.FullnameCluster != nil { + specVal, err := + } } request := &dataprotectionmodels.VmwareTanzuManageV1alpha1ClusterDataprotectionCreateDataProtectionRequest{ diff --git a/internal/resources/cluster/dataprotection/schema.go b/internal/resources/dataprotection/schema.go similarity index 91% rename from internal/resources/cluster/dataprotection/schema.go rename to internal/resources/dataprotection/schema.go index a21db9902..7cb7e9f09 100644 --- a/internal/resources/cluster/dataprotection/schema.go +++ b/internal/resources/dataprotection/schema.go @@ -7,8 +7,9 @@ package dataprotection import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common" + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" + dataprotectionscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection/scope" ) const ( @@ -37,6 +38,7 @@ var enableDataProtectionSchema = map[string]*schema.Schema{ SpecKey: specSchema, common.MetaKey: common.Meta, DeletionPolicyKey: deletionPolicySchema, + commonscope.ScopeKey: dataprotectionscope.ScopeSchema, } var clusterNameSchema = &schema.Schema{ diff --git a/internal/resources/dataprotection/scope/cluster.go b/internal/resources/dataprotection/scope/cluster.go new file mode 100644 index 000000000..6d2ad0b58 --- /dev/null +++ b/internal/resources/dataprotection/scope/cluster.go @@ -0,0 +1,50 @@ +/* +Copyright © 2023 VMware, Inc. All Rights Reserved. +SPDX-License-Identifier: MPL-2.0 +*/ + +package dataprotectionscope + +import ( + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" + dataprotectionclustermodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" +) + +func ConstructDataProtectionClusterFullname(data []interface{}) (fullname *dataprotectionclustermodels.VmwareTanzuManageV1alpha1ClusterDataprotectionFullName) { + if len(data) == 0 || data[0] == nil { + return fullname + } + + fullNameData, _ := data[0].(map[string]interface{}) + + fullname = &dataprotectionclustermodels.VmwareTanzuManageV1alpha1ClusterDataprotectionFullName{} + + if managementClusterNameValue, ok := fullNameData[commonscope.ManagementClusterNameKey]; ok { + helper.SetPrimitiveValue(managementClusterNameValue, &fullname.ManagementClusterName, commonscope.ManagementClusterNameKey) + } + + if provisionerNameValue, ok := fullNameData[commonscope.ProvisionerNameKey]; ok { + helper.SetPrimitiveValue(provisionerNameValue, &fullname.ProvisionerName, commonscope.ProvisionerNameKey) + } + + if nameValue, ok := fullNameData[commonscope.NameKey]; ok { + helper.SetPrimitiveValue(nameValue, &fullname.ClusterName, commonscope.NameKey) + } + + return fullname +} + +func FlattenDataProtectionClusterFullname(fullname *dataprotectionclustermodels.VmwareTanzuManageV1alpha1ClusterDataprotectionFullName) (data []interface{}) { + if fullname == nil { + return data + } + + flattenFullname := make(map[string]interface{}) + + flattenFullname[commonscope.ManagementClusterNameKey] = fullname.ManagementClusterName + flattenFullname[commonscope.ProvisionerNameKey] = fullname.ProvisionerName + flattenFullname[commonscope.NameKey] = fullname.ClusterName + + return []interface{}{flattenFullname} +} diff --git a/internal/resources/dataprotection/scope/cluster_group.go b/internal/resources/dataprotection/scope/cluster_group.go new file mode 100644 index 000000000..52c9d9af2 --- /dev/null +++ b/internal/resources/dataprotection/scope/cluster_group.go @@ -0,0 +1,40 @@ +/* +Copyright © 2023 VMware, Inc. All Rights Reserved. +SPDX-License-Identifier: MPL-2.0 +*/ + +package dataprotectionscope + +import ( + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" + dataprotectionclustergroupmodels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/clustergroup/dataprotection" + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" +) + +func ConstructDataProtectionClusterGroupFullname(data []interface{}) (fullname *dataprotectionclustergroupmodels.VmwareTanzuManageV1alpha1ClusterGroupDataprotectionFullName) { + if len(data) == 0 || data[0] == nil { + return fullname + } + + fullNameData, _ := data[0].(map[string]interface{}) + + fullname = &dataprotectionclustergroupmodels.VmwareTanzuManageV1alpha1ClusterGroupDataprotectionFullName{} + + if nameValue, ok := fullNameData[commonscope.NameKey]; ok { + helper.SetPrimitiveValue(nameValue, &fullname.ClusterGroupName, commonscope.NameKey) + } + + return fullname +} + +func FlattenDataProtectionClusterGroupFullname(fullname *dataprotectionclustergroupmodels.VmwareTanzuManageV1alpha1ClusterGroupDataprotectionFullName) (data []interface{}) { + if fullname == nil { + return data + } + + flattenFullname := make(map[string]interface{}) + + flattenFullname[commonscope.NameKey] = fullname.ClusterGroupName + + return []interface{}{flattenFullname} +} diff --git a/internal/resources/dataprotection/scope/scope.go b/internal/resources/dataprotection/scope/scope.go new file mode 100644 index 000000000..74e757ca8 --- /dev/null +++ b/internal/resources/dataprotection/scope/scope.go @@ -0,0 +1,68 @@ +/* +Copyright © 2023 VMware, Inc. All Rights Reserved. +SPDX-License-Identifier: MPL-2.0 +*/ + +package dataprotectionscope + +import ( + "fmt" + "golang.org/x/exp/slices" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + dataprotectionclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" + dataprotectionclustergroupmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/clustergroup/dataprotection" + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" +) + +// ScopedFullname is a struct for all types of helm release full names. +type ScopedFullname struct { + Scope commonscope.Scope + FullnameCluster *dataprotectionclustermodel.VmwareTanzuManageV1alpha1ClusterDataprotectionFullName + FullnameClusterGroup *dataprotectionclustergroupmodel.VmwareTanzuManageV1alpha1ClusterGroupDataprotectionFullName +} + +var ( + ScopesAllowed = [...]string{commonscope.ClusterKey, commonscope.ClusterGroupKey} + ScopeSchema = commonscope.GetScopeSchema( + commonscope.WithDescription(fmt.Sprintf("Scope for the Data Protection, having one of the valid scopes: %v.", strings.Join(ScopesAllowed[:], `, `))), + commonscope.WithScopes(ScopesAllowed[:])) +) + +func ConstructScope(d *schema.ResourceData) (scopedFullnameData *ScopedFullname) { + value, ok := d.GetOk(commonscope.ScopeKey) + + if !ok { + return scopedFullnameData + } + + data, _ := value.([]interface{}) + + if len(data) == 0 || data[0] == nil { + return scopedFullnameData + } + + scopeData := data[0].(map[string]interface{}) + + if clusterData, ok := scopeData[commonscope.ClusterKey]; ok && slices.Contains(ScopesAllowed[:], commonscope.ClusterKey) { + if clusterValue, ok := clusterData.([]interface{}); ok && len(clusterValue) != 0 { + scopedFullnameData = &ScopedFullname{ + Scope: commonscope.ClusterScope, + FullnameCluster: ConstructDataProtectionClusterFullname(clusterValue), + } + } + } + + if clusterGroupData, ok := scopeData[commonscope.ClusterGroupKey]; ok && slices.Contains(ScopesAllowed[:], commonscope.ClusterGroupKey) { + if clusterGroupValue, ok := clusterGroupData.([]interface{}); ok && len(clusterGroupValue) != 0 { + scopedFullnameData = &ScopedFullname{ + Scope: commonscope.ClusterGroupScope, + FullnameClusterGroup: ConstructDataProtectionClusterGroupFullname(clusterGroupValue), + } + } + } + + return scopedFullnameData +} diff --git a/internal/resources/cluster/dataprotection/tests/enable_data_protection_test.go b/internal/resources/dataprotection/tests/enable_data_protection_test.go similarity index 100% rename from internal/resources/cluster/dataprotection/tests/enable_data_protection_test.go rename to internal/resources/dataprotection/tests/enable_data_protection_test.go diff --git a/internal/resources/cluster/dataprotection/tests/helper_test.go b/internal/resources/dataprotection/tests/helper_test.go similarity index 78% rename from internal/resources/cluster/dataprotection/tests/helper_test.go rename to internal/resources/dataprotection/tests/helper_test.go index 82912f81c..032e88916 100644 --- a/internal/resources/cluster/dataprotection/tests/helper_test.go +++ b/internal/resources/dataprotection/tests/helper_test.go @@ -9,6 +9,7 @@ SPDX-License-Identifier: MPL-2.0 package dataprotectiontests import ( + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -16,7 +17,6 @@ import ( "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" - dataprotectionres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" ) @@ -28,8 +28,8 @@ func initTestProvider(t *testing.T) *schema.Provider { testAccProvider := &schema.Provider{ Schema: authctx.ProviderAuthSchema(), ResourcesMap: map[string]*schema.Resource{ - dataprotectionres.ResourceName: dataprotectionres.ResourceEnableDataProtection(), - cluster.ResourceName: cluster.ResourceTMCCluster(), + dataprotection.ResourceName: dataprotection.ResourceEnableDataProtection(), + cluster.ResourceName: cluster.ResourceTMCCluster(), }, ConfigureContextFunc: authctx.ProviderConfigureContext, } diff --git a/internal/resources/cluster/dataprotection/tests/resource_tf_configs.go b/internal/resources/dataprotection/tests/resource_tf_configs.go similarity index 97% rename from internal/resources/cluster/dataprotection/tests/resource_tf_configs.go rename to internal/resources/dataprotection/tests/resource_tf_configs.go index 7a36997f3..5d8f86501 100644 --- a/internal/resources/cluster/dataprotection/tests/resource_tf_configs.go +++ b/internal/resources/dataprotection/tests/resource_tf_configs.go @@ -7,10 +7,10 @@ package dataprotectiontests import ( "fmt" + dataprotectionres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection" "strings" clusterres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" - dataprotectionres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" ) diff --git a/internal/resources/targetlocation/tests/datasource_tf_configs.go b/internal/resources/targetlocation/tests/datasource_tf_configs.go index 232d5a5a4..4b40c00a3 100644 --- a/internal/resources/targetlocation/tests/datasource_tf_configs.go +++ b/internal/resources/targetlocation/tests/datasource_tf_configs.go @@ -7,8 +7,8 @@ package targetlocationtests import ( "fmt" + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection/tests" - dataprotectiontests "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection/tests" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" targetlocationres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/targetlocation" ) diff --git a/internal/resources/targetlocation/tests/helper_test.go b/internal/resources/targetlocation/tests/helper_test.go index 4d54cb4e4..0b3a2ef12 100644 --- a/internal/resources/targetlocation/tests/helper_test.go +++ b/internal/resources/targetlocation/tests/helper_test.go @@ -9,6 +9,7 @@ SPDX-License-Identifier: MPL-2.0 package targetlocationtests import ( + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/dataprotection" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -16,7 +17,6 @@ import ( "github.com/vmware/terraform-provider-tanzu-mission-control/internal/authctx" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster" - dataprotectionres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster/dataprotection" "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/clustergroup" commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" credentialres "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/credential" @@ -37,7 +37,7 @@ func initTestProvider(t *testing.T) *schema.Provider { Schema: authctx.ProviderAuthSchema(), ResourcesMap: map[string]*schema.Resource{ targetlocationres.ResourceName: targetlocationres.ResourceTargetLocation(), - dataprotectionres.ResourceName: dataprotectionres.ResourceEnableDataProtection(), + dataprotection.ResourceName: dataprotection.ResourceEnableDataProtection(), cluster.ResourceName: cluster.ResourceTMCCluster(), clustergroup.ResourceName: clustergroup.ResourceClusterGroup(), credentialres.ResourceName: credentialres.ResourceCredential(),