-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_nsxt_lb_profile.go
127 lines (111 loc) · 3.43 KB
/
resource_nsxt_lb_profile.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
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
package diffvalidation
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
profiles = "profile_type"
httpProfile = "http_profile"
tcpProfile = "tcp_profile"
udpProfile = "udp_profile"
cookieProfile = "cookie_profile"
sourceIPProfile = "sourceip_profile"
genericProfile = "generic_profile"
clientProfile = "client_profile"
serverProfile = "server_profile"
LBHttpProfile = "LBHttpProfile"
LBFastTCPProfile = "LBFastTcpProfile"
LBFastUDPProfile = "LBFastUdpProfile"
LBCookiePersistenceProfile = "LBCookiePersistenceProfile"
LBSourceIPPersistenceProfile = "LBSourceIpPersistenceProfile"
LBGenericPersistenceProfile = "LBGenericPersistenceProfile"
LBClientSslProfile = "LBClientSslProfile"
LBServerSslProfile = "LBServerSslProfile"
applicationProfile = "application-profile"
persistenceProfile = "persistence-profile"
sslProfile = "ssl-profile"
)
type LoadBalancerProfile struct {
diff *schema.ResourceDiff
}
func NewLoadBalancerProfileValidate(diff *schema.ResourceDiff) *LoadBalancerProfile {
return &LoadBalancerProfile{
diff: diff,
}
}
func (l *LoadBalancerProfile) DiffValidate() error {
err := l.validateProfile()
if err != nil {
return err
}
return nil
}
func (l *LoadBalancerProfile) validateProfile() error {
types := l.diff.Get(profiles)
switch types {
case applicationProfile:
httpType := l.diff.Get(httpProfile)
tcpType := l.diff.Get(tcpProfile)
udpType := l.diff.Get(udpProfile)
if len((httpType).([]interface{})) != 0 {
err := l.validateProfilesTypes(httpType, LBHttpProfile)
if err != nil {
return err
}
} else if len((tcpType).([]interface{})) != 0 {
err := l.validateProfilesTypes(tcpType, LBFastTCPProfile)
if err != nil {
return err
}
} else if len((udpType).([]interface{})) != 0 {
err := l.validateProfilesTypes(udpType, LBFastUDPProfile)
if err != nil {
return err
}
}
case persistenceProfile:
cookieType := l.diff.Get(cookieProfile)
sourceipType := l.diff.Get(sourceIPProfile)
genericType := l.diff.Get(genericProfile)
if len((cookieType).([]interface{})) != 0 {
err := l.validateProfilesTypes(cookieType, LBCookiePersistenceProfile)
if err != nil {
return err
}
} else if len((sourceipType).([]interface{})) != 0 {
err := l.validateProfilesTypes(sourceipType, LBSourceIPPersistenceProfile)
if err != nil {
return err
}
} else if len((genericType).([]interface{})) != 0 {
err := l.validateProfilesTypes(genericType, LBGenericPersistenceProfile)
if err != nil {
return err
}
}
case sslProfile:
serverType := l.diff.Get(serverProfile)
clientType := l.diff.Get(clientProfile)
if len((serverType).([]interface{})) != 0 {
err := l.validateProfilesTypes(serverType, LBServerSslProfile)
if err != nil {
return err
}
}
err := l.validateProfilesTypes(clientType, LBClientSslProfile)
if err != nil {
return err
}
}
return nil
}
func (l *LoadBalancerProfile) validateProfilesTypes(profileType interface{}, serviceType string) error {
for _, profile := range profileType.([]interface{}) {
serviceTypes := profile.(map[string]interface{})["service_type"].(string)
if serviceTypes != serviceType {
return fmt.Errorf("please provide serviceType as " + serviceType + " for the Configuration")
}
}
return nil
}