...

Source file src/github.com/Microsoft/hcsshim/internal/hns/namespace.go

Documentation: github.com/Microsoft/hcsshim/internal/hns

     1  //go:build windows
     2  
     3  package hns
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  )
    12  
    13  type namespaceRequest struct {
    14  	IsDefault bool `json:",omitempty"`
    15  }
    16  
    17  type namespaceEndpointRequest struct {
    18  	ID string `json:"Id"`
    19  }
    20  
    21  type NamespaceResource struct {
    22  	Type string
    23  	Data json.RawMessage
    24  }
    25  
    26  type namespaceResourceRequest struct {
    27  	Type string
    28  	Data interface{}
    29  }
    30  
    31  type Namespace struct {
    32  	ID            string
    33  	IsDefault     bool                `json:",omitempty"`
    34  	ResourceList  []NamespaceResource `json:",omitempty"`
    35  	CompartmentId uint32              `json:",omitempty"`
    36  }
    37  
    38  func issueNamespaceRequest(id *string, method, subpath string, request interface{}) (*Namespace, error) {
    39  	var err error
    40  	hnspath := "/namespaces/"
    41  	if id != nil {
    42  		hnspath = path.Join(hnspath, *id)
    43  	}
    44  	if subpath != "" {
    45  		hnspath = path.Join(hnspath, subpath)
    46  	}
    47  	var reqJSON []byte
    48  	if request != nil {
    49  		if reqJSON, err = json.Marshal(request); err != nil {
    50  			return nil, err
    51  		}
    52  	}
    53  	var ns Namespace
    54  	err = hnsCall(method, hnspath, string(reqJSON), &ns)
    55  	if err != nil {
    56  		if strings.Contains(err.Error(), "Element not found.") {
    57  			return nil, os.ErrNotExist
    58  		}
    59  		return nil, fmt.Errorf("%s %s: %s", method, hnspath, err)
    60  	}
    61  	return &ns, err
    62  }
    63  
    64  func CreateNamespace() (string, error) {
    65  	req := namespaceRequest{}
    66  	ns, err := issueNamespaceRequest(nil, "POST", "", &req)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  	return ns.ID, nil
    71  }
    72  
    73  func RemoveNamespace(id string) error {
    74  	_, err := issueNamespaceRequest(&id, "DELETE", "", nil)
    75  	return err
    76  }
    77  
    78  func GetNamespaceEndpoints(id string) ([]string, error) {
    79  	ns, err := issueNamespaceRequest(&id, "GET", "", nil)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	var endpoints []string
    84  	for _, rsrc := range ns.ResourceList {
    85  		if rsrc.Type == "Endpoint" {
    86  			var endpoint namespaceEndpointRequest
    87  			err = json.Unmarshal(rsrc.Data, &endpoint)
    88  			if err != nil {
    89  				return nil, fmt.Errorf("unmarshal endpoint: %s", err)
    90  			}
    91  			endpoints = append(endpoints, endpoint.ID)
    92  		}
    93  	}
    94  	return endpoints, nil
    95  }
    96  
    97  func AddNamespaceEndpoint(id string, endpointID string) error {
    98  	resource := namespaceResourceRequest{
    99  		Type: "Endpoint",
   100  		Data: namespaceEndpointRequest{endpointID},
   101  	}
   102  	_, err := issueNamespaceRequest(&id, "POST", "addresource", &resource)
   103  	return err
   104  }
   105  
   106  func RemoveNamespaceEndpoint(id string, endpointID string) error {
   107  	resource := namespaceResourceRequest{
   108  		Type: "Endpoint",
   109  		Data: namespaceEndpointRequest{endpointID},
   110  	}
   111  	_, err := issueNamespaceRequest(&id, "POST", "removeresource", &resource)
   112  	return err
   113  }
   114  

View as plain text