github.com/Cloud-Foundations/Dominator@v0.3.4/lib/mdb/api.go (about) 1 /* 2 Package mdb implements a simple in-memory Machine DataBase. 3 */ 4 package mdb 5 6 import ( 7 "io" 8 9 "github.com/Cloud-Foundations/Dominator/lib/tags" 10 "github.com/Cloud-Foundations/Dominator/lib/verstr" 11 ) 12 13 type AwsMetadata struct { 14 AccountId string 15 AccountName string 16 InstanceId string 17 Region string 18 Tags tags.Tags 19 } 20 21 // Machine describes a single machine with a unique Hostname and optional 22 // metadata about the machine. 23 type Machine struct { 24 Hostname string 25 IpAddress string `json:",omitempty"` 26 Location string `json:",omitempty"` 27 RequiredImage string `json:",omitempty"` 28 PlannedImage string `json:",omitempty"` 29 DisableUpdates bool `json:",omitempty"` 30 OwnerGroup string `json:",omitempty"` 31 OwnerGroups []string `json:",omitempty"` 32 OwnerUsers []string `json:",omitempty"` 33 Tags tags.Tags `json:",omitempty"` 34 AwsMetadata *AwsMetadata `json:",omitempty"` 35 } 36 37 func (left Machine) Compare(right Machine) bool { 38 return left.compare(right) 39 } 40 41 // UpdateFrom updates dest with data from source. 42 func (dest *Machine) UpdateFrom(source Machine) { 43 dest.updateFrom(source) 44 } 45 46 // Mdb describes a list of Machines. It implements sort.Interface. 47 type Mdb struct { 48 Machines []Machine 49 } 50 51 // DebugWrite writes the JSON representation to w. 52 func (mdb *Mdb) DebugWrite(w io.Writer) error { 53 return mdb.debugWrite(w) 54 } 55 56 // Len returns the number of machines. 57 func (mdb *Mdb) Len() int { 58 return len(mdb.Machines) 59 } 60 61 // Less compares the hostnames of left and right. 62 func (mdb *Mdb) Less(left, right int) bool { 63 return verstr.Less(mdb.Machines[left].Hostname, 64 mdb.Machines[right].Hostname) 65 } 66 67 // Swap swaps two entries in mdb. 68 func (mdb *Mdb) Swap(left, right int) { 69 tmp := mdb.Machines[left] 70 mdb.Machines[left] = mdb.Machines[right] 71 mdb.Machines[right] = tmp 72 }