vpc.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ecloud
  15. import (
  16. "yunion.io/x/jsonutils"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. )
  21. type SVpc struct {
  22. multicloud.SVpc
  23. EcloudTags
  24. region *SRegion
  25. Id string `json:"id"`
  26. Name string `json:"name"`
  27. Region string `json:"region"`
  28. EcStatus string `json:"ecStatus"`
  29. RouterId string `json:"routerId"`
  30. Scale string `json:"scale"`
  31. UserId string `json:"userId"`
  32. UserName string `json:"userName"`
  33. }
  34. func (v *SVpc) GetId() string {
  35. return v.Id
  36. }
  37. func (v *SVpc) GetName() string {
  38. return v.Name
  39. }
  40. func (v *SVpc) GetGlobalId() string {
  41. return v.GetId()
  42. }
  43. func (v *SVpc) GetStatus() string {
  44. switch v.EcStatus {
  45. case "ACTIVE":
  46. return api.VPC_STATUS_AVAILABLE
  47. case "DOWN", "BUILD", "ERROR":
  48. return api.VPC_STATUS_UNAVAILABLE
  49. case "PENDING_DELETE":
  50. return api.VPC_STATUS_DELETING
  51. case "PENDING_CREATE", "PENDING_UPDATE":
  52. return api.VPC_STATUS_PENDING
  53. default:
  54. return api.VPC_STATUS_UNKNOWN
  55. }
  56. }
  57. func (v *SVpc) Refresh() error {
  58. n, err := v.region.GetVpc(v.Id)
  59. if err != nil {
  60. return err
  61. }
  62. return jsonutils.Update(v, n)
  63. }
  64. func (v *SVpc) GetRegion() cloudprovider.ICloudRegion {
  65. return v.region
  66. }
  67. func (v *SVpc) GetIsDefault() bool {
  68. return false
  69. }
  70. func (v *SVpc) GetCidrBlock() string {
  71. return ""
  72. }
  73. func (v *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  74. zones, err := v.region.GetZones()
  75. if err != nil {
  76. return nil, err
  77. }
  78. ret := []cloudprovider.ICloudWire{}
  79. for i := range zones {
  80. ret = append(ret, &SWire{
  81. vpc: v,
  82. zone: &zones[i],
  83. })
  84. }
  85. return ret, nil
  86. }
  87. func (v *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  88. // 移动云安全组为 region 维度,返回本 region 下全部安全组
  89. return v.region.GetISecurityGroups()
  90. }
  91. func (v *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  92. return nil, cloudprovider.ErrNotImplemented
  93. }
  94. func (v *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  95. return nil, nil
  96. }
  97. func (v *SVpc) Delete() error {
  98. return v.region.DeleteVpc(v.RouterId)
  99. }
  100. func (v *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  101. iwires, err := v.GetIWires()
  102. if err != nil {
  103. return nil, err
  104. }
  105. for i := range iwires {
  106. if iwires[i].GetGlobalId() == wireId {
  107. return iwires[i], nil
  108. }
  109. }
  110. return nil, cloudprovider.ErrNotFound
  111. }