wire.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 zstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  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. type SWire struct {
  24. multicloud.SResourceBase
  25. ZStackTags
  26. vpc *SVpc
  27. inetworks []cloudprovider.ICloudNetwork
  28. ZStackBasic
  29. Vlan int `json:"vlan"`
  30. ZoneUUID string `json:"zoneUuid"`
  31. PhysicalInterface string `json:"physicalInterface"`
  32. Type string `json:"type"`
  33. ZStackTime
  34. AttachedClusterUUIDs []string `json:"attachedClusterUuids"`
  35. }
  36. func (region *SRegion) GetWire(wireId string) (*SWire, error) {
  37. wire := &SWire{vpc: region.GetVpc()}
  38. return wire, region.client.getResource("l2-networks", wireId, wire)
  39. }
  40. func (region *SRegion) GetWires(zoneId string, wireId string, clusterId string) ([]SWire, error) {
  41. wires := []SWire{}
  42. clusterIds, err := region.GetClusterIds()
  43. if err != nil {
  44. return nil, err
  45. }
  46. params := url.Values{}
  47. params.Add("q", "attachedClusterUuids?="+strings.Join(clusterIds, ","))
  48. if len(clusterId) > 0 {
  49. params.Set("q", "attachedClusterUuids?="+clusterId)
  50. }
  51. if len(zoneId) > 0 {
  52. params.Add("q", "zone.uuid="+zoneId)
  53. }
  54. if len(wireId) > 0 {
  55. params.Add("q", "uuid="+wireId)
  56. }
  57. err = region.client.listAll("l2-networks", params, &wires)
  58. if err != nil {
  59. return nil, err
  60. }
  61. for i := 0; i < len(wires); i++ {
  62. wires[i].vpc = region.GetVpc()
  63. }
  64. return wires, nil
  65. }
  66. func (wire *SWire) GetId() string {
  67. return wire.UUID
  68. }
  69. func (wire *SWire) GetName() string {
  70. return wire.Name
  71. }
  72. func (wire *SWire) IsEmulated() bool {
  73. return false
  74. }
  75. func (wire *SWire) GetStatus() string {
  76. return api.WIRE_STATUS_AVAILABLE
  77. }
  78. func (wire *SWire) Refresh() error {
  79. return nil
  80. }
  81. func (wire *SWire) GetGlobalId() string {
  82. return wire.UUID
  83. }
  84. func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc {
  85. return nil
  86. }
  87. func (wire *SWire) GetIZone() cloudprovider.ICloudZone {
  88. zone, _ := wire.vpc.region.GetZone(wire.ZoneUUID)
  89. return zone
  90. }
  91. func (wire *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  92. if wire.inetworks == nil || len(wire.inetworks) == 0 {
  93. networks, err := wire.vpc.region.GetNetworks(wire.ZoneUUID, wire.UUID, "", "")
  94. if err != nil {
  95. return nil, err
  96. }
  97. wire.inetworks = []cloudprovider.ICloudNetwork{}
  98. for i := 0; i < len(networks); i++ {
  99. networks[i].wire = wire
  100. wire.inetworks = append(wire.inetworks, &networks[i])
  101. }
  102. }
  103. return wire.inetworks, nil
  104. }
  105. func (wire *SWire) GetBandwidth() int {
  106. return 10000
  107. }
  108. func (wire *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  109. network, err := wire.vpc.region.CreateNetwork(opts.Name, opts.Cidr, wire.UUID, opts.Desc)
  110. if err != nil {
  111. return nil, err
  112. }
  113. network.wire = wire
  114. return network, nil
  115. }
  116. func (wire *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  117. idInfo := strings.Split(netid, "/")
  118. if len(idInfo) == 2 {
  119. network, err := wire.vpc.region.GetNetwork(wire.ZoneUUID, wire.UUID, idInfo[0], idInfo[1])
  120. if err != nil {
  121. return nil, err
  122. }
  123. network.wire = wire
  124. return network, nil
  125. }
  126. return nil, fmt.Errorf("invalid netid %s", netid)
  127. }