yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SProject struct {
    27  	multicloud.SProjectBase
    28  	GoogleTags
    29  	Name           string
    30  	CreateTime     time.Time
    31  	LifecycleState string
    32  	ProjectId      string
    33  	ProjectNumber  string
    34  }
    35  
    36  func (cli *SGoogleClient) GetProject(id string) (*SProject, error) {
    37  	project := &SProject{}
    38  	resp, err := cli.managerGet(id)
    39  	if err != nil {
    40  		return nil, errors.Wrap(err, "managerGet")
    41  	}
    42  	err = resp.Unmarshal(project)
    43  	if err != nil {
    44  		return nil, errors.Wrap(err, "resp.Unmarshal")
    45  	}
    46  	return project, nil
    47  }
    48  
    49  func (cli *SGoogleClient) GetProjects() ([]SProject, error) {
    50  	nextPageToken := ""
    51  	params := map[string]string{}
    52  	result := []SProject{}
    53  	for {
    54  		if len(nextPageToken) > 0 {
    55  			params["pageToken"] = nextPageToken
    56  		}
    57  		resp, err := cli.managerList("projects", params)
    58  		if err != nil {
    59  			return nil, errors.Wrap(err, "managerList")
    60  		}
    61  		_result := []SProject{}
    62  		if resp.Contains("projects") {
    63  			err = resp.Unmarshal(&_result, "projects")
    64  			if err != nil {
    65  				return nil, errors.Wrap(err, "data.Unmarshal")
    66  			}
    67  		}
    68  		result = append(result, _result...)
    69  		nextPageToken, _ = resp.GetString("nextPageToken")
    70  		if len(nextPageToken) == 0 || len(_result) == 0 {
    71  			break
    72  		}
    73  	}
    74  	return result, nil
    75  }
    76  
    77  func (p *SProject) GetName() string {
    78  	return p.Name
    79  }
    80  
    81  func (p *SProject) GetId() string {
    82  	return p.ProjectId
    83  }
    84  
    85  func (p *SProject) GetGlobalId() string {
    86  	return p.ProjectId
    87  }
    88  
    89  func (p *SProject) GetStatus() string {
    90  	return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
    91  }
    92  
    93  func (p *SProject) Refresh() error {
    94  	return nil
    95  }
    96  
    97  func (p *SProject) IsEmulated() bool {
    98  	return false
    99  }