hnsnetwork.go 4.4 KB

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