|
| 1 | +/* |
| 2 | +Copyright © 2023 VMware, Inc. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MPL-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package dataprotectionscope |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "golang.org/x/exp/slices" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 14 | + |
| 15 | + dataprotectionclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/cluster/dataprotection" |
| 16 | + dataprotectionclustergroupmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/dataprotection/clustergroup/dataprotection" |
| 17 | + commonscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common/scope" |
| 18 | +) |
| 19 | + |
| 20 | +// ScopedFullname is a struct for all types of helm release full names. |
| 21 | +type ScopedFullname struct { |
| 22 | + Scope commonscope.Scope |
| 23 | + FullnameCluster *dataprotectionclustermodel.VmwareTanzuManageV1alpha1ClusterDataprotectionFullName |
| 24 | + FullnameClusterGroup *dataprotectionclustergroupmodel.VmwareTanzuManageV1alpha1ClusterGroupDataprotectionFullName |
| 25 | +} |
| 26 | + |
| 27 | +var ( |
| 28 | + ScopesAllowed = [...]string{commonscope.ClusterKey, commonscope.ClusterGroupKey} |
| 29 | + ScopeSchema = commonscope.GetScopeSchema( |
| 30 | + commonscope.WithDescription(fmt.Sprintf("Scope for the Data Protection, having one of the valid scopes: %v.", strings.Join(ScopesAllowed[:], `, `))), |
| 31 | + commonscope.WithScopes(ScopesAllowed[:])) |
| 32 | +) |
| 33 | + |
| 34 | +func ConstructScope(d *schema.ResourceData) (scopedFullnameData *ScopedFullname) { |
| 35 | + value, ok := d.GetOk(commonscope.ScopeKey) |
| 36 | + |
| 37 | + if !ok { |
| 38 | + return scopedFullnameData |
| 39 | + } |
| 40 | + |
| 41 | + data, _ := value.([]interface{}) |
| 42 | + |
| 43 | + if len(data) == 0 || data[0] == nil { |
| 44 | + return scopedFullnameData |
| 45 | + } |
| 46 | + |
| 47 | + scopeData := data[0].(map[string]interface{}) |
| 48 | + |
| 49 | + if clusterData, ok := scopeData[commonscope.ClusterKey]; ok && slices.Contains(ScopesAllowed[:], commonscope.ClusterKey) { |
| 50 | + if clusterValue, ok := clusterData.([]interface{}); ok && len(clusterValue) != 0 { |
| 51 | + scopedFullnameData = &ScopedFullname{ |
| 52 | + Scope: commonscope.ClusterScope, |
| 53 | + FullnameCluster: ConstructDataProtectionClusterFullname(clusterValue), |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if clusterGroupData, ok := scopeData[commonscope.ClusterGroupKey]; ok && slices.Contains(ScopesAllowed[:], commonscope.ClusterGroupKey) { |
| 59 | + if clusterGroupValue, ok := clusterGroupData.([]interface{}); ok && len(clusterGroupValue) != 0 { |
| 60 | + scopedFullnameData = &ScopedFullname{ |
| 61 | + Scope: commonscope.ClusterGroupScope, |
| 62 | + FullnameClusterGroup: ConstructDataProtectionClusterGroupFullname(clusterGroupValue), |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return scopedFullnameData |
| 68 | +} |
0 commit comments