-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
190 lines (160 loc) · 8.6 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
package webappasset
import (
"context"
"fmt"
"strings"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
models "github.com/CheckPointSW/terraform-provider-infinity-next/internal/models/web-app-asset"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func UpdateWebApplicationAssetInputFromResourceData(d *schema.ResourceData, asset models.WebApplicationAsset) (models.UpdateWebApplicationAssetInput, error) {
var updateInput models.UpdateWebApplicationAssetInput
if _, newName, hasChange := utils.GetChangeWithParse(d, "name", utils.MustValueAs[string]); hasChange {
updateInput.Name = newName
}
if _, newUpstreamURL, hasChange := utils.GetChangeWithParse(d, "upstream_url", utils.MustValueAs[string]); hasChange {
updateInput.UpstreamURL = newUpstreamURL
}
if _, newObjectState, hasChange := utils.GetChangeWithParse(d, "state", utils.MustValueAs[string]); hasChange {
updateInput.State = newObjectState
}
if oldProfilesString, newProfilesString, hasChange := utils.GetChangeWithParse(d, "profiles", utils.MustSchemaCollectionToSlice[string]); hasChange {
updateInput.AddProfiles, updateInput.RemoveProfiles = utils.SlicesDiff(oldProfilesString, newProfilesString)
}
if oldBehaviorsStringList, newBehaviorsStringList, hasChange := utils.GetChangeWithParse(d, "trusted_sources", utils.MustSchemaCollectionToSlice[string]); hasChange {
updateInput.AddBehaviors, updateInput.RemoveBehaviors = utils.SlicesDiff(oldBehaviorsStringList, newBehaviorsStringList)
}
if oldURLsString, newURLsString, hasChange := utils.GetChangeWithParse(d, "urls", utils.MustSchemaCollectionToSlice[string]); hasChange {
oldURLsIDs := utils.MustResourceDataCollectionToSlice[string](d, "urls_ids")
oldURLsToIDsMap := make(map[string]string)
for _, oldURLID := range oldURLsIDs {
urlAndID := strings.Split(oldURLID, models.URLIDSeparator)
oldURLsToIDsMap[urlAndID[0]] = urlAndID[1]
}
addedURLs, removedURLs := utils.SlicesDiff(oldURLsString, newURLsString)
updateInput.AddURLs = addedURLs
for _, removedURL := range removedURLs {
updateInput.RemoveURLs = append(updateInput.RemoveURLs, oldURLsToIDsMap[removedURL])
}
}
if oldPracticeWrappers, newPracticeWrappers, hasChange := utils.GetChangeWithParse(d, "practice", parseSchemaPracticeWrappers); hasChange {
practiceWrappersInputsToAdd, practiceWrappersInputsToRemove := utils.SlicesDiff(oldPracticeWrappers, newPracticeWrappers)
practiceWrappersInputsToAdd = utils.Filter(practiceWrappersInputsToAdd, validatePracticeWrapperInput)
practiceWrappersInputsToRemove = utils.Filter(practiceWrappersInputsToRemove, validatePracticeWrapperInput)
practiceWrappersToAdd := utils.Map(practiceWrappersInputsToAdd, utils.MustUnmarshalAs[models.AddPracticeWrapper, models.PracticeWrapperInput])
practicesIDsToRemove := utils.Map(practiceWrappersInputsToRemove, func(wrapper models.PracticeWrapperInput) string { return wrapper.PracticeID })
updateInput.AddPracticeWrappers = practiceWrappersToAdd
updateInput.RemovePracticeWrappers = practicesIDsToRemove
}
if oldProxySettings, newProxySettings, hasChange := utils.GetChangeWithParse(d, "proxy_setting", parseSchemaProxySettings); hasChange {
oldProxySettingsIndicators := oldProxySettings.ToIndicatorsMap()
for _, newSetting := range newProxySettings {
// if key does not exist then this is a new setting to add
if _, ok := oldProxySettingsIndicators[newSetting.Key]; !ok {
updateInput.AddProxySetting = append(updateInput.AddProxySetting, models.AddProxySetting{
Key: newSetting.Key,
Value: newSetting.Value,
})
continue
}
// we know the key exist
// if the value is different - update the setting
oldSetting := oldProxySettingsIndicators[newSetting.Key]
if oldSetting.Value != newSetting.Value {
updateInput.UpdateProxySetting = append(updateInput.UpdateProxySetting, models.UpdateProxySetting{
Key: newSetting.Key,
Value: newSetting.Value,
ID: oldSetting.ID,
})
}
}
newProxySettingsIndicators := newProxySettings.ToIndicatorsMap()
for _, oldSetting := range oldProxySettings {
if _, ok := newProxySettingsIndicators[oldSetting.Key]; !ok {
updateInput.RemoveProxySetting = append(updateInput.RemoveProxySetting, oldSetting.ID)
}
}
}
if oldSourceIdentifiers, newSourceIdentifiers, hasChange := utils.GetChangeWithParse(d, "source_identifier", parseSchemaSourceIdentifiers); hasChange {
oldSourceIdentifiersIndicatorMap := oldSourceIdentifiers.ToIndicatorsMap()
for _, newSourceIdentifier := range newSourceIdentifiers {
// if source identifier does not exist - add it
if _, ok := oldSourceIdentifiersIndicatorMap[newSourceIdentifier.SourceIdentifier]; !ok {
updateInput.AddSourceIdentifiers = append(updateInput.AddSourceIdentifiers, models.AddSourceIdentifier{
SourceIdentifier: newSourceIdentifier.SourceIdentifier,
Values: newSourceIdentifier.Values,
})
continue
}
// source identifier exist - check if it needs to be updated
oldSourceIdentifier := oldSourceIdentifiersIndicatorMap[newSourceIdentifier.SourceIdentifier]
valuesToAdd, valuesToRemove := utils.SlicesDiff(oldSourceIdentifier.Values, newSourceIdentifier.Values)
valuesIDsIndicatorsMap := oldSourceIdentifier.ValuesIDs.ToIndicatorsMap()
var valuesIDsToRemove []string
for _, valueToRemove := range valuesToRemove {
valuesIDsToRemove = append(valuesIDsToRemove, valuesIDsIndicatorsMap[valueToRemove])
}
updateInput.UpdateSourceIdentifiers = append(updateInput.UpdateSourceIdentifiers, models.UpdateSourceIdentifier{
ID: oldSourceIdentifier.ID,
SourceIdentifier: oldSourceIdentifier.SourceIdentifier,
AddValues: valuesToAdd,
RemoveValues: valuesIDsToRemove,
UpdateValues: []string{},
})
}
newSourceIdentifiersIndicatorMap := newSourceIdentifiers.ToIndicatorsMap()
for _, oldSourceIdentifier := range oldSourceIdentifiers {
if _, ok := newSourceIdentifiersIndicatorMap[oldSourceIdentifier.SourceIdentifier]; !ok {
updateInput.RemoveSourceIdentifiers = append(updateInput.RemoveSourceIdentifiers, oldSourceIdentifier.ID)
}
}
}
return updateInput, nil
}
func UpdateWebApplicationAsset(ctx context.Context, c *api.Client, id any, input models.UpdateWebApplicationAssetInput) (bool, error) {
vars := map[string]any{"assetInput": input, "id": id}
res, err := c.MakeGraphQLRequest(ctx, `
mutation updateWebApplicationAsset($assetInput: WebApplicationAssetUpdateInput!, $id: ID!)
{
updateWebApplicationAsset(assetInput: $assetInput, id: $id)
}
`, "updateWebApplicationAsset", vars)
if err != nil {
return false, err
}
isUpdated, ok := res.(bool)
if !ok {
return false, fmt.Errorf("invalid updateWebApplicationAsset response %#v should be of type bool", res)
}
return isUpdated, err
}
// parseSchemaSourceIdentifiers converts the source identifiers (type schema.TypeSet) to a slice of map[string]any
// and then converts the it to a slice of modles.SourceIdentifierInput
func parseSchemaSourceIdentifiers(sourceIdentifiersFromResourceData any) models.SourceIdentifiersInputs {
return utils.Map(utils.MustSchemaCollectionToSlice[map[string]any](sourceIdentifiersFromResourceData), mapToSourceIdentifierInput)
}
// parseSchemaPracticeWrappers converts the practice wrappers (type schema.TypeSet) to a slice of map[string]any
// and then converts the it to a slice of modles.PracticeWrapperInput
func parseSchemaPracticeWrappers(practiceWrappersFromResourceData any) []models.PracticeWrapperInput {
return utils.Map(utils.MustSchemaCollectionToSlice[map[string]any](practiceWrappersFromResourceData), mapToPracticeWrapperInput)
}
// parseSchemaProxySettings converts the proxy settings (type schema.TypeSet) to a slice of map[string]any
// and then converts the it to a slice of modles.PracticeWrapperInput
func parseSchemaProxySettings(proxySettingsInterfaceFromResourceData any) models.ProxySettingInputs {
return utils.Map(utils.MustSchemaCollectionToSlice[map[string]any](proxySettingsInterfaceFromResourceData), mapToProxySettingInput)
}
// validatePracticeWrapperInput validates that there is no empty modes in the input (because this falis the update api call)
// this function is used during update of a practice since the getChange func of the terraform helper package
// sometimes returns an extra empty practice
func validatePracticeWrapperInput(pracitce models.PracticeWrapperInput) bool {
if pracitce.PracticeID == "" || pracitce.MainMode == "" {
return false
}
for _, mode := range pracitce.SubPracticeModes {
if mode.Mode == "" {
return false
}
}
return true
}