diff --git a/cmd/climc/shell/notify/topic.go b/cmd/climc/shell/notify/topic.go index be1ef3f7ab..a13b56b9cf 100644 --- a/cmd/climc/shell/notify/topic.go +++ b/cmd/climc/shell/notify/topic.go @@ -27,4 +27,6 @@ func init() { cmd.Update(new(options.TopicUpdateOptions)) cmd.Show(new(options.TopicOptions)) cmd.Delete(new(options.TopicOptions)) + cmd.Perform("add-actions", new(options.TopicAddActionInput)) + cmd.Perform("add-resources", new(options.TopicAddResourcesInput)) } diff --git a/pkg/apis/notify/topic.go b/pkg/apis/notify/topic.go index 582a18a5fa..03a18cc131 100644 --- a/pkg/apis/notify/topic.go +++ b/pkg/apis/notify/topic.go @@ -94,3 +94,11 @@ type STopicCreateInput struct { Actions []string `json:"actions"` WebconsoleDisable bool `json:"webconsole_disable"` } + +type TopicAddActionInput struct { + Actions []string `json:"actions"` +} + +type TopicAddResourcesInput struct { + Resources []string `json:"resources"` +} diff --git a/pkg/compute/tasks/disk/disk_batch_create_task.go b/pkg/compute/tasks/disk/disk_batch_create_task.go index 9636149d05..070f59c0d2 100644 --- a/pkg/compute/tasks/disk/disk_batch_create_task.go +++ b/pkg/compute/tasks/disk/disk_batch_create_task.go @@ -138,6 +138,11 @@ func (task *DiskBatchCreateTask) SaveScheduleResult(ctx context.Context, obj uti task.SetStageFailed(ctx, jsonutils.NewString(err.Error())) db.OpsLog.LogEvent(disk, db.ACT_ALLOCATE_FAIL, err, task.UserCred) notifyclient.NotifySystemErrorWithCtx(ctx, disk.Id, disk.Name, api.DISK_ALLOC_FAILED, err.Error()) + notifyclient.EventNotify(ctx, task.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionCreate, + IsFail: true, + }) } data := utils.GetBatchParamsAtIndex(task, index) diff --git a/pkg/compute/tasks/disk/disk_change_storage_type_task.go b/pkg/compute/tasks/disk/disk_change_storage_type_task.go index c16e4a113b..9d2ca55fd7 100644 --- a/pkg/compute/tasks/disk/disk_change_storage_type_task.go +++ b/pkg/compute/tasks/disk/disk_change_storage_type_task.go @@ -24,6 +24,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/util/logclient" ) @@ -82,5 +83,10 @@ func (self *DiskChangeStorageTypeTask) OnInit(ctx context.Context, obj db.IStand func (self *DiskChangeStorageTypeTask) taskFail(ctx context.Context, disk *models.SDisk, err error) { disk.SetStatus(ctx, self.GetUserCred(), api.DISK_MIGRATE_FAIL, "") logclient.AddActionLogWithStartable(self, disk, logclient.ACT_MIGRATE, err, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionMigrate, + IsFail: true, + }) self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) } diff --git a/pkg/compute/tasks/disk/disk_create_task.go b/pkg/compute/tasks/disk/disk_create_task.go index 3c2a4788d9..3cfa982653 100644 --- a/pkg/compute/tasks/disk/disk_create_task.go +++ b/pkg/compute/tasks/disk/disk_create_task.go @@ -102,6 +102,11 @@ func (self *DiskCreateTask) OnStorageCacheImageComplete(ctx context.Context, dis func (self *DiskCreateTask) OnStartAllocateFailed(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) { disk.SetStatus(ctx, self.UserCred, api.DISK_ALLOC_FAILED, data.String()) logclient.AddActionLogWithStartable(self, disk, logclient.ACT_ALLOCATE, data, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionCreate, + IsFail: true, + }) self.SetStageFailed(ctx, data) } @@ -155,6 +160,11 @@ func (self *DiskCreateTask) OnDiskReadyFailed(ctx context.Context, disk *models. } disk.SetStatus(ctx, self.UserCred, status, data.String()) logclient.AddActionLogWithStartable(self, disk, logclient.ACT_ALLOCATE, data, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionCreate, + IsFail: true, + }) self.SetStageFailed(ctx, data) } diff --git a/pkg/compute/tasks/disk/disk_delete_task.go b/pkg/compute/tasks/disk/disk_delete_task.go index a35301f1ae..988ece1d18 100644 --- a/pkg/compute/tasks/disk/disk_delete_task.go +++ b/pkg/compute/tasks/disk/disk_delete_task.go @@ -231,6 +231,11 @@ func (self *DiskDeleteTask) OnGuestDiskDeleteCompleteFailed(ctx context.Context, disk.SetStatus(ctx, self.GetUserCred(), api.DISK_DEALLOC_FAILED, reason.String()) self.SetStageFailed(ctx, reason) db.OpsLog.LogEvent(disk, db.ACT_DELOCATE_FAIL, disk.GetShortDesc(ctx), self.GetUserCred()) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionDelete, + IsFail: true, + }) logclient.AddActionLogWithContext(ctx, disk, logclient.ACT_DELOCATE, reason, self.UserCred, false) } @@ -276,5 +281,10 @@ func (self *StorageDeleteRbdDiskTask) DeleteDisk(ctx context.Context, storage *m func (self *StorageDeleteRbdDiskTask) OnDeleteDiskFailed(ctx context.Context, storage *models.SStorage, data jsonutils.JSONObject) { deleteDisk, _ := data.GetString("delete_disk") db.OpsLog.LogEvent(storage, db.ACT_DELETE_OBJECT, fmt.Sprintf("delete disk %s failed", deleteDisk), self.UserCred) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: storage, + Action: notifyclient.ActionDelete, + IsFail: true, + }) self.DeleteDisk(ctx, storage, self.Params) } diff --git a/pkg/compute/tasks/disk/disk_reset_task.go b/pkg/compute/tasks/disk/disk_reset_task.go index 64ae8737f2..411c864a42 100644 --- a/pkg/compute/tasks/disk/disk_reset_task.go +++ b/pkg/compute/tasks/disk/disk_reset_task.go @@ -54,6 +54,11 @@ func (self *DiskResetTask) getSnapshot() (*models.SSnapshot, error) { func (self *DiskResetTask) TaskFailed(ctx context.Context, disk *models.SDisk, reason error) { disk.SetStatus(ctx, self.UserCred, api.DISK_READY, "") logclient.AddActionLogWithStartable(self, disk, logclient.ACT_RESET_DISK, reason, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionReset, + IsFail: true, + }) snapshot, _ := self.getSnapshot() if snapshot != nil { logclient.AddActionLogWithStartable(self, snapshot, logclient.ACT_RESET_DISK, reason, self.UserCred, false) diff --git a/pkg/compute/tasks/disk/disk_resize_task.go b/pkg/compute/tasks/disk/disk_resize_task.go index c352d2e5a9..b13c4c1345 100644 --- a/pkg/compute/tasks/disk/disk_resize_task.go +++ b/pkg/compute/tasks/disk/disk_resize_task.go @@ -25,6 +25,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/logclient" @@ -97,6 +98,11 @@ func (self *DiskResizeTask) OnStartResizeDiskFailed(ctx context.Context, disk *m self.SetStageFailed(ctx, jsonutils.Marshal(reason)) db.OpsLog.LogEvent(disk, db.ACT_RESIZE_FAIL, reason, self.GetUserCred()) logclient.AddActionLogWithStartable(self, disk, logclient.ACT_RESIZE, reason, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionResize, + IsFail: true, + }) } func (self *DiskResizeTask) OnDiskResizeComplete(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) { @@ -157,5 +163,10 @@ func (self *DiskResizeTask) OnDiskResizeCompleteFailed(ctx context.Context, disk self.SetDiskReady(ctx, disk, self.GetUserCred(), data.String()) db.OpsLog.LogEvent(disk, db.ACT_RESIZE_FAIL, disk.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, disk, logclient.ACT_RESIZE, data, self.UserCred, false) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionResize, + IsFail: true, + }) self.SetStageFailed(ctx, data) } diff --git a/pkg/compute/tasks/disk/ha_disk_create_task.go b/pkg/compute/tasks/disk/ha_disk_create_task.go index 29e4169d98..920d06be83 100644 --- a/pkg/compute/tasks/disk/ha_disk_create_task.go +++ b/pkg/compute/tasks/disk/ha_disk_create_task.go @@ -24,6 +24,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/compute/models" ) @@ -83,6 +84,11 @@ func (self *HADiskCreateTask) OnDiskReady( func (self *HADiskCreateTask) OnBackupAllocateFailed(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) { disk.SetStatus(ctx, self.UserCred, api.DISK_BACKUP_ALLOC_FAILED, data.String()) + notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ + Obj: disk, + Action: notifyclient.ActionCreate, + IsFail: true, + }) self.SetStageFailed(ctx, data) } diff --git a/pkg/compute/tasks/disk/dns_record_create_task.go b/pkg/compute/tasks/dnszone/dns_record_create_task.go similarity index 99% rename from pkg/compute/tasks/disk/dns_record_create_task.go rename to pkg/compute/tasks/dnszone/dns_record_create_task.go index 00b199d9bc..b4c4726d47 100644 --- a/pkg/compute/tasks/disk/dns_record_create_task.go +++ b/pkg/compute/tasks/dnszone/dns_record_create_task.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package disk +package dnszone import ( "context" diff --git a/pkg/compute/tasks/disk/dns_record_delete_task.go b/pkg/compute/tasks/dnszone/dns_record_delete_task.go similarity index 99% rename from pkg/compute/tasks/disk/dns_record_delete_task.go rename to pkg/compute/tasks/dnszone/dns_record_delete_task.go index bc1bc22c5b..eff3d5f737 100644 --- a/pkg/compute/tasks/disk/dns_record_delete_task.go +++ b/pkg/compute/tasks/dnszone/dns_record_delete_task.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package disk +package dnszone import ( "context" diff --git a/pkg/compute/tasks/disk/dns_record_set_enabled_task.go b/pkg/compute/tasks/dnszone/dns_record_set_enabled_task.go similarity index 99% rename from pkg/compute/tasks/disk/dns_record_set_enabled_task.go rename to pkg/compute/tasks/dnszone/dns_record_set_enabled_task.go index 5a5d9342c8..cc60eced60 100644 --- a/pkg/compute/tasks/disk/dns_record_set_enabled_task.go +++ b/pkg/compute/tasks/dnszone/dns_record_set_enabled_task.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package disk +package dnszone import ( "context" diff --git a/pkg/compute/tasks/disk/dns_record_update_task.go b/pkg/compute/tasks/dnszone/dns_record_update_task.go similarity index 99% rename from pkg/compute/tasks/disk/dns_record_update_task.go rename to pkg/compute/tasks/dnszone/dns_record_update_task.go index fb9b27b816..4cce9ae537 100644 --- a/pkg/compute/tasks/disk/dns_record_update_task.go +++ b/pkg/compute/tasks/dnszone/dns_record_update_task.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package disk +package dnszone import ( "context" diff --git a/pkg/mcclient/options/notify/topic.go b/pkg/mcclient/options/notify/topic.go index 41a4de1288..20893ec4f9 100644 --- a/pkg/mcclient/options/notify/topic.go +++ b/pkg/mcclient/options/notify/topic.go @@ -118,3 +118,21 @@ type TopicCreateOptions struct { func (rl *TopicCreateOptions) Params() (jsonutils.JSONObject, error) { return jsonutils.Marshal(rl), nil } + +type TopicAddActionInput struct { + TopicOptions + Actions []string `json:"actions"` +} + +func (rl *TopicAddActionInput) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(rl), nil +} + +type TopicAddResourcesInput struct { + TopicOptions + Resources []string `json:"resources"` +} + +func (rl *TopicAddResourcesInput) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(rl), nil +} diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index 2a9b4b5ca5..3bc8fb5db5 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -75,7 +75,7 @@ type STopic struct { db.SEnabledStatusStandaloneResourceBase Type string `width:"20" nullable:"false" create:"required" update:"user" list:"user"` - Results tristate.TriState `default:"true"` + Results tristate.TriState `default:"true" create:"optional" get:"user" list:"user"` TitleCn string `length:"medium" nullable:"true" charset:"utf8" list:"user" update:"user" create:"optional"` TitleEn string `length:"medium" nullable:"true" charset:"utf8" list:"user" update:"user" create:"optional"` ContentCn string `length:"medium" nullable:"true" charset:"utf8" list:"user" update:"user" create:"optional"` @@ -485,6 +485,7 @@ func initTopicElement(name string, t *STopic) { api.TOPIC_RESOURCE_LOADBALANCER, api.TOPIC_RESOURCE_DBINSTANCE, api.TOPIC_RESOURCE_ELASTICCACHE, + api.TOPIC_RESOURCE_DISK, api.TOPIC_RESOURCE_CLOUDPHONE, ) t.addAction( @@ -947,6 +948,32 @@ func (t *STopic) PreCheckPerformAction( return nil } +func (t *STopic) PerformAddActions( + ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject, data api.TopicAddActionInput, +) (jsonutils.JSONObject, error) { + for _, action := range data.Actions { + if len(action) == 0 { + continue + } + t.addAction(notify.SAction(action)) + } + return nil, nil +} + +func (t *STopic) PerformAddResources( + ctx context.Context, userCred mcclient.TokenCredential, + query jsonutils.JSONObject, data api.TopicAddResourcesInput, +) (jsonutils.JSONObject, error) { + for _, resource := range data.Resources { + if len(resource) == 0 { + continue + } + t.addResources(resource) + } + return nil, nil +} + func init() { converter = &sConverter{ resource2Value: &sync.Map{},