From f378283e3ed0e575600b333e293d7a2546694c4c Mon Sep 17 00:00:00 2001 From: Miguel Moll Date: Thu, 9 May 2024 10:21:51 -0400 Subject: [PATCH 1/2] feat: ability to set specific context fields --- cloud/instance/instance.go | 2 ++ cloud/instance/template.go | 60 +++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/cloud/instance/instance.go b/cloud/instance/instance.go index b04132a..4787db2 100644 --- a/cloud/instance/instance.go +++ b/cloud/instance/instance.go @@ -121,6 +121,7 @@ type Template struct { MemoryMax string MemoryResizeMode string MemorySlots string + Name string NIC []NIC NICAlias []NICAlias NICDefault string @@ -156,6 +157,7 @@ type Context struct { Network bool SSHPublicKey string Target string + ProjectName string } // OS is the API payload based on the legacy xmlrpc backend. diff --git a/cloud/instance/template.go b/cloud/instance/template.go index d0a20fc..c00706a 100644 --- a/cloud/instance/template.go +++ b/cloud/instance/template.go @@ -232,35 +232,49 @@ func newInstanceContext(m map[string]any) (*Context, error) { var dst Context for key, value := range m { - if v, ok := value.(string); ok { - switch key { - case "DISK_ID": - n, err := strconv.Atoi(v) - if err != nil { - return nil, fmt.Errorf("invalid DISK_ID value %q: %w", v, err) - } - dst.DiskID = n - case "FIRMWARE": - dst.Firmware = v - case "GUESTOS": - dst.GuestOS = v - case "NETWORK": - b, err := api.Str2Bool(v) - if err != nil { - return nil, fmt.Errorf("invalid NETWORK value %q: %w", v, err) - } - dst.Network = b - case "SSH_PUBLIC_KEY": - dst.SSHPublicKey = v - case "TARGET": - dst.Target = v - } + err := SetContextValue(&dst, key, value) + if err != nil { + return nil, err } } return &dst, nil } +// SetContextValue sets a key:value for the given context +// The key must match known fields and named with capital letters with underscores +// e.g. DISK_ID, SSH_PUBLIC_KEY, etc. +func SetContextValue(dst *Context, key string, value any) error { + if v, ok := value.(string); ok { + switch key { + case "DISK_ID": + n, err := strconv.Atoi(v) + if err != nil { + return fmt.Errorf("invalid DISK_ID value %q: %w", v, err) + } + dst.DiskID = n + case "FIRMWARE": + dst.Firmware = v + case "GUESTOS": + dst.GuestOS = v + case "NETWORK": + b, err := api.Str2Bool(v) + if err != nil { + return fmt.Errorf("invalid NETWORK value %q: %w", v, err) + } + dst.Network = b + case "SSH_PUBLIC_KEY": + dst.SSHPublicKey = v + case "TARGET": + dst.Target = v + case "PROJECT_NAME": + dst.ProjectName = v + } + } + + return fmt.Errorf("unknown key: %s", key) +} + func newInstanceCPUModel(m map[string]any) *CPUModel { var dst CPUModel From 29657709183a6b0d7b9eebe08347a2a871727bde Mon Sep 17 00:00:00 2001 From: Miguel Moll Date: Tue, 28 May 2024 10:23:30 -0400 Subject: [PATCH 2/2] fix: permission access for setting context --- cloud/instance/template.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cloud/instance/template.go b/cloud/instance/template.go index c00706a..0ebd5b8 100644 --- a/cloud/instance/template.go +++ b/cloud/instance/template.go @@ -232,7 +232,7 @@ func newInstanceContext(m map[string]any) (*Context, error) { var dst Context for key, value := range m { - err := SetContextValue(&dst, key, value) + err := dst.Set(key, value) if err != nil { return nil, err } @@ -241,10 +241,14 @@ func newInstanceContext(m map[string]any) (*Context, error) { return &dst, nil } -// SetContextValue sets a key:value for the given context +// Set sets a key:value for the given context // The key must match known fields and named with capital letters with underscores // e.g. DISK_ID, SSH_PUBLIC_KEY, etc. -func SetContextValue(dst *Context, key string, value any) error { +func (c *Context) Set(key string, value any) error { + return setContextValue(c, key, value) +} + +func setContextValue(dst *Context, key string, value any) error { if v, ok := value.(string); ok { switch key { case "DISK_ID": @@ -269,10 +273,12 @@ func SetContextValue(dst *Context, key string, value any) error { dst.Target = v case "PROJECT_NAME": dst.ProjectName = v + default: + return fmt.Errorf("unknown key: %s", key) } } - return fmt.Errorf("unknown key: %s", key) + return nil } func newInstanceCPUModel(m map[string]any) *CPUModel {