fix(region): vendor update (#18464)

This commit is contained in:
屈轩
2023-10-27 16:07:53 +08:00
committed by GitHub
parent 908a43cbd8
commit 615ef32d46
29 changed files with 366 additions and 540 deletions

2
go.mod
View File

@@ -83,7 +83,7 @@ require (
k8s.io/client-go v0.19.3
k8s.io/cluster-bootstrap v0.19.3
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027025551-a53fc39099d0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027072226-b82b6cc2b7d2
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
yunion.io/x/jsonutils v1.0.1-0.20230613121553-0f3b41e2ef19
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361

4
go.sum
View File

@@ -1179,8 +1179,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027025551-a53fc39099d0 h1:vIfj2xB25ZhXF8HQgsUfUIzos81nkz+n+RCMvxeDqe8=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027025551-a53fc39099d0/go.mod h1:McRjoG2gaOUisB+Qa41kLCNZhr0lsCu4apEjTiphXVY=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027072226-b82b6cc2b7d2 h1:767uclY/nlFXrd4LN6WRtkphTZHT+KYHDaH2PdhZnAI=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027072226-b82b6cc2b7d2/go.mod h1:McRjoG2gaOUisB+Qa41kLCNZhr0lsCu4apEjTiphXVY=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=

View File

@@ -20,20 +20,21 @@ const (
)
const (
VNC = "vnc"
ALIYUN = "aliyun"
QCLOUD = "qcloud"
OPENSTACK = "openstack"
SPICE = "spice"
WMKS = "wmks"
WS = "websocket"
VMRC = "vmrc"
ZSTACK = "zstack"
CTYUN = "ctyun"
HUAWEI = "huawei"
HCS = "hcs"
APSARA = "apsara"
JDCLOUD = "jdcloud"
CLOUDPODS = "cloudpods"
PROXMOX = "proxmox"
VNC = "vnc"
ALIYUN = "aliyun"
QCLOUD = "qcloud"
OPENSTACK = "openstack"
SPICE = "spice"
WMKS = "wmks"
WS = "websocket"
VMRC = "vmrc"
ZSTACK = "zstack"
CTYUN = "ctyun"
HUAWEI = "huawei"
HCS = "hcs"
APSARA = "apsara"
JDCLOUD = "jdcloud"
CLOUDPODS = "cloudpods"
PROXMOX = "proxmox"
VOLC_ENGINE = "volcengine"
)

View File

@@ -245,7 +245,8 @@ func handleServerRemoteConsole(ctx context.Context, w http.ResponseWriter, r *ht
case session.ALIYUN, session.QCLOUD, session.OPENSTACK,
session.VMRC, session.ZSTACK, session.CTYUN,
session.HUAWEI, session.HCS, session.APSARA,
session.JDCLOUD, session.CLOUDPODS, session.PROXMOX:
session.JDCLOUD, session.CLOUDPODS, session.PROXMOX,
session.VOLCENGINE:
responsePublicCloudConsole(ctx, info, w)
case session.VNC, session.SPICE, session.WMKS:
handleDataSession(ctx, info, w, url.Values{"password": {info.GetPassword()}}, true)

View File

@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/appctx"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/httperrors"
@@ -58,7 +59,12 @@ func (s *ConnectionServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch protocol {
case session.VNC, session.SPICE:
info := sessionObj.ISessionData.(*session.RemoteConsoleInfo)
if info.Hypervisor == api.HYPERVISOR_OPENSTACK || info.Hypervisor == api.HYPERVISOR_PROXMOX || info.Hypervisor == api.HYPERVISOR_CTYUN {
if utils.IsInStringArray(info.Hypervisor, []string{
api.HYPERVISOR_OPENSTACK,
api.HYPERVISOR_PROXMOX,
api.HYPERVISOR_CTYUN,
api.HYPERVISOR_VOLCENGINE,
}) {
srv, err = NewWebsocketProxyServer(sessionObj)
} else {
srv, err = NewWebsockifyServer(sessionObj)

View File

@@ -30,22 +30,23 @@ import (
)
const (
VNC = api.VNC
ALIYUN = api.ALIYUN
QCLOUD = api.QCLOUD
OPENSTACK = api.OPENSTACK
SPICE = api.SPICE
WMKS = api.WMKS
WS = api.WS
VMRC = api.VMRC
ZSTACK = api.ZSTACK
CTYUN = api.CTYUN
HUAWEI = api.HUAWEI
HCS = api.HCS
APSARA = api.APSARA
JDCLOUD = api.JDCLOUD
CLOUDPODS = api.CLOUDPODS
PROXMOX = api.PROXMOX
VNC = api.VNC
ALIYUN = api.ALIYUN
QCLOUD = api.QCLOUD
OPENSTACK = api.OPENSTACK
SPICE = api.SPICE
WMKS = api.WMKS
WS = api.WS
VMRC = api.VMRC
ZSTACK = api.ZSTACK
CTYUN = api.CTYUN
HUAWEI = api.HUAWEI
HCS = api.HCS
APSARA = api.APSARA
JDCLOUD = api.JDCLOUD
CLOUDPODS = api.CLOUDPODS
PROXMOX = api.PROXMOX
VOLCENGINE = api.VOLC_ENGINE
)
type RemoteConsoleInfo struct {
@@ -124,7 +125,7 @@ func (info *RemoteConsoleInfo) GetConnectParams() (string, error) {
return info.getQcloudURL()
case CLOUDPODS:
return info.getCloudpodsURL()
case OPENSTACK, VMRC, ZSTACK, CTYUN, HUAWEI, HCS, JDCLOUD, PROXMOX:
case OPENSTACK, VMRC, ZSTACK, CTYUN, HUAWEI, HCS, JDCLOUD, PROXMOX, VOLCENGINE:
return info.Url, nil
default:
return "", fmt.Errorf("Can't convert protocol %s to connect params", info.Protocol)

2
vendor/modules.txt vendored
View File

@@ -1438,7 +1438,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027025551-a53fc39099d0
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231027072226-b82b6cc2b7d2
## explicit; go 1.18
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing

View File

@@ -86,3 +86,7 @@ func (self *SInstanceBase) GetInternetMaxBandwidthOut() int {
func (ins *SInstanceBase) GetPowerStates() string {
return ""
}
func (instance *SInstanceBase) GetError() error {
return nil
}

View File

@@ -16,27 +16,8 @@ package volcengine
import (
"time"
api "yunion.io/x/cloudmux/pkg/apis/billing"
)
const (
PrePaidInstanceChargeType TChargeType = "PrePaid"
PostPaidInstanceChargeType TChargeType = "PostPaid"
DefaultInstanceChargeType = PostPaidInstanceChargeType
)
func convertChargeType(ct TChargeType) string {
switch ct {
case PrePaidInstanceChargeType:
return api.BILLING_TYPE_PREPAID
case PostPaidInstanceChargeType:
return api.BILLING_TYPE_POSTPAID
default:
return ""
}
}
func convertExpiredAt(expired time.Time) time.Time {
if !expired.IsZero() {
now := time.Now()

View File

@@ -20,10 +20,10 @@ import (
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
@@ -46,10 +46,11 @@ type SDisk struct {
Kind string
CreatedAt time.Time
UpdatedAt time.Time
BillingType TChargeType
BillingType string
PayType string
TradeStatus int
ExpiredTime time.Time
ProjectName string
DeleteWithInstance bool
}
@@ -58,28 +59,7 @@ func (disk *SDisk) GetId() string {
}
func (disk *SDisk) Delete(ctx context.Context) error {
_, err := disk.storage.zone.region.getDisk(disk.VolumeId)
if err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
return errors.Wrapf(err, "Failed to find disk %s when delete", disk.VolumeId)
}
for {
err := disk.storage.zone.region.DeleteDisk(disk.VolumeId)
if err != nil {
if isError(err, "IncorrectDiskStatus") {
log.Infof("The disk is initializing, try later ...")
time.Sleep(10 * time.Second)
} else {
return errors.Wrapf(err, "DeleteDisk fail")
}
} else {
break
}
}
return cloudprovider.WaitDeleted(disk, 10*time.Second, 300*time.Second) // 5minutes
return disk.storage.zone.region.DeleteDisk(disk.VolumeId)
}
func (disk *SDisk) Resize(ctx context.Context, sizeMb int64) error {
@@ -111,17 +91,11 @@ func (disk *SDisk) GetStatus() string {
}
func (disk *SDisk) Refresh() error {
new, err := disk.storage.zone.region.getDisk(disk.VolumeId)
_disk, err := disk.storage.zone.region.getDisk(disk.VolumeId)
if err != nil {
return err
}
return jsonutils.Update(disk, new)
}
func (disk *SDisk) ResizeDisk(newSize int64) error {
// newSize 单位为 GB. 只能扩容,不能缩减。范围参考下面链接。
// https://www.volcengine.com/docs/6396/76561
return disk.storage.zone.region.ResizeDisk(disk.VolumeId, newSize)
return jsonutils.Update(disk, _disk)
}
func (disk *SDisk) GetDiskFormat() string {
@@ -180,11 +154,14 @@ func (disk *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
}
func (disk *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
return "", disk.storage.zone.region.resetDisk(disk.VolumeId, snapshotId)
return "", cloudprovider.ErrNotSupported
}
func (disk *SDisk) GetBillingType() string {
return convertChargeType(disk.BillingType)
if disk.BillingType == "post" {
return billing_api.BILLING_TYPE_POSTPAID
}
return billing_api.BILLING_TYPE_PREPAID
}
func (disk *SDisk) GetCreatedAt() time.Time {
@@ -209,25 +186,13 @@ func (disk *SDisk) Rebuild(ctx context.Context) error {
}
func (disk *SDisk) GetProjectId() string {
return ""
return disk.ProjectName
}
// Snapshot API is not supported, refer to
// https://www.volcengine.com/docs/6460/195549
func (disk *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
snapshotId, err := disk.storage.zone.region.CreateSnapshot(disk.VolumeId, name, desc)
if err != nil {
return nil, errors.Wrapf(err, "CreateSnapshot")
}
snapshot, err := disk.storage.zone.region.GetISnapshotById(snapshotId)
if err != nil {
return nil, errors.Wrapf(err, "getSnapshot(%s)", snapshotId)
}
err = cloudprovider.WaitStatus(snapshot, api.SNAPSHOT_READY, 15*time.Second, 3600*time.Second)
if err != nil {
return nil, errors.Wrapf(err, "cloudprovider.WaitStatus")
}
return snapshot, nil
return nil, cloudprovider.ErrNotSupported
}
// region
@@ -248,11 +213,9 @@ func (region *SRegion) GetDisks(instanceId string, zoneId string, category strin
if len(category) > 0 {
params["VolumeType"] = category
}
if len(diskIds) > 0 {
for index, id := range diskIds {
key := fmt.Sprintf("VolumeIds.%d", index+1)
params[key] = id
}
for index, id := range diskIds {
key := fmt.Sprintf("VolumeIds.%d", index+1)
params[key] = id
}
body, err := region.storageRequest("DescribeVolumes", params)
@@ -261,11 +224,11 @@ func (region *SRegion) GetDisks(instanceId string, zoneId string, category strin
}
disks := make([]SDisk, 0)
err = body.Unmarshal(&disks, "Result", "Volumes")
err = body.Unmarshal(&disks, "Volumes")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal disk details fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return disks, int(total), nil
}
@@ -287,7 +250,7 @@ func (region *SRegion) CreateDisk(zoneId string, category string, name string, s
if err != nil {
return "", err
}
return body.GetString("Result", "VolumeId")
return body.GetString("VolumeId")
}
func (region *SRegion) getDisk(diskId string) (*SDisk, error) {
@@ -306,7 +269,6 @@ func (region *SRegion) getDisk(diskId string) (*SDisk, error) {
func (region *SRegion) DeleteDisk(diskId string) error {
params := make(map[string]string)
params["VolumeId"] = diskId
_, err := region.storageRequest("DeleteVolume", params)
return err
}
@@ -323,12 +285,3 @@ func (region *SRegion) ResizeDisk(diskId string, sizeGb int64) error {
return nil
}
func (region *SRegion) resetDisk(diskId, snapshotId string) error {
// not supported API
return errors.Wrapf(cloudprovider.ErrNotImplemented, "resetDisk")
}
func (region *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) {
return "", errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateSnapshot")
}

View File

@@ -252,7 +252,7 @@ func (region *SRegion) GetEips(eipIds []string, associatedId string, addresses [
}
eips := make([]SEipAddress, 0)
err = body.Unmarshal(&eips, "Result", "EipAddresses")
err = body.Unmarshal(&eips, "EipAddresses")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal EipAddress details fail")
}
@@ -308,7 +308,7 @@ func (region *SRegion) AllocateEIP(opts *cloudprovider.SEip) (*SEipAddress, erro
return nil, errors.Wrapf(err, "AllocateEipAddress fail")
}
eipId, err := body.GetString("Result", "AllocationId")
eipId, err := body.GetString("AllocationId")
if err != nil {
return nil, errors.Wrapf(err, "get AllocationId after created fail")
}

View File

@@ -7,9 +7,7 @@ import (
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/billing"
)
type SHost struct {
@@ -174,11 +172,8 @@ func (host *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
return inst, nil
}
func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
vmId, err := host._createVM(desc.Name, desc.Hostname, desc.ExternalImageId, desc.SysDisk, desc.Cpu, desc.MemoryMB,
desc.InstanceType, desc.ExternalNetworkId, desc.IpAddr, desc.Description, desc.Password,
desc.DataDisks, desc.PublicKey, desc.ExternalSecgroupIds, desc.UserData, desc.BillingCycle,
desc.ProjectId, desc.Tags, desc.SPublicIpInfo)
func (host *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
vmId, err := host._createVM(opts)
if err != nil {
return nil, err
}
@@ -189,57 +184,21 @@ func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudpr
return vm, nil
}
func (host *SHost) _createVM(name string, hostname string, imgId string,
sysDisk cloudprovider.SDiskInfo, cpu int, memMB int, instanceType string,
networkID string, ipAddr string, desc string, passwd string,
dataDisks []cloudprovider.SDiskInfo, publicKey string, secgroupIds []string,
userData string, bc *billing.SBillingCycle, projectId string,
tags map[string]string, publicIp cloudprovider.SPublicIpInfo,
) (string, error) {
func (host *SHost) _createVM(opts *cloudprovider.SManagedVMCreateConfig) (string, error) {
var err error
keypair := ""
if len(publicKey) > 0 {
keypair, err = host.zone.region.syncKeypair(publicKey)
if len(opts.PublicKey) > 0 {
opts.KeypairName, err = host.zone.region.syncKeypair(opts.PublicKey)
if err != nil {
return "", err
}
}
img, err := host.zone.region.GetImage(imgId)
if err != nil {
return "", errors.Wrapf(err, "GetImage fail")
}
if img.Status != ImageStatusAvailable {
log.Errorf("image %s status %s", imgId, img.Status)
return "", fmt.Errorf("image not ready")
}
disks := make([]SDisk, len(dataDisks)+1)
disks[0].Size = img.Size
if sysDisk.SizeGB > 0 && sysDisk.SizeGB > img.Size {
disks[0].Size = sysDisk.SizeGB
}
storage, err := host.zone.getStorageByCategory(sysDisk.StorageType)
if err != nil {
return "", fmt.Errorf("storage %s not avaiable: %s", sysDisk.StorageType, err)
}
disks[0].VolumeType = storage.storageType
for i, dataDisk := range dataDisks {
disks[i+1].Size = dataDisk.SizeGB
storage, err := host.zone.getStorageByCategory(dataDisk.StorageType)
if err != nil {
return "", fmt.Errorf("storage %s not avaiable: %s", dataDisk.StorageType, err)
}
disks[i+1].VolumeType = storage.storageType
}
if len(instanceType) == 0 {
if len(opts.InstanceType) == 0 {
return "", fmt.Errorf("instance type must be specified")
}
vmId, err := host.zone.region.CreateInstance(name, hostname, imgId, instanceType, secgroupIds, host.zone.ZoneId, desc, passwd, disks, networkID, ipAddr, keypair, userData, bc, projectId, tags)
vmId, err := host.zone.region.CreateInstance(host.zone.ZoneId, opts)
if err != nil {
return "", errors.Wrapf(err, "Failed to create %s", instanceType)
return "", errors.Wrapf(err, "Failed to create %s", opts.InstanceType)
}
return vmId, nil
}

View File

@@ -120,7 +120,7 @@ func (region *SRegion) ImportImage(name string, osArch string, osType string, pl
if err != nil {
return "", errors.Wrapf(err, "ImportImage")
}
imageId, err := body.GetString("Result", "ImageId")
imageId, err := body.GetString("ImageId")
if err != nil {
return "", errors.Wrap(err, "Unmarsh imageId failed")
}
@@ -138,7 +138,7 @@ func (region *SRegion) ExportImage(imageId, bucketName string) (string, error) {
if err != nil {
return "", errors.Wrapf(err, "ExportImage")
}
taskId, err := body.GetString("Result", "TaskId")
taskId, err := body.GetString("TaskId")
if err != nil {
return "", errors.Wrapf(err, "Unmarshal")
}
@@ -298,11 +298,11 @@ func (region *SRegion) getImages(params map[string]string) ([]SImage, string, er
}
images := make([]SImage, 0)
err = body.Unmarshal(&images, "Result", "Images")
err = body.Unmarshal(&images, "Images")
if err != nil {
return nil, "", errors.Wrapf(err, "Unmarshal images fail")
}
nextToken, _ := body.GetString("Result", "NextToken")
nextToken, _ := body.GetString("NextToken")
return images, nextToken, nil
}

View File

@@ -16,19 +16,17 @@ package volcengine
import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
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/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/billing"
"yunion.io/x/pkg/util/cloudinit"
"yunion.io/x/pkg/util/imagetools"
"yunion.io/x/pkg/util/osprofile"
"yunion.io/x/pkg/utils"
@@ -47,8 +45,6 @@ const (
InstanceStatusDeleting = "DELETING"
)
type TChargeType string
type SSecurityGroupIds []string
type SRdmaIPAddress []string
@@ -80,7 +76,7 @@ type SInstance struct {
RdmaIpAddress SRdmaIPAddress
KeyPairName string
KeyPairId string
InstanceChargeType TChargeType
InstanceChargeType string
StoppedMode string
SpotStrategy string
DeploymentSetId string
@@ -124,7 +120,7 @@ func (instance *SInstance) GetUserData() (string, error) {
if err != nil {
return "", errors.Wrapf(err, "GetUserData")
}
userData, err := body.GetString("Result", "UserData")
userData, err := body.GetString("UserData")
if err != nil {
return "", errors.Wrapf(err, "GetUserData")
}
@@ -136,10 +132,12 @@ func (region *SRegion) GetInstance(instanceId string) (*SInstance, error) {
if err != nil {
return nil, err
}
if len(instances) == 0 {
return nil, cloudprovider.ErrNotFound
for i := range instances {
if instances[i].InstanceId == instanceId {
return &instances[i], nil
}
}
return &instances[0], nil
return nil, errors.Wrapf(cloudprovider.ErrNotFound, instanceId)
}
func (region *SRegion) GetInstances(zoneId string, ids []string, limit int, token string) ([]SInstance, string, error) {
@@ -165,11 +163,11 @@ func (region *SRegion) GetInstances(zoneId string, ids []string, limit int, toke
return nil, "", errors.Wrapf(err, "GetInstances fail")
}
instances := make([]SInstance, 0)
err = body.Unmarshal(&instances, "Result", "Instances")
err = body.Unmarshal(&instances, "Instances")
if err != nil {
return nil, "", errors.Wrapf(err, "Unmarshal details fail")
}
nextToken, _ := body.GetString("Result", "NextToken")
nextToken, _ := body.GetString("NextToken")
return instances, nextToken, nil
}
@@ -198,11 +196,8 @@ func (instance *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
idisks := make([]cloudprovider.ICloudDisk, len(disks))
for i := 0; i < len(disks); i += 1 {
store, err := instance.host.zone.getStorageByCategory(disks[i].VolumeType)
if err != nil {
return nil, errors.Wrap(err, "getStorageByCategory")
}
disks[i].storage = store
storage := &SStorage{zone: instance.host.zone, storageType: disks[i].VolumeType}
disks[i].storage = storage
idisks[i] = &disks[i]
}
return idisks, nil
@@ -226,18 +221,12 @@ func (instance *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
}
func (instance *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {
networkInterfaces := instance.NetworkInterfaces
nics := make([]cloudprovider.ICloudNic, 0)
for _, ni := range networkInterfaces {
nic := SInstanceNic{
instance: instance,
id: ni.NetworkInterfaceId,
ipAddr: ni.PrimaryIpAddress,
macAddr: ni.MacAddress,
}
nics = append(nics, &nic)
ret := []cloudprovider.ICloudNic{}
for i := range instance.NetworkInterfaces {
instance.NetworkInterfaces[i].region = instance.host.zone.region
ret = append(ret, &instance.NetworkInterfaces[i])
}
return nics, nil
return ret, nil
}
func (instance *SInstance) GetId() string {
@@ -264,11 +253,16 @@ func (instance *SInstance) GetInstanceType() string {
}
func (instance *SInstance) GetSecurityGroupIds() ([]string, error) {
ret := []string{}
for _, net := range instance.NetworkInterfaces {
ret = append(ret, net.SecurityGroupIds...)
nics, _, err := instance.host.zone.region.GetNetworkInterfaces(instance.InstanceId, 1, 10)
if err != nil {
return nil, err
}
return ret, nil
for _, nic := range nics {
if len(nic.SecurityGroupIds) > 0 {
return nic.SecurityGroupIds, nil
}
}
return []string{}, nil
}
func (instance *SInstance) GetVcpuCount() int {
@@ -365,18 +359,37 @@ func (instance *SInstance) GetCreatedAt() time.Time {
}
func (instance *SInstance) GetExpiredAt() time.Time {
// return instance.ExpiredAt
if instance.InstanceChargeType != "PostPaid" {
return instance.ExpiredAt
}
return time.Time{}
}
func (instance *SInstance) SetSecurityGroups(secgroupIds []string) error {
return errors.Wrapf(cloudprovider.ErrNotImplemented, "SetSecurityGroups")
func (instance *SInstance) GetBillingType() string {
if instance.InstanceChargeType == "PostPaid" {
return billing_api.BILLING_TYPE_POSTPAID
}
return billing_api.BILLING_TYPE_PREPAID
}
func (instance *SInstance) GetError() error {
func (instance *SInstance) SetSecurityGroups(secgroupIds []string) error {
for _, nic := range instance.NetworkInterfaces {
return instance.host.zone.region.ModifyNetworkInterfaceAttributes(nic.NetworkInterfaceId, secgroupIds)
}
return nil
}
func (self *SRegion) ModifyNetworkInterfaceAttributes(id string, secgroupIds []string) error {
params := map[string]string{
"NetworkInterfaceId": id,
}
for i, id := range secgroupIds {
params[fmt.Sprintf("SecurityGroupIds.%d", i+1)] = id
}
_, err := self.vpcRequest("ModifyNetworkInterfaceAttributes", params)
return err
}
func (instance *SInstance) ChangeConfig(ctx context.Context, config *cloudprovider.SManagedVMChangeConfig) error {
if config.InstanceType == "nil" {
return errors.Wrapf(cloudprovider.ErrInputParameter, "InstanceType")
@@ -385,7 +398,32 @@ func (instance *SInstance) ChangeConfig(ctx context.Context, config *cloudprovid
}
func (instance *SInstance) GetVNCInfo(input *cloudprovider.ServerVncInput) (*cloudprovider.ServerVncOutput, error) {
return nil, cloudprovider.ErrNotSupported
url, err := instance.host.zone.region.DescribeInstanceVncUrl(instance.InstanceId)
if err != nil {
return nil, err
}
protocol := api.HYPERVISOR_VOLCENGINE
if strings.HasPrefix(url, "wss") {
protocol = "vnc"
}
ret := &cloudprovider.ServerVncOutput{
Url: url,
Protocol: protocol,
InstanceId: instance.InstanceId,
Hypervisor: api.HYPERVISOR_VOLCENGINE,
}
return ret, nil
}
func (self *SRegion) DescribeInstanceVncUrl(id string) (string, error) {
params := map[string]string{
"InstanceId": id,
}
resp, err := self.ecsRequest("DescribeInstanceVncUrl", params)
if err != nil {
return "", err
}
return resp.GetString("VncUrl")
}
func (instance *SInstance) StartVM(ctx context.Context) error {
@@ -402,20 +440,7 @@ func (instance *SInstance) StopVM(ctx context.Context, opts *cloudprovider.Serve
}
func (instance *SInstance) DeleteVM(ctx context.Context) error {
for {
err := instance.host.zone.region.DeleteVM(instance.InstanceId)
if err != nil {
if isError(err, "IncorrectInstanceStatus.Initializing") {
log.Infof("The instance is initializing, try later ...")
time.Sleep(10 * time.Second)
} else {
return errors.Wrapf(err, "DeleteVM fail")
}
} else {
break
}
}
return cloudprovider.WaitDeleted(instance, 10*time.Second, 300*time.Second)
return instance.host.zone.region.DeleteVM(instance.InstanceId)
}
func (instance *SInstance) UpdateVM(ctx context.Context, input cloudprovider.SInstanceUpdateOptions) error {
@@ -455,78 +480,7 @@ func (instance *SInstance) GetProjectId() string {
}
func (instance *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
udata, err := instance.GetUserData()
if err != nil {
return "", err
}
image, err := instance.host.zone.region.GetImage(desc.ImageId)
if err != nil {
return "", errors.Wrapf(err, "GetImage fail")
}
keypairName := instance.KeyPairName
if len(desc.PublicKey) > 0 {
keypairName, err = instance.host.zone.region.syncKeypair(desc.PublicKey)
if err != nil {
return "", fmt.Errorf("RebuildRoot.syncKeypair %s", err)
}
}
userdata := ""
srcOsType := strings.ToLower(string(instance.GetOsType()))
destOsType := strings.ToLower(string(image.GetOsType()))
winOS := strings.ToLower(osprofile.OS_TYPE_WINDOWS)
cloudconfig := &cloudinit.SCloudConfig{}
if srcOsType != winOS && len(udata) > 0 {
_cloudconfig, err := cloudinit.ParseUserDataBase64(udata)
if err != nil {
log.Debugf("RebuildRoot invalid instance user data %s", udata)
} else {
cloudconfig = _cloudconfig
}
}
if (srcOsType != winOS && destOsType != winOS) || (srcOsType == winOS && destOsType != winOS) {
// linux/windows to linux
loginUser := cloudinit.NewUser(api.VM_AWS_DEFAULT_LOGIN_USER)
loginUser.SudoPolicy(cloudinit.USER_SUDO_NOPASSWD)
if len(desc.PublicKey) > 0 {
loginUser.SshKey(desc.PublicKey)
cloudconfig.MergeUser(loginUser)
} else if len(desc.Password) > 0 {
cloudconfig.SshPwauth = cloudinit.SSH_PASSWORD_AUTH_ON
loginUser.Password(desc.Password)
cloudconfig.MergeUser(loginUser)
}
userdata = cloudconfig.UserDataBase64()
} else {
// linux/windows to windows
data := ""
if len(desc.Password) > 0 {
cloudconfig.SshPwauth = cloudinit.SSH_PASSWORD_AUTH_ON
loginUser := cloudinit.NewUser(api.VM_AWS_DEFAULT_WINDOWS_LOGIN_USER)
loginUser.SudoPolicy(cloudinit.USER_SUDO_NOPASSWD)
loginUser.Password(desc.Password)
cloudconfig.MergeUser(loginUser)
data = fmt.Sprintf("<powershell>%s</powershell>", cloudconfig.UserDataPowerShell())
} else {
if len(udata) > 0 {
data = fmt.Sprintf("<powershell>%s</powershell>", udata)
}
}
userdata = base64.StdEncoding.EncodeToString([]byte(data))
}
diskId, err := instance.host.zone.region.ReplaceSystemDisk(ctx, instance.InstanceId, desc.ImageId, desc.Password, keypairName, userdata)
if err != nil {
return "", err
}
return diskId, nil
return "", cloudprovider.ErrNotSupported
}
func (instance *SInstance) SaveImage(opts *cloudprovider.SaveImageOptions) (cloudprovider.ICloudImage, error) {
@@ -538,96 +492,87 @@ func (instance *SInstance) SaveImage(opts *cloudprovider.SaveImageOptions) (clou
}
// region
func (region *SRegion) CreateInstance(
name string,
hostname string,
imageId string,
instanceType string,
securityGroupIds []string,
zoneId string,
desc string,
passwd string,
disks []SDisk,
networkID string,
ipAddr string,
keypair string,
userData string,
bc *billing.SBillingCycle,
projectId string,
tags map[string]string,
) (string, error) {
func (region *SRegion) CreateInstance(zoneId string, opts *cloudprovider.SManagedVMCreateConfig) (string, error) {
params := make(map[string]string)
params["RegionId"] = region.RegionId
params["ImageId"] = imageId
params["InstanceType"] = instanceType
params["ImageId"] = opts.ExternalImageId
params["InstanceType"] = opts.InstanceType
params["ZoneId"] = zoneId
params["InstanceName"] = name
params["ProjectName"] = projectId
if len(hostname) > 0 {
params["HostName"] = hostname
params["InstanceName"] = opts.Name
if len(opts.ProjectId) > 0 {
params["ProjectName"] = opts.ProjectId
}
params["Description"] = desc
if len(passwd) > 0 {
params["Password"] = passwd
} else {
if len(opts.Hostname) > 0 {
params["HostName"] = opts.Hostname
}
params["Description"] = opts.Description
if len(opts.Password) > 0 {
params["Password"] = opts.Password
}
if len(opts.KeypairName) > 0 {
params["KeyPairName"] = opts.KeypairName
}
if len(opts.Password) == 0 && len(opts.KeypairName) == 0 {
params["KeepImageCredential"] = "True"
}
if len(keypair) > 0 {
params["KeyPairName"] = keypair
if len(opts.UserData) > 0 {
params["UserData"] = opts.UserData
}
if len(userData) > 0 {
params["UserData"] = userData
tagIdx := 1
for k, v := range opts.Tags {
params[fmt.Sprintf("Tags.%d.Key", tagIdx)] = k
params[fmt.Sprintf("Tags.%d.Value", tagIdx)] = v
tagIdx += 1
}
if len(tags) > 0 {
tagIdx := 1
for k, v := range tags {
params[fmt.Sprintf("Tag.%d.Key", tagIdx)] = k
params[fmt.Sprintf("Tag.%d.Value", tagIdx)] = v
tagIdx += 1
}
params["Volumes.1.Size"] = fmt.Sprintf("%d", opts.SysDisk.SizeGB)
params["Volumes.1.VolumeType"] = opts.SysDisk.StorageType
for idx, disk := range opts.DataDisks {
params[fmt.Sprintf("Volumes.%d.Size", idx+2)] = fmt.Sprintf("%d", disk.SizeGB)
params[fmt.Sprintf("Volumes.%d.VolumeType", idx+2)] = disk.StorageType
}
if len(disks) > 0 {
for idx, disk := range disks {
diskIdx := idx + 1
params[fmt.Sprintf("Volumes.%d.Size", diskIdx)] = fmt.Sprintf("%d", disk.Size)
params[fmt.Sprintf("Volumes.%d.VolumeType", diskIdx)] = disk.VolumeType
}
params["NetworkInterfaces.1.SubnetId"] = opts.ExternalNetworkId
if len(opts.IpAddr) > 0 {
//params["NetworkInterfaces.1.IpAddr"] = opts.IpAddr
}
params["NetworkInterfaces.1.SubnetId"] = ipAddr
// currently only support binding the first NetworkInterface securitygroup
for idx, id := range securityGroupIds {
for idx, id := range opts.ExternalSecgroupIds {
params[fmt.Sprintf("NetworkInterfaces.1.SecurityGroupIds.%d", idx+1)] = id
}
if bc != nil {
params["InstanceChargeType"] = "PostPaid"
params["SpotStrategy"] = "NoSpot"
if opts.BillingCycle != nil {
params["InstanceChargeType"] = "PrePaid"
err := billingCycle2Params(bc, params)
err := billingCycle2Params(opts.BillingCycle, params)
if err != nil {
return "", err
}
if bc.AutoRenew {
params["AutoRenew"] = "False"
if opts.BillingCycle.AutoRenew {
params["AutoRenew"] = "true"
params["AutoRenewPeriod"] = "1"
} else {
params["AutoRenew"] = "False"
}
} else {
params["InstanceChargeType"] = "PostPaid"
params["SpotStrategy"] = "NoSpot"
}
params["ClientToken"] = utils.GenRequestId(20)
body, err := region.ecsRequest("CreateInstance", params)
resp, err := region.ecsRequest("RunInstances", params)
if err != nil {
return "", errors.Wrapf(err, "CreateInstance fail")
return "", errors.Wrapf(err, "RunInstances")
}
instanceId, _ := body.GetString("InstanceId")
return instanceId, nil
ids := []string{}
err = resp.Unmarshal(&ids, "InstanceIds")
if err != nil {
return "", err
}
for _, id := range ids {
return id, nil
}
return "", errors.Wrapf(cloudprovider.ErrNotFound, "after created")
}
func (region *SRegion) RenewInstance(instanceId string, bc billing.SBillingCycle) error {
@@ -677,15 +622,7 @@ func (region *SRegion) StopVM(instanceId string, isForce, stopCharging bool) err
}
func (region *SRegion) DeleteVM(instanceId string) error {
status, err := region.GetInstanceStatus(instanceId)
if err != nil {
return errors.Wrapf(err, "Fail to get instance status on DeleteVM")
}
log.Debugf("Instance status on delete is %s", status)
if status != InstanceStatusStopped {
log.Warningf("DeleteVM: vm status is %s expect %s", status, InstanceStatusStopped)
}
return region.doDeleteVM(instanceId)
return region.instanceOperation(instanceId, "DeleteInstance", nil)
}
func (region *SRegion) doStartVM(instanceId string) error {
@@ -706,10 +643,6 @@ func (region *SRegion) doStopVM(instanceId string, isForce, stopCharging bool) e
return region.instanceOperation(instanceId, "StopInstance", params)
}
func (region *SRegion) doDeleteVM(instanceId string) error {
return region.instanceOperation(instanceId, "DeleteInstance", nil)
}
func (region *SRegion) modifyInstanceAttribute(instanceId string, params map[string]string) error {
return region.instanceOperation(instanceId, "ModifyInstanceAttribute", params)
}
@@ -757,21 +690,18 @@ func (region *SRegion) DeployVM(instanceId string, name string, password string,
if len(params) > 0 {
return region.modifyInstanceAttribute(instanceId, params)
} else {
return nil
}
return nil
}
func (region *SRegion) DetachDisk(instanceId string, diskId string) error {
params := make(map[string]string)
params["InstanceId"] = instanceId
params["VolumeId"] = diskId
log.Infof("Detach instance %s disk %s", instanceId, diskId)
_, err := region.storageRequest("DetachVolume", params)
if err != nil {
return errors.Wrap(err, "DetachDisk")
}
return nil
}
@@ -783,30 +713,9 @@ func (region *SRegion) AttachDisk(instanceId string, diskId string) error {
if err != nil {
return errors.Wrapf(err, "AttachDisk %s to %s fail", diskId, instanceId)
}
return nil
}
func (region *SRegion) ReplaceSystemDisk(ctx context.Context, instanceId string, imageId string, passwd string, keypairName string, userdata string) (string, error) {
params := make(map[string]string)
params["InstanceId"] = instanceId
params["ImageId"] = imageId
if len(passwd) > 0 {
params["Password"] = passwd
} else {
params["KeepImageCredential"] = "True"
}
if len(keypairName) > 0 {
params["KeyPairName"] = keypairName
}
_, err := region.ecsRequest("ReplaceSystemVolume", params)
if err != nil {
return "", err
}
// volcengine does not return volumeId
return "", nil
}
func (region *SRegion) SaveImage(instanceId string, opts *cloudprovider.SaveImageOptions) (*SImage, error) {
params := map[string]string{
"InstanceId": instanceId,
@@ -818,7 +727,7 @@ func (region *SRegion) SaveImage(instanceId string, opts *cloudprovider.SaveImag
if err != nil {
return nil, errors.Wrapf(err, "CreateImage")
}
imageId, err := body.GetString("Result", "IamgeId")
imageId, err := body.GetString("IamgeId")
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal")
}

View File

@@ -16,14 +16,14 @@ package volcengine
import (
"fmt"
"time"
"github.com/golang-plus/errors"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
"yunion.io/x/pkg/errors"
)
type SInstanceNic struct {
cloudprovider.DummyICloudNic
instance *SInstance
id string
@@ -31,40 +31,60 @@ type SInstanceNic struct {
macAddr string
}
func (nic *SInstanceNic) GetId() string {
return nic.id
type SNetworkInterface struct {
cloudprovider.DummyICloudNic
multicloud.SNetworkInterfaceBase
VolcEngineTags
region *SRegion
InstanceId string
NetworkInterfaceId string
VpcId string
SubnetId string
PrimaryIpAddress string
Type string
MacAddress string
CreationTime time.Time
NetworkInterfaceName string
PrivateIpSets SPrivateIpSets
ResourceGroupId string
SecurityGroupIds SSecurityGroupIds
Status string
ZoneId string
PrivateIpAddresses []string
AssociatedElasticIp SAssociatedElasticIp
}
func (nic *SInstanceNic) GetIP() string {
return nic.ipAddr
func (nic *SNetworkInterface) GetIP() string {
return nic.PrimaryIpAddress
}
func (nic *SInstanceNic) GetMAC() string {
return nic.macAddr
func (nic *SNetworkInterface) GetMAC() string {
return nic.MacAddress
}
func (nic *SInstanceNic) InClassicNetwork() bool {
func (nic *SNetworkInterface) InClassicNetwork() bool {
return false
}
func (nic *SInstanceNic) GetDriver() string {
func (nic *SNetworkInterface) GetDriver() string {
return "virtio"
}
func (nic *SInstanceNic) GetINetworkId() string {
return nic.instance.NetworkInterfaces[0].SubnetId
func (nic *SNetworkInterface) GetINetworkId() string {
return nic.SubnetId
}
func (nic *SInstanceNic) GetSubAddress() ([]string, error) {
return nic.instance.host.zone.region.GetSubAddress(nic.id)
func (nic *SNetworkInterface) GetSubAddress() ([]string, error) {
return nic.region.GetSubAddress(nic.NetworkInterfaceId)
}
func (nic *SInstanceNic) AssignAddress(ipAddrs []string) error {
return nic.instance.host.zone.region.AssignAddres(nic.id, ipAddrs)
func (nic *SNetworkInterface) AssignAddress(ipAddrs []string) error {
return nic.region.AssignAddres(nic.NetworkInterfaceId, ipAddrs)
}
func (nic *SInstanceNic) UnassignAddress(ipAddrs []string) error {
return nic.instance.host.zone.region.UnassignAddress(nic.id, ipAddrs)
func (nic *SNetworkInterface) UnassignAddress(ipAddrs []string) error {
return nic.region.UnassignAddress(nic.NetworkInterfaceId, ipAddrs)
}
func (region *SRegion) GetSubAddress(nicId string) ([]string, error) {
@@ -77,7 +97,7 @@ func (region *SRegion) GetSubAddress(nicId string) ([]string, error) {
}
interfaces := []SNetworkInterface{}
err = body.Unmarshal(&interfaces, "Result", "NetworkInterfaceSets")
err = body.Unmarshal(&interfaces, "NetworkInterfaceSets")
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal")
}

View File

@@ -23,7 +23,6 @@ import (
"github.com/aokoli/goutils"
"golang.org/x/crypto/ssh"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
)
@@ -54,11 +53,11 @@ func (region *SRegion) GetKeypairs(finger string, name string, limit int, token
}
keypairs := make([]SKeypair, 0)
err = body.Unmarshal(&keypairs, "Result", "KeyPairs")
err = body.Unmarshal(&keypairs, "KeyPairs")
if err != nil {
return nil, "", errors.Wrapf(err, "Unmarshal keypair fail")
}
nextToken, _ := body.GetString("Result", "NextToken")
nextToken, _ := body.GetString("NextToken")
return keypairs, nextToken, nil
}
@@ -72,9 +71,8 @@ func (region *SRegion) ImportKeypair(name string, pubKey string) (*SKeypair, err
return nil, errors.Wrapf(err, "ImportKeypair fail")
}
log.Debugf("%s", body)
keypair := SKeypair{}
err = body.Unmarshal(&keypair, "Result")
err = body.Unmarshal(&keypair)
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal keypair fail")
}

View File

@@ -125,11 +125,11 @@ func (region *SRegion) GetDnatEntries(natGatewayId string, pageNumber int, pageS
return nil, 0, errors.Wrapf(err, "DescribeDnatEntries fail")
}
entries := make([]SDNATEntry, 0)
err = body.Unmarshal(&entries, "Result", "DnatEntries")
err = body.Unmarshal(&entries, "DnatEntries")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal entries fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return entries, int(total), nil
}
@@ -142,7 +142,7 @@ func (region *SRegion) GetDnatEntry(natGatewayId string, dnatEntryID string) (SD
return SDNATEntry{}, errors.Wrapf(err, "DescribeDnatEntries fail")
}
entries := make([]SDNATEntry, 0)
err = body.Unmarshal(&entries, "Result", "DnatEntries")
err = body.Unmarshal(&entries, "DnatEntries")
if err != nil {
return SDNATEntry{}, errors.Wrapf(err, "Unmarshal entries fail")
}
@@ -164,7 +164,7 @@ func (region *SRegion) CreateDnatEntry(rule cloudprovider.SNatDRule, natGatewayI
if err != nil {
return "", err
}
entryID, _ := body.GetString("Result", "DnatEntryId")
entryID, _ := body.GetString("DnatEntryId")
return entryID, nil
}

View File

@@ -220,11 +220,11 @@ func (region *SRegion) GetNatGateways(vpcId string, natGatewayId string, pageNum
return nil, 0, errors.Wrapf(err, "DescribeNatGateways")
}
gateways := make([]SNatGateway, 0)
err = body.Unmarshal(&gateways, "Result", "NatGateways")
err = body.Unmarshal(&gateways, "NatGateways")
if err != nil {
return nil, 0, errors.Wrapf(err, "body.Unmarshal")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return gateways, int(total), nil
}
@@ -257,7 +257,7 @@ func (region *SRegion) CreateNatGateway(opts *cloudprovider.NatGatewayCreateOpti
if err != nil {
return nil, errors.Wrapf(err, "CreateNatGateway")
}
natId, err := resp.GetString("Result", "NatGatewayId")
natId, err := resp.GetString("NatGatewayId")
if err != nil {
return nil, errors.Wrapf(err, "resp.Get(NatGatewayId)")
}

View File

@@ -138,11 +138,11 @@ func (region *SRegion) GetSnatEntries(natGatewayId string, pageNumber int, pageS
return nil, 0, errors.Wrapf(err, "DescribeSNATEntries fail")
}
entries := make([]SSNATEntry, 0)
err = body.Unmarshal(&entries, "Result", "SnatEntries")
err = body.Unmarshal(&entries, "SnatEntries")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal entries fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return entries, int(total), nil
}
@@ -155,7 +155,7 @@ func (region *SRegion) GetSnatEntry(natGatewayId string, snatEntryID string) (SS
return SSNATEntry{}, errors.Wrapf(err, "DescribeSnatEntries fail")
}
entries := make([]SSNATEntry, 0)
err = body.Unmarshal(&entries, "Result", "SnatEntries")
err = body.Unmarshal(&entries, "SnatEntries")
if err != nil {
return SSNATEntry{}, errors.Wrapf(err, "Unmarshal entries fail")
}
@@ -182,7 +182,7 @@ func (region *SRegion) CreateSnatEntry(rule cloudprovider.SNatSRule, natGatewayI
return "", err
}
entryID, _ := body.GetString("Result", "SnatEntryId")
entryID, _ := body.GetString("SnatEntryId")
return entryID, nil
}

View File

@@ -144,7 +144,7 @@ func (region *SRegion) CreateSubnet(zoneId string, vpcId string, name string, ci
if err != nil {
return "", err
}
return body.GetString("Result", "SubnetId")
return body.GetString("SubnetId")
}
func (region *SRegion) DeleteSubnet(SubnetId string) error {
@@ -228,11 +228,11 @@ func (region *SRegion) GetSubnets(ids []string, zoneId string, vpcId string, pag
}
subnets := make([]SNetwork, 0)
err = body.Unmarshal(&subnets, "Result", "Subnets")
err = body.Unmarshal(&subnets, "Subnets")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal subnets fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return subnets, int(total), nil
}
@@ -247,7 +247,7 @@ func (region *SRegion) GetSubnetAttributes(SubnetId string) (*SNetwork, error) {
log.Debugf("%s", body.PrettyString())
}
subnet := SNetwork{}
err = body.Unmarshal(&subnet, "Result")
err = body.Unmarshal(&subnet)
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal subnet fail")
}

View File

@@ -16,13 +16,11 @@ package volcengine
import (
"fmt"
"time"
"yunion.io/x/pkg/errors"
api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
)
type SPrivateIp struct {
@@ -56,29 +54,6 @@ type SPrivateIpSets struct {
PrivateIpSet []SPrivateIp
}
type SNetworkInterface struct {
multicloud.SNetworkInterfaceBase
VolcEngineTags
region *SRegion
InstanceId string
NetworkInterfaceId string
VpcId string
SubnetId string
PrimaryIpAddress string
Type string
MacAddress string
CreationTime time.Time
NetworkInterfaceName string
PrivateIpSets SPrivateIpSets
ResourceGroupId string
SecurityGroupIds SSecurityGroupIds
Status string
ZoneId string
PrivateIpAddresses []string
AssociatedElasticIp SAssociatedElasticIp
}
func (nic *SNetworkInterface) GetName() string {
return nic.NetworkInterfaceName
}
@@ -165,10 +140,10 @@ func (region *SRegion) GetNetworkInterfaces(instanceId string, pageNumber int, p
}
interfaces := []SNetworkInterface{}
err = body.Unmarshal(&interfaces, "Result", "NetworkInterfaceSets")
err = body.Unmarshal(&interfaces, "NetworkInterfaceSets")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return interfaces, int(total), nil
}

View File

@@ -82,7 +82,7 @@ func (client *SVolcEngineClient) GetProject(name string) (*SProject, error) {
return nil, err
}
project := &SProject{client: client}
err = body.Unmarshal(project, "Result")
err = body.Unmarshal(project)
if err != nil {
return nil, errors.Wrap(err, "resp.Unmarshal")
}
@@ -102,11 +102,11 @@ func (client *SVolcEngineClient) ListProjects(limit int, offset int) ([]SProject
return nil, 0, errors.Wrap(err, "iamRequest.ListProjects")
}
projects := []SProject{}
err = resp.Unmarshal(&projects, "Result", "Projects")
err = resp.Unmarshal(&projects, "Projects")
if err != nil {
return nil, 0, errors.Wrap(err, "resp.Unmarshal")
}
total, _ := resp.Int("Result", "Total")
total, _ := resp.Int("Total")
return projects, int(total), nil
}

View File

@@ -49,6 +49,11 @@ var RegionEndpoint = map[string]string{
"cn-guangzhou": "cn-beijing.volces.com",
}
type sStorageType struct {
Id string
Zones []string
}
type SRegion struct {
multicloud.SRegion
multicloud.SNoLbRegion
@@ -59,6 +64,7 @@ type SRegion struct {
ivpcs []cloudprovider.ICloudVpc
storageTypes []sStorageType
storageCache *SStoragecache
}
@@ -135,7 +141,7 @@ func (region *SRegion) GetZones(id string) ([]SZone, error) {
return nil, err
}
ret := []SZone{}
err = body.Unmarshal(&ret, "Result", "AvailableZones")
err = body.Unmarshal(&ret, "AvailableZones")
if err != nil {
return nil, err
}
@@ -195,7 +201,7 @@ func (region *SRegion) CreateVpc(opts *cloudprovider.VpcCreateOptions) (*SVpc, e
if err != nil {
return nil, err
}
vpcId, err := body.GetString("Result", "VpcId")
vpcId, err := body.GetString("VpcId")
if err != nil {
return nil, err
}
@@ -250,11 +256,11 @@ func (region *SRegion) GetVpcs(vpcIds []string, pageNumber int, pageSize int) ([
return nil, 0, errors.Wrapf(err, "GetVpcs fail")
}
vpcs := make([]SVpc, 0)
err = body.Unmarshal(&vpcs, "Result", "Vpcs")
err = body.Unmarshal(&vpcs, "Vpcs")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal vpcs fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return vpcs, int(total), nil
}
@@ -572,7 +578,7 @@ func (region *SRegion) GetRouteTables(ids []string, pageNumber int, pageSize int
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal routetables fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return routetables, int(total), nil
}

View File

@@ -182,11 +182,11 @@ func (table *SRouteTable) RemoteGetRoutes(pageNumber int, pageSize int) ([]*SRou
}
entries := SRouteEntrys{}
err = body.Unmarshal(&entries, "Result", "RouteEntries")
err = body.Unmarshal(&entries, "RouteEntries")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal routeEntrys fail")
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return entries, int(total), nil
}
@@ -262,14 +262,14 @@ func (vpc *SVpc) RemoteGetRouteTableList(pageNumber int, pageSize int) ([]*SRout
}
routeTables := make([]*SRouteTable, 0)
err = body.Unmarshal(&routeTables, "Result", "RouterTableList")
err = body.Unmarshal(&routeTables, "RouterTableList")
if err != nil {
return nil, 0, errors.Wrapf(err, "Unmarshal routeTables fail")
}
for _, routeTable := range routeTables {
routeTable.region = vpc.region
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return routeTables, int(total), nil
}

View File

@@ -70,7 +70,7 @@ func (region *SRegion) CreateSecurityGroup(opts *cloudprovider.SecurityGroupCrea
if err != nil {
return "", errors.Wrap(err, "CreateSecurityGroup")
}
return body.GetString("Result", "SecurityGroupId")
return body.GetString("SecurityGroupId")
}
func (region *SRegion) GetSecurityGroup(secGroupId string) (*SSecurityGroup, error) {
@@ -95,7 +95,7 @@ func (region *SRegion) GetSecurityGroupRules(secGroupId string) ([]SSecurityGrou
return nil, err
}
ret := []SSecurityGroupRule{}
err = body.Unmarshal(&ret, "Result", "Permissions")
err = body.Unmarshal(&ret, "Permissions")
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal security group details fail")
}
@@ -198,12 +198,12 @@ func (region *SRegion) GetSecurityGroups(vpcId, name string, securityGroupIds []
}
secgrps := make([]SSecurityGroup, 0)
err = body.Unmarshal(&secgrps, "Result", "SecurityGroups")
err = body.Unmarshal(&secgrps, "SecurityGroups")
if err != nil {
log.Errorf("Unmarshal security groups fail %s", err)
return nil, 0, err
}
total, _ := body.Int("Result", "TotalCount")
total, _ := body.Int("TotalCount")
return secgrps, int(total), nil
}

View File

@@ -98,13 +98,12 @@ func (storage *SStorage) GetStorageConf() jsonutils.JSONObject {
}
func (storage *SStorage) GetStatus() string {
if storage.storageType == api.STORAGE_VOLCENGINE_PTSSD {
return api.STORAGE_OFFLINE
}
return api.STORAGE_ONLINE
}
func (storage *SStorage) Refresh() error {
return nil
}
func (storage *SStorage) GetEnabled() bool {
return true
}

View File

@@ -54,7 +54,7 @@ func (client *SVolcEngineClient) GetCallerIdentity() (*SCallerIdentity, error) {
}
id := &SCallerIdentity{}
users := []SUser{}
err = body.Unmarshal(&users, "Result", "UserMetadata")
err = body.Unmarshal(&users, "UserMetadata")
if err != nil {
return nil, errors.Wrap(err, "resp.Unmarshal")
}

View File

@@ -123,7 +123,7 @@ func (client *SVolcEngineClient) fetchRegions() error {
return errors.Wrapf(err, "DescribeRegions")
}
regions := make([]SRegion, 0)
err = body.Unmarshal(&regions, "Result", "Regions")
err = body.Unmarshal(&regions, "Regions")
if err != nil {
return errors.Wrapf(err, "resp.Unmarshal")
}
@@ -310,7 +310,13 @@ func (client *SVolcEngineClient) jsonRequest(cred sdk.Credentials, domain string
}
cli := httputils.NewJsonClient(_cli)
_, resp, err := cli.Send(context.Background(), req, vErr, client.debug)
return resp, err
if err != nil {
return nil, errors.Wrapf(err, apiName)
}
if resp.Contains("Result") {
return resp.Get("Result")
}
return resp, nil
}
func (client *SVolcEngineClient) getSdkCredential(region string, service string, token string) sdk.Credentials {

View File

@@ -21,14 +21,9 @@ import (
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
)
var StorageTypes = []string{
api.STORAGE_VOLCENGINE_FlexPL,
api.STORAGE_VOLCENGINE_PL0,
api.STORAGE_VOLCENGINE_PTSSD,
}
type SSupportedResource struct {
Status string
Value string
@@ -46,12 +41,9 @@ type SZone struct {
host *SHost
istorages []cloudprovider.ICloudStorage
Status string
AvailableResources []SAvailableResource
storageTypes []string
ZoneId string
RegionId string
LocalName string
@@ -92,10 +84,6 @@ func (zone *SZone) GetStatus() string {
return api.ZONE_ENABLE
}
func (zone *SZone) Refresh() error {
return nil
}
func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
return zone.region
}
@@ -125,24 +113,6 @@ func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetIHostById")
}
// Storage
func (zone *SZone) getStorageType() {
if len(zone.storageTypes) == 0 {
zone.storageTypes = StorageTypes
}
}
func (zone *SZone) fetchStorages() error {
zone.getStorageType()
zone.istorages = make([]cloudprovider.ICloudStorage, len(zone.storageTypes))
for i, sc := range zone.storageTypes {
storage := SStorage{zone: zone, storageType: sc}
zone.istorages[i] = &storage
}
return nil
}
func (zone *SZone) getStorageByCategory(category string) (*SStorage, error) {
storages, err := zone.GetIStorages()
if err != nil {
@@ -154,30 +124,67 @@ func (zone *SZone) getStorageByCategory(category string) (*SStorage, error) {
return storage, nil
}
}
return nil, fmt.Errorf("no such storage %s", category)
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "getStorageByCategory %s", category)
}
func (zone *SZone) GetStorages() ([]SStorage, error) {
storages, err := zone.region.GetStorageTypes("")
if err != nil {
return nil, err
}
ret := []SStorage{}
for i := range storages {
if utils.IsInStringArray(zone.ZoneId, storages[i].Zones) {
ret = append(ret, SStorage{storageType: storages[i].Id, zone: zone})
}
}
ret = append(ret, SStorage{storageType: api.STORAGE_VOLCENGINE_PTSSD, zone: zone})
return ret, nil
}
func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
if zone.istorages == nil {
err := zone.fetchStorages()
if err != nil {
return nil, errors.Wrapf(err, "fetchStorages")
}
storages, err := zone.GetStorages()
if err != nil {
return nil, err
}
return zone.istorages, nil
ret := []cloudprovider.ICloudStorage{}
for i := range storages {
ret = append(ret, &storages[i])
}
return ret, nil
}
func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
if zone.istorages == nil {
err := zone.fetchStorages()
if err != nil {
return nil, errors.Wrapf(err, "fetchStorages")
storages, err := zone.GetIStorages()
if err != nil {
return nil, err
}
for i := range storages {
if storages[i].GetGlobalId() == id {
return storages[i], nil
}
}
for i := 0; i < len(zone.istorages); i += 1 {
if zone.istorages[i].GetGlobalId() == id {
return zone.istorages[i], nil
}
}
return nil, cloudprovider.ErrNotFound
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
}
func (self *SRegion) GetStorageTypes(zoneId string) ([]sStorageType, error) {
if len(self.storageTypes) > 0 {
return self.storageTypes, nil
}
params := map[string]string{
"PageSize": "100",
}
if len(zoneId) > 0 {
params["ZoneId"] = zoneId
}
resp, err := self.storageRequest("DescribeVolumeType", params)
if err != nil {
return nil, err
}
self.storageTypes = []sStorageType{}
err = resp.Unmarshal(&self.storageTypes, "VolumeTypes")
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal VolumeTypes")
}
return self.storageTypes, nil
}