Skip to content
Snippets Groups Projects
fetch.go 5.74 KiB
Newer Older
package fetch

import (
	"fmt"
	"log"

	"crypto/tls"
	"encoding/json"
	"net/http"
)

var Hosts = map[string]Host{}
Hugo Hörnquist's avatar
Hugo Hörnquist committed

const apiurl = "https://chapman.lysator.liu.se/api/"
Hugo Hörnquist's avatar
Hugo Hörnquist committed

// which classes a given host has
var hostClasses map[float64][]Puppetclass

Hugo Hörnquist's avatar
Hugo Hörnquist committed
var AllClasses map[string]Puppetclass
Hugo Hörnquist's avatar
Hugo Hörnquist committed

// password for foreman access, TODO replace with API key
var password string
var client *http.Client

Hugo Hörnquist's avatar
Hugo Hörnquist committed
// echo curl --user "hugo:$(pass lysator/hugo)" -k https://chapman.lysator.liu.se/api/hosts

type Host struct {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Data HostRecord
Hugo Hörnquist's avatar
Hugo Hörnquist committed
func Init(pw string) {

	password = pw

	hostClasses = map[float64][]Puppetclass{}
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	AllClasses = map[string]Puppetclass{}
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

Hugo Hörnquist's avatar
Hugo Hörnquist committed
	client = &http.Client{Transport: tr}
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	var m HostMessage
	fff_pages("hosts", &m)
Hugo Hörnquist's avatar
Hugo Hörnquist committed

	for _, record := range m.Results {
		Hosts[record.Name] = Host{record}
	}

Hugo Hörnquist's avatar
Hugo Hörnquist committed
	var m2 PuppetclassMessage
Hugo Hörnquist's avatar
Hugo Hörnquist committed

Hugo Hörnquist's avatar
Hugo Hörnquist committed
	fff("puppetclasses", &m2)

	for group, entries := range m2.Results {
		log.Print("Handling ", group)
		for _, entry := range entries {
			// e := entry.(map[string]interface{})
			AllClasses[entry.Name] = entry
		}
	}
Hugo Hörnquist's avatar
Hugo Hörnquist committed
}

func fff (url string, message interface{}) {

	request, err := http.NewRequest("GET", apiurl + url, nil)

	if err != nil {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		log.Fatal(err)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	request.SetBasicAuth("hugo", password)

	resp, err := client.Do(request)
	if err != nil {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		log.Fatal(err)
	}
	defer resp.Body.Close()


Hugo Hörnquist's avatar
Hugo Hörnquist committed
	decoder := json.NewDecoder(resp.Body)
Hugo Hörnquist's avatar
Hugo Hörnquist committed

Hugo Hörnquist's avatar
Hugo Hörnquist committed
	err = decoder.Decode(message)
	if err != nil {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		log.Fatal(err)
Hugo Hörnquist's avatar
Hugo Hörnquist committed

}

func fff_pages(url string, message IsMessage) {
	fff(url, &message)
	log.Printf("message = %+v", message.GetMessage())
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	// TODO
	// logic here for fetching additional pages
	// (preferably async)
	// IsMessage should require a Merge method
	// TODO also figure out pointer stuff
Hugo Hörnquist's avatar
Hugo Hörnquist committed
func GetClassList(host HostRecord) []string {
	var record []Puppetclass
	if val, ok := hostClasses[host.Id]; ok {
		record = val
	} else {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		url := fmt.Sprintf("hosts/%d/puppetclasses", int(host.Id))
		var m PuppetclassMessage
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		fff_pages(url, &m)
		for _, values := range m.Results {
			record = append(record, values...)
		}
		hostClasses[host.Id] = record
	}

	str := []string{}
	for _, r := range record {
		str = append(str, r.Name)
	}
	return str
}

Hugo Hörnquist's avatar
Hugo Hörnquist committed
type HostRecord struct {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Ip               string  // "130.236.254.139",
	Ip6              string  // "2001:6b0:17:f0a0::8b",
	Environment_id   float64 // 1,
	Environment_name string  // "production",
	Last_report      string  // "2020-10-28 19:10:57 UTC",
	Mac              string  // "1c:c1:de:03:e7:b6",
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"realm_id": null,
		"realm_name": null,
		"sp_mac": null,
		"sp_ip": null,
		"sp_name": null,
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Domain_id            float64 // 1,
	Domain_name          string  // "lysator.liu.se",
	Architecture_id      float64 // 1,
	Architecture_name    string  // "x86_64",
	Operatingsystem_id   float64 // 6,
	Operatingsystem_name string  // "CentOS Linux 7.8.2003",
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"subnet_id": null,
		"subnet_name": null,
		"subnet6_id": null,
		"subnet6_name": null,
		"sp_subnet_id": null,
		"ptable_id": null,
		"ptable_name": null,
		"medium_id": null,
		"medium_name": null,
		"pxe_loader": null,
		"build": false,
	*/
	Comment string // "",
	/*
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"disk": null,
		"installed_at": null,
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Model_id     float64 // 3,
	Hostgroup_id float64 // 6,
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Owner_id     float64 // 5,
	Owner_name   string  // "Henrik Henriksson",
	Owner_type   string  // "User",
	Enabled      bool    // true,
	Managed      bool    // false,
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"use_image": null,
	*/
	Image_file string // "",
	/*
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"uuid": null,
		"compute_resource_id": null,
		"compute_resource_name": null,
		"compute_profile_id": null,
		"compute_profile_name": null,
	*/
	// capabilities []interface{} // [ "build" ],
	Provision_method string // "build",
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Certname         string // "analysator-system.lysator.liu.se",
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"image_id": null,
		"image_name": null,
	*/
	Created_at string // "2020-08-16 19:51:59 UTC",
	Updated_at string // "2020-10-28 19:11:26 UTC",
	/*
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"last_compile": null,
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Global_status              float64 // 0,
	Global_status_label        string  // "OK",
	Uptime_seconds             float64 // 20857465,
	Organization_id            float64 // 1,
	Organization_name          string  // "Lysator",
	Location_id                float64 // 2,
	Location_name              string  // "Foo",
	Puppet_status              float64 // 0,
	Model_name                 string  // "ProLiant DL180 G6",
	Configuration_status       float64 // 0,
	Configuration_status_label string  // "No changes",
	Name                       string  // "analysator-system.lysator.liu.se",
	Id                         float64 // 13,
	Puppet_proxy_id            float64 // 1,
	Puppet_proxy_name          string  // "chapman.lysator.liu.se",
	Puppet_ca_proxy_id         float64 // 1,
	Puppet_ca_proxy_name       string  // "chapman.lysator.liu.se",
Hugo Hörnquist's avatar
Hugo Hörnquist committed
		"puppet_proxy": {
			"name": "chapman.lysator.liu.se",
			"id": 1,
			"url": "https://chapman.lysator.liu.se:8443"
		},
		"puppet_ca_proxy": {
			"name": "chapman.lysator.liu.se",
			"id": 1,
			"url": "https://chapman.lysator.liu.se:8443"
		},
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Hostgroup_name  string // "System",
	Hostgroup_title string // "Analysator/System"
}

Hugo Hörnquist's avatar
Hugo Hörnquist committed

type IsMessage interface {
	GetMessage() Message
	// TODO
	// MergeMessage(Message) Message
}

type Message struct {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Total, Subtotal, Page, Per_page float64
	// search
	// sort { by, order null }
}

Hugo Hörnquist's avatar
Hugo Hörnquist committed
type HostMessage struct {
	Message
	Results []HostRecord
}

func (m HostMessage) GetMessage() Message {
	return m.Message
}

type Puppetclass struct {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Name       string // "profiles::localtime"
	Created_at string // "2020-10-27T07:57:47.102Z"
	Updated_at string // "2020-10-27T07:57:47.102Z"
}

type PuppetclassMessage struct {
Hugo Hörnquist's avatar
Hugo Hörnquist committed
	Message
	Results map[string][]Puppetclass
Hugo Hörnquist's avatar
Hugo Hörnquist committed

func (m PuppetclassMessage) GetMessage() Message {
	return m.Message
}