Files
compute/object/machine.go
zeekayandHanzo Dev 8020a23639 deps: name hanzoai/orm for the schema types, not the engine underneath it
store_base already opened its engine through hanzoai/orm/relational, but ten
object files still reached past it to hanzoai/xorm/schemas for one type —
schemas.PK, the composite key every Update and Delete in this package builds.
So visor named both the namespace and the engine, which is the thing having a
namespace is supposed to prevent.

orm v0.6.20 re-exports that vocabulary, so the import path is the whole change:
the types are aliases, and a PK built through orm IS the engine's PK. xorm drops
to indirect in go.mod, which is what it should have been — the engine, arriving
under the ORM rather than named beside it.

No behaviour change. Tests are clean on both this and the parent commit: zero
failing packages either side.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-03 11:44:57 -07:00

236 lines
6.3 KiB
Go

// Copyright 2024 Hanzo Industries Inc. All Rights Reserved.
//
// 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 object
import (
"fmt"
"github.com/hanzoai/visor/util"
"github.com/hanzoai/orm/relational/schemas"
)
type Machine struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
// Project is the second attribution dimension alongside Owner (the tenant
// boundary): the project WITHIN the org that owns this machine. Not part of the
// (Owner, Name) primary key — an additive, Sync2-safe column that defaults to
// "" (the org's default project), so existing rows and callers are unaffected.
Project string `xorm:"varchar(100)" json:"project"`
Id string `xorm:"varchar(100)" json:"id"`
Provider string `xorm:"varchar(100)" json:"provider"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
ExpireTime string `xorm:"varchar(100)" json:"expireTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Region string `xorm:"varchar(100)" json:"region"`
Zone string `xorm:"varchar(100)" json:"zone"`
Category string `xorm:"varchar(100)" json:"category"`
Type string `xorm:"varchar(100)" json:"type"`
Size string `xorm:"varchar(100)" json:"size"`
Tag string `xorm:"varchar(500)" json:"tag"`
State string `xorm:"varchar(100)" json:"state"`
Image string `xorm:"varchar(100)" json:"image"`
Os string `xorm:"varchar(100)" json:"os"`
PublicIp string `xorm:"varchar(100)" json:"publicIp"`
PrivateIp string `xorm:"varchar(100)" json:"privateIp"`
CpuSize string `xorm:"varchar(100)" json:"cpuSize"`
MemSize string `xorm:"varchar(100)" json:"memSize"`
// DB info
RemoteProtocol string `xorm:"varchar(100)" json:"remoteProtocol"`
RemotePort int `json:"remotePort"`
RemoteUsername string `xorm:"varchar(100)" json:"remoteUsername"`
RemotePassword string `xorm:"varchar(100)" json:"remotePassword"`
}
func GetMachineCount(owner, field, value string) (int64, error) {
session := GetSession(owner, -1, -1, field, value, "", "")
return session.Count(&Machine{})
}
func GetMachines(owner string) ([]*Machine, error) {
machines := []*Machine{}
engine, err := EngineFor(owner)
if err != nil {
return machines, err
}
err = engine.Desc("created_time").Find(&machines, &Machine{Owner: owner})
if err != nil {
return machines, err
}
return machines, nil
}
func GetPaginationMachines(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Machine, error) {
machines := []*Machine{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&machines)
if err != nil {
return machines, err
}
return machines, nil
}
func getMachine(owner string, name string) (*Machine, error) {
if owner == "" || name == "" {
return nil, nil
}
engine, err := EngineFor(owner)
if err != nil {
return nil, err
}
machine := Machine{Owner: owner, Name: name}
existed, err := engine.Get(&machine)
if err != nil {
return &machine, err
}
if existed {
return &machine, nil
} else {
return nil, nil
}
}
func GetMachine(id string) (*Machine, error) {
owner, name := util.GetOwnerAndNameFromId(id)
return getMachine(owner, name)
}
func GetMaskedMachine(machine *Machine, errs ...error) (*Machine, error) {
if len(errs) > 0 && errs[0] != nil {
return nil, errs[0]
}
if machine == nil {
return nil, nil
}
if machine.RemotePassword != "" {
machine.RemotePassword = "***"
}
return machine, nil
}
func GetMaskedMachines(machines []*Machine, errs ...error) ([]*Machine, error) {
if len(errs) > 0 && errs[0] != nil {
return nil, errs[0]
}
var err error
for _, machine := range machines {
machine, err = GetMaskedMachine(machine)
if err != nil {
return nil, err
}
}
return machines, nil
}
func UpdateMachine(id string, machine *Machine) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
oldMachine, err := getMachine(owner, name)
if err != nil {
return false, err
} else if oldMachine == nil {
return false, nil
}
if machine.RemotePassword == "***" {
machine.RemotePassword = oldMachine.RemotePassword
}
_, err = updateMachineCloud(oldMachine, machine)
if err != nil {
return false, err
}
engine, err := EngineFor(owner)
if err != nil {
return false, err
}
affected, err := engine.ID(schemas.PK{owner, name}).AllCols().Update(machine)
if err != nil {
return false, err
}
return affected != 0, nil
}
func AddMachine(machine *Machine) (bool, error) {
engine, err := EngineFor(machine.Owner)
if err != nil {
return false, err
}
affected, err := engine.Insert(machine)
if err != nil {
return false, err
}
return affected != 0, nil
}
// addMachines batch-inserts machines that all belong to owner (they come from a
// single owner's cloud sync), so one EngineFor(owner) routes the whole batch.
func addMachines(owner string, machines []*Machine) (bool, error) {
engine, err := EngineFor(owner)
if err != nil {
return false, err
}
affected, err := engine.Insert(machines)
if err != nil {
return false, err
}
return affected != 0, nil
}
func DeleteMachine(machine *Machine) (bool, error) {
engine, err := EngineFor(machine.Owner)
if err != nil {
return false, err
}
affected, err := engine.ID(schemas.PK{machine.Owner, machine.Name}).Delete(&Machine{})
if err != nil {
return false, err
}
return affected != 0, nil
}
func deleteMachines(owner string) (bool, error) {
engine, err := EngineFor(owner)
if err != nil {
return false, err
}
affected, err := engine.Delete(&Machine{Owner: owner})
if err != nil {
return false, err
}
return affected != 0, nil
}
func (machine *Machine) GetId() string {
return fmt.Sprintf("%s/%s", machine.Owner, machine.Name)
}