yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/mount_target.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 aliyun 16 17 import ( 18 "fmt" 19 "strings" 20 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 ) 26 27 type ClientMasterNode struct { 28 EcsIp string 29 EcsId string 30 DefaultPasswd string 31 } 32 33 type ClientMasterNodes struct { 34 ClientMasterNode []ClientMasterNode 35 } 36 37 type SMountTarget struct { 38 fs *SFileSystem 39 40 Status string 41 NetworkType string 42 VswId string 43 VpcId string 44 MountTargetDomain string 45 AccessGroup string 46 ClientMasterNodes ClientMasterNodes 47 } 48 49 func (self *SMountTarget) GetGlobalId() string { 50 return self.MountTargetDomain 51 } 52 53 func (self *SMountTarget) GetName() string { 54 return self.MountTargetDomain 55 } 56 57 func (self *SMountTarget) GetNetworkType() string { 58 return strings.ToLower(self.NetworkType) 59 } 60 61 func (self *SMountTarget) GetStatus() string { 62 switch self.Status { 63 case "Active": 64 return api.MOUNT_TARGET_STATUS_AVAILABLE 65 case "Inactive": 66 return api.MOUNT_TARGET_STATUS_UNAVAILABLE 67 case "Pending": 68 return api.MOUNT_TARGET_STATUS_CREATING 69 case "Deleting": 70 return api.MOUNT_TARGET_STATUS_DELETING 71 default: 72 return strings.ToLower(self.Status) 73 } 74 } 75 76 func (self *SMountTarget) GetVpcId() string { 77 return self.VpcId 78 } 79 80 func (self *SMountTarget) GetNetworkId() string { 81 return self.VswId 82 } 83 84 func (self *SMountTarget) GetDomainName() string { 85 return self.MountTargetDomain 86 } 87 88 func (self *SMountTarget) GetAccessGroupId() string { 89 return fmt.Sprintf("%s/%s", self.fs.FileSystemType, self.AccessGroup) 90 } 91 92 func (self *SMountTarget) Delete() error { 93 return self.fs.region.DeleteMountTarget(self.fs.FileSystemId, self.MountTargetDomain) 94 } 95 96 func (self *SRegion) GetMountTargets(fsId, domainName string, pageSize, pageNum int) ([]SMountTarget, int, error) { 97 if pageSize < 1 || pageSize > 100 { 98 pageSize = 50 99 } 100 if pageNum < 1 { 101 pageNum = 1 102 } 103 params := map[string]string{ 104 "RegionId": self.RegionId, 105 "FileSystemId": fsId, 106 "PageSize": fmt.Sprintf("%d", pageSize), 107 "PageNumber": fmt.Sprintf("%d", pageNum), 108 } 109 if len(domainName) > 0 { 110 params["MountTargetDomain"] = domainName 111 } 112 resp, err := self.nasRequest("DescribeMountTargets", params) 113 if err != nil { 114 return nil, 0, errors.Wrapf(err, "DescribeMountTargets") 115 } 116 ret := struct { 117 TotalCount int 118 MountTargets struct { 119 MountTarget []SMountTarget 120 } 121 }{} 122 err = resp.Unmarshal(&ret) 123 if err != nil { 124 return nil, 0, errors.Wrapf(err, "resp.Unmarshal") 125 } 126 return ret.MountTargets.MountTarget, ret.TotalCount, nil 127 } 128 129 func (self *SFileSystem) GetMountTargets() ([]cloudprovider.ICloudMountTarget, error) { 130 targets := []SMountTarget{} 131 num := 1 132 for { 133 part, total, err := self.region.GetMountTargets(self.FileSystemId, "", 50, num) 134 if err != nil { 135 return nil, errors.Wrapf(err, "GetMountTargets") 136 } 137 targets = append(targets, part...) 138 if len(part) >= total { 139 break 140 } 141 } 142 ret := []cloudprovider.ICloudMountTarget{} 143 for i := range targets { 144 targets[i].fs = self 145 ret = append(ret, &targets[i]) 146 } 147 return ret, nil 148 } 149 150 func (self *SRegion) DeleteMountTarget(fsId, id string) error { 151 params := map[string]string{ 152 "RegionId": self.RegionId, 153 "FileSystemId": fsId, 154 "MountTargetDomain": id, 155 } 156 _, err := self.nasRequest("DeleteMountTarget", params) 157 return errors.Wrapf(err, "DeleteMountTarget") 158 }