yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/project.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 "time" 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 DepartmentInfo struct { 29 Department string 30 DepartmentName string 31 ResourceGroup string 32 ResourceGroupName string 33 ResourceGroupId string 34 } 35 36 func (self DepartmentInfo) GetProjectId() string { 37 return self.ResourceGroup 38 } 39 40 type SResourceGroup struct { 41 multicloud.SProjectBase 42 ApsaraTags 43 client *SApsaraClient 44 45 Creator string 46 GmtCreated string 47 Id string 48 OrganizationId string 49 OrganizationName string 50 ResourceGroupName string 51 RsId string 52 } 53 54 func (self *SResourceGroup) GetGlobalId() string { 55 return self.Id 56 } 57 58 func (self *SResourceGroup) GetId() string { 59 return self.Id 60 } 61 62 func (self *SResourceGroup) GetDomainName() string { 63 return "" 64 } 65 66 func (self *SResourceGroup) GetDomainId() string { 67 return "" 68 } 69 70 func (self *SResourceGroup) GetName() string { 71 return self.ResourceGroupName 72 } 73 74 func (self *SResourceGroup) Refresh() error { 75 return nil 76 } 77 78 func (self *SResourceGroup) GetStatus() string { 79 return api.EXTERNAL_PROJECT_STATUS_AVAILABLE 80 } 81 82 func (self *SApsaraClient) GetResourceGroups(pageNumber int, pageSize int) ([]SResourceGroup, int, error) { 83 if pageSize <= 0 || pageSize > 100 { 84 pageSize = 10 85 } 86 if pageNumber <= 0 { 87 pageNumber = 1 88 } 89 params := map[string]string{ 90 "PageNumber": fmt.Sprintf("%d", pageNumber), 91 "PageSize": fmt.Sprintf("%d", pageSize), 92 } 93 resp, err := self.ascmRequest("ListResourceGroup", params) 94 if err != nil { 95 return nil, 0, errors.Wrap(err, "rmRequest.ListResourceGroup") 96 } 97 groups := []SResourceGroup{} 98 err = resp.Unmarshal(&groups, "data") 99 if err != nil { 100 return nil, 0, errors.Wrap(err, "resp.Unmarshal") 101 } 102 total, _ := resp.Int("pageInfo", "total") 103 return groups, int(total), nil 104 } 105 106 func (self *SApsaraClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) { 107 group, err := self.CreateResourceGroup(name) 108 if err != nil { 109 return nil, errors.Wrap(err, "CreateProject") 110 } 111 return group, nil 112 } 113 114 func (self *SApsaraClient) CreateResourceGroup(name string) (*SResourceGroup, error) { 115 params := map[string]string{ 116 "DisplayName": name, 117 "Name": name, 118 } 119 resp, err := self.ascmRequest("CreateResourceGroup", params) 120 if err != nil { 121 return nil, errors.Wrap(err, "CreateResourceGroup") 122 } 123 group := &SResourceGroup{client: self} 124 err = resp.Unmarshal(group, "ResourceGroup") 125 if err != nil { 126 return nil, errors.Wrap(err, "resp.Unmarshal") 127 } 128 err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3) 129 if err != nil { 130 return nil, errors.Wrap(err, "WaitStatus") 131 } 132 return group, nil 133 } 134 135 func (self *SApsaraClient) GetResourceGroup(id string) (*SResourceGroup, error) { 136 params := map[string]string{ 137 "ResourceGroupId": id, 138 } 139 resp, err := self.ascmRequest("GetResourceGroup", params) 140 if err != nil { 141 return nil, err 142 } 143 group := &SResourceGroup{client: self} 144 err = resp.Unmarshal(group, "ResourceGroup") 145 if err != nil { 146 return nil, errors.Wrap(err, "resp.Unmarshal") 147 } 148 return group, nil 149 }