yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/zone.go (about)

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