hnsnetwork.go 4.1 KB

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