hnsnetwork.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //go:build windows
  2. package hns
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "net"
  7. "github.com/sirupsen/logrus"
  8. )
  9. // Subnet is associated with a network and represents a list
  10. // of subnets available to the network
  11. type Subnet struct {
  12. AddressPrefix string `json:",omitempty"`
  13. GatewayAddress string `json:",omitempty"`
  14. Policies []json.RawMessage `json:",omitempty"`
  15. }
  16. // MacPool is associated with a network and represents a list
  17. // of macaddresses available to the network
  18. type MacPool struct {
  19. StartMacAddress string `json:",omitempty"`
  20. EndMacAddress string `json:",omitempty"`
  21. }
  22. // HNSNetwork represents a network in HNS
  23. type HNSNetwork struct {
  24. Id string `json:"ID,omitempty"`
  25. Name string `json:",omitempty"`
  26. Type string `json:",omitempty"`
  27. NetworkAdapterName string `json:",omitempty"`
  28. SourceMac string `json:",omitempty"`
  29. Policies []json.RawMessage `json:",omitempty"`
  30. MacPools []MacPool `json:",omitempty"`
  31. Subnets []Subnet `json:",omitempty"`
  32. DNSSuffix string `json:",omitempty"`
  33. DNSServerList string `json:",omitempty"`
  34. DNSServerCompartment uint32 `json:",omitempty"`
  35. ManagementIP string `json:",omitempty"`
  36. AutomaticDNS bool `json:",omitempty"`
  37. }
  38. type hnsResponse struct {
  39. Success bool
  40. Error string
  41. Output json.RawMessage
  42. }
  43. // HNSNetworkRequest makes a call into HNS to update/query a single network
  44. func HNSNetworkRequest(method, path, request string) (*HNSNetwork, error) {
  45. var network HNSNetwork
  46. err := hnsCall(method, "/networks/"+path, request, &network)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &network, nil
  51. }
  52. // HNSListNetworkRequest makes a HNS call to query the list of available networks
  53. func HNSListNetworkRequest(method, path, request string) ([]HNSNetwork, error) {
  54. var network []HNSNetwork
  55. err := hnsCall(method, "/networks/"+path, request, &network)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return network, nil
  60. }
  61. // GetHNSNetworkByID
  62. func GetHNSNetworkByID(networkID string) (*HNSNetwork, error) {
  63. return HNSNetworkRequest("GET", networkID, "")
  64. }
  65. // GetHNSNetworkName filtered by Name
  66. func GetHNSNetworkByName(networkName string) (*HNSNetwork, error) {
  67. hsnnetworks, err := HNSListNetworkRequest("GET", "", "")
  68. if err != nil {
  69. return nil, err
  70. }
  71. for _, hnsnetwork := range hsnnetworks {
  72. if hnsnetwork.Name == networkName {
  73. return &hnsnetwork, nil
  74. }
  75. }
  76. return nil, NetworkNotFoundError{NetworkName: networkName}
  77. }
  78. // Create Network by sending NetworkRequest to HNS.
  79. func (network *HNSNetwork) Create() (*HNSNetwork, error) {
  80. operation := "Create"
  81. title := "hcsshim::HNSNetwork::" + operation
  82. logrus.Debugf(title+" id=%s", network.Id)
  83. for _, subnet := range network.Subnets {
  84. if (subnet.AddressPrefix != "") && (subnet.GatewayAddress == "") {
  85. return nil, errors.New("network create error, subnet has address prefix but no gateway specified")
  86. }
  87. }
  88. jsonString, err := json.Marshal(network)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return HNSNetworkRequest("POST", "", string(jsonString))
  93. }
  94. // Delete Network by sending NetworkRequest to HNS
  95. func (network *HNSNetwork) Delete() (*HNSNetwork, error) {
  96. operation := "Delete"
  97. title := "hcsshim::HNSNetwork::" + operation
  98. logrus.Debugf(title+" id=%s", network.Id)
  99. return HNSNetworkRequest("DELETE", network.Id, "")
  100. }
  101. // Creates an endpoint on the Network.
  102. func (network *HNSNetwork) NewEndpoint(ipAddress net.IP, macAddress net.HardwareAddr) *HNSEndpoint {
  103. return &HNSEndpoint{
  104. VirtualNetwork: network.Id,
  105. IPAddress: ipAddress,
  106. MacAddress: string(macAddress),
  107. }
  108. }
  109. func (network *HNSNetwork) CreateEndpoint(endpoint *HNSEndpoint) (*HNSEndpoint, error) {
  110. operation := "CreateEndpoint"
  111. title := "hcsshim::HNSNetwork::" + operation
  112. logrus.Debugf(title+" id=%s, endpointId=%s", network.Id, endpoint.Id)
  113. endpoint.VirtualNetwork = network.Id
  114. return endpoint.Create()
  115. }
  116. func (network *HNSNetwork) CreateRemoteEndpoint(endpoint *HNSEndpoint) (*HNSEndpoint, error) {
  117. operation := "CreateRemoteEndpoint"
  118. title := "hcsshim::HNSNetwork::" + operation
  119. logrus.Debugf(title+" id=%s", network.Id)
  120. endpoint.IsRemoteEndpoint = true
  121. return network.CreateEndpoint(endpoint)
  122. }