Skip to content

Commit

Permalink
fix transfer self; (#3493)
Browse files Browse the repository at this point in the history
add GetBillingCount db Interface
  • Loading branch information
bxy4543 authored Jul 12, 2023
1 parent fadfcba commit 0fc3f44
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions controllers/account/controllers/transfer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ func (r *TransferReconciler) check(ctx context.Context, transfer *accountv1.Tran
}
from := transfer.Namespace
to := transfer.Spec.To
if getUsername(from) == getUsername(to) {
return fmt.Errorf("can not transfer to self")
}
fromAccount := accountv1.Account{}
if r.Get(ctx, client.ObjectKey{Namespace: r.AccountSystemNamespace, Name: getUsername(from)}, &fromAccount) != nil {
return fmt.Errorf("owner %s account not found", from)
Expand Down
Empty file modified controllers/account/testdata/account/recharge.go
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions controllers/account/testdata/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"context"

"github.com/labring/sealos/controllers/pkg/crypto"

accountv1 "github.com/labring/sealos/controllers/account/api/v1"
baseapi "github.com/labring/sealos/test/testdata/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -16,6 +18,7 @@ func RechargeAccount(AccountName, namespace string, amount int64) error {
return err
}
account.Status.Balance = amount
account.Status.EncryptBalance, _ = crypto.EncryptInt64(amount)
client := baseapi.GetDefaultDynamicClient()
gvr := accountv1.GroupVersion.WithResource("accounts")
unstructured2, err := runtime.DefaultUnstructuredConverter.ToUnstructured(account)
Expand All @@ -36,6 +39,7 @@ func DeductionAccount(AccountName, namespace string, amount int64) error {
return err
}
account.Status.DeductionBalance = amount
account.Status.EncryptDeductionBalance, _ = crypto.EncryptInt64(amount)
client := baseapi.GetDefaultDynamicClient()
gvr := accountv1.GroupVersion.WithResource("accounts")
unstructured2, err := runtime.DefaultUnstructuredConverter.ToUnstructured(account)
Expand Down
1 change: 1 addition & 0 deletions controllers/pkg/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Interface interface {
QueryBillingRecords(billingRecordQuery *accountv1.BillingRecordQuery, owner string) error
GetUpdateTimeForCategoryAndPropertyFromMetering(category string, property string) (time.Time, error)
GetAllPricesMap() (map[string]common.Price, error)
GetBillingCount(accountType accountv1.Type, startTime, endTime time.Time) (count, amount int64, err error)
GenerateMeteringData(startTime, endTime time.Time, prices map[string]common.Price) error
InsertMonitor(ctx context.Context, monitors ...*common.Monitor) error
Disconnect(ctx context.Context) error
Expand Down
33 changes: 33 additions & 0 deletions controllers/pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,39 @@ func (m *MongoDB) QueryBillingRecords(billingRecordQuery *accountv1.BillingRecor
return nil
}

func (m *MongoDB) GetBillingCount(accountType accountv1.Type, startTime, endTime time.Time) (count, amount int64, err error) {
filter := bson.M{
"type": accountType,
"time": bson.M{
"$gte": startTime,
"$lte": endTime,
},
}
cursor, err := m.getBillingCollection().Find(context.Background(), filter)
if err != nil {
return 0, 0, err
}
defer cursor.Close(context.Background())
var accountBalanceList []AccountBalanceSpecBSON
err = cursor.All(context.Background(), &accountBalanceList)
if err != nil {
return 0, 0, fmt.Errorf("failed to decode all billing record: %w", err)
}
for i := range accountBalanceList {
count++
amount += accountBalanceList[i].Amount
}
//for cursor.Next(context.Background()) {
// var accountBalance AccountBalanceSpecBSON
// if err := cursor.Decode(&accountBalance); err != nil {
// return 0, 0, err
// }
// count++
// amount += accountBalance.Amount
//}
return
}

func (m *MongoDB) getMeteringCollection() *mongo.Collection {
return m.Client.Database(m.DBName).Collection(m.MeteringConn)
}
Expand Down

0 comments on commit 0fc3f44

Please sign in to comment.