hnsendpoint.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package hcsshim
  2. import (
  3. "github.com/Microsoft/hcsshim/internal/hns"
  4. )
  5. // HNSEndpoint represents a network endpoint in HNS
  6. type HNSEndpoint = hns.HNSEndpoint
  7. // Namespace represents a Compartment.
  8. type Namespace = hns.Namespace
  9. //SystemType represents the type of the system on which actions are done
  10. type SystemType string
  11. // SystemType const
  12. const (
  13. ContainerType SystemType = "Container"
  14. VirtualMachineType SystemType = "VirtualMachine"
  15. HostType SystemType = "Host"
  16. )
  17. // EndpointAttachDetachRequest is the structure used to send request to the container to modify the system
  18. // Supported resource types are Network and Request Types are Add/Remove
  19. type EndpointAttachDetachRequest = hns.EndpointAttachDetachRequest
  20. // EndpointResquestResponse is object to get the endpoint request response
  21. type EndpointResquestResponse = hns.EndpointResquestResponse
  22. // HNSEndpointRequest makes a HNS call to modify/query a network endpoint
  23. func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
  24. return hns.HNSEndpointRequest(method, path, request)
  25. }
  26. // HNSListEndpointRequest makes a HNS call to query the list of available endpoints
  27. func HNSListEndpointRequest() ([]HNSEndpoint, error) {
  28. return hns.HNSListEndpointRequest()
  29. }
  30. // HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
  31. func HotAttachEndpoint(containerID string, endpointID string) error {
  32. return modifyNetworkEndpoint(containerID, endpointID, Add)
  33. }
  34. // HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
  35. func HotDetachEndpoint(containerID string, endpointID string) error {
  36. return modifyNetworkEndpoint(containerID, endpointID, Remove)
  37. }
  38. // ModifyContainer corresponding to the container id, by sending a request
  39. func modifyContainer(id string, request *ResourceModificationRequestResponse) error {
  40. container, err := OpenContainer(id)
  41. if err != nil {
  42. if IsNotExist(err) {
  43. return ErrComputeSystemDoesNotExist
  44. }
  45. return getInnerError(err)
  46. }
  47. defer container.Close()
  48. err = container.Modify(request)
  49. if err != nil {
  50. if IsNotSupported(err) {
  51. return ErrPlatformNotSupported
  52. }
  53. return getInnerError(err)
  54. }
  55. return nil
  56. }
  57. func modifyNetworkEndpoint(containerID string, endpointID string, request RequestType) error {
  58. requestMessage := &ResourceModificationRequestResponse{
  59. Resource: Network,
  60. Request: request,
  61. Data: endpointID,
  62. }
  63. err := modifyContainer(containerID, requestMessage)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. // GetHNSEndpointByID get the Endpoint by ID
  70. func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
  71. return hns.GetHNSEndpointByID(endpointID)
  72. }
  73. // GetHNSEndpointByName gets the endpoint filtered by Name
  74. func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
  75. return hns.GetHNSEndpointByName(endpointName)
  76. }