2018-05-02 10:15:52 -07:00
|
|
|
package hcsshim
|
|
|
|
|
|
|
|
import (
|
2018-08-31 01:18:39 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hns"
|
2018-05-02 10:15:52 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// HNSEndpoint represents a network endpoint in HNS
|
2018-08-31 01:18:39 +00:00
|
|
|
type HNSEndpoint = hns.HNSEndpoint
|
2018-10-18 21:37:23 +02:00
|
|
|
|
2021-11-05 14:54:08 +01:00
|
|
|
// HNSEndpointStats represent the stats for an networkendpoint in HNS
|
|
|
|
type HNSEndpointStats = hns.EndpointStats
|
|
|
|
|
2018-09-27 17:37:58 +02:00
|
|
|
// Namespace represents a Compartment.
|
|
|
|
type Namespace = hns.Namespace
|
2018-05-02 10:15:52 -07:00
|
|
|
|
|
|
|
//SystemType represents the type of the system on which actions are done
|
|
|
|
type SystemType string
|
|
|
|
|
|
|
|
// SystemType const
|
|
|
|
const (
|
|
|
|
ContainerType SystemType = "Container"
|
|
|
|
VirtualMachineType SystemType = "VirtualMachine"
|
|
|
|
HostType SystemType = "Host"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointAttachDetachRequest is the structure used to send request to the container to modify the system
|
|
|
|
// Supported resource types are Network and Request Types are Add/Remove
|
2018-08-31 01:18:39 +00:00
|
|
|
type EndpointAttachDetachRequest = hns.EndpointAttachDetachRequest
|
2018-05-02 10:15:52 -07:00
|
|
|
|
|
|
|
// EndpointResquestResponse is object to get the endpoint request response
|
2018-08-31 01:18:39 +00:00
|
|
|
type EndpointResquestResponse = hns.EndpointResquestResponse
|
2018-05-02 10:15:52 -07:00
|
|
|
|
|
|
|
// HNSEndpointRequest makes a HNS call to modify/query a network endpoint
|
|
|
|
func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
|
2018-08-31 01:18:39 +00:00
|
|
|
return hns.HNSEndpointRequest(method, path, request)
|
2018-05-02 10:15:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HNSListEndpointRequest makes a HNS call to query the list of available endpoints
|
|
|
|
func HNSListEndpointRequest() ([]HNSEndpoint, error) {
|
2018-08-31 01:18:39 +00:00
|
|
|
return hns.HNSListEndpointRequest()
|
2018-05-02 10:15:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
|
|
|
|
func HotAttachEndpoint(containerID string, endpointID string) error {
|
2019-11-25 09:58:10 -08:00
|
|
|
endpoint, err := GetHNSEndpointByID(endpointID)
|
2021-04-07 22:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-25 09:58:10 -08:00
|
|
|
isAttached, err := endpoint.IsAttached(containerID)
|
|
|
|
if isAttached {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-02 10:15:52 -07:00
|
|
|
return modifyNetworkEndpoint(containerID, endpointID, Add)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
|
|
|
|
func HotDetachEndpoint(containerID string, endpointID string) error {
|
2019-11-25 09:58:10 -08:00
|
|
|
endpoint, err := GetHNSEndpointByID(endpointID)
|
2021-04-07 22:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-25 09:58:10 -08:00
|
|
|
isAttached, err := endpoint.IsAttached(containerID)
|
|
|
|
if !isAttached {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-02 10:15:52 -07:00
|
|
|
return modifyNetworkEndpoint(containerID, endpointID, Remove)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModifyContainer corresponding to the container id, by sending a request
|
|
|
|
func modifyContainer(id string, request *ResourceModificationRequestResponse) error {
|
|
|
|
container, err := OpenContainer(id)
|
|
|
|
if err != nil {
|
|
|
|
if IsNotExist(err) {
|
|
|
|
return ErrComputeSystemDoesNotExist
|
|
|
|
}
|
|
|
|
return getInnerError(err)
|
|
|
|
}
|
|
|
|
defer container.Close()
|
|
|
|
err = container.Modify(request)
|
|
|
|
if err != nil {
|
|
|
|
if IsNotSupported(err) {
|
|
|
|
return ErrPlatformNotSupported
|
|
|
|
}
|
|
|
|
return getInnerError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func modifyNetworkEndpoint(containerID string, endpointID string, request RequestType) error {
|
|
|
|
requestMessage := &ResourceModificationRequestResponse{
|
|
|
|
Resource: Network,
|
|
|
|
Request: request,
|
|
|
|
Data: endpointID,
|
|
|
|
}
|
|
|
|
err := modifyContainer(containerID, requestMessage)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHNSEndpointByID get the Endpoint by ID
|
|
|
|
func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
|
2018-08-31 01:18:39 +00:00
|
|
|
return hns.GetHNSEndpointByID(endpointID)
|
2018-05-02 10:15:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHNSEndpointByName gets the endpoint filtered by Name
|
|
|
|
func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
|
2018-08-31 01:18:39 +00:00
|
|
|
return hns.GetHNSEndpointByName(endpointName)
|
2018-05-02 10:15:52 -07:00
|
|
|
}
|
2021-11-05 14:54:08 +01:00
|
|
|
|
|
|
|
// GetHNSEndpointStats gets the endpoint stats by ID
|
|
|
|
func GetHNSEndpointStats(endpointName string) (*HNSEndpointStats, error) {
|
|
|
|
return hns.GetHNSEndpointStats(endpointName)
|
|
|
|
}
|