Skip to content

Commit

Permalink
Merge pull request #1159 from ioito/hotfix/qx-ksyun-vm-check
Browse files Browse the repository at this point in the history
fix(ksyun): vm filter
  • Loading branch information
ioito authored Jan 9, 2025
2 parents 1b422fb + e9a6fc3 commit 0489f50
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
30 changes: 19 additions & 11 deletions pkg/multicloud/ksyun/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ type DataDisks struct {
type SInstance struct {
multicloud.SInstanceBase
SKsTag
host *SHost
host *SHost
region *SRegion

InstanceID string `json:"InstanceId"`
ProjectID string `json:"ProjectId"`
Expand Down Expand Up @@ -186,30 +187,37 @@ func (region *SRegion) GetInstance(instanceId string) (*SInstance, error) {
if err != nil {
return nil, errors.Wrap(err, "GetInstances")
}
for _, instance := range instances {
if instance.GetGlobalId() == instanceId {
return &instance, nil
for i := range instances {
if instances[i].GetGlobalId() == instanceId {
return &instances[i], nil
}
}
return nil, errors.Wrapf(err, "instance id:%s", instanceId)
return nil, errors.Wrapf(cloudprovider.ErrNotFound, instanceId)
}

func (ins *SInstance) Refresh() error {
extIns, err := ins.host.zone.region.GetInstance(ins.GetGlobalId())
extIns, err := ins.getRegion().GetInstance(ins.GetGlobalId())
if err != nil {
return errors.Wrap(err, "GetInstance")
}
return jsonutils.Update(ins, extIns)
}

func (ins *SInstance) GetTags() (map[string]string, error) {
tags, err := ins.host.zone.region.ListTags("kec-instance", ins.InstanceID)
tags, err := ins.getRegion().ListTags("kec-instance", ins.InstanceID)
if err != nil {
return nil, err
}
return tags.GetTags(), nil
}

func (ins *SInstance) getRegion() *SRegion {
if ins.region != nil {
return ins.region
}
return ins.host.zone.region
}

func (ins *SInstance) AssignSecurityGroup(secgroupId string) error {
return cloudprovider.ErrNotImplemented
}
Expand Down Expand Up @@ -320,7 +328,7 @@ func (ins *SInstance) GetHypervisor() string {
}

func (ins *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
disks, err := ins.host.zone.region.GetDiskByInstanceId(ins.GetId())
disks, err := ins.getRegion().GetDiskByInstanceId(ins.GetId())
if err != nil {
return nil, errors.Wrap(err, "getDisks")
}
Expand All @@ -339,7 +347,7 @@ func (ins *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
if len(eipIds) == 0 {
return nil, cloudprovider.ErrNotFound
}
eips, err := ins.host.zone.region.GetEips(eipIds)
eips, err := ins.getRegion().GetEips(eipIds)
if err != nil {
return nil, errors.Wrap(err, "get eips")
}
Expand All @@ -348,7 +356,7 @@ func (ins *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
}
for _, eip := range eips {
if utils.IsInStringArray(eip.GetId(), eipIds) {
eip.region = ins.host.zone.region
eip.region = ins.getRegion()
return &eip, nil
}
}
Expand All @@ -371,7 +379,7 @@ func (ins *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {

func (ins *SInstance) GetVNCInfo(input *cloudprovider.ServerVncInput) (*cloudprovider.ServerVncOutput, error) {
// TODO
resp, err := ins.host.zone.region.ecsRequest("GetVNCAddress", map[string]string{"InstanceId": ins.InstanceID})
resp, err := ins.getRegion().ecsRequest("GetVNCAddress", map[string]string{"InstanceId": ins.InstanceID})
if err != nil {
return nil, errors.Wrap(err, "GetVNCAddress")
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/multicloud/ksyun/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,28 @@ func (region *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
return iStores, nil
}

func (r *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
vms, err := r.GetInstances("", nil)
if err != nil {
return nil, err
}
ret := []cloudprovider.ICloudVM{}
for i := range vms {
vms[i].region = r
ret = append(ret, &vms[i])
}
return ret, nil
}

func (r *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
vm, err := r.GetInstance(id)
if err != nil {
return nil, err
}
vm.region = r
return vm, nil
}

func (region *SRegion) ecsRequest(action string, params map[string]string) (jsonutils.JSONObject, error) {
return region.client.ec2Request(region.Region, action, params)
}
Expand Down

0 comments on commit 0489f50

Please sign in to comment.