yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/organization.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 apsara 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 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SResourceGroupList struct { 29 tree *SOrganizationTree 30 31 Creator string 32 GmtCreated int64 33 GmtModified int64 34 Id string 35 OrganizationId int 36 OrganizationName string 37 ResourceGroupName string 38 ResourceGroupType int 39 } 40 41 type SOrganization struct { 42 Active bool 43 Alias string 44 Id string 45 Name string 46 ParentId string 47 MultiCCloudStatus string 48 ResourceGroupList []SResourceGroupList 49 SupportRegions string 50 UUID string 51 } 52 53 type ResourceGroupList []SResourceGroupList 54 55 func (rgs ResourceGroupList) ToProjects(tags []string) []SProject { 56 ret := []SProject{} 57 for _, rg := range rgs { 58 name := rg.ResourceGroupName 59 if strings.HasPrefix(name, "ResourceSet(") { 60 name = strings.TrimPrefix(name, "ResourceSet(") 61 name = strings.TrimSuffix(name, ")") 62 } 63 proj := SProject{ 64 Id: rg.Id, 65 Name: name, 66 Tags: tags, 67 } 68 ret = append(ret, proj) 69 } 70 return ret 71 } 72 73 type SOrganizationTree struct { 74 Active bool 75 Alias string 76 Id string 77 Name string 78 ParentId string 79 MultiCCloudStatus string 80 Children []SOrganizationTree 81 ResourceGroupList ResourceGroupList 82 SupportRegions string 83 UUID string 84 } 85 86 func (self *SOrganizationTree) GetProject(tags []string) []SProject { 87 ret := []SProject{} 88 if self.Name != "root" { 89 tags = append(tags, self.Name) 90 } 91 ret = append(ret, self.ResourceGroupList.ToProjects(tags)...) 92 if len(self.Children) == 0 { 93 return ret 94 } 95 for _, child := range self.Children { 96 ret = append(ret, child.GetProject(tags)...) 97 } 98 return ret 99 } 100 101 func (self *SApsaraClient) GetOrganizationTree(id string) (*SOrganizationTree, error) { 102 if len(id) == 0 { 103 id = "1" 104 } 105 params := map[string]string{ 106 "Id": id, 107 } 108 resp, err := self.ascmRequest("GetOrganizationTree", params) 109 if err != nil { 110 return nil, err 111 } 112 tree := SOrganizationTree{} 113 err = resp.Unmarshal(&tree, "data") 114 if err != nil { 115 return nil, errors.Wrapf(err, "resp.Unmarshal") 116 } 117 return &tree, nil 118 } 119 120 func (self *SApsaraClient) GetOrganizationList() ([]SOrganization, error) { 121 params := map[string]string{"Id": self.organizationId} 122 resp, err := self.ascmRequest("GetOrganizationList", params) 123 if err != nil { 124 return nil, err 125 } 126 result := []SOrganization{} 127 err = resp.Unmarshal(&result, "data") 128 if err != nil { 129 return nil, errors.Wrapf(err, "resp.Unmarshal") 130 } 131 return result, nil 132 } 133 134 func (self *SOrganizationTree) ListProjects() []SResourceGroupList { 135 ret := []SResourceGroupList{} 136 for i := range self.ResourceGroupList { 137 self.ResourceGroupList[i].tree = self 138 ret = append(ret, self.ResourceGroupList[i]) 139 } 140 for i := range self.Children { 141 ret = append(ret, self.Children[i].ListProjects()...) 142 } 143 return ret 144 } 145 146 type SProject struct { 147 multicloud.SProjectBase 148 149 client *SApsaraClient 150 Id string 151 Name string 152 Tags []string 153 } 154 155 func (self *SProject) GetId() string { 156 return self.Id 157 } 158 159 func (self *SProject) GetGlobalId() string { 160 return self.Id 161 } 162 163 func (self *SProject) GetName() string { 164 return self.Name 165 } 166 167 func (self *SProject) GetStatus() string { 168 return api.EXTERNAL_PROJECT_STATUS_AVAILABLE 169 } 170 171 func (self *SProject) GetSysTags() map[string]string { 172 return nil 173 } 174 175 func (self *SProject) SetTags(tags map[string]string, replace bool) error { 176 return cloudprovider.ErrNotSupported 177 } 178 179 func (self *SProject) GetTags() (map[string]string, error) { 180 ret := map[string]string{} 181 for i, key := range self.Tags { 182 ret[fmt.Sprintf("L%d", i+1)] = key 183 } 184 return ret, nil 185 } 186 187 func (self *SApsaraClient) GetIProjects() ([]cloudprovider.ICloudProject, error) { 188 tree, err := self.GetOrganizationTree(self.organizationId) 189 if err != nil { 190 return nil, errors.Wrapf(err, "GetOrganizationTree") 191 } 192 ret := []cloudprovider.ICloudProject{} 193 projects := tree.GetProject([]string{}) 194 for i := range projects { 195 ret = append(ret, &projects[i]) 196 } 197 return ret, nil 198 }