instancenic.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package azure
  15. import (
  16. "net/url"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type PublicIPAddress struct {
  25. ID string
  26. Name string
  27. Location string
  28. Properties PublicIPAddressPropertiesFormat
  29. }
  30. type InterfaceIPConfigurationPropertiesFormat struct {
  31. PrivateIPAddress string `json:"privateIPAddress,omitempty"`
  32. PrivateIPAddressVersion string `json:"privateIPAddressVersion,omitempty"`
  33. PrivateIPAllocationMethod string `json:"privateIPAllocationMethod,omitempty"`
  34. Subnet SNetwork `json:"subnet,omitempty"`
  35. Primary bool `json:"primary,omitempty"`
  36. PublicIPAddress *PublicIPAddress `json:"publicIPAddress,omitempty"`
  37. }
  38. type InterfaceIPConfiguration struct {
  39. Properties InterfaceIPConfigurationPropertiesFormat `json:"properties,omitempty"`
  40. Name string
  41. ID string
  42. }
  43. type InterfacePropertiesFormat struct {
  44. NetworkSecurityGroup *SSecurityGroup `json:"networkSecurityGroup,omitempty"`
  45. IPConfigurations []InterfaceIPConfiguration `json:"ipConfigurations,omitempty"`
  46. MacAddress string `json:"macAddress,omitempty"`
  47. Primary bool `json:"primary,omitempty"`
  48. VirtualMachine SubResource `json:"virtualMachine,omitempty"`
  49. }
  50. type SInstanceNic struct {
  51. multicloud.SResourceBase
  52. AzureTags
  53. instance *SInstance
  54. ID string
  55. Name string
  56. Type string
  57. Location string
  58. Properties InterfacePropertiesFormat `json:"properties,omitempty"`
  59. cloudprovider.DummyICloudNic
  60. }
  61. func (self *SInstanceNic) GetIP() string {
  62. if len(self.Properties.IPConfigurations) > 0 {
  63. return self.Properties.IPConfigurations[0].Properties.PrivateIPAddress
  64. }
  65. return ""
  66. }
  67. func (region *SRegion) DeleteNetworkInterface(interfaceId string) error {
  68. return region.del(interfaceId)
  69. }
  70. func (self *SInstanceNic) Delete() error {
  71. return self.instance.host.zone.region.DeleteNetworkInterface(self.ID)
  72. }
  73. func (self *SInstanceNic) GetMAC() string {
  74. mac := self.Properties.MacAddress
  75. return strings.Replace(strings.ToLower(mac), "-", ":", -1)
  76. }
  77. func (self *SInstanceNic) GetDriver() string {
  78. return "virtio"
  79. }
  80. func (self *SInstanceNic) InClassicNetwork() bool {
  81. return false
  82. }
  83. func (self *SInstanceNic) updateSecurityGroup(secgroupId string) error {
  84. region := self.instance.host.zone.region
  85. if len(secgroupId) > 0 {
  86. self.Properties.NetworkSecurityGroup = &SSecurityGroup{ID: secgroupId}
  87. }
  88. return region.update(jsonutils.Marshal(self), nil)
  89. }
  90. func (self *SInstanceNic) revokeSecurityGroup() error {
  91. return self.updateSecurityGroup("")
  92. }
  93. func (self *SInstanceNic) assignSecurityGroup(secgroupId string) error {
  94. return self.updateSecurityGroup(secgroupId)
  95. }
  96. func (self *SInstanceNic) GetINetworkId() string {
  97. if len(self.Properties.IPConfigurations) > 0 {
  98. return strings.ToLower(self.Properties.IPConfigurations[0].Properties.Subnet.ID)
  99. }
  100. return ""
  101. }
  102. func (self *SRegion) GetNetworkInterface(interfaceId string) (*SInstanceNic, error) {
  103. instancenic := SInstanceNic{}
  104. return &instancenic, self.get(interfaceId, url.Values{}, &instancenic)
  105. }
  106. func (self *SRegion) GetNetworkInterfaces() ([]SInstanceNic, error) {
  107. interfaces := []SInstanceNic{}
  108. err := self.list("Microsoft.Network/networkInterfaces", url.Values{}, &interfaces)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return interfaces, nil
  113. }
  114. func (self *SRegion) CreateNetworkInterface(resourceGroup string, nicName string, ipAddr string, subnetId string, secgrpId string) (*SInstanceNic, error) {
  115. allocMethod := "Static"
  116. if len(ipAddr) == 0 {
  117. allocMethod = "Dynamic"
  118. }
  119. params := jsonutils.Marshal(map[string]interface{}{
  120. "Name": nicName,
  121. "Location": self.Name,
  122. "Properties": map[string]interface{}{
  123. "IPConfigurations": []map[string]interface{}{
  124. map[string]interface{}{
  125. "Name": nicName,
  126. "Properties": map[string]interface{}{
  127. "PrivateIPAddress": ipAddr,
  128. "PrivateIPAddressVersion": "IPv4",
  129. "PrivateIPAllocationMethod": allocMethod,
  130. "Subnet": map[string]string{
  131. "Id": subnetId,
  132. },
  133. },
  134. },
  135. },
  136. },
  137. "Type": "Microsoft.Network/networkInterfaces",
  138. }).(*jsonutils.JSONDict)
  139. if len(secgrpId) > 0 {
  140. params.Add(jsonutils.Marshal(map[string]string{"id": secgrpId}), "Properties", "NetworkSecurityGroup")
  141. }
  142. nic := SInstanceNic{}
  143. return &nic, self.create(resourceGroup, params, &nic)
  144. }
  145. func (self *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) {
  146. nics, err := self.GetNetworkInterfaces()
  147. if err != nil {
  148. return nil, errors.Wrapf(err, "GetNetworkInterfaces")
  149. }
  150. ret := []cloudprovider.ICloudNetworkInterface{}
  151. for i := range nics {
  152. if len(nics[i].Properties.VirtualMachine.ID) > 0 {
  153. continue
  154. }
  155. ret = append(ret, &nics[i])
  156. }
  157. return ret, nil
  158. }
  159. func (self *SInstanceNic) GetAssociateId() string {
  160. return ""
  161. }
  162. func (self *SInstanceNic) GetAssociateType() string {
  163. return ""
  164. }
  165. func (self *SInstanceNic) GetId() string {
  166. return self.ID
  167. }
  168. func (self *SInstanceNic) GetName() string {
  169. return self.Name
  170. }
  171. func (self *SInstanceNic) GetStatus() string {
  172. return api.NETWORK_INTERFACE_STATUS_AVAILABLE
  173. }
  174. func (self *SInstanceNic) GetGlobalId() string {
  175. return strings.ToLower(self.ID)
  176. }
  177. func (self *SInstanceNic) GetMacAddress() string {
  178. return self.Properties.MacAddress
  179. }
  180. func (self *SInstanceNic) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
  181. addrs := []cloudprovider.ICloudInterfaceAddress{}
  182. for i := range self.Properties.IPConfigurations {
  183. addrs = append(addrs, &self.Properties.IPConfigurations[i])
  184. }
  185. return addrs, nil
  186. }
  187. func (self *InterfaceIPConfiguration) GetGlobalId() string {
  188. return strings.ToLower(self.ID)
  189. }
  190. func (self *InterfaceIPConfiguration) GetINetworkId() string {
  191. return strings.ToLower(self.Properties.Subnet.ID)
  192. }
  193. func (self *InterfaceIPConfiguration) GetIP() string {
  194. return self.Properties.PrivateIPAddress
  195. }
  196. func (self *InterfaceIPConfiguration) IsPrimary() bool {
  197. return self.Properties.Primary
  198. }