wire.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 aws
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SWire struct {
  23. multicloud.SResourceBase
  24. AwsTags
  25. zone *SZone
  26. vpc *SVpc
  27. }
  28. func (self *SWire) GetId() string {
  29. return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.zone.GetId())
  30. }
  31. func (self *SWire) GetName() string {
  32. return self.GetId()
  33. }
  34. func (self *SWire) GetGlobalId() string {
  35. return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.zone.GetGlobalId())
  36. }
  37. func (self *SWire) GetStatus() string {
  38. return api.WIRE_STATUS_AVAILABLE
  39. }
  40. func (self *SWire) IsEmulated() bool {
  41. return true
  42. }
  43. func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
  44. return self.vpc
  45. }
  46. func (self *SWire) GetIZone() cloudprovider.ICloudZone {
  47. return self.zone
  48. }
  49. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  50. networks, err := self.vpc.region.GetNetwroks(nil, self.zone.ZoneName, self.vpc.VpcId)
  51. if err != nil {
  52. return nil, err
  53. }
  54. ret := []cloudprovider.ICloudNetwork{}
  55. for i := range networks {
  56. networks[i].wire = self
  57. ret = append(ret, &networks[i])
  58. }
  59. return ret, nil
  60. }
  61. func (self *SWire) GetBandwidth() int {
  62. return 10000
  63. }
  64. func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
  65. networks, err := self.vpc.region.GetNetwroks([]string{netid}, self.zone.ZoneName, self.vpc.VpcId)
  66. if err != nil {
  67. return nil, err
  68. }
  69. for i := range networks {
  70. networks[i].wire = self
  71. if networks[i].GetGlobalId() == netid || networks[i].GetId() == netid {
  72. return &networks[i], nil
  73. }
  74. }
  75. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetINetworkById %s", netid)
  76. }
  77. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  78. network, err := self.zone.region.CreateNetwork(self.zone.ZoneName, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
  79. if err != nil {
  80. return nil, errors.Wrap(err, "CreateNetwork")
  81. }
  82. network.wire = self
  83. if opts.AssignPublicIp {
  84. self.zone.region.ModifySubnetAttribute(network.SubnetId, opts.AssignPublicIp)
  85. }
  86. return network, nil
  87. }
  88. func (self *SWire) GetDescription() string {
  89. return self.AwsTags.GetDescription()
  90. }