-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quality of life features for ContainerConfig (#165)
Shamelessly copied TagsSlice and Tag helper functions from VirtualMachine implementation Add Indexed fields helper maps
- Loading branch information
Showing
5 changed files
with
309 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package proxmox | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// mergeIndexedString uses reflection to merge the ordinal/indexed fields | ||
// returned by the Proxmox API. Such as Mp0..9, and Net0..9 | ||
func (cc *ContainerConfig) mergeIndexedFields(prefix string) map[string]string { | ||
stringMap := make(map[string]string) | ||
t := reflect.TypeOf(*cc) | ||
v := reflect.ValueOf(*cc) | ||
count := v.NumField() | ||
|
||
for i := 0; i < count; i++ { | ||
fn := t.Field(i).Name | ||
fv := v.Field(i).String() | ||
if fv == "" { | ||
continue | ||
} | ||
if strings.HasPrefix(fn, prefix) { | ||
// Ignore non-numeric suffixes like SCSIHW | ||
suffix := strings.TrimPrefix(fn, prefix) | ||
if _, err := strconv.Atoi(suffix); err != nil { | ||
continue | ||
} | ||
stringMap[strings.ToLower(fn)] = fv | ||
} | ||
} | ||
|
||
return stringMap | ||
} | ||
|
||
// MergeDevs merges and assigns the indexed Dev0..9 fields to a string map | ||
func (cc *ContainerConfig) MergeDevs() map[string]string { | ||
if cc.Devs == nil { | ||
cc.Devs = cc.mergeIndexedFields("Dev") | ||
} | ||
return cc.Devs | ||
} | ||
|
||
// MergeDevs merges and assigns the indexed Mp0..9 fields to a string map | ||
func (cc *ContainerConfig) MergeMps() map[string]string { | ||
if cc.Mps == nil { | ||
cc.Mps = cc.mergeIndexedFields("Mp") | ||
} | ||
return cc.Mps | ||
} | ||
|
||
// MergeDevs merges and assigns the indexed Net0..9 fields to a string map | ||
func (cc *ContainerConfig) MergeNets() map[string]string { | ||
if cc.Nets == nil { | ||
cc.Nets = cc.mergeIndexedFields("Net") | ||
} | ||
return cc.Nets | ||
} | ||
|
||
// MergeDevs merges and assigns the indexed Unused0..9 fields to a string map | ||
func (cc *ContainerConfig) MergeUnuseds() map[string]string { | ||
if cc.Unuseds == nil { | ||
cc.Unuseds = cc.mergeIndexedFields("Unused") | ||
} | ||
return cc.Unuseds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.