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