Skip to content

Commit abde106

Browse files
author
John Howard
authored
Merge pull request #126 from madhanrm/policylist
Expose HNS Related APIs for Endpoints/Networks/PolicyLists/Policies
2 parents 5401bea + 84301e3 commit abde106

File tree

5 files changed

+607
-231
lines changed

5 files changed

+607
-231
lines changed

hnsendpoint.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package hcsshim
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net"
7+
8+
"github.com/Sirupsen/logrus"
9+
)
10+
11+
// HNSEndpoint represents a network endpoint in HNS
12+
type HNSEndpoint struct {
13+
Id string `json:"ID,omitempty"`
14+
Name string `json:",omitempty"`
15+
VirtualNetwork string `json:",omitempty"`
16+
VirtualNetworkName string `json:",omitempty"`
17+
Policies []json.RawMessage `json:",omitempty"`
18+
MacAddress string `json:",omitempty"`
19+
IPAddress net.IP `json:",omitempty"`
20+
DNSSuffix string `json:",omitempty"`
21+
DNSServerList string `json:",omitempty"`
22+
GatewayAddress string `json:",omitempty"`
23+
EnableInternalDNS bool `json:",omitempty"`
24+
DisableICC bool `json:",omitempty"`
25+
PrefixLength uint8 `json:",omitempty"`
26+
IsRemoteEndpoint bool `json:",omitempty"`
27+
}
28+
29+
// HNSEndpointRequest makes a HNS call to modify/query a network endpoint
30+
func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
31+
endpoint := &HNSEndpoint{}
32+
err := hnsCall(method, "/endpoints/"+path, request, &endpoint)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return endpoint, nil
38+
}
39+
40+
// HNSListEndpointRequest makes a HNS call to query the list of available endpoints
41+
func HNSListEndpointRequest() ([]HNSEndpoint, error) {
42+
var endpoint []HNSEndpoint
43+
err := hnsCall("GET", "/endpoints/", "", &endpoint)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return endpoint, nil
49+
}
50+
51+
// HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
52+
func HotAttachEndpoint(containerID string, endpointID string) error {
53+
return modifyNetworkEndpoint(containerID, endpointID, Add)
54+
}
55+
56+
// HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
57+
func HotDetachEndpoint(containerID string, endpointID string) error {
58+
return modifyNetworkEndpoint(containerID, endpointID, Remove)
59+
}
60+
61+
// ModifyContainer corresponding to the container id, by sending a request
62+
func modifyContainer(id string, request *ResourceModificationRequestResponse) error {
63+
container, err := OpenContainer(id)
64+
if err != nil {
65+
if IsNotExist(err) {
66+
return ErrComputeSystemDoesNotExist
67+
}
68+
return getInnerError(err)
69+
}
70+
defer container.Close()
71+
err = container.Modify(request)
72+
if err != nil {
73+
if IsNotSupported(err) {
74+
return ErrPlatformNotSupported
75+
}
76+
return getInnerError(err)
77+
}
78+
79+
return nil
80+
}
81+
82+
func modifyNetworkEndpoint(containerID string, endpointID string, request RequestType) error {
83+
requestMessage := &ResourceModificationRequestResponse{
84+
Resource: Network,
85+
Request: request,
86+
Data: endpointID,
87+
}
88+
err := modifyContainer(containerID, requestMessage)
89+
90+
if err != nil {
91+
return err
92+
}
93+
94+
return nil
95+
}
96+
97+
// GetHNSEndpointByID
98+
func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
99+
return HNSEndpointRequest("GET", endpointID, "")
100+
}
101+
102+
// GetHNSNetworkName filtered by Name
103+
func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
104+
hnsResponse, err := HNSListEndpointRequest()
105+
if err != nil {
106+
return nil, err
107+
}
108+
for _, hnsEndpoint := range hnsResponse {
109+
if hnsEndpoint.Name == endpointName {
110+
return &hnsEndpoint, nil
111+
}
112+
}
113+
return nil, fmt.Errorf("Endpoint %v not found", endpointName)
114+
}
115+
116+
// Create Endpoint by sending EndpointRequest to HNS. TODO: Create a separate HNS interface to place all these methods
117+
func (endpoint *HNSEndpoint) Create() (*HNSEndpoint, error) {
118+
operation := "Create"
119+
title := "HCSShim::HNSEndpoint::" + operation
120+
logrus.Debugf(title+" id=%s", endpoint.Id)
121+
122+
jsonString, err := json.Marshal(endpoint)
123+
if err != nil {
124+
return nil, err
125+
}
126+
return HNSEndpointRequest("POST", "", string(jsonString))
127+
}
128+
129+
// Delete Endpoint by sending EndpointRequest to HNS
130+
func (endpoint *HNSEndpoint) Delete() (*HNSEndpoint, error) {
131+
operation := "Delete"
132+
title := "HCSShim::HNSEndpoint::" + operation
133+
logrus.Debugf(title+" id=%s", endpoint.Id)
134+
135+
return HNSEndpointRequest("DELETE", endpoint.Id, "")
136+
}
137+
138+
// Delete Endpoint by sending EndpointRequest to HNS
139+
func (endpoint *HNSEndpoint) Update() (*HNSEndpoint, error) {
140+
operation := "Update"
141+
title := "HCSShim::HNSEndpoint::" + operation
142+
logrus.Debugf(title+" id=%s", endpoint.Id)
143+
jsonString, err := json.Marshal(endpoint)
144+
if err != nil {
145+
return nil, err
146+
}
147+
err = hnsCall("POST", "/endpoints/"+endpoint.Id+"/update", string(jsonString), &endpoint)
148+
149+
return endpoint, err
150+
}
151+
152+
// Hot Attach an endpoint to a container
153+
func (endpoint *HNSEndpoint) HotAttach(containerID string) error {
154+
operation := "HotAttach"
155+
title := "HCSShim::HNSEndpoint::" + operation
156+
logrus.Debugf(title+" id=%s, containerId=%s", endpoint.Id, containerID)
157+
158+
return modifyNetworkEndpoint(containerID, endpoint.Id, Add)
159+
}
160+
161+
// Hot Detach an endpoint from a container
162+
func (endpoint *HNSEndpoint) HotDetach(containerID string) error {
163+
operation := "HotDetach"
164+
title := "HCSShim::HNSEndpoint::" + operation
165+
logrus.Debugf(title+" id=%s, containerId=%s", endpoint.Id, containerID)
166+
167+
return modifyNetworkEndpoint(containerID, endpoint.Id, Remove)
168+
}
169+
170+
// Apply Acl Policy on the Endpoint
171+
func (endpoint *HNSEndpoint) ApplyACLPolicy(policy *ACLPolicy) error {
172+
operation := "ApplyACLPolicy"
173+
title := "HCSShim::HNSEndpoint::" + operation
174+
logrus.Debugf(title+" id=%s", endpoint.Id)
175+
176+
jsonString, err := json.Marshal(policy)
177+
if err != nil {
178+
return err
179+
}
180+
endpoint.Policies[0] = jsonString
181+
_, err = endpoint.Update()
182+
return err
183+
}

0 commit comments

Comments
 (0)