hnsendpoint.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package hns
  2. import (
  3. "encoding/json"
  4. "net"
  5. "github.com/sirupsen/logrus"
  6. )
  7. // HNSEndpoint represents a network endpoint in HNS
  8. type HNSEndpoint struct {
  9. Id string `json:"ID,omitempty"`
  10. Name string `json:",omitempty"`
  11. VirtualNetwork string `json:",omitempty"`
  12. VirtualNetworkName string `json:",omitempty"`
  13. Policies []json.RawMessage `json:",omitempty"`
  14. MacAddress string `json:",omitempty"`
  15. IPAddress net.IP `json:",omitempty"`
  16. DNSSuffix string `json:",omitempty"`
  17. DNSServerList string `json:",omitempty"`
  18. GatewayAddress string `json:",omitempty"`
  19. EnableInternalDNS bool `json:",omitempty"`
  20. DisableICC bool `json:",omitempty"`
  21. PrefixLength uint8 `json:",omitempty"`
  22. IsRemoteEndpoint bool `json:",omitempty"`
  23. EnableLowMetric bool `json:",omitempty"`
  24. Namespace *Namespace `json:",omitempty"`
  25. EncapOverhead uint16 `json:",omitempty"`
  26. }
  27. //SystemType represents the type of the system on which actions are done
  28. type SystemType string
  29. // SystemType const
  30. const (
  31. ContainerType SystemType = "Container"
  32. VirtualMachineType SystemType = "VirtualMachine"
  33. HostType SystemType = "Host"
  34. )
  35. // EndpointAttachDetachRequest is the structure used to send request to the container to modify the system
  36. // Supported resource types are Network and Request Types are Add/Remove
  37. type EndpointAttachDetachRequest struct {
  38. ContainerID string `json:"ContainerId,omitempty"`
  39. SystemType SystemType `json:"SystemType"`
  40. CompartmentID uint16 `json:"CompartmentId,omitempty"`
  41. VirtualNICName string `json:"VirtualNicName,omitempty"`
  42. }
  43. // EndpointResquestResponse is object to get the endpoint request response
  44. type EndpointResquestResponse struct {
  45. Success bool
  46. Error string
  47. }
  48. // HNSEndpointRequest makes a HNS call to modify/query a network endpoint
  49. func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
  50. endpoint := &HNSEndpoint{}
  51. err := hnsCall(method, "/endpoints/"+path, request, &endpoint)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return endpoint, nil
  56. }
  57. // HNSListEndpointRequest makes a HNS call to query the list of available endpoints
  58. func HNSListEndpointRequest() ([]HNSEndpoint, error) {
  59. var endpoint []HNSEndpoint
  60. err := hnsCall("GET", "/endpoints/", "", &endpoint)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return endpoint, nil
  65. }
  66. // GetHNSEndpointByID get the Endpoint by ID
  67. func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
  68. return HNSEndpointRequest("GET", endpointID, "")
  69. }
  70. // GetHNSEndpointByName gets the endpoint filtered by Name
  71. func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
  72. hnsResponse, err := HNSListEndpointRequest()
  73. if err != nil {
  74. return nil, err
  75. }
  76. for _, hnsEndpoint := range hnsResponse {
  77. if hnsEndpoint.Name == endpointName {
  78. return &hnsEndpoint, nil
  79. }
  80. }
  81. return nil, EndpointNotFoundError{EndpointName: endpointName}
  82. }
  83. // Create Endpoint by sending EndpointRequest to HNS. TODO: Create a separate HNS interface to place all these methods
  84. func (endpoint *HNSEndpoint) Create() (*HNSEndpoint, error) {
  85. operation := "Create"
  86. title := "hcsshim::HNSEndpoint::" + operation
  87. logrus.Debugf(title+" id=%s", endpoint.Id)
  88. jsonString, err := json.Marshal(endpoint)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return HNSEndpointRequest("POST", "", string(jsonString))
  93. }
  94. // Delete Endpoint by sending EndpointRequest to HNS
  95. func (endpoint *HNSEndpoint) Delete() (*HNSEndpoint, error) {
  96. operation := "Delete"
  97. title := "hcsshim::HNSEndpoint::" + operation
  98. logrus.Debugf(title+" id=%s", endpoint.Id)
  99. return HNSEndpointRequest("DELETE", endpoint.Id, "")
  100. }
  101. // Update Endpoint
  102. func (endpoint *HNSEndpoint) Update() (*HNSEndpoint, error) {
  103. operation := "Update"
  104. title := "hcsshim::HNSEndpoint::" + operation
  105. logrus.Debugf(title+" id=%s", endpoint.Id)
  106. jsonString, err := json.Marshal(endpoint)
  107. if err != nil {
  108. return nil, err
  109. }
  110. err = hnsCall("POST", "/endpoints/"+endpoint.Id, string(jsonString), &endpoint)
  111. return endpoint, err
  112. }
  113. // ApplyACLPolicy applies a set of ACL Policies on the Endpoint
  114. func (endpoint *HNSEndpoint) ApplyACLPolicy(policies ...*ACLPolicy) error {
  115. operation := "ApplyACLPolicy"
  116. title := "hcsshim::HNSEndpoint::" + operation
  117. logrus.Debugf(title+" id=%s", endpoint.Id)
  118. for _, policy := range policies {
  119. if policy == nil {
  120. continue
  121. }
  122. jsonString, err := json.Marshal(policy)
  123. if err != nil {
  124. return err
  125. }
  126. endpoint.Policies = append(endpoint.Policies, jsonString)
  127. }
  128. _, err := endpoint.Update()
  129. return err
  130. }
  131. // ContainerAttach attaches an endpoint to container
  132. func (endpoint *HNSEndpoint) ContainerAttach(containerID string, compartmentID uint16) error {
  133. operation := "ContainerAttach"
  134. title := "hcsshim::HNSEndpoint::" + operation
  135. logrus.Debugf(title+" id=%s", endpoint.Id)
  136. requestMessage := &EndpointAttachDetachRequest{
  137. ContainerID: containerID,
  138. CompartmentID: compartmentID,
  139. SystemType: ContainerType,
  140. }
  141. response := &EndpointResquestResponse{}
  142. jsonString, err := json.Marshal(requestMessage)
  143. if err != nil {
  144. return err
  145. }
  146. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/attach", string(jsonString), &response)
  147. }
  148. // ContainerDetach detaches an endpoint from container
  149. func (endpoint *HNSEndpoint) ContainerDetach(containerID string) error {
  150. operation := "ContainerDetach"
  151. title := "hcsshim::HNSEndpoint::" + operation
  152. logrus.Debugf(title+" id=%s", endpoint.Id)
  153. requestMessage := &EndpointAttachDetachRequest{
  154. ContainerID: containerID,
  155. SystemType: ContainerType,
  156. }
  157. response := &EndpointResquestResponse{}
  158. jsonString, err := json.Marshal(requestMessage)
  159. if err != nil {
  160. return err
  161. }
  162. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/detach", string(jsonString), &response)
  163. }
  164. // HostAttach attaches a nic on the host
  165. func (endpoint *HNSEndpoint) HostAttach(compartmentID uint16) error {
  166. operation := "HostAttach"
  167. title := "hcsshim::HNSEndpoint::" + operation
  168. logrus.Debugf(title+" id=%s", endpoint.Id)
  169. requestMessage := &EndpointAttachDetachRequest{
  170. CompartmentID: compartmentID,
  171. SystemType: HostType,
  172. }
  173. response := &EndpointResquestResponse{}
  174. jsonString, err := json.Marshal(requestMessage)
  175. if err != nil {
  176. return err
  177. }
  178. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/attach", string(jsonString), &response)
  179. }
  180. // HostDetach detaches a nic on the host
  181. func (endpoint *HNSEndpoint) HostDetach() error {
  182. operation := "HostDetach"
  183. title := "hcsshim::HNSEndpoint::" + operation
  184. logrus.Debugf(title+" id=%s", endpoint.Id)
  185. requestMessage := &EndpointAttachDetachRequest{
  186. SystemType: HostType,
  187. }
  188. response := &EndpointResquestResponse{}
  189. jsonString, err := json.Marshal(requestMessage)
  190. if err != nil {
  191. return err
  192. }
  193. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/detach", string(jsonString), &response)
  194. }
  195. // VirtualMachineNICAttach attaches a endpoint to a virtual machine
  196. func (endpoint *HNSEndpoint) VirtualMachineNICAttach(virtualMachineNICName string) error {
  197. operation := "VirtualMachineNicAttach"
  198. title := "hcsshim::HNSEndpoint::" + operation
  199. logrus.Debugf(title+" id=%s", endpoint.Id)
  200. requestMessage := &EndpointAttachDetachRequest{
  201. VirtualNICName: virtualMachineNICName,
  202. SystemType: VirtualMachineType,
  203. }
  204. response := &EndpointResquestResponse{}
  205. jsonString, err := json.Marshal(requestMessage)
  206. if err != nil {
  207. return err
  208. }
  209. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/attach", string(jsonString), &response)
  210. }
  211. // VirtualMachineNICDetach detaches a endpoint from a virtual machine
  212. func (endpoint *HNSEndpoint) VirtualMachineNICDetach() error {
  213. operation := "VirtualMachineNicDetach"
  214. title := "hcsshim::HNSEndpoint::" + operation
  215. logrus.Debugf(title+" id=%s", endpoint.Id)
  216. requestMessage := &EndpointAttachDetachRequest{
  217. SystemType: VirtualMachineType,
  218. }
  219. response := &EndpointResquestResponse{}
  220. jsonString, err := json.Marshal(requestMessage)
  221. if err != nil {
  222. return err
  223. }
  224. return hnsCall("POST", "/endpoints/"+endpoint.Id+"/detach", string(jsonString), &response)
  225. }