-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhelm_repository_data_source.go
171 lines (137 loc) · 6.17 KB
/
helm_repository_data_source.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
163
164
165
166
167
168
169
170
171
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package helmrepository
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"
repositoryclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/helmrepository"
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"
helmscope "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/helmrepository/scope"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/helmrepository/spec"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/helmrepository/status"
)
const (
ResourceName = "tanzu-mission-control_helm_repository"
nameKey = "name"
namespaceNameKey = "namespace_name"
statusKey = "status"
)
type dataFromServer struct {
UID string
meta *objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta
clusterScopeStatus *repositoryclustermodel.VmwareTanzuManageV1alpha1ClusterNamespaceFluxcdHelmRepositoryStatus
atomicSpec *repositoryclustermodel.VmwareTanzuManageV1alpha1ClusterNamespaceFluxcdHelmRepositorySpec
}
func DataSourceHelmRepository() *schema.Resource {
return &schema.Resource{
Schema: helmSchema,
ReadContext: dataSourceHelmRepositoryRead,
}
}
var helmSchema = map[string]*schema.Schema{
nameKey: {
Type: schema.TypeString,
Description: "Name of the helm repository.",
Optional: true,
},
namespaceNameKey: {
Type: schema.TypeString,
Description: "Name of Namespace.",
Optional: true,
Default: "*",
},
commonscope.ScopeKey: helmscope.ScopeSchema,
common.MetaKey: common.Meta,
spec.SpecKey: spec.SpecSchema,
statusKey: status.StatusSchema,
}
func dataSourceHelmRepositoryRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
config := m.(authctx.TanzuContext)
namespaceName, ok := d.Get(namespaceNameKey).(string)
if !ok {
return diag.Errorf("Unable to read repository namespace name")
}
scopedFullnameData := helmscope.ConstructScope(d)
if scopedFullnameData == nil {
return diag.Errorf("Unable to get Tanzu Mission Control helm repository entry; Scope full name is empty")
}
scopedFullnameData.FullnameCluster.NamespaceName = namespaceName
helmRepoDataFromServer, err := retrieveHelmRepositoryDataFromServer(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(helmRepoDataFromServer.UID)
if err := d.Set(common.MetaKey, common.FlattenMeta(helmRepoDataFromServer.meta)); err != nil {
return diag.FromErr(err)
}
var (
flattenedSpec []interface{}
flattenedStatus interface{}
)
flattenedSpec = spec.FlattenSpecForClusterScope(helmRepoDataFromServer.atomicSpec)
flattenedStatus = status.FlattenStatusForClusterScope(helmRepoDataFromServer.clusterScopeStatus)
if err := d.Set(spec.SpecKey, flattenedSpec); err != nil {
return diag.FromErr(err)
}
if err := d.Set(statusKey, flattenedStatus); err != nil {
return diag.FromErr(err)
}
return diags
}
func retrieveHelmRepositoryDataFromServer(config authctx.TanzuContext, scopedFullnameData *helmscope.ScopedFullname, d *schema.ResourceData) (*dataFromServer, error) {
var helmRepoDataFromServer = &dataFromServer{}
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
if scopedFullnameData.FullnameCluster != nil {
resp, err := config.TMCConnection.ClusterHelmRepositoryResourceService.VmwareTanzuManageV1alpha1ClusterFluxcdHelmRepositoryResourceServiceList(&repositoryclustermodel.VmwareTanzuManageV1alpha1ClusterNamespaceFluxcdHelmRepositorySearchScope{
ClusterName: scopedFullnameData.FullnameCluster.ClusterName,
ManagementClusterName: scopedFullnameData.FullnameCluster.ManagementClusterName,
ProvisionerName: scopedFullnameData.FullnameCluster.ProvisionerName,
NamespaceName: scopedFullnameData.FullnameCluster.NamespaceName,
})
if err != nil {
if clienterrors.IsNotFoundError(err) {
d.SetId("")
return helmRepoDataFromServer, err
}
return helmRepoDataFromServer, errors.Wrapf(err, "Unable to get Tanzu Mission Control Helm Repository entry for cluster, name : %s", scopedFullnameData.FullnameCluster.ClusterName)
}
if len(resp.Repositories) == 0 {
return helmRepoDataFromServer, errors.Errorf("No entry found for Tanzu Mission Control Helm Repository for cluster, name : %s", scopedFullnameData.FullnameCluster.ClusterName)
}
repo := resp.Repositories[0]
scopedFullnameData.FullnameCluster = repo.FullName
helmRepoDataFromServer.UID = repo.Meta.UID
helmRepoDataFromServer.meta = repo.Meta
helmRepoDataFromServer.atomicSpec = repo.Spec
helmRepoDataFromServer.clusterScopeStatus = repo.Status
}
case commonscope.UnknownScope:
return helmRepoDataFromServer, errors.Errorf("no valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(helmscope.ScopesAllowed[:], `, `))
}
fullName, name, namespace := helmscope.FlattenScope(scopedFullnameData)
if err := d.Set(nameKey, name); err != nil {
return helmRepoDataFromServer, err
}
if err := d.Set(namespaceNameKey, namespace); err != nil {
return helmRepoDataFromServer, err
}
if err := d.Set(commonscope.ScopeKey, fullName); err != nil {
return helmRepoDataFromServer, err
}
return helmRepoDataFromServer, nil
}