wire.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 huawei
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/netutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. // 华为云的子网有点特殊。子网在整个region可用。
  24. type SWire struct {
  25. multicloud.SResourceBase
  26. HuaweiTags
  27. vpc *SVpc
  28. }
  29. func (self *SWire) GetId() string {
  30. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.vpc.region.GetId())
  31. }
  32. func (self *SWire) GetName() string {
  33. return self.GetId()
  34. }
  35. func (self *SWire) GetGlobalId() string {
  36. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.vpc.region.GetGlobalId())
  37. }
  38. func (self *SWire) GetStatus() string {
  39. return api.WIRE_STATUS_AVAILABLE
  40. }
  41. func (self *SWire) Refresh() error {
  42. return nil
  43. }
  44. func (self *SWire) IsEmulated() bool {
  45. return true
  46. }
  47. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  48. return self.vpc
  49. }
  50. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  51. return nil
  52. }
  53. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  54. networks, err := self.vpc.region.GetNetworks(self.vpc.ID)
  55. if err != nil {
  56. return nil, errors.Wrapf(err, "GetNetwroks")
  57. }
  58. ret := []cloudprovider.ICloudNetwork{}
  59. for i := range networks {
  60. networks[i].wire = self
  61. ret = append(ret, &networks[i])
  62. }
  63. return ret, nil
  64. }
  65. func (self *SWire) GetBandwidth() int {
  66. return 10000
  67. }
  68. func (self *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) {
  69. network, err := self.vpc.region.GetNetwork(id)
  70. if err != nil {
  71. return nil, err
  72. }
  73. network.wire = self
  74. return network, nil
  75. }
  76. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  77. network, err := self.vpc.region.CreateNetwork(self.vpc.ID, opts)
  78. if err != nil {
  79. return nil, errors.Wrapf(err, "CreateNetwork")
  80. }
  81. network.wire = self
  82. return network, nil
  83. }
  84. func getDefaultGateWay(cidr string) (string, error) {
  85. pref, err := netutils.NewIPV4Prefix(cidr)
  86. if err != nil {
  87. return "", errors.Wrap(err, "getDefaultGateWay.NewIPV4Prefix")
  88. }
  89. startIp := pref.Address.NetAddr(pref.MaskLen) // 0
  90. startIp = startIp.StepUp() // 1
  91. return startIp.String(), nil
  92. }
  93. // https://console.huaweicloud.com/apiexplorer/#/openapi/VPC/doc?version=v2&api=ListSubnets
  94. func (self *SRegion) CreateNetwork(vpcId string, opts *cloudprovider.SNetworkCreateOptions) (*SNetwork, error) {
  95. gateway, err := getDefaultGateWay(opts.Cidr)
  96. if err != nil {
  97. return nil, err
  98. }
  99. params := map[string]interface{}{
  100. "name": opts.Name,
  101. "description": opts.Desc,
  102. "vpc_id": vpcId,
  103. "cidr": opts.Cidr,
  104. "gateway_ip": gateway,
  105. }
  106. resp, err := self.post(SERVICE_VPC, "subnets", map[string]interface{}{"subnet": params})
  107. if err != nil {
  108. return nil, errors.Wrapf(err, "create subnet")
  109. }
  110. subnet := &SNetwork{}
  111. err = resp.Unmarshal(subnet, "subnet")
  112. if err != nil {
  113. return nil, err
  114. }
  115. return subnet, nil
  116. }