-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdata_source_git_repository.go
168 lines (137 loc) · 7.04 KB
/
data_source_git_repository.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
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package gitrepository
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"
gitrepositoryclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/gitrepository/cluster"
gitrepositoryclustergroupmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/gitrepository/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/gitrepository/scope"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/gitrepository/spec"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/gitrepository/status"
)
type dataFromServer struct {
UID string
meta *objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta
atomicSpec *gitrepositoryclustermodel.VmwareTanzuManageV1alpha1ClusterNamespaceFluxcdGitrepositorySpec
clusterScopeStatus *gitrepositoryclustermodel.VmwareTanzuManageV1alpha1ClusterNamespaceFluxcdGitrepositoryStatus
clusterGroupScopeStatus *gitrepositoryclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupNamespaceFluxcdGitrepositoryStatus
}
func DataSourceGitRepository() *schema.Resource {
return &schema.Resource{
Schema: getGitRepositorySchema(true),
ReadContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return dataSourceGitRepositoryRead(helper.GetContextWithCaller(ctx, helper.DataRead), d, m)
},
}
}
func dataSourceGitRepositoryRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
config := m.(authctx.TanzuContext)
gitRepositoryName, ok := d.Get(nameKey).(string)
if !ok {
return diag.Errorf("Unable to read git repository name")
}
gitRepositoryNamespaceName, ok := d.Get(namespaceNameKey).(string)
if !ok {
return diag.Errorf("Unable to read git repository namespace name")
}
scopedFullnameData := scope.ConstructScope(d, gitRepositoryName, gitRepositoryNamespaceName)
if scopedFullnameData == nil {
return diag.Errorf("Unable to get Tanzu Mission Control git repository entry; Scope full name is empty")
}
gitRepositoryDataFromServer, err := retrieveGitRepositoryDataFromServer(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(gitRepositoryDataFromServer.UID)
if err := d.Set(common.MetaKey, common.FlattenMeta(gitRepositoryDataFromServer.meta)); err != nil {
return diag.FromErr(err)
}
var (
flattenedSpec []interface{}
flattenedStatus interface{}
)
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
flattenedSpec = spec.FlattenSpecForClusterScope(gitRepositoryDataFromServer.atomicSpec)
flattenedStatus = status.FlattenStatusForClusterScope(gitRepositoryDataFromServer.clusterScopeStatus)
case commonscope.ClusterGroupScope:
clusterGroupScopeSpec := &gitrepositoryclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupNamespaceFluxcdGitrepositorySpec{
AtomicSpec: gitRepositoryDataFromServer.atomicSpec,
}
flattenedSpec = spec.FlattenSpecForClusterGroupScope(clusterGroupScopeSpec)
flattenedStatus = status.FlattenStatusForClusterGroupScope(gitRepositoryDataFromServer.clusterGroupScopeStatus)
}
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 retrieveGitRepositoryDataFromServer(config authctx.TanzuContext, scopedFullnameData *scope.ScopedFullname, d *schema.ResourceData) (*dataFromServer, error) {
var gitRepositoryDataFromServer = &dataFromServer{}
switch scopedFullnameData.Scope {
case commonscope.ClusterScope:
if scopedFullnameData.FullnameCluster != nil {
resp, err := config.TMCConnection.ClusterGitRepositoryResourceService.VmwareTanzuManageV1alpha1ClusterFluxcdGitrepositoryResourceServiceGet(scopedFullnameData.FullnameCluster)
if err != nil {
if clienterrors.IsNotFoundError(err) {
d.SetId("")
return gitRepositoryDataFromServer, err
}
return gitRepositoryDataFromServer, errors.Wrapf(err, "Unable to get Tanzu Mission Control cluster git repository entry, name : %s", scopedFullnameData.FullnameCluster.Name)
}
scopedFullnameData.FullnameCluster = resp.GitRepository.FullName
gitRepositoryDataFromServer.UID = resp.GitRepository.Meta.UID
gitRepositoryDataFromServer.meta = resp.GitRepository.Meta
gitRepositoryDataFromServer.atomicSpec = resp.GitRepository.Spec
gitRepositoryDataFromServer.clusterScopeStatus = resp.GitRepository.Status
}
case commonscope.ClusterGroupScope:
if scopedFullnameData.FullnameClusterGroup != nil {
resp, err := config.TMCConnection.ClusterGroupGitRepositoryResourceService.VmwareTanzuManageV1alpha1ClustergroupFluxcdGitrepositoryResourceServiceGet(scopedFullnameData.FullnameClusterGroup)
if err != nil {
if clienterrors.IsNotFoundError(err) {
d.SetId("")
return gitRepositoryDataFromServer, err
}
return gitRepositoryDataFromServer, errors.Wrapf(err, "Unable to get Tanzu Mission Control cluster group git repository entry, name : %s", scopedFullnameData.FullnameClusterGroup.Name)
}
scopedFullnameData.FullnameClusterGroup = resp.GitRepository.FullName
gitRepositoryDataFromServer.UID = resp.GitRepository.Meta.UID
gitRepositoryDataFromServer.meta = resp.GitRepository.Meta
gitRepositoryDataFromServer.atomicSpec = resp.GitRepository.Spec.AtomicSpec
gitRepositoryDataFromServer.clusterGroupScopeStatus = resp.GitRepository.Status
}
case commonscope.UnknownScope:
return gitRepositoryDataFromServer, 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, name, namespace := scope.FlattenScope(scopedFullnameData)
if err := d.Set(nameKey, name); err != nil {
return gitRepositoryDataFromServer, err
}
if err := d.Set(namespaceNameKey, namespace); err != nil {
return gitRepositoryDataFromServer, err
}
if err := d.Set(commonscope.ScopeKey, fullName); err != nil {
return gitRepositoryDataFromServer, err
}
return gitRepositoryDataFromServer, nil
}