github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/mdbd/loadCis.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  	"github.com/Cloud-Foundations/Dominator/lib/mdb"
    10  )
    11  
    12  func newCisGenerator(args []string, logger log.DebugLogger) (generator, error) {
    13  	return sourceGenerator{loadCis, 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  	decoder := json.NewDecoder(reader)
    47  	if err := decoder.Decode(&inMdb); err != nil {
    48  		return nil, errors.New("Error decoding: " + err.Error())
    49  	}
    50  	for _, hit := range inMdb.Hits.Hits {
    51  		var outMachine mdb.Machine
    52  		if hit.Source.Fqdn != "" {
    53  			outMachine.Hostname = hit.Source.Fqdn
    54  		} else {
    55  			outMachine.Hostname = hit.Source.HostName
    56  		}
    57  		outMachine.RequiredImage = hit.Source.InstanceMetadata.RequiredImage
    58  		outMachine.PlannedImage = hit.Source.InstanceMetadata.PlannedImage
    59  		outMachine.DisableUpdates = hit.Source.InstanceMetadata.DisableUpdates
    60  		outMachine.OwnerGroup = hit.Source.InstanceMetadata.OwnerGroup
    61  		outMdb.Machines = append(outMdb.Machines, outMachine)
    62  	}
    63  	return &outMdb, nil
    64  }