mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feature: api for sending emails with attachments (#15201)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
// 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 db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/sqlchemy"
|
||||
"yunion.io/x/sqlchemy/backends/clickhouse"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
type SLogBaseManager struct {
|
||||
SModelBaseManager
|
||||
}
|
||||
|
||||
type SLogBase struct {
|
||||
SModelBase
|
||||
|
||||
Id int64 `primary:"true" auto_increment:"true" list:"user" clickhouse_partition_by:"toInt64(divide(id,100000000000))"`
|
||||
}
|
||||
|
||||
func NewLogBaseManager(model interface{}, table string, keyword, keywordPlural string, timeCol string, useClickHouse bool) SLogBaseManager {
|
||||
if useClickHouse {
|
||||
man := SLogBaseManager{NewModelBaseManagerWithDBName(
|
||||
model,
|
||||
table,
|
||||
keyword,
|
||||
keywordPlural,
|
||||
ClickhouseDB,
|
||||
)}
|
||||
col := man.TableSpec().ColumnSpec("timeCol")
|
||||
if clickCol, ok := col.(clickhouse.IClickhouseColumnSpec); ok {
|
||||
clickCol.SetTTL(consts.SplitableMaxKeepMonths(), "MONTH")
|
||||
}
|
||||
return man
|
||||
} else {
|
||||
return SLogBaseManager{NewModelBaseManagerWithSplitable(
|
||||
model,
|
||||
table,
|
||||
keyword,
|
||||
keywordPlural,
|
||||
"id",
|
||||
timeCol,
|
||||
consts.SplitableMaxDuration(),
|
||||
consts.SplitableMaxKeepMonths(),
|
||||
)}
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SLogBaseManager) CreateByInsertOrUpdate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CurrentTimestamp(t time.Time) int64 {
|
||||
ret := int64(0)
|
||||
const (
|
||||
yOffset = 10000000000000
|
||||
mOffset = 100000000000
|
||||
dOffset = 1000000000
|
||||
hOffset = 10000000
|
||||
iOffset = 100000
|
||||
sOffset = 1000
|
||||
)
|
||||
ret += int64(t.Year()) * yOffset
|
||||
ret += int64(t.Month()) * mOffset
|
||||
ret += int64(t.Day()) * dOffset
|
||||
ret += int64(t.Hour()) * hOffset
|
||||
ret += int64(t.Minute()) * iOffset
|
||||
ret += int64(t.Second()) * sOffset
|
||||
ret += int64(t.Nanosecond()) / 1000000
|
||||
return ret
|
||||
}
|
||||
|
||||
func (opslog *SLogBase) BeforeInsert() {
|
||||
t := time.Now().UTC()
|
||||
opslog.Id = CurrentTimestamp(t)
|
||||
}
|
||||
|
||||
func (opslog *SLogBase) GetId() string {
|
||||
return fmt.Sprintf("%d", opslog.Id)
|
||||
}
|
||||
|
||||
func (self *SLogBase) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
|
||||
return httperrors.NewForbiddenError("not allow to delete log")
|
||||
}
|
||||
|
||||
func (self *SLogBaseManager) FilterById(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
return q.Equals("id", id)
|
||||
}
|
||||
|
||||
func (self *SLogBaseManager) FilterByNotId(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
return q.NotEquals("id", id)
|
||||
}
|
||||
|
||||
func (self *SLogBaseManager) FilterByName(q *sqlchemy.SQuery, name string) *sqlchemy.SQuery {
|
||||
return q
|
||||
}
|
||||
|
||||
func (manager *SLogBaseManager) GetPagingConfig() *SPagingConfig {
|
||||
return &SPagingConfig{
|
||||
Order: sqlchemy.SQL_ORDER_DESC,
|
||||
MarkerFields: []string{"id"},
|
||||
DefaultLimit: 20,
|
||||
}
|
||||
}
|
||||
|
||||
func (lb *SLogBase) GetRecordTime() time.Time {
|
||||
log.Fatalf("not implemented yet!")
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (manager *SLogBaseManager) FetchById(idStr string) (IModel, error) {
|
||||
return FetchById(manager.GetIModelManager(), idStr)
|
||||
}
|
||||
|
||||
func (l *SLogBase) ValidateUpdateData(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
data jsonutils.JSONObject,
|
||||
) (jsonutils.JSONObject, error) {
|
||||
return nil, errors.Wrap(httperrors.ErrForbidden, "not allow")
|
||||
}
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -30,7 +29,6 @@ import (
|
||||
"yunion.io/x/pkg/util/stringutils"
|
||||
"yunion.io/x/pkg/util/timeutils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
"yunion.io/x/sqlchemy/backends/clickhouse"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
@@ -42,13 +40,12 @@ import (
|
||||
)
|
||||
|
||||
type SOpsLogManager struct {
|
||||
SModelBaseManager
|
||||
SLogBaseManager
|
||||
}
|
||||
|
||||
type SOpsLog struct {
|
||||
SModelBase
|
||||
SLogBase
|
||||
|
||||
Id int64 `primary:"true" auto_increment:"true" list:"user" clickhouse_partition_by:"toInt64(divide(id,100000000000))"`
|
||||
ObjType string `width:"40" charset:"ascii" nullable:"false" list:"user" create:"required"`
|
||||
ObjId string `width:"128" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
|
||||
ObjName string `width:"128" charset:"utf8" nullable:"false" list:"user" create:"required"`
|
||||
@@ -81,41 +78,21 @@ var _ IModel = (*SOpsLog)(nil)
|
||||
var opslogQueryWorkerMan *appsrv.SWorkerManager
|
||||
var opslogWriteWorkerMan *appsrv.SWorkerManager
|
||||
|
||||
func InitOpsLog() {
|
||||
if consts.OpsLogWithClickhouse {
|
||||
OpsLog = &SOpsLogManager{NewModelBaseManagerWithDBName(
|
||||
SOpsLog{},
|
||||
"opslog_tbl",
|
||||
"event",
|
||||
"events",
|
||||
ClickhouseDB,
|
||||
)}
|
||||
col := OpsLog.TableSpec().ColumnSpec("ops_time")
|
||||
if clickCol, ok := col.(clickhouse.IClickhouseColumnSpec); ok {
|
||||
clickCol.SetTTL(consts.SplitableMaxKeepMonths(), "MONTH")
|
||||
}
|
||||
} else {
|
||||
OpsLog = &SOpsLogManager{NewModelBaseManagerWithSplitable(
|
||||
SOpsLog{},
|
||||
"opslog_tbl",
|
||||
"event",
|
||||
"events",
|
||||
"id",
|
||||
"ops_time",
|
||||
consts.SplitableMaxDuration(),
|
||||
consts.SplitableMaxKeepMonths(),
|
||||
)}
|
||||
func NewOpsLogManager(opslog interface{}, tblName string, keyword, keywordPlural string, timeField string, clickhouse bool) SOpsLogManager {
|
||||
return SOpsLogManager{
|
||||
SLogBaseManager: NewLogBaseManager(opslog, tblName, keyword, keywordPlural, timeField, clickhouse),
|
||||
}
|
||||
}
|
||||
|
||||
func InitOpsLog() {
|
||||
tmp := NewOpsLogManager(SOpsLog{}, "opslog_tbl", "event", "events", "ops_time", consts.OpsLogWithClickhouse)
|
||||
OpsLog = &tmp
|
||||
OpsLog.SetVirtualObject(OpsLog)
|
||||
|
||||
opslogQueryWorkerMan = appsrv.NewWorkerManager("opslog_query_worker", 2, 512, true)
|
||||
opslogWriteWorkerMan = appsrv.NewWorkerManager("opslog_write_worker", 1, 2048, true)
|
||||
}
|
||||
|
||||
func (manager *SOpsLogManager) CreateByInsertOrUpdate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (manager *SOpsLogManager) CustomizeHandlerInfo(info *appsrv.SHandlerInfo) {
|
||||
manager.SModelBaseManager.CustomizeHandlerInfo(info)
|
||||
|
||||
@@ -125,35 +102,6 @@ func (manager *SOpsLogManager) CustomizeHandlerInfo(info *appsrv.SHandlerInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
func CurrentTimestamp(t time.Time) int64 {
|
||||
ret := int64(0)
|
||||
const (
|
||||
yOffset = 10000000000000
|
||||
mOffset = 100000000000
|
||||
dOffset = 1000000000
|
||||
hOffset = 10000000
|
||||
iOffset = 100000
|
||||
sOffset = 1000
|
||||
)
|
||||
ret += int64(t.Year()) * yOffset
|
||||
ret += int64(t.Month()) * mOffset
|
||||
ret += int64(t.Day()) * dOffset
|
||||
ret += int64(t.Hour()) * hOffset
|
||||
ret += int64(t.Minute()) * iOffset
|
||||
ret += int64(t.Second()) * sOffset
|
||||
ret += int64(t.Nanosecond()) / 1000000
|
||||
return ret
|
||||
}
|
||||
|
||||
func (opslog *SOpsLog) BeforeInsert() {
|
||||
t := time.Now().UTC()
|
||||
opslog.Id = CurrentTimestamp(t)
|
||||
}
|
||||
|
||||
func (opslog *SOpsLog) GetId() string {
|
||||
return fmt.Sprintf("%d", opslog.Id)
|
||||
}
|
||||
|
||||
func (opslog *SOpsLog) GetName() string {
|
||||
return fmt.Sprintf("%s-%s", opslog.ObjType, opslog.Action)
|
||||
}
|
||||
@@ -410,24 +358,6 @@ func (manager *SOpsLogManager) LogSyncUpdate(m IModel, uds sqlchemy.UpdateDiffs,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SOpsLog) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
|
||||
return httperrors.NewForbiddenError("not allow to delete log")
|
||||
}
|
||||
|
||||
func (self *SOpsLogManager) FilterById(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
return q.Equals("id", id)
|
||||
}
|
||||
|
||||
func (self *SOpsLogManager) FilterByNotId(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery {
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
return q.NotEquals("id", id)
|
||||
}
|
||||
|
||||
func (self *SOpsLogManager) FilterByName(q *sqlchemy.SQuery, name string) *sqlchemy.SQuery {
|
||||
return q
|
||||
}
|
||||
|
||||
func (self *SOpsLogManager) FilterByOwner(q *sqlchemy.SQuery, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope) *sqlchemy.SQuery {
|
||||
if ownerId != nil {
|
||||
switch scope {
|
||||
@@ -487,14 +417,6 @@ func (manager *SOpsLogManager) ResourceScope() rbacutils.TRbacScope {
|
||||
return rbacutils.ScopeUser
|
||||
}
|
||||
|
||||
func (manager *SOpsLogManager) GetPagingConfig() *SPagingConfig {
|
||||
return &SPagingConfig{
|
||||
Order: sqlchemy.SQL_ORDER_DESC,
|
||||
MarkerFields: []string{"id"},
|
||||
DefaultLimit: 20,
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SOpsLogManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
|
||||
ownerId := SOwnerId{}
|
||||
err := data.Unmarshal(&ownerId)
|
||||
@@ -533,6 +455,7 @@ func (log *SOpsLog) CustomizeCreate(ctx context.Context,
|
||||
return log.SModelBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
|
||||
}
|
||||
|
||||
// override
|
||||
func (log *SOpsLog) GetRecordTime() time.Time {
|
||||
return log.OpsTime
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user