-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdata_source_helm_feature.go
162 lines (132 loc) · 6.11 KB
/
data_source_helm_feature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package helmfeature
import (
"context"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"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"
helmclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/helmfeature/cluster"
helmclustergroupmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/helmfeature/clustergroup"
objectmetamodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/objectmeta"
"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/helmfeature/scope"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/helmfeature/status"
)
func DataSourceHelm() *schema.Resource {
return &schema.Resource{
Schema: helmSchema,
ReadContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return dataSourceHelmRead(helper.GetContextWithCaller(ctx, helper.DataRead), d, m)
},
}
}
type dataFromServer struct {
UID string
meta *objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta
clusterScopeStatus *helmclustermodel.VmwareTanzuManageV1alpha1ClusterFluxcdHelmStatus
clusterGroupScopeStatus *helmclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupFluxcdHelmStatus
}
func dataSourceHelmRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
config := m.(authctx.TanzuContext)
scopedFullnameData := scope.ConstructScope(d)
if scopedFullnameData == nil {
return diag.Errorf("Unable to get Tanzu Mission Control helm feature entry; Scope full name is empty")
}
helmDataFromServer, err := retrieveHelmFeatureDataFromServer(config, scopedFullnameData, d)
if err != nil {
if clienterrors.IsNotFoundError(err) && !helper.IsDataRead(ctx) {
_ = schema.RemoveFromState(d, m)
return diags
}
return diag.FromErr(err)
}
// always run
d.SetId(helmDataFromServer.UID)
if err := d.Set(common.MetaKey, common.FlattenMeta(helmDataFromServer.meta)); err != nil {
return diag.FromErr(err)
}
var (
flattenedStatus interface{}
)
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
flattenedStatus = status.FlattenStatusForClusterScope(helmDataFromServer.clusterScopeStatus)
case commonscope.ClusterGroupScope:
flattenedStatus = status.FlattenStatusForClusterGroupScope(helmDataFromServer.clusterGroupScopeStatus)
}
if err := d.Set(statusKey, flattenedStatus); err != nil {
return diag.FromErr(err)
}
return diags
}
func retrieveHelmFeatureDataFromServer(config authctx.TanzuContext, scopedFullnameData *scope.ScopedFullname, d *schema.ResourceData) (*dataFromServer, error) {
var helmDataFromServer = &dataFromServer{}
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
if scopedFullnameData.FullnameCluster != nil {
resp, err := config.TMCConnection.ClusterHelmResourceService.VmwareTanzuManageV1alpha1ClusterHelmResourceServiceList(
&helmclustermodel.VmwareTanzuManageV1alpha1ClusterFluxcdHelmListHelmRequestParameters{
SearchScope: &helmclustermodel.VmwareTanzuManageV1alpha1ClusterFluxcdHelmSearchScope{
ClusterName: scopedFullnameData.FullnameCluster.ClusterName,
ManagementClusterName: scopedFullnameData.FullnameCluster.ManagementClusterName,
ProvisionerName: scopedFullnameData.FullnameCluster.ProvisionerName,
},
},
)
if err != nil {
if clienterrors.IsNotFoundError(err) {
d.SetId("")
return helmDataFromServer, err
}
return helmDataFromServer, errors.Wrap(err, "Unable to get Tanzu Mission Control cluster helm feature entry")
}
if len(resp.Helms) == 0 {
return helmDataFromServer, errors.New("Tanzu Mission Control no helm feature entry found")
}
helmRes := resp.Helms[0]
scopedFullnameData.FullnameCluster = helmRes.FullName
helmDataFromServer.UID = helmRes.Meta.UID
helmDataFromServer.meta = helmRes.Meta
helmDataFromServer.clusterScopeStatus = helmRes.Status
}
case commonscope.ClusterGroupScope:
if scopedFullnameData.FullnameClusterGroup != nil {
resp, err := config.TMCConnection.ClusterGroupHelmResourceService.VmwareTanzuManageV1alpha1ClustergroupHelmResourceServiceList(
&helmclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupHelmListHelmRequestParameters{
SearchScope: &helmclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupFluxcdHelmSearchScope{
ClusterGroupName: scopedFullnameData.FullnameClusterGroup.ClusterGroupName,
},
},
)
if err != nil {
if clienterrors.IsNotFoundError(err) {
d.SetId("")
return helmDataFromServer, err
}
return helmDataFromServer, errors.Wrap(err, "Unable to get Tanzu Mission Control cluster helm feature entry")
}
if len(resp.Helms) == 0 {
return helmDataFromServer, errors.New("Tanzu Mission Control no helm feature entry found")
}
helmRes := resp.Helms[0]
scopedFullnameData.FullnameClusterGroup = helmRes.FullName
helmDataFromServer.UID = helmRes.Meta.UID
helmDataFromServer.meta = helmRes.Meta
helmDataFromServer.clusterGroupScopeStatus = helmRes.Status
}
case commonscope.UnknownScope:
return helmDataFromServer, errors.Errorf("no valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(scope.ScopesAllowed[:], `, `))
}
fullName := scope.FlattenScope(scopedFullnameData)
if err := d.Set(commonscope.ScopeKey, fullName); err != nil {
return helmDataFromServer, err
}
return helmDataFromServer, nil
}