yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    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 SProject struct {
    30  	multicloud.SProjectBase
    31  	QcloudTags
    32  	client *SQcloudClient
    33  
    34  	ProjectName string    `json:"projectName"`
    35  	ProjectId   string    `json:"projectId"`
    36  	CreateTime  time.Time `json:"createTime"`
    37  	CreateorUin int       `json:"creatorUin"`
    38  	ProjectInfo string    `json:"projectInfo"`
    39  }
    40  
    41  func (p *SProject) GetId() string {
    42  	var pId string
    43  	pos := strings.Index(p.ProjectId, ".")
    44  	if pos >= 0 {
    45  		pId = p.ProjectId[:pos]
    46  	} else {
    47  		pId = p.ProjectId
    48  	}
    49  	return pId
    50  }
    51  
    52  func (p *SProject) GetGlobalId() string {
    53  	return p.GetId()
    54  }
    55  
    56  func (p *SProject) GetName() string {
    57  	return p.ProjectName
    58  }
    59  
    60  func (p *SProject) GetStatus() string {
    61  	return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
    62  }
    63  
    64  func (p *SProject) IsEmulated() bool {
    65  	return false
    66  }
    67  
    68  func (p *SProject) Refresh() error {
    69  	return nil
    70  }
    71  
    72  func (client *SQcloudClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
    73  	return client.CreateProject(name, "")
    74  }
    75  
    76  func (client *SQcloudClient) GetProjects() ([]SProject, error) {
    77  	projects := []SProject{}
    78  	params := map[string]string{"allList": "1"}
    79  	resp, err := client.accountRequestRequest("DescribeProject", params)
    80  	if err != nil {
    81  		return nil, errors.Wrapf(err, "DescribeProject")
    82  	}
    83  	err = resp.Unmarshal(&projects)
    84  	if err != nil {
    85  		return nil, errors.Wrap(err, "resp.Unmarshal")
    86  	}
    87  	return projects, nil
    88  }
    89  
    90  func (client *SQcloudClient) CreateProject(name, desc string) (*SProject, error) {
    91  	params := map[string]string{
    92  		"projectName": name,
    93  	}
    94  	if len(desc) > 0 {
    95  		params["projectDesc"] = desc
    96  	}
    97  	body, err := client.accountRequestRequest("AddProject", params)
    98  	if err != nil {
    99  		return nil, errors.Wrap(err, "AddProject")
   100  	}
   101  	projectId, _ := body.GetString("projectId")
   102  	if len(projectId) == 0 {
   103  		return nil, fmt.Errorf("empty project reture")
   104  	}
   105  	projects, err := client.GetProjects()
   106  	if err != nil {
   107  		return nil, errors.Wrap(err, "GetProjects")
   108  	}
   109  	for i := range projects {
   110  		if projects[i].GetId() == projectId {
   111  			return &projects[i], nil
   112  		}
   113  	}
   114  	return nil, fmt.Errorf("failedd to found created project")
   115  }