yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/jdcloud.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 jdcloud
    16  
    17  import (
    18  	"github.com/jdcloud-api/jdcloud-sdk-go/core"
    19  
    20  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  )
    23  
    24  type SJDCloudClient struct {
    25  	*JDCloudClientConfig
    26  
    27  	iregion []cloudprovider.ICloudRegion
    28  }
    29  
    30  type JDCloudClientConfig struct {
    31  	cpcfg        cloudprovider.ProviderConfig
    32  	accessKey    string
    33  	accessSecret string
    34  	debug        bool
    35  }
    36  
    37  func NewJDCloudClientConfig(accessKey, accessSecret string) *JDCloudClientConfig {
    38  	cfg := &JDCloudClientConfig{
    39  		accessKey:    accessKey,
    40  		accessSecret: accessSecret,
    41  	}
    42  	return cfg
    43  }
    44  
    45  func (cfg *JDCloudClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *JDCloudClientConfig {
    46  	cfg.cpcfg = cpcfg
    47  	return cfg
    48  }
    49  
    50  func (cfg *JDCloudClientConfig) Debug(debug bool) *JDCloudClientConfig {
    51  	cfg.debug = debug
    52  	return cfg
    53  }
    54  
    55  func (self *SJDCloudClient) getCredential() *core.Credential {
    56  	return core.NewCredentials(self.accessKey, self.accessSecret)
    57  }
    58  
    59  func NewJDCloudClient(cfg *JDCloudClientConfig) (*SJDCloudClient, error) {
    60  	client := SJDCloudClient{
    61  		JDCloudClientConfig: cfg,
    62  	}
    63  	_, err := client.DescribeAccountAmount()
    64  	return &client, err
    65  }
    66  
    67  func (self *SJDCloudClient) GetIRegions() []cloudprovider.ICloudRegion {
    68  	if self.iregion != nil {
    69  		return self.iregion
    70  	}
    71  
    72  	self.iregion = []cloudprovider.ICloudRegion{}
    73  	for id, name := range regionList {
    74  		self.iregion = append(self.iregion, &SRegion{
    75  			ID:     id,
    76  			Name:   name,
    77  			client: self,
    78  		})
    79  	}
    80  	return self.iregion
    81  }
    82  
    83  func (self *SJDCloudClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
    84  	subAccount := cloudprovider.SSubAccount{}
    85  	subAccount.Name = self.cpcfg.Name
    86  	subAccount.Account = self.accessKey
    87  	subAccount.HealthStatus = api.CLOUD_PROVIDER_HEALTH_NORMAL
    88  	return []cloudprovider.SSubAccount{subAccount}, nil
    89  }
    90  
    91  func (self *SJDCloudClient) GetAccountId() string {
    92  	return self.accessKey
    93  }
    94  
    95  func (self *SJDCloudClient) GetRegion(regionId string) *SRegion {
    96  	iregions := self.GetIRegions()
    97  	for i := range iregions {
    98  		if len(regionId) == 0 || iregions[i].GetId() == regionId {
    99  			return iregions[i].(*SRegion)
   100  		}
   101  	}
   102  	return nil
   103  }