-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathupdate.go
202 lines (166 loc) · 7.69 KB
/
update.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package policyoperations
import (
"context"
"log"
"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"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
objectmetamodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/objectmeta"
policymodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy"
policyclustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/cluster"
policyclustergroupmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/clustergroup"
policyorganizationmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/organization"
policyworkspacemodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/policy/workspace"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy"
policykindcustom "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/custom"
policykindimage "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/image"
policykindmutation "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/mutation"
policykindnetwork "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/network"
policykindquota "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/quota"
policykindsecurity "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/kind/security"
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/policy/scope"
)
func ResourcePolicyInPlaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, rn string) (diags diag.Diagnostics) {
config := m.(authctx.TanzuContext)
policyName, ok := d.Get(policy.NameKey).(string)
if !ok {
return diag.Errorf("unable to read %s policy name", rn)
}
scopedFullnameData := scope.ConstructScope(d, policyName)
if scopedFullnameData == nil {
return diag.Errorf("Unable to update Tanzu Mission Control %s policy entry; Scope full name is empty", rn)
}
_, meta, spec, err := RetrievePolicyUIDMetaAndSpecFromServer(config, scopedFullnameData, d, rn, policyName)
if err != nil {
return diag.FromErr(err)
}
var updateAvailable bool
if updateCheckForMeta(d, meta) {
updateAvailable = true
}
if updateCheckForSpec(d, spec, rn) {
updateAvailable = true
}
if !updateAvailable {
log.Printf("[INFO] %s policy update is not required", rn)
return diags
}
switch scopedFullnameData.Scope {
case scope.ClusterScope:
if scopedFullnameData.FullnameCluster != nil {
policyReq := &policyclustermodel.VmwareTanzuManageV1alpha1ClusterPolicyPolicyRequest{
Policy: &policyclustermodel.VmwareTanzuManageV1alpha1ClusterPolicyPolicy{
FullName: scopedFullnameData.FullnameCluster,
Meta: meta,
Spec: spec,
},
}
_, err := config.TMCConnection.ClusterPolicyResourceService.ManageV1alpha1ClusterPolicyResourceServiceUpdate(policyReq)
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Unable to update Tanzu Mission Control cluster %s policy entry, name : %s", rn, policyName))
}
}
case scope.ClusterGroupScope:
if scopedFullnameData.FullnameClusterGroup != nil {
policyReq := &policyclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupPolicyPolicyRequest{
Policy: &policyclustergroupmodel.VmwareTanzuManageV1alpha1ClustergroupPolicyPolicy{
FullName: scopedFullnameData.FullnameClusterGroup,
Meta: meta,
Spec: spec,
},
}
_, err := config.TMCConnection.ClusterGroupPolicyResourceService.ManageV1alpha1ClustergroupPolicyResourceServiceUpdate(policyReq)
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Unable to update Tanzu Mission Control cluster group %s policy entry, name : %s", rn, policyName))
}
}
case scope.WorkspaceScope:
if scopedFullnameData.FullnameWorkspace != nil {
policyReq := &policyworkspacemodel.VmwareTanzuManageV1alpha1WorkspacePolicyPolicyRequest{
Policy: &policyworkspacemodel.VmwareTanzuManageV1alpha1WorkspacePolicyPolicy{
FullName: scopedFullnameData.FullnameWorkspace,
Meta: meta,
Spec: spec,
},
}
_, err := config.TMCConnection.WorkspacePolicyResourceService.ManageV1alpha1WorkspacePolicyResourceServiceUpdate(policyReq)
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Unable to update Tanzu Mission Control workspace %s policy entry, name : %s", rn, policyName))
}
}
case scope.OrganizationScope:
if scopedFullnameData.FullnameOrganization != nil {
policyReq := &policyorganizationmodel.VmwareTanzuManageV1alpha1OrganizationPolicyPolicyRequest{
Policy: &policyorganizationmodel.VmwareTanzuManageV1alpha1OrganizationPolicyPolicy{
FullName: scopedFullnameData.FullnameOrganization,
Meta: meta,
Spec: spec,
},
}
_, err := config.TMCConnection.OrganizationPolicyResourceService.ManageV1alpha1OrganizationPolicyResourceServiceUpdate(policyReq)
if err != nil {
return diag.FromErr(errors.Wrapf(err, "Unable to update Tanzu Mission Control organization %s policy entry, name : %s", rn, policyName))
}
}
case scope.UnknownScope:
return diag.Errorf("no valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(ScopeMap[rn], `, `))
}
log.Printf("[INFO] %s policy update successful", rn)
return ResourcePolicyRead(ctx, d, m, rn)
}
func updateCheckForMeta(d *schema.ResourceData, meta *objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta) bool {
if !common.HasMetaChanged(d) {
return false
}
objectMeta := common.ConstructMeta(d)
if value, ok := meta.Labels[common.CreatorLabelKey]; ok {
objectMeta.Labels[common.CreatorLabelKey] = value
}
meta.Labels = objectMeta.Labels
meta.Description = objectMeta.Description
log.Printf("[INFO] updating policy meta data")
return true
}
func updateCheckForSpec(d *schema.ResourceData, spec *policymodel.VmwareTanzuManageV1alpha1CommonPolicySpec, rn string) bool {
if !hasSpecChanged(d) {
return false
}
var policySpec *policymodel.VmwareTanzuManageV1alpha1CommonPolicySpec
switch rn {
case policykindcustom.ResourceName:
policySpec = policykindcustom.ConstructSpec(d)
case policykindsecurity.ResourceName:
policySpec = policykindsecurity.ConstructSpec(d)
case policykindimage.ResourceName:
policySpec = policykindimage.ConstructSpec(d)
case policykindquota.ResourceName:
policySpec = policykindquota.ConstructSpec(d)
case policykindnetwork.ResourceName:
policySpec = policykindnetwork.ConstructSpec(d)
case policykindmutation.ResourceName:
policySpec = policykindmutation.ConstructSpec(d)
}
spec.Input = policySpec.Input
spec.NamespaceSelector = policySpec.NamespaceSelector
spec.Recipe = policySpec.Recipe
spec.RecipeVersion = policySpec.RecipeVersion
log.Printf("[INFO] updating policy spec")
return true
}
func hasSpecChanged(d *schema.ResourceData) bool {
updateRequired := false
switch {
case d.HasChange(helper.GetFirstElementOf(policy.SpecKey, policy.InputKey)):
fallthrough
case d.HasChange(helper.GetFirstElementOf(policy.SpecKey, policy.NamespaceSelectorKey)):
updateRequired = true
}
return updateRequired
}