-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.go
128 lines (110 loc) · 3.97 KB
/
create.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
package appsecgatewayprofile
import (
"context"
"fmt"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
models "github.com/CheckPointSW/terraform-provider-infinity-next/internal/models/appsec-gateway-profile"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
ProfileSubTypeAws string = "Aws"
ProfileSubTypeAzure string = "Azure"
ProfileSubTypeVMware string = "VMware"
ProfileSubTypeHyperV string = "HyperV"
UpgradeModeAutomatic string = "Automatic"
UpgradeModeManual string = "Manual"
UpgradeModeScheduled string = "Scheduled"
ScheduleTypeDaily string = "Daily"
ScheduleTypeDaysInWeek string = "DaysInWeek"
ScheduleTypeDaysInMonth string = "DaysInMonth"
WeekDaySunday string = "Sunday"
WeekDayMonday string = "Monday"
WeekDayTuesday string = "Tuesday"
WeekDayWednesday string = "Wednesday"
WeekDayThursday string = "Thursday"
WeekDayFriday string = "Friday"
WeekDaySaturday string = "Saturday"
)
func CreateCloudGuardAppSecGatewayProfileInputFromResourceData(d *schema.ResourceData) (models.CreateCloudGuardAppSecGatewayProfileInput, error) {
var res models.CreateCloudGuardAppSecGatewayProfileInput
res.Name = d.Get("name").(string)
res.UpgradeMode = d.Get("upgrade_mode").(string)
res.ProfileSubType = d.Get("profile_sub_type").(string)
if res.UpgradeMode == UpgradeModeScheduled {
upgradeTime := handleScheduledUpgradeMode(d)
res.UpgradeTime = &upgradeTime
}
res.ReverseProxyUpstreamTimeout = d.Get("reverseproxy_upstream_timeout").(int)
res.Authentication.MaxNumberOfAgents = d.Get("max_number_of_agents").(int)
res.ReverseProxyAdditionalSettings = mapToKeyValueInput(d, "reverseproxy_additional_settings")
res.AdditionalSettings = mapToKeyValueInput(d, "additional_settings")
return res, nil
}
func mapToKeyValueInput(d *schema.ResourceData, key string) []models.KeyValueInput {
mapUserInput := d.Get(key).(map[string]any)
res := make([]models.KeyValueInput, 0, len(mapUserInput))
for key, val := range mapUserInput {
res = append(res,
models.KeyValueInput{
Key: key,
Value: val.(string),
})
}
return res
}
func handleScheduledUpgradeMode(d *schema.ResourceData) models.UpgradeTimeInput {
var res models.UpgradeTimeInput
res.ScheduleType = d.Get("upgrade_time_schedule_type").(string)
res.Time = d.Get("upgrade_time_hour").(string)
res.Duration = d.Get("upgrade_time_duration").(int)
res.WeekDays = utils.MustResourceDataCollectionToSlice[string](d, "upgrade_time_week_days")
return res
}
func NewAppSecGatewayProfile(ctx context.Context, c *api.Client, input models.CreateCloudGuardAppSecGatewayProfileInput) (models.CloudGuardAppSecGatewayProfile, error) {
vars := map[string]any{"profileInput": input}
res, err := c.MakeGraphQLRequest(ctx, `
mutation newCloudGuardAppSecGatewayProfile($profileInput: CloudGuardAppSecGatewayProfileInput)
{
newCloudGuardAppSecGatewayProfile (profileInput: $profileInput) {
id
name
profileType
profileSubType
authentication {
token
authenticationType
maxNumberOfAgents
}
additionalSettings {
id
key
value
}
upgradeMode
upgradeTime {
scheduleType
duration
time
... on ScheduleDaysInWeek {
weekDays
}
}
reverseProxyUpstreamTimeout
reverseProxyAdditionalSettings {
id
key
value
}
}
}
`, "newCloudGuardAppSecGatewayProfile", vars)
if err != nil {
return models.CloudGuardAppSecGatewayProfile{}, fmt.Errorf("failed to create new CloudGuardAppSecGatewayProfile: %w", err)
}
profile, err := utils.UnmarshalAs[models.CloudGuardAppSecGatewayProfile](res)
if err != nil {
return models.CloudGuardAppSecGatewayProfile{}, fmt.Errorf("failed to convert graphQL response to CloudGuardAppSecGatewayProfile struct. Error: %w", err)
}
return profile, err
}