github.com/Cloud-Foundations/Dominator@v0.3.4/cmd/mdbd/loadCis.go (about) 1 package main 2 3 import ( 4 "errors" 5 "io" 6 7 "github.com/Cloud-Foundations/Dominator/lib/json" 8 "github.com/Cloud-Foundations/Dominator/lib/log" 9 "github.com/Cloud-Foundations/Dominator/lib/mdb" 10 ) 11 12 func newCisGenerator(params makeGeneratorParams) (generator, error) { 13 return sourceGenerator{loadCis, params.args[0]}, nil 14 } 15 16 func loadCis(reader io.Reader, datacentre string, logger log.Logger) ( 17 *mdb.Mdb, error) { 18 19 type instanceMetadataType struct { 20 RequiredImage string `json:"required_image"` 21 PlannedImage string `json:"planned_image"` 22 DisableUpdates bool `json:"disable_updates"` 23 OwnerGroup string `json:"owner_group"` 24 } 25 26 type sourceType struct { 27 HostName string `json:"host_name"` 28 InstanceMetadata instanceMetadataType `json:"instance_metadata"` 29 Fqdn string 30 } 31 32 type hitType struct { 33 Source sourceType `json:"_source"` 34 } 35 36 type hitListType struct { 37 Hits []hitType 38 } 39 40 type inMdbType struct { 41 Hits hitListType 42 } 43 44 var inMdb inMdbType 45 var outMdb mdb.Mdb 46 if err := json.Read(reader, &inMdb); err != nil { 47 return nil, errors.New("error decoding: " + err.Error()) 48 } 49 for _, hit := range inMdb.Hits.Hits { 50 var outMachine mdb.Machine 51 if hit.Source.Fqdn != "" { 52 outMachine.Hostname = hit.Source.Fqdn 53 } else { 54 outMachine.Hostname = hit.Source.HostName 55 } 56 outMachine.RequiredImage = hit.Source.InstanceMetadata.RequiredImage 57 outMachine.PlannedImage = hit.Source.InstanceMetadata.PlannedImage 58 outMachine.DisableUpdates = hit.Source.InstanceMetadata.DisableUpdates 59 outMachine.OwnerGroup = hit.Source.InstanceMetadata.OwnerGroup 60 outMdb.Machines = append(outMdb.Machines, outMachine) 61 } 62 return &outMdb, nil 63 }