added better id handling
This commit is contained in:
parent
d703fdea5f
commit
78b40626e6
4 changed files with 77 additions and 33 deletions
35
server.go
35
server.go
|
|
@ -84,10 +84,11 @@ func (s *Server) Ref() *ServerRef {
|
|||
type ServerRefList []*ServerRef
|
||||
|
||||
type ServerRef struct {
|
||||
ID int `json:"vmid"`
|
||||
Name string `json:"name"`
|
||||
Node string
|
||||
Pool string
|
||||
ID int `json:"vmid"`
|
||||
Name string `json:"name"`
|
||||
Template int `json:"template,omitempty"`
|
||||
Node string `json:"node,omitempty"`
|
||||
Pool string `json:"pool,omitempty"`
|
||||
}
|
||||
|
||||
func (ref *ServerRef) InstanceID() string {
|
||||
|
|
@ -98,6 +99,10 @@ func (ref *ServerRef) K8sID() string {
|
|||
return NewURL(ref.Pool, "", ref.ID)
|
||||
}
|
||||
|
||||
func (ref *ServerRef) IsTemplate() bool {
|
||||
return ref.Template == 1
|
||||
}
|
||||
|
||||
type Resources struct {
|
||||
Cores Cores `json:"cores"`
|
||||
Memory Memory `json:"memory"`
|
||||
|
|
@ -221,7 +226,7 @@ type ServerClient struct {
|
|||
}
|
||||
|
||||
func (c *ServerClient) NextID(ctx context.Context) (id int, err error) {
|
||||
id = -1
|
||||
id = InvalidID
|
||||
req, err := c.client.NewRequest(ctx, "GET", "/cluster/nextid", nil)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -289,8 +294,9 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
|
|||
}
|
||||
|
||||
type ServerTemplateOpts struct {
|
||||
Name string
|
||||
TemplateID int
|
||||
Name string
|
||||
TemplateID int
|
||||
TemplateName string
|
||||
// TemplateNode is optional
|
||||
TemplateNode string
|
||||
Pool string
|
||||
|
|
@ -302,8 +308,19 @@ func (o *ServerTemplateOpts) Validate(ctx context.Context, c *Client) error {
|
|||
if o.Name == "" {
|
||||
return errors.New("missing name")
|
||||
}
|
||||
if o.TemplateID <= 0 {
|
||||
return errors.New("missing template id")
|
||||
if ValidateID(o.TemplateID) != nil {
|
||||
if o.TemplateName == "" {
|
||||
return errors.New("missing or invalid template id")
|
||||
}
|
||||
ref, err := c.Node.FindServerByName(ctx, o.TemplateName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("template id not found: %w", err)
|
||||
}
|
||||
if !ref.IsTemplate() {
|
||||
return errors.New("server is not a vm template")
|
||||
}
|
||||
o.TemplateID = ref.ID
|
||||
o.TemplateNode = ref.Node
|
||||
}
|
||||
if o.TemplateNode == "" {
|
||||
ref, err := c.Node.FindServerByID(ctx, o.TemplateID)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue