1 Star 0 Fork 0

player1 / restful-api-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
host.go 5.00 KB
一键复制 编辑 原始数据 按行查看 历史
package host
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/go-playground/validator/v10"
)
// 定义结构
type Vendor int
const (
Private_IDC Vendor = iota + 1
ALIYUN
TXYUN
)
/*
type HOST_FIELD string
const (
HOST_ID HOST_FIELD = "id"
HOST_VENDOR HOST_FIELD = "vendor"
)
*/
var HOST_MODIFY_FIELD_NUM int = 19
// Host 模型定义
type Host struct {
*Resource // 公有属性
*Describe // 资源独有属性
}
func NewHost() *Host {
return &Host{Resource: &Resource{}, Describe: &Describe{}}
}
func (h *Host) Validate() error {
validate := validator.New()
return validate.Struct(h)
}
func (h *Host) InjectDefaultTime() {
// 注入当前时间到Description
currentTime := time.Now()
location, _ := time.LoadLocation("Asia/Shanghai")
chinaTime := currentTime.In(location)
formatChinaTime := chinaTime.Format("2006-01-02 15:04:05")
// 生成随机id
randNumber := rand.Intn(1000) + 1
// 默认值逻辑
if h.Id == "" {
h.Id = strconv.Itoa(randNumber)
h.Description = formatChinaTime
}
}
// Put - 全量更新
func (h *Host) Put(req *UpdateHostRequest) {
// h.Describe = obj 整个对象地址进行更换了,仅做更新不需要更换
/*if req.Id != h.Id {
return fmt.Errorf("not allowed to update ID! id:%s\n", req.Ins.Id)
}*/
*h.Resource = *req.Ins.Resource
*h.Describe = *req.Ins.Describe // 对原先对象的值进行更换,地址不更换
//return nil
}
// Patch - 局部更新
func (h *Host) Patch(req *UpdateHostRequest) {
for rFieldname, _ := range req.ResourceUpdates {
switch rFieldname {
/* case "Vendor":
h.Vendor = req.Ins.Vendor*/
case "Region":
h.Region = req.Ins.Region
case "CreateAt":
h.CreateAt = req.Ins.CreateAt
case "ExpireAt":
h.ExpireAt = req.Ins.ExpireAt
case "Type":
h.Type = req.Ins.Type
case "Name":
h.Name = req.Ins.Name
case "Description":
h.Description = req.Ins.Description
case "Status":
h.Status = req.Ins.Status
case "UpdateAt":
h.UpdateAt = req.Ins.UpdateAt
case "SyncAt":
h.SyncAt = req.Ins.SyncAt
case "Account":
h.Account = req.Ins.Account
case "PublicIP":
h.PublicIP = req.Ins.PublicIP
case "PrivateIP":
h.PrivateIP = req.Ins.PrivateIP
}
fmt.Printf("%s has been updated\n", rFieldname)
}
for dfieldname, _ := range req.DescribeUpdates {
switch dfieldname {
case "CPU":
h.CPU = req.Ins.CPU
case "Memory":
h.Memory = req.Ins.Memory
case "GPUAmount":
h.GPUAmount = req.Ins.GPUAmount
case "GPU":
h.GPUSpec = req.Ins.GPUSpec
case "OSType":
h.OSType = req.Ins.OSType
case "OSName":
h.OSName = req.Ins.OSName
case "SerialNumber":
h.SerialNumber = req.Ins.SerialNumber
}
fmt.Printf("%s has been updated\n", dfieldname)
}
}
type Resource struct {
Id string `json:"id" validate:"required"` // 全局唯一Id
Vendor Vendor `json:"vendor"` // 厂商
Region string `json:"region"` // 地域
CreateAt int64 `json:"create_at"` // 创建时间
ExpireAt int64 `json:"expire_at"` // 过期时间
Type string `json:"type"` // 规格
Name string `json:"name"` // 名称
Description string `json:"description"` // 描述
Status string `json:"status"` // 服务商中的状态
//Tags map[string]string `json:"tags"` // 标签
UpdateAt int64 `json:"update_at"` // 更新时间
SyncAt int64 `json:"sync_at"` // 同步时间
Account string `json:"account"` // 资源的所属账号
PublicIP string `json:"public_ip"` // 公网IP
PrivateIP string `json:"private_ip"` // 内网IP
}
// Describe - Host私有属性
type Describe struct {
CPU int `json:"cpu"` // 核数
Memory int `json:"memory"` // 内存
GPUAmount int `json:"gpu_amount"` // GPU数量
GPUSpec string `json:"gpu_spec"` // GPU类型
OSType string `json:"os_type"` // 操作系统类型,分为Windows和Linux
OSName string `json:"os_name"` // 操作系统名称
SerialNumber string `json:"serial_number"` // 序列号
}
// FieldToTableMap - 支持更新的字段 对应的表
/*var FieldToTableMap = map[string]string{
"Region": "resource",
"CreateAt": "resource",
"ExpireAt": "resource",
"Type": "resource",
"Name": "resource",
"Description": "resource",
"Status": "resource",
"UpdateAt": "resource",
"SyncAt": "resource",
"Account": "resource",
"PublicIP": "resource",
"PrivateIP": "resource",
"CPU": "host",
"Memory": "host",
"GPUAmount": "host",
"GPUSpec": "host",
"OSType": "host",
"OSName": "host",
"SerialNumber": "host",
}*/
type HostSet struct {
Total int `json:"total"` //计算有几页
Items []*Host `json:"items"`
}
func NewHostSet() *HostSet {
return &HostSet{
Items: []*Host{},
Total: 0,
}
}
func (h *HostSet) Add(item *Host) *HostSet {
h.Items = append(h.Items, item)
return h
}
Go
1
https://gitee.com/player1111/restful-api-demo.git
git@gitee.com:player1111/restful-api-demo.git
player1111
restful-api-demo
restful-api-demo
19fd67857982

搜索帮助