wire.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 hcso
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/netutils"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  27. )
  28. // 华为云的子网有点特殊。子网在整个region可用。
  29. type SWire struct {
  30. multicloud.SResourceBase
  31. huawei.HuaweiTags
  32. region *SRegion
  33. vpc *SVpc
  34. inetworks []cloudprovider.ICloudNetwork
  35. }
  36. func (self *SWire) GetId() string {
  37. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.region.GetId())
  38. }
  39. func (self *SWire) GetName() string {
  40. return self.GetId()
  41. }
  42. func (self *SWire) GetGlobalId() string {
  43. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.region.GetGlobalId())
  44. }
  45. func (self *SWire) GetStatus() string {
  46. return api.WIRE_STATUS_AVAILABLE
  47. }
  48. func (self *SWire) Refresh() error {
  49. return nil
  50. }
  51. func (self *SWire) IsEmulated() bool {
  52. return true
  53. }
  54. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  55. return self.vpc
  56. }
  57. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  58. return nil
  59. }
  60. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  61. if self.inetworks == nil {
  62. err := self.vpc.fetchNetworks()
  63. if err != nil {
  64. return nil, err
  65. }
  66. }
  67. return self.inetworks, nil
  68. }
  69. func (self *SWire) GetBandwidth() int {
  70. return 10000
  71. }
  72. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  73. networks, err := self.GetINetworks()
  74. if err != nil {
  75. return nil, err
  76. }
  77. for i := 0; i < len(networks); i += 1 {
  78. if networks[i].GetGlobalId() == netid {
  79. return networks[i], nil
  80. }
  81. }
  82. return nil, cloudprovider.ErrNotFound
  83. }
  84. /*
  85. 华为云子网可用区,类似一个zone标签。即使指定了zone子网在整个region依然是可用。
  86. 通过华为web控制台创建子网需要指定可用区。这里是不指定的。
  87. */
  88. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  89. networkId, err := self.region.createNetwork(self.vpc.GetId(), opts.Name, opts.Cidr, opts.Desc)
  90. if err != nil {
  91. log.Errorf("createNetwork error %s", err)
  92. return nil, err
  93. }
  94. var network *SNetwork
  95. err = cloudprovider.WaitCreated(5*time.Second, 60*time.Second, func() bool {
  96. self.inetworks = nil
  97. network = self.getNetworkById(networkId)
  98. if network == nil {
  99. return false
  100. } else {
  101. return true
  102. }
  103. })
  104. if err != nil {
  105. log.Errorf("cannot find network after create????")
  106. return nil, err
  107. }
  108. network.wire = self
  109. return network, nil
  110. }
  111. func (self *SWire) addNetwork(network *SNetwork) {
  112. if self.inetworks == nil {
  113. self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
  114. }
  115. find := false
  116. for i := 0; i < len(self.inetworks); i += 1 {
  117. if self.inetworks[i].GetId() == network.ID {
  118. find = true
  119. break
  120. }
  121. }
  122. if !find {
  123. self.inetworks = append(self.inetworks, network)
  124. }
  125. }
  126. func (self *SWire) getNetworkById(networkId string) *SNetwork {
  127. networks, err := self.GetINetworks()
  128. if err != nil {
  129. return nil
  130. }
  131. log.Debugf("search for networks %d", len(networks))
  132. for i := 0; i < len(networks); i += 1 {
  133. log.Debugf("search %s", networks[i].GetName())
  134. network := networks[i]
  135. if network.GetId() == networkId {
  136. return network.(*SNetwork)
  137. }
  138. }
  139. return nil
  140. }
  141. func getDefaultGateWay(cidr string) (string, error) {
  142. pref, err := netutils.NewIPV4Prefix(cidr)
  143. if err != nil {
  144. return "", errors.Wrap(err, "getDefaultGateWay.NewIPV4Prefix")
  145. }
  146. startIp := pref.Address.NetAddr(pref.MaskLen) // 0
  147. startIp = startIp.StepUp() // 1
  148. return startIp.String(), nil
  149. }
  150. // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090590.html
  151. // cidr 掩码长度不能大于28
  152. func (self *SRegion) createNetwork(vpcId string, name string, cidr string, desc string) (string, error) {
  153. gateway, err := getDefaultGateWay(cidr)
  154. if err != nil {
  155. return "", err
  156. }
  157. params := jsonutils.NewDict()
  158. subnetObj := jsonutils.NewDict()
  159. subnetObj.Add(jsonutils.NewString(name), "name")
  160. subnetObj.Add(jsonutils.NewString(vpcId), "vpc_id")
  161. subnetObj.Add(jsonutils.NewString(cidr), "cidr")
  162. subnetObj.Add(jsonutils.NewString(gateway), "gateway_ip")
  163. // hard code for hcso
  164. // https://support.huaweicloud.com/dns_faq/dns_faq_002.html
  165. // https://support.huaweicloud.com/api-dns/dns_api_69001.html
  166. if self.client != nil && len(self.client.endpoints.DefaultSubnetDns) > 0 {
  167. dns := strings.Split(self.client.endpoints.DefaultSubnetDns, ",")
  168. if len(dns) > 0 && len(dns[0]) > 0 {
  169. subnetObj.Add(jsonutils.NewString(dns[0]), "primary_dns")
  170. }
  171. if len(dns) > 1 && len(dns[1]) > 0 {
  172. subnetObj.Add(jsonutils.NewString(dns[1]), "secondary_dns")
  173. }
  174. }
  175. params.Add(subnetObj, "subnet")
  176. subnet := SNetwork{}
  177. err = DoCreate(self.ecsClient.Subnets.Create, params, &subnet)
  178. return subnet.ID, err
  179. }