zone.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 google
  15. import (
  16. "fmt"
  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 SZone struct {
  22. multicloud.SResourceBase
  23. GoogleTags
  24. region *SRegion
  25. Description string
  26. ID string
  27. Kind string
  28. Name string
  29. Region string
  30. SelfLink string
  31. AvailableCpuPlatforms []string
  32. Status string
  33. }
  34. func (region *SRegion) GetZone(id string) (*SZone, error) {
  35. zone := &SZone{region: region}
  36. return zone, region.GetBySelfId(id, zone)
  37. }
  38. func (region *SRegion) GetZones(regionId string, maxResults int, pageToken string) ([]SZone, error) {
  39. zones := []SZone{}
  40. params := map[string]string{}
  41. if len(regionId) > 0 {
  42. params["filter"] = fmt.Sprintf(`region="%s/%s/projects/%s/regions/%s"`, GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, region.GetProjectId(), regionId)
  43. }
  44. resource := "zones"
  45. return zones, region.List(resource, params, maxResults, pageToken, &zones)
  46. }
  47. func (zone *SZone) GetName() string {
  48. return zone.Name
  49. }
  50. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  51. table := cloudprovider.SModelI18nTable{}
  52. table["name"] = cloudprovider.NewSModelI18nEntry(zone.GetName()).CN(zone.GetName())
  53. return table
  54. }
  55. func (zone *SZone) GetGlobalId() string {
  56. return fmt.Sprintf("%s/%s", zone.region.GetGlobalId(), zone.Name)
  57. }
  58. func (zone *SZone) GetId() string {
  59. return zone.Name
  60. }
  61. func (zone *SZone) GetIHostById(hostId string) (cloudprovider.ICloudHost, error) {
  62. if hostId != zone.getHost().GetGlobalId() {
  63. return nil, cloudprovider.ErrNotFound
  64. }
  65. return &SHost{zone: zone}, nil
  66. }
  67. func (zone *SZone) getHost() *SHost {
  68. return &SHost{zone: zone}
  69. }
  70. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  71. host := zone.getHost()
  72. return []cloudprovider.ICloudHost{host}, nil
  73. }
  74. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  75. return zone.region
  76. }
  77. func (zone *SZone) GetIStorageById(storageId string) (cloudprovider.ICloudStorage, error) {
  78. storage, err := zone.region.GetStorage(storageId)
  79. if err != nil {
  80. return nil, err
  81. }
  82. storage.zone = zone
  83. return storage, nil
  84. }
  85. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  86. storages, err := zone.region.GetStorages(zone.Name, 0, "")
  87. if err != nil {
  88. return nil, err
  89. }
  90. istorages := []cloudprovider.ICloudStorage{}
  91. for i := range storages {
  92. storages[i].zone = zone
  93. istorages = append(istorages, &storages[i])
  94. }
  95. return istorages, nil
  96. }
  97. func (zone *SZone) IsEmulated() bool {
  98. return false
  99. }
  100. func (zone *SZone) Refresh() error {
  101. return nil
  102. }
  103. func (zone *SZone) GetStatus() string {
  104. if zone.Status == "UP" {
  105. return api.ZONE_ENABLE
  106. }
  107. return api.ZONE_SOLDOUT
  108. }