| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package baidu
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "encoding/hex"
- "fmt"
- "net/url"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/utils"
- )
- type SHost struct {
- multicloud.SHostBase
- zone *SZone
- }
- func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
- vms, err := host.zone.region.GetInstances("", []string{})
- if err != nil {
- return nil, err
- }
- ivms := make([]cloudprovider.ICloudVM, len(vms))
- for i := 0; i < len(vms); i += 1 {
- vms[i].host = host
- ivms[i] = &vms[i]
- }
- return ivms, nil
- }
- func (host *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
- vm, err := host.zone.region.CreateInstance(host.zone.ZoneName, opts)
- if err != nil {
- return nil, err
- }
- vm.host = host
- return vm, nil
- }
- func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
- padding := blockSize - len(ciphertext)%blockSize
- padtext := bytes.Repeat([]byte{byte(padding)}, padding)
- return append(ciphertext, padtext...)
- }
- type ecb struct {
- b cipher.Block
- blockSize int
- }
- func newECB(b cipher.Block) *ecb {
- return &ecb{
- b: b,
- blockSize: b.BlockSize(),
- }
- }
- type ecbEncrypter ecb
- func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
- return (*ecbEncrypter)(newECB(b))
- }
- func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
- func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
- if len(src)%x.blockSize != 0 {
- panic("crypto/cipher: input not full blocks")
- }
- if len(dst) < len(src) {
- panic("crypto/cipher: output smaller than input")
- }
- for len(src) > 0 {
- x.b.Encrypt(dst, src[:x.blockSize])
- src = src[x.blockSize:]
- dst = dst[x.blockSize:]
- }
- }
- func AesECBEncryptHex(key, message string) (string, error) {
- // ECB is left out intentionally because it's insecure, check https://github.com/golang/go/issues/5597
- if len(key) < 16 {
- return "", fmt.Errorf("Invalid SecretKey")
- }
- keyBytes := []byte(key[:16])
- msgBytes := []byte(message)
- block, err := aes.NewCipher(keyBytes)
- if err != nil {
- return "", err
- }
- blockSize := block.BlockSize()
- msgBytes = PKCS7Padding(msgBytes, blockSize)
- blockMode := NewECBEncrypter(block)
- crypted := make([]byte, len(msgBytes))
- blockMode.CryptBlocks(crypted, msgBytes)
- return hex.EncodeToString(crypted), nil
- }
- func (region *SRegion) CreateInstance(zoneName string, opts *cloudprovider.SManagedVMCreateConfig) (*SInstance, error) {
- params := url.Values{}
- params.Set("clientToken", utils.GenRequestId(20))
- tags := []BaiduTag{}
- for k, v := range opts.Tags {
- tags = append(tags, BaiduTag{
- TagKey: k,
- TagValue: v,
- })
- }
- billing := map[string]interface{}{
- "paymentTiming": "Postpaid",
- }
- if opts.BillingCycle != nil {
- billing["paymentTiming"] = "Prepaid"
- reservation := map[string]interface{}{}
- if opts.BillingCycle.GetYears() > 0 {
- reservation["reservationTimeUnit"] = "year"
- reservation["reservationLength"] = opts.BillingCycle.GetYears()
- } else if opts.BillingCycle.GetMonths() > 0 {
- reservation["reservationTimeUnit"] = "month"
- reservation["reservationLength"] = opts.BillingCycle.GetMonths()
- }
- billing["reservation"] = reservation
- }
- disks := []map[string]interface{}{}
- for _, disk := range opts.DataDisks {
- disks = append(disks, map[string]interface{}{
- "cdsSizeInGB": disk.SizeGB,
- "storageType": disk.StorageType,
- })
- }
- body := map[string]interface{}{
- "imageId": opts.ExternalImageId,
- "spec": opts.InstanceType,
- "rootDiskSizeInGb": opts.SysDisk.SizeGB,
- "rootDiskStorageType": opts.SysDisk.StorageType,
- "networkCapacityInMbps": opts.PublicIpBw,
- "name": opts.Name,
- "hostname": opts.Hostname,
- "adminPass": opts.Password,
- "zoneName": zoneName,
- "subnetId": opts.ExternalNetworkId,
- "tags": tags,
- "userData": opts.UserData,
- "billing": billing,
- "createCdsList": disks,
- }
- if len(opts.Password) > 0 {
- var err error
- body["adminPass"], err = AesECBEncryptHex(region.client.accessKeySecret, opts.Password)
- if err != nil {
- return nil, errors.Wrapf(err, "AesECBEncryptHex")
- }
- }
- if len(opts.IpAddr) > 0 {
- body["internalIps"] = []string{opts.IpAddr}
- }
- if len(opts.PublicKey) > 0 {
- keypair, err := region.SyncKeypair(opts.KeypairName, opts.PublicKey)
- if err != nil {
- return nil, err
- }
- body["keypairId"] = keypair.KeypairId
- }
- if len(opts.ExternalSecgroupIds) > 0 {
- body["securityGroupId"] = opts.ExternalSecgroupIds[0]
- }
- resp, err := region.bccPost("v2/instanceBySpec", params, body)
- if err != nil {
- return nil, err
- }
- ret := struct {
- InstanceIds []string
- WarningList []string
- }{}
- err = resp.Unmarshal(&ret)
- if err != nil {
- return nil, errors.Wrapf(err, "Unmarshal %s", resp.String())
- }
- for _, vmId := range ret.InstanceIds {
- return region.GetInstance(vmId)
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "after create %s", resp.String())
- }
- func (host *SHost) GetAccessIp() string {
- return ""
- }
- func (host *SHost) GetAccessMac() string {
- return ""
- }
- func (host *SHost) GetName() string {
- return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetId())
- }
- func (host *SHost) GetNodeCount() int8 {
- return 0
- }
- func (host *SHost) GetSN() string {
- return ""
- }
- func (host *SHost) GetStatus() string {
- return api.HOST_STATUS_RUNNING
- }
- func (host *SHost) GetCpuCount() int {
- return 0
- }
- func (host *SHost) GetCpuDesc() string {
- return ""
- }
- func (host *SHost) GetCpuMhz() int {
- return 0
- }
- func (host *SHost) GetMemSizeMB() int {
- return 0
- }
- func (host *SHost) GetStorageSizeMB() int64 {
- return 0
- }
- func (host *SHost) GetStorageClass() string {
- return ""
- }
- func (host *SHost) GetStorageType() string {
- return api.DISK_TYPE_HYBRID
- }
- func (host *SHost) GetEnabled() bool {
- return true
- }
- func (host *SHost) GetIsMaintenance() bool {
- return false
- }
- func (host *SHost) IsEmulated() bool {
- return true
- }
- func (host *SHost) GetGlobalId() string {
- return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
- }
- func (host *SHost) GetId() string {
- return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
- }
- func (host *SHost) GetHostStatus() string {
- return api.HOST_ONLINE
- }
- func (host *SHost) GetHostType() string {
- return api.HOST_TYPE_BAIDU
- }
- func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
- wires, err := host.zone.GetIWires()
- if err != nil {
- return nil, errors.Wrap(err, "GetIWires")
- }
- return cloudprovider.GetHostNetifs(host, wires), nil
- }
- func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
- return host.zone.GetIStorageById(id)
- }
- func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
- return host.zone.GetIStorages()
- }
- func (host *SHost) GetIVMById(vmId string) (cloudprovider.ICloudVM, error) {
- vm, err := host.zone.region.GetInstance(vmId)
- if err != nil {
- return nil, err
- }
- vm.host = host
- return vm, nil
- }
- func (host *SHost) GetSysInfo() jsonutils.JSONObject {
- info := jsonutils.NewDict()
- info.Add(jsonutils.NewString(CLOUD_PROVIDER_BAIDU_CN), "manufacture")
- return info
- }
- func (host *SHost) GetVersion() string {
- return ""
- }
|