1
2
3 package hns
4
5 import (
6 "encoding/json"
7
8 "github.com/sirupsen/logrus"
9 )
10
11
12 type RoutePolicy struct {
13 Policy
14 DestinationPrefix string `json:"DestinationPrefix,omitempty"`
15 NextHop string `json:"NextHop,omitempty"`
16 EncapEnabled bool `json:"NeedEncap,omitempty"`
17 }
18
19
20 type ELBPolicy struct {
21 LBPolicy
22 SourceVIP string `json:"SourceVIP,omitempty"`
23 VIPs []string `json:"VIPs,omitempty"`
24 ILB bool `json:"ILB,omitempty"`
25 DSR bool `json:"IsDSR,omitempty"`
26 }
27
28
29 type LBPolicy struct {
30 Policy
31 Protocol uint16 `json:"Protocol,omitempty"`
32 InternalPort uint16
33 ExternalPort uint16
34 }
35
36
37 type PolicyList struct {
38 ID string `json:"ID,omitempty"`
39 EndpointReferences []string `json:"References,omitempty"`
40 Policies []json.RawMessage `json:"Policies,omitempty"`
41 }
42
43
44 func HNSPolicyListRequest(method, path, request string) (*PolicyList, error) {
45 var policy PolicyList
46 err := hnsCall(method, "/policylists/"+path, request, &policy)
47 if err != nil {
48 return nil, err
49 }
50
51 return &policy, nil
52 }
53
54
55 func HNSListPolicyListRequest() ([]PolicyList, error) {
56 var plist []PolicyList
57 err := hnsCall("GET", "/policylists/", "", &plist)
58 if err != nil {
59 return nil, err
60 }
61
62 return plist, nil
63 }
64
65
66 func PolicyListRequest(method, path, request string) (*PolicyList, error) {
67 policylist := &PolicyList{}
68 err := hnsCall(method, "/policylists/"+path, request, &policylist)
69 if err != nil {
70 return nil, err
71 }
72
73 return policylist, nil
74 }
75
76
77 func GetPolicyListByID(policyListID string) (*PolicyList, error) {
78 return PolicyListRequest("GET", policyListID, "")
79 }
80
81
82 func (policylist *PolicyList) Create() (*PolicyList, error) {
83 operation := "Create"
84 title := "hcsshim::PolicyList::" + operation
85 logrus.Debugf(title+" id=%s", policylist.ID)
86 jsonString, err := json.Marshal(policylist)
87 if err != nil {
88 return nil, err
89 }
90 return PolicyListRequest("POST", "", string(jsonString))
91 }
92
93
94 func (policylist *PolicyList) Delete() (*PolicyList, error) {
95 operation := "Delete"
96 title := "hcsshim::PolicyList::" + operation
97 logrus.Debugf(title+" id=%s", policylist.ID)
98
99 return PolicyListRequest("DELETE", policylist.ID, "")
100 }
101
102
103 func (policylist *PolicyList) AddEndpoint(endpoint *HNSEndpoint) (*PolicyList, error) {
104 operation := "AddEndpoint"
105 title := "hcsshim::PolicyList::" + operation
106 logrus.Debugf(title+" id=%s, endpointId:%s", policylist.ID, endpoint.Id)
107
108 _, err := policylist.Delete()
109 if err != nil {
110 return nil, err
111 }
112
113
114 policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
115
116 return policylist.Create()
117 }
118
119
120 func (policylist *PolicyList) RemoveEndpoint(endpoint *HNSEndpoint) (*PolicyList, error) {
121 operation := "RemoveEndpoint"
122 title := "hcsshim::PolicyList::" + operation
123 logrus.Debugf(title+" id=%s, endpointId:%s", policylist.ID, endpoint.Id)
124
125 _, err := policylist.Delete()
126 if err != nil {
127 return nil, err
128 }
129
130 elementToRemove := "/endpoints/" + endpoint.Id
131
132 var references []string
133
134 for _, endpointReference := range policylist.EndpointReferences {
135 if endpointReference == elementToRemove {
136 continue
137 }
138 references = append(references, endpointReference)
139 }
140 policylist.EndpointReferences = references
141 return policylist.Create()
142 }
143
144
145 func AddLoadBalancer(endpoints []HNSEndpoint, isILB bool, sourceVIP, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*PolicyList, error) {
146 operation := "AddLoadBalancer"
147 title := "hcsshim::PolicyList::" + operation
148 logrus.Debugf(title+" endpointId=%v, isILB=%v, sourceVIP=%s, vip=%s, protocol=%v, internalPort=%v, externalPort=%v", endpoints, isILB, sourceVIP, vip, protocol, internalPort, externalPort)
149
150 policylist := &PolicyList{}
151
152 elbPolicy := &ELBPolicy{
153 SourceVIP: sourceVIP,
154 ILB: isILB,
155 }
156
157 if len(vip) > 0 {
158 elbPolicy.VIPs = []string{vip}
159 }
160 elbPolicy.Type = ExternalLoadBalancer
161 elbPolicy.Protocol = protocol
162 elbPolicy.InternalPort = internalPort
163 elbPolicy.ExternalPort = externalPort
164
165 for _, endpoint := range endpoints {
166 policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
167 }
168
169 jsonString, err := json.Marshal(elbPolicy)
170 if err != nil {
171 return nil, err
172 }
173 policylist.Policies = append(policylist.Policies, jsonString)
174 return policylist.Create()
175 }
176
177
178 func AddRoute(endpoints []HNSEndpoint, destinationPrefix string, nextHop string, encapEnabled bool) (*PolicyList, error) {
179 operation := "AddRoute"
180 title := "hcsshim::PolicyList::" + operation
181 logrus.Debugf(title+" destinationPrefix:%s", destinationPrefix)
182
183 policylist := &PolicyList{}
184
185 rPolicy := &RoutePolicy{
186 DestinationPrefix: destinationPrefix,
187 NextHop: nextHop,
188 EncapEnabled: encapEnabled,
189 }
190 rPolicy.Type = Route
191
192 for _, endpoint := range endpoints {
193 policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
194 }
195
196 jsonString, err := json.Marshal(rPolicy)
197 if err != nil {
198 return nil, err
199 }
200
201 policylist.Policies = append(policylist.Policies, jsonString)
202 return policylist.Create()
203 }
204
View as plain text