yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/sfs-turbo.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 huawei 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 25 billing_api "yunion.io/x/cloudmux/pkg/apis/billing" 26 api "yunion.io/x/cloudmux/pkg/apis/compute" 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/cloudmux/pkg/multicloud" 29 ) 30 31 type SfsTurbo struct { 32 multicloud.SNasBase 33 HuaweiTags 34 region *SRegion 35 36 EnterpriseProjectId string 37 Actions []string 38 AvailCapacity float64 39 AvailabilityZone string 40 AzName string 41 CreatedAt time.Time 42 CryptKeyId string 43 ExpandType string 44 ExportLocation string 45 Id string 46 Name string 47 PayModel string 48 Region string 49 SecurityGroupId string 50 ShareProto string 51 ShareType string 52 Size float64 53 Status string 54 SubStatus string 55 SubnetId string 56 VpcId string 57 Description string 58 } 59 60 func (self *SfsTurbo) GetName() string { 61 return self.Name 62 } 63 64 func (self *SfsTurbo) GetId() string { 65 return self.Id 66 } 67 68 func (self *SfsTurbo) GetGlobalId() string { 69 return self.Id 70 } 71 72 func (self *SfsTurbo) GetFileSystemType() string { 73 return "SFS Turbo" 74 } 75 76 func (self *SfsTurbo) Refresh() error { 77 sf, err := self.region.GetSfsTurbo(self.Id) 78 if err != nil { 79 return errors.Wrapf(err, "GetSfsTurbo") 80 } 81 return jsonutils.Update(self, sf) 82 } 83 84 func (self *SfsTurbo) GetBillingType() string { 85 if self.PayModel == "0" { 86 return billing_api.BILLING_TYPE_POSTPAID 87 } 88 return billing_api.BILLING_TYPE_PREPAID 89 } 90 91 func (self *SfsTurbo) GetStorageType() string { 92 if len(self.ExpandType) == 0 { 93 return strings.ToLower(self.ShareType) 94 } 95 return strings.ToLower(self.ShareType) + ".enhanced" 96 } 97 98 func (self *SfsTurbo) GetProtocol() string { 99 return self.ShareProto 100 } 101 102 func (self *SfsTurbo) GetStatus() string { 103 switch self.Status { 104 case "100": 105 return api.NAS_STATUS_CREATING 106 case "200": 107 return api.NAS_STATUS_AVAILABLE 108 case "300": 109 return api.NAS_STATUS_UNKNOWN 110 case "303": 111 return api.NAS_STATUS_CREATE_FAILED 112 case "400": 113 return api.NAS_STATUS_DELETING 114 case "800": 115 return api.NAS_STATUS_UNAVAILABLE 116 default: 117 return self.Status 118 } 119 } 120 121 func (self *SfsTurbo) GetCreatedAt() time.Time { 122 return self.CreatedAt 123 } 124 125 func (self *SfsTurbo) GetCapacityGb() int64 { 126 return int64(self.Size) 127 } 128 129 func (self *SfsTurbo) GetUsedCapacityGb() int64 { 130 return int64(self.Size - self.AvailCapacity) 131 } 132 133 func (self *SfsTurbo) GetMountTargetCountLimit() int { 134 return 1 135 } 136 137 func (self *SfsTurbo) GetZoneId() string { 138 return self.AvailabilityZone 139 } 140 141 func (self *SfsTurbo) GetMountTargets() ([]cloudprovider.ICloudMountTarget, error) { 142 mt := &sMoutTarget{sfs: self} 143 return []cloudprovider.ICloudMountTarget{mt}, nil 144 } 145 146 func (self *SfsTurbo) CreateMountTarget(opts *cloudprovider.SMountTargetCreateOptions) (cloudprovider.ICloudMountTarget, error) { 147 return nil, errors.Wrap(cloudprovider.ErrNotSupported, "CreateMountTarget") 148 } 149 150 func (self *SfsTurbo) Delete() error { 151 return self.region.DeleteSfsTurbo(self.Id) 152 } 153 154 func (self *SRegion) GetICloudFileSystems() ([]cloudprovider.ICloudFileSystem, error) { 155 sfs, err := self.GetSfsTurbos() 156 if err != nil { 157 return nil, errors.Wrapf(err, "self.GetSfsTurbos") 158 } 159 ret := []cloudprovider.ICloudFileSystem{} 160 for i := range sfs { 161 sfs[i].region = self 162 ret = append(ret, &sfs[i]) 163 } 164 return ret, nil 165 } 166 167 func (self *SRegion) GetICloudFileSystemById(id string) (cloudprovider.ICloudFileSystem, error) { 168 sf, err := self.GetSfsTurbo(id) 169 if err != nil { 170 return nil, errors.Wrapf(err, "GetSfsTurbo(%s)", id) 171 } 172 return sf, nil 173 } 174 175 func (self *SRegion) GetSfsTurbos() ([]SfsTurbo, error) { 176 queues := make(map[string]string) 177 sfs := make([]SfsTurbo, 0, 2) 178 err := doListAllWithOffset(self.ecsClient.SfsTurbos.List, queues, &sfs) 179 if err != nil { 180 return nil, errors.Wrapf(err, "doListAllWithOffset") 181 } 182 return sfs, nil 183 } 184 185 func (self *SRegion) GetSfsTurbo(id string) (*SfsTurbo, error) { 186 sf := &SfsTurbo{region: self} 187 err := DoGet(self.ecsClient.SfsTurbos.Get, id, nil, &sf) 188 return sf, errors.Wrapf(err, "self.ecsClient.SfsTurbos.Get") 189 } 190 191 func (self *SRegion) DeleteSfsTurbo(id string) error { 192 return DoDelete(self.ecsClient.SfsTurbos.Delete, id, nil, nil) 193 } 194 195 func (self *SRegion) GetSysDefaultSecgroupId() (string, error) { 196 secs, err := self.GetSecurityGroups("default", "") 197 if err != nil { 198 return "", errors.Wrapf(err, "GetSecurityGroups") 199 } 200 if len(secs) > 0 { 201 return secs[0].ID, nil 202 } 203 return "", fmt.Errorf("not found default security group") 204 } 205 206 func (self *SRegion) CreateICloudFileSystem(opts *cloudprovider.FileSystemCraeteOptions) (cloudprovider.ICloudFileSystem, error) { 207 fs, err := self.CreateSfsTurbo(opts) 208 if err != nil { 209 return nil, errors.Wrapf(err, "CreateSfsTurbo") 210 } 211 return fs, nil 212 } 213 214 func (self *SRegion) CreateSfsTurbo(opts *cloudprovider.FileSystemCraeteOptions) (*SfsTurbo, error) { 215 secId, err := self.GetSysDefaultSecgroupId() 216 if err != nil { 217 return nil, errors.Wrapf(err, "GetSysDefaultSecgroupId") 218 } 219 metadata := map[string]string{} 220 if strings.HasSuffix(opts.StorageType, ".enhanced") { 221 metadata["expand_type"] = "bandwidth" 222 } 223 params := map[string]interface{}{ 224 "share": map[string]interface{}{ 225 "name": opts.Name, 226 "share_proto": strings.ToUpper(opts.Protocol), 227 "share_type": strings.ToUpper(strings.TrimSuffix(opts.StorageType, ".enhanced")), 228 "size": opts.Capacity, 229 "availability_zone": opts.ZoneId, 230 "vpc_id": opts.VpcId, 231 "subnet_id": opts.NetworkId, 232 "security_group_id": secId, 233 "description": opts.Desc, 234 "metadata": metadata, 235 }, 236 } 237 resp, err := self.ecsClient.SfsTurbos.Create(jsonutils.Marshal(params)) 238 if err != nil { 239 return nil, errors.Wrapf(err, "Create") 240 } 241 id, err := resp.GetString("id") 242 if err != nil { 243 return nil, errors.Wrapf(err, "resp.GetString(id)") 244 } 245 return self.GetSfsTurbo(id) 246 } 247 248 func (self *SRegion) GetICloudAccessGroups() ([]cloudprovider.ICloudAccessGroup, error) { 249 return []cloudprovider.ICloudAccessGroup{}, nil 250 } 251 252 func (self *SRegion) CreateICloudAccessGroup(opts *cloudprovider.SAccessGroup) (cloudprovider.ICloudAccessGroup, error) { 253 return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "CreateICloudAccessGroup") 254 } 255 256 func (self *SRegion) GetICloudAccessGroupById(id string) (cloudprovider.ICloudAccessGroup, error) { 257 return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetICloudAccessGroupById(%s)", id) 258 } 259 260 type sMoutTarget struct { 261 sfs *SfsTurbo 262 } 263 264 func (self *sMoutTarget) GetName() string { 265 return self.sfs.Name 266 } 267 268 func (self *sMoutTarget) GetGlobalId() string { 269 return self.sfs.GetGlobalId() 270 } 271 272 func (self *sMoutTarget) GetAccessGroupId() string { 273 return "" 274 } 275 276 func (self *sMoutTarget) GetDomainName() string { 277 return self.sfs.ExportLocation 278 } 279 280 func (self *sMoutTarget) GetNetworkType() string { 281 return api.NETWORK_TYPE_VPC 282 } 283 284 func (self *sMoutTarget) GetNetworkId() string { 285 return self.sfs.SubnetId 286 } 287 288 func (self *sMoutTarget) GetVpcId() string { 289 return self.sfs.VpcId 290 } 291 292 func (self *sMoutTarget) GetStatus() string { 293 return api.MOUNT_TARGET_STATUS_AVAILABLE 294 } 295 296 func (self *sMoutTarget) Delete() error { 297 return nil 298 }