huaweicli support unsubscirbe instance

This commit is contained in:
TangBin
2019-01-19 19:04:05 +08:00
parent 919f164f95
commit 4dc2e05a6e
3 changed files with 41 additions and 1 deletions
@@ -68,3 +68,10 @@ func (self *SOrderManager) Get(id string, querys map[string]string) (jsonutils.J
// !!!特殊调用
return self.GetInContextWithSpec(self.orderCtx, "orders-resource", id, querys, "")
}
func (self *SOrderManager) PerformAction(action string, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
request := self.newRequest("POST", id, action, self.orderCtx)
request.SetContent([]byte(getContent(params)))
return self._do(request, "")
}
+16 -1
View File
@@ -658,11 +658,12 @@ func (self *SRegion) CreateInstance(name string, imageId string, instanceType st
return "", err
}
// 按需计费
var ids []string
if params.Extendparam.ChargingMode == POST_PAID {
// 按需计费
ids, err = self.GetAllSubTaskEntityIDs(self.ecsClient.Servers.ServiceType(), _id, "server_id")
} else {
// 包年包月
err = cloudprovider.WaitCreated(10*time.Second, 180*time.Second, func() bool {
ids, err = self.getAllResIdsByType(_id, RESOURCE_TYPE_VM)
if err != nil {
@@ -997,3 +998,17 @@ func (self *SRegion) GetInstanceSecrityGroupIds(instanceId string) ([]string, er
return securitygroupIds, nil
}
// https://support.huaweicloud.com/api-oce/zh-cn_topic_0082522030.html
func (self *SRegion) UnsubscribeInstance(instanceId string, domianId string) (jsonutils.JSONObject, error) {
unsubObj := jsonutils.NewDict()
unsubObj.Add(jsonutils.NewInt(1), "unSubType")
unsubObj.Add(jsonutils.NewInt(5), "unsubscribeReasonType")
unsubObj.Add(jsonutils.NewString("no reason"), "unsubscribeReason")
resList := jsonutils.NewArray()
resList.Add(jsonutils.NewString(instanceId))
unsubObj.Add(resList, "resourceIds")
self.ecsClient.Orders.SetDomainId(domianId)
return self.ecsClient.Orders.PerformAction("resources/delete", "", unsubObj)
}
+18
View File
@@ -137,4 +137,22 @@ func init() {
}
return nil
})
type InstanceOrderUnsubscribeOptions struct {
ID string `help:"instance ID"`
DOMAIN string `help:"domain ID"`
}
shellutils.R(&InstanceOrderUnsubscribeOptions{}, "instance-order-unsubscribe", "Unsubscribe a prepaid server", func(cli *huawei.SRegion, args *InstanceOrderUnsubscribeOptions) error {
instance, e := cli.GetInstanceByID(args.ID)
if e != nil {
return e
}
_, err := cli.UnsubscribeInstance(instance.GetId(), args.DOMAIN)
if err != nil {
return err
}
return nil
})
}