yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SResourceGroup struct {
    30  	multicloud.SProjectBase
    31  	AliyunTags
    32  	client *SAliyunClient
    33  
    34  	Status      string
    35  	AccountId   string
    36  	DisplayName string
    37  	Id          string
    38  	CreateDate  time.Time
    39  	Name        string
    40  }
    41  
    42  func (self *SResourceGroup) GetGlobalId() string {
    43  	return self.Id
    44  }
    45  
    46  func (self *SResourceGroup) GetId() string {
    47  	return self.Id
    48  }
    49  
    50  func (self *SResourceGroup) GetName() string {
    51  	if len(self.DisplayName) > 0 {
    52  		return self.DisplayName
    53  	}
    54  	return self.Name
    55  }
    56  
    57  func (self *SResourceGroup) Refresh() error {
    58  	group, err := self.client.GetResourceGroup(self.Id)
    59  	if err != nil {
    60  		return errors.Wrap(err, "GetResourceGroup")
    61  	}
    62  	return jsonutils.Update(self, group)
    63  }
    64  
    65  func (self *SResourceGroup) GetStatus() string {
    66  	switch self.Status {
    67  	case "Creating":
    68  		return api.EXTERNAL_PROJECT_STATUS_CREATING
    69  	case "OK":
    70  		return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
    71  	case "Deleted", "Deleting", "PendingDelete":
    72  		return api.EXTERNAL_PROJECT_STATUS_DELETING
    73  	default:
    74  		return api.EXTERNAL_PROJECT_STATUS_UNKNOWN
    75  	}
    76  }
    77  
    78  func (self *SAliyunClient) GetResourceGroups(pageNumber int, pageSize int) ([]SResourceGroup, int, error) {
    79  	if pageSize <= 0 || pageSize > 100 {
    80  		pageSize = 10
    81  	}
    82  	if pageNumber <= 0 {
    83  		pageNumber = 1
    84  	}
    85  	params := map[string]string{
    86  		"PageNumber": fmt.Sprintf("%d", pageNumber),
    87  		"PageSize":   fmt.Sprintf("%d", pageSize),
    88  	}
    89  	resp, err := self.rmRequest("ListResourceGroups", params)
    90  	if err != nil {
    91  		return nil, 0, errors.Wrap(err, "rmRequest.ListResourceGroups")
    92  	}
    93  	groups := []SResourceGroup{}
    94  	err = resp.Unmarshal(&groups, "ResourceGroups", "ResourceGroup")
    95  	if err != nil {
    96  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
    97  	}
    98  	total, _ := resp.Int("TotalCount")
    99  	return groups, int(total), nil
   100  }
   101  
   102  func (self *SAliyunClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   103  	group, err := self.CreateResourceGroup(name)
   104  	if err != nil {
   105  		return nil, errors.Wrap(err, "CreateProject")
   106  	}
   107  	return group, nil
   108  }
   109  
   110  func (self *SAliyunClient) CreateResourceGroup(name string) (*SResourceGroup, error) {
   111  	params := map[string]string{
   112  		"DisplayName": name,
   113  		"Name":        name,
   114  	}
   115  	resp, err := self.rmRequest("CreateResourceGroup", params)
   116  	if err != nil {
   117  		return nil, errors.Wrap(err, "CreateResourceGroup")
   118  	}
   119  	group := &SResourceGroup{client: self}
   120  	err = resp.Unmarshal(group, "ResourceGroup")
   121  	if err != nil {
   122  		return nil, errors.Wrap(err, "resp.Unmarshal")
   123  	}
   124  	err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3)
   125  	if err != nil {
   126  		return nil, errors.Wrap(err, "WaitStatus")
   127  	}
   128  	return group, nil
   129  }
   130  
   131  func (self *SAliyunClient) GetResourceGroup(id string) (*SResourceGroup, error) {
   132  	params := map[string]string{
   133  		"ResourceGroupId": id,
   134  	}
   135  	resp, err := self.rmRequest("GetResourceGroup", params)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	group := &SResourceGroup{client: self}
   140  	err = resp.Unmarshal(group, "ResourceGroup")
   141  	if err != nil {
   142  		return nil, errors.Wrap(err, "resp.Unmarshal")
   143  	}
   144  	return group, nil
   145  }