yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    21  	"yunion.io/x/cloudmux/pkg/multicloud"
    22  )
    23  
    24  type sZoneIden struct {
    25  	Id   string
    26  	Name string
    27  }
    28  
    29  var ZonesInRegion = map[string][]sZoneIden{
    30  	"cn-north-1": {
    31  		{
    32  			Id:   "cn-north-1a",
    33  			Name: "可用区A",
    34  		},
    35  		{
    36  			Id:   "cn-north-1b",
    37  			Name: "可用区B",
    38  		},
    39  		{
    40  			Id:   "cn-north-1c",
    41  			Name: "可用区C",
    42  		},
    43  	},
    44  	"cn-east-1": {
    45  		{
    46  			Id:   "cn-east-1a",
    47  			Name: "可用区A",
    48  		},
    49  	},
    50  	"cn-east-2": {
    51  		{
    52  			Id:   "cn-east-2a",
    53  			Name: "可用区A",
    54  		},
    55  		{
    56  			Id:   "cn-east-2b",
    57  			Name: "可用区B",
    58  		},
    59  		{
    60  			Id:   "cn-east-2c",
    61  			Name: "可用区C",
    62  		},
    63  	},
    64  	"cn-south-1": {
    65  		{
    66  			Id:   "cn-south-1a",
    67  			Name: "可用区A",
    68  		},
    69  		{
    70  			Id:   "cn-south-1b",
    71  			Name: "可用区B",
    72  		},
    73  		{
    74  			Id:   "cn-south-1c",
    75  			Name: "可用区C",
    76  		},
    77  	},
    78  }
    79  
    80  type SZone struct {
    81  	multicloud.SResourceBase
    82  	JdcloudTags
    83  	region *SRegion
    84  
    85  	ihost     cloudprovider.ICloudHost
    86  	istorages []cloudprovider.ICloudStorage
    87  
    88  	ID   string
    89  	Name string
    90  }
    91  
    92  func (z *SZone) GetId() string {
    93  	return z.ID
    94  }
    95  
    96  func (z *SZone) GetName() string {
    97  	return z.Name
    98  }
    99  
   100  func (z *SZone) GetGlobalId() string {
   101  	return fmt.Sprintf("%s/%s", z.region.GetGlobalId(), z.ID)
   102  }
   103  
   104  func (z *SZone) GetStatus() string {
   105  	return "enable"
   106  }
   107  
   108  func (z *SZone) Refresh() error {
   109  	return nil
   110  }
   111  
   112  func (z *SZone) IsEmulated() bool {
   113  	return false
   114  }
   115  
   116  func (z *SZone) GetI18n() cloudprovider.SModelI18nTable {
   117  	table := cloudprovider.SModelI18nTable{}
   118  	table["name"] = cloudprovider.NewSModelI18nEntry(z.GetName()).CN(z.GetName()).EN(fmt.Sprintf("%s %s", CLOUD_PROVIDER_JDCLOUD_EN, z.Name))
   119  	return table
   120  }
   121  
   122  func (z *SZone) GetIRegion() cloudprovider.ICloudRegion {
   123  	return z.region
   124  }
   125  
   126  func (z *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   127  	if z.ihost != nil {
   128  		return []cloudprovider.ICloudHost{z.ihost}, nil
   129  	}
   130  	z.ihost = &SHost{
   131  		zone: z,
   132  	}
   133  	return []cloudprovider.ICloudHost{z.ihost}, nil
   134  }
   135  
   136  func (z *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   137  	ihosts, err := z.GetIHosts()
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	for i := range ihosts {
   142  		if ihosts[i].GetGlobalId() == id {
   143  			return ihosts[i], nil
   144  		}
   145  	}
   146  	return nil, cloudprovider.ErrNotFound
   147  }
   148  
   149  func (z *SZone) fetchStorages() error {
   150  	istorages := make([]cloudprovider.ICloudStorage, len(storageTypes))
   151  	for i := range istorages {
   152  		istorages[i] = &SStorage{
   153  			zone:        z,
   154  			storageType: storageTypes[i],
   155  		}
   156  	}
   157  	z.istorages = istorages
   158  	return nil
   159  }
   160  
   161  func (z *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   162  	if z.istorages == nil {
   163  		err := z.fetchStorages()
   164  		if err != nil {
   165  			return nil, err
   166  		}
   167  	}
   168  	return z.istorages, nil
   169  }
   170  
   171  func (z *SZone) getStorageByType(t string) (*SStorage, error) {
   172  	istorages, err := z.GetIStorages()
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  	for i := range istorages {
   177  		if istorages[i].GetStorageType() == t {
   178  			return istorages[i].(*SStorage), nil
   179  		}
   180  	}
   181  	return nil, cloudprovider.ErrNotFound
   182  }
   183  
   184  func (z *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   185  	istorages, err := z.GetIStorages()
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  	for i := range istorages {
   190  		if istorages[i].GetGlobalId() == id {
   191  			return istorages[i], nil
   192  		}
   193  	}
   194  	return nil, cloudprovider.ErrNotFound
   195  }