feat: implement license platform business flows and delivery tooling
Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
92
backend/internal/httpapi/instance_freeze.go
Normal file
92
backend/internal/httpapi/instance_freeze.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (a *BusinessAPI) freezeInstance(c *gin.Context) {
|
||||
key, ok := parseOperationKey(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Frozen *bool `json:"frozen"`
|
||||
}
|
||||
if c.ShouldBindJSON(&in) != nil || in.Frozen == nil {
|
||||
apiError(c, 400, "invalid_request", "必须指定 frozen")
|
||||
return
|
||||
}
|
||||
state := "none"
|
||||
if *in.Frozen {
|
||||
state = "frozen"
|
||||
}
|
||||
fingerprint := sha256.Sum256([]byte(c.Param("instance_id") + "\x00" + state))
|
||||
tx, err := a.db.BeginTx(c, nil)
|
||||
if err != nil {
|
||||
apiError(c, 500, "internal_error", "服务暂时不可用")
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
fail := func() { apiError(c, 500, "internal_error", "冻结操作失败,请使用相同幂等键重试") }
|
||||
actor := c.MustGet("admin_id")
|
||||
var adminID uint64
|
||||
if err = tx.QueryRowContext(c, "SELECT id FROM admins WHERE id=? FOR UPDATE", actor).Scan(&adminID); err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
var id uint64
|
||||
var previous string
|
||||
err = tx.QueryRowContext(c, "SELECT id,freeze_state FROM license_instances WHERE instance_id=? FOR UPDATE", c.Param("instance_id")).Scan(&id, &previous)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
apiError(c, 404, "not_found", "实例不存在")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
var oldFingerprint, oldResponse []byte
|
||||
err = tx.QueryRowContext(c, "SELECT request_fingerprint,response FROM instance_state_operations WHERE actor_admin_id=? AND idempotency_key=?", actor, key).Scan(&oldFingerprint, &oldResponse)
|
||||
if err == nil {
|
||||
if !bytesEqual(oldFingerprint, fingerprint[:]) {
|
||||
apiError(c, 409, "idempotency_conflict", "幂等键请求不同")
|
||||
return
|
||||
}
|
||||
var replay any
|
||||
if json.Unmarshal(oldResponse, &replay) != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
c.JSON(200, replay)
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
if _, err = tx.ExecContext(c, "UPDATE license_instances SET freeze_state=? WHERE id=?", state, id); err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
response := gin.H{"data": gin.H{"instance_id": c.Param("instance_id"), "freeze_state": state}}
|
||||
encoded, _ := json.Marshal(response)
|
||||
if _, err = tx.ExecContext(c, "INSERT INTO instance_state_operations(instance_id,actor_admin_id,idempotency_key,request_fingerprint,response) VALUES(?,?,?,?,?)", id, actor, key, fingerprint[:], encoded); err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
details, _ := json.Marshal(gin.H{"previous_state": previous, "freeze_state": state})
|
||||
if _, err = tx.ExecContext(c, "INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,'instance.freeze_changed','license_instance',?,?)", actor, id, details); err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
fail()
|
||||
return
|
||||
}
|
||||
c.JSON(200, response)
|
||||
}
|
||||
Reference in New Issue
Block a user