yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/provider/provider.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 provider
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/url"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"yunion.io/x/jsonutils"
    25  	"yunion.io/x/pkg/errors"
    26  	"yunion.io/x/pkg/util/regutils"
    27  
    28  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    29  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    30  	"yunion.io/x/onecloud/pkg/httperrors"
    31  	"yunion.io/x/onecloud/pkg/mcclient"
    32  	"yunion.io/x/cloudmux/pkg/multicloud/proxmox"
    33  )
    34  
    35  type SProxmoxProviderFactory struct {
    36  	cloudprovider.SPrivateCloudBaseProviderFactory
    37  }
    38  
    39  func (self *SProxmoxProviderFactory) GetId() string {
    40  	return proxmox.CLOUD_PROVIDER_PROXMOX
    41  }
    42  
    43  func (self *SProxmoxProviderFactory) GetName() string {
    44  	return proxmox.CLOUD_PROVIDER_PROXMOX
    45  }
    46  
    47  func (self *SProxmoxProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error {
    48  	return fmt.Errorf("Changing %s bandwidth is not supported", proxmox.CLOUD_PROVIDER_PROXMOX)
    49  }
    50  
    51  func (self *SProxmoxProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    52  	output := cloudprovider.SCloudaccount{}
    53  	if len(input.Username) == 0 {
    54  		return output, errors.Wrap(httperrors.ErrMissingParameter, "username")
    55  	}
    56  	if len(input.Password) == 0 {
    57  		return output, errors.Wrap(httperrors.ErrMissingParameter, "password")
    58  	}
    59  	if len(input.Host) == 0 {
    60  		return output, errors.Wrap(httperrors.ErrMissingParameter, "host")
    61  	}
    62  	if !regutils.MatchIPAddr(input.Host) && !regutils.MatchDomainName(input.Host) {
    63  		return output, errors.Wrap(httperrors.ErrInputParameter, "host should be ip or domain name")
    64  	}
    65  	if input.Port == 0 {
    66  		input.Port = 8006
    67  	}
    68  	output.AccessUrl = fmt.Sprintf("https://%s:%d", input.Host, input.Port)
    69  	output.Account = input.Username
    70  	output.Secret = input.Password
    71  	return output, nil
    72  }
    73  
    74  func (self *SProxmoxProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    75  	output := cloudprovider.SCloudaccount{}
    76  	if len(input.Username) == 0 {
    77  		return output, errors.Wrap(httperrors.ErrMissingParameter, "username")
    78  	}
    79  	if len(input.Password) == 0 {
    80  		return output, errors.Wrap(httperrors.ErrMissingParameter, "password")
    81  	}
    82  	output = cloudprovider.SCloudaccount{
    83  		Account: input.Username,
    84  		Secret:  input.Password,
    85  	}
    86  	return output, nil
    87  }
    88  
    89  func parseHostPort(_url string) (string, int, error) {
    90  	urlParse, err := url.Parse(_url)
    91  	if err != nil {
    92  		return "", 0, errors.Wrapf(err, "parse %s", _url)
    93  	}
    94  	port := func() int {
    95  		if len(urlParse.Port()) > 0 {
    96  			_port, _ := strconv.Atoi(urlParse.Port())
    97  			return _port
    98  		}
    99  		return 8006
   100  	}()
   101  	return strings.TrimSuffix(urlParse.Host, fmt.Sprintf(":%d", port)), port, nil
   102  }
   103  
   104  func (self *SProxmoxProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
   105  	host, port, err := parseHostPort(cfg.URL)
   106  	if err != nil {
   107  		return nil, errors.Wrapf(err, "parseHostPort")
   108  	}
   109  
   110  	client, err := proxmox.NewProxmoxClient(
   111  		proxmox.NewProxmoxClientConfig(
   112  			cfg.Account, cfg.Secret, host, port,
   113  		).CloudproviderConfig(cfg),
   114  	)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return &SProxmoxProvider{
   119  		SBaseProvider: cloudprovider.NewBaseProvider(self),
   120  		client:        client,
   121  	}, nil
   122  }
   123  
   124  func (self *SProxmoxProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   125  	host, port, err := parseHostPort(info.Url)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	return map[string]string{
   131  		"PROXMOX_HOST":     host,
   132  		"PROXMOX_PORT":     fmt.Sprintf("%d", port),
   133  		"PROXMOX_USERNAME": info.Account,
   134  		"PROXMOX_PASSWORD": info.Secret,
   135  	}, nil
   136  }
   137  
   138  func init() {
   139  	factory := SProxmoxProviderFactory{}
   140  	cloudprovider.RegisterFactory(&factory)
   141  }
   142  
   143  type SProxmoxProvider struct {
   144  	cloudprovider.SBaseProvider
   145  	client *proxmox.SProxmoxClient
   146  }
   147  
   148  func (self *SProxmoxProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   149  	return jsonutils.NewDict(), nil
   150  }
   151  
   152  func (self *SProxmoxProvider) GetVersion() string {
   153  	return "1.0"
   154  }
   155  
   156  func (self *SProxmoxProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   157  	return self.client.GetSubAccounts()
   158  }
   159  
   160  func (self *SProxmoxProvider) GetAccountId() string {
   161  	return self.client.GetAccountId()
   162  }
   163  
   164  func (self *SProxmoxProvider) GetIRegions() []cloudprovider.ICloudRegion {
   165  	return self.client.GetIRegions()
   166  }
   167  
   168  func (self *SProxmoxProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
   169  	regions := self.GetIRegions()
   170  	for i := range regions {
   171  		if regions[i].GetGlobalId() == id {
   172  			return regions[i], nil
   173  		}
   174  	}
   175  	return nil, cloudprovider.ErrNotFound
   176  }
   177  
   178  func (self *SProxmoxProvider) GetBalance() (float64, string, error) {
   179  	return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported
   180  }
   181  
   182  func (self *SProxmoxProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   183  	return []cloudprovider.ICloudProject{}, nil
   184  }
   185  
   186  func (self *SProxmoxProvider) GetStorageClasses(regionId string) []string {
   187  	return nil
   188  }
   189  
   190  func (self *SProxmoxProvider) GetBucketCannedAcls(regionId string) []string {
   191  	return nil
   192  }
   193  
   194  func (self *SProxmoxProvider) GetObjectCannedAcls(regionId string) []string {
   195  	return nil
   196  }
   197  
   198  func (self *SProxmoxProvider) GetCapabilities() []string {
   199  	return self.client.GetCapabilities()
   200  }