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