wire.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 cloudpods
  15. import (
  16. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SWire struct {
  24. CloudpodsTags
  25. multicloud.SResourceBase
  26. vpc *SVpc
  27. api.WireDetails
  28. }
  29. func (self *SWire) GetId() string {
  30. return self.Id
  31. }
  32. func (self *SWire) GetGlobalId() string {
  33. return self.Id
  34. }
  35. func (self *SWire) GetName() string {
  36. return self.Name
  37. }
  38. func (self *SWire) GetStatus() string {
  39. return self.Status
  40. }
  41. func (self *SWire) GetBandwidth() int {
  42. return self.Bandwidth
  43. }
  44. func (self *SWire) IsEmulated() bool {
  45. return self.SWire.IsEmulated
  46. }
  47. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  48. return self.vpc
  49. }
  50. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  51. if len(self.ZoneId) == 0 {
  52. return nil
  53. }
  54. zone, err := self.vpc.region.GetZone(self.ZoneId)
  55. if err != nil {
  56. return nil
  57. }
  58. return zone
  59. }
  60. func (self *SWire) Refresh() error {
  61. wire, err := self.vpc.region.GetWire(self.Id)
  62. if err != nil {
  63. return err
  64. }
  65. return jsonutils.Update(self, wire)
  66. }
  67. func (self *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) {
  68. wire, err := self.region.GetWire(id)
  69. if err != nil {
  70. return nil, errors.Wrapf(err, "GetWire(%s)", id)
  71. }
  72. wire.vpc = self
  73. return wire, nil
  74. }
  75. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  76. wires, err := self.region.GetWires(self.Id, "")
  77. if err != nil {
  78. return nil, errors.Wrapf(err, "GetWires")
  79. }
  80. ret := []cloudprovider.ICloudWire{}
  81. for i := range wires {
  82. wires[i].vpc = self
  83. ret = append(ret, &wires[i])
  84. }
  85. return ret, nil
  86. }
  87. func (self *SRegion) GetWires(vpcId, hostId string) ([]SWire, error) {
  88. wires := []SWire{}
  89. params := map[string]interface{}{"cloud_env": ""}
  90. if len(vpcId) > 0 {
  91. params["vpc_id"] = vpcId
  92. }
  93. if len(hostId) > 0 {
  94. params["host_id"] = hostId
  95. }
  96. return wires, self.list(&modules.Wires, params, &wires)
  97. }
  98. func (self *SRegion) GetWire(id string) (*SWire, error) {
  99. wire := &SWire{}
  100. return wire, self.cli.get(&modules.Wires, id, nil, wire)
  101. }