yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    21  	"yunion.io/x/cloudmux/pkg/multicloud"
    22  )
    23  
    24  type SZone struct {
    25  	multicloud.SResourceBase
    26  	EcloudTags
    27  	region *SRegion
    28  	host   *SHost
    29  
    30  	istorages []cloudprovider.ICloudStorage
    31  
    32  	ID      string `json:"id"`
    33  	Name    string `json:"name"`
    34  	Region  string `json:"region"`
    35  	Deleted bool
    36  	Visible bool
    37  }
    38  
    39  func (z *SZone) GetId() string {
    40  	return z.ID
    41  }
    42  
    43  func (z *SZone) GetName() string {
    44  	//return fmt.Sprintf("%s %s", CLOUD_PROVIDER_ECLOUD_CN, z.Name)
    45  	return z.Name
    46  }
    47  
    48  func (z *SZone) GetGlobalId() string {
    49  	return fmt.Sprintf("%s/%s", z.region.GetGlobalId(), z.ID)
    50  }
    51  
    52  func (z *SZone) GetStatus() string {
    53  	if z.Deleted || !z.Visible {
    54  		return "disable"
    55  	}
    56  	return "enable"
    57  }
    58  
    59  func (z *SZone) Refresh() error {
    60  	return nil
    61  }
    62  
    63  func (z *SZone) IsEmulated() bool {
    64  	return false
    65  }
    66  
    67  func (z *SZone) GetI18n() cloudprovider.SModelI18nTable {
    68  	table := cloudprovider.SModelI18nTable{}
    69  	table["name"] = cloudprovider.NewSModelI18nEntry(z.GetName()).CN(z.GetName()).EN(fmt.Sprintf("%s %s", CLOUD_PROVIDER_ECLOUD_EN, z.Name))
    70  	return table
    71  }
    72  
    73  func (z *SZone) GetIRegion() cloudprovider.ICloudRegion {
    74  	return z.region
    75  }
    76  
    77  func (z *SZone) getHost() cloudprovider.ICloudHost {
    78  	if z.host == nil {
    79  		z.host = &SHost{
    80  			zone: z,
    81  		}
    82  	}
    83  	return z.host
    84  }
    85  
    86  func (z *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
    87  	return []cloudprovider.ICloudHost{z.getHost()}, nil
    88  }
    89  
    90  func (z *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
    91  	host := z.getHost()
    92  	if host.GetGlobalId() == id {
    93  		return host, nil
    94  	}
    95  	return nil, cloudprovider.ErrNotFound
    96  }
    97  
    98  func (z *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
    99  	if z.istorages == nil {
   100  		err := z.fetchStorages()
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  	}
   105  	return z.istorages, nil
   106  }
   107  
   108  func (z *SZone) fetchStorages() error {
   109  	istorages := make([]cloudprovider.ICloudStorage, len(storageTypes))
   110  	for i := range istorages {
   111  		istorages[i] = &SStorage{
   112  			zone:        z,
   113  			storageType: storageTypes[i],
   114  		}
   115  	}
   116  	z.istorages = istorages
   117  	return nil
   118  }
   119  
   120  func (z *SZone) getStorageByType(t string) (*SStorage, error) {
   121  	istorages, err := z.GetIStorages()
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	for i := range istorages {
   126  		if istorages[i].GetStorageType() == t {
   127  			return istorages[i].(*SStorage), nil
   128  		}
   129  	}
   130  	return nil, cloudprovider.ErrNotFound
   131  }
   132  
   133  func (z *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   134  	istorages, err := z.GetIStorages()
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  	for i := range istorages {
   139  		if istorages[i].GetGlobalId() == id {
   140  			return istorages[i], nil
   141  		}
   142  	}
   143  	return nil, cloudprovider.ErrNotFound
   144  }
   145  
   146  type SZoneRegionBase struct {
   147  	Region string
   148  }