goforeman

package module
v0.0.0-...-4842a01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2018 License: MIT Imports: 11 Imported by: 0

README

goforeman

Go bindings for Foreman REST API

Heavily inspired by DigitalOcean GoDo : https://github.com/digitalocean/godo

!! Work In Progress !!

Usage

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/amine7536/goforeman"
)

type AuthTransport struct {
	*http.Transport
	Username string
	Password string
}

func (t AuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
	r.SetBasicAuth(t.Username, t.Password)
	return t.Transport.RoundTrip(r)
}

func main() {

	foremanURL := os.Getenv("FOREMAN_URL")
	foremanUser := os.Getenv("FOREMAN_USER")
	foremanPassword := os.Getenv("FOREMAN_PASSWORD")

	client := http.Client{
		Transport: AuthTransport{
			&http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
			foremanUser,
			foremanPassword,
		},
	}

	foreman := goforeman.New(&client, foremanURL)

	ctx := context.TODO()

	hosts, _, err := foreman.Hosts.List(ctx)
	if err != nil {
		log.Fatalln(err.Error())
	}

	for _, h := range hosts {
		v, _, _ := foreman.Hosts.Get(ctx, h.Name)
		if err != nil {
			log.Fatalln(err.Error())
		}
		fmt.Println(v)
	}

	d, _, err := foreman.Dashboard.Get(ctx)
	if err != nil {
		log.Fatalf("Error: %s", err.Error())
	}
	fmt.Println(d)

	facts, _, err := foreman.Facts.Get(ctx, "foremanserver.lab.local.dev")
	if err != nil {
		log.Fatalf("Error: %s", err.Error())
	}
	fmt.Println(facts)

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

func Stringify

func Stringify(message interface{}) string

Stringify attempts to create a string representation

Types

type Client

type Client struct {
	BaseURL *url.URL

	Hosts     HostsService
	Dashboard DashboardService
	Facts     FactsService
	// contains filtered or unexported fields
}

func New

func New(httpClient *http.Client, apiUrl string) *Client

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response will be written to v, without attempting to decode it.

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}) (*http.Request, error)

type Dashboard

type Dashboard struct {
	TotalHosts            int `json:"total_hosts"`
	BadHosts              int `json:"bad_hosts"`
	BadHostsEnabled       int `json:"bad_hosts_enabled"`
	ActiveHosts           int `json:"active_hosts"`
	ActiveHostsOk         int `json:"active_hosts_ok"`
	ActiveHostsOkEnabled  int `json:"active_hosts_ok_enabled"`
	OkHosts               int `json:"ok_hosts"`
	OkHostsEnabled        int `json:"ok_hosts_enabled"`
	OutOfSyncHosts        int `json:"out_of_sync_hosts"`
	OutOfSyncHostsEnabled int `json:"out_of_sync_hosts_enabled"`
	DisabledHosts         int `json:"disabled_hosts"`
	PendingHosts          int `json:"pending_hosts"`
	PendingHostsEnabled   int `json:"pending_hosts_enabled"`
	GoodHosts             int `json:"good_hosts"`
	GoodHostsEnabled      int `json:"good_hosts_enabled"`
	Percentage            int `json:"percentage"`
	ReportsMissing        int `json:"reports_missing"`
	Glossary              struct {
		TotalHosts            string `json:"total_hosts"`
		BadHosts              string `json:"bad_hosts"`
		BadHostsEnabled       string `json:"bad_hosts_enabled"`
		ActiveHosts           string `json:"active_hosts"`
		ActiveHostsOk         string `json:"active_hosts_ok"`
		ActiveHostsOkEnabled  string `json:"active_hosts_ok_enabled"`
		OkHosts               string `json:"ok_hosts"`
		OkHostsEnabled        string `json:"ok_hosts_enabled"`
		OutOfSyncHosts        string `json:"out_of_sync_hosts"`
		OutOfSyncHostsEnabled string `json:"out_of_sync_hosts_enabled"`
		DisabledHosts         string `json:"disabled_hosts"`
		PendingHosts          string `json:"pending_hosts"`
		PendingHostsEnabled   string `json:"pending_hosts_enabled"`
		GoodHosts             string `json:"good_hosts"`
		GoodHostsEnabled      string `json:"good_hosts_enabled"`
		Percentage            string `json:"percentage"`
		ReportsMissing        string `json:"reports_missing"`
	} `json:"glossary"`
}

func (Dashboard) String

func (d Dashboard) String() string

type DashboardService

type DashboardService interface {
	Get(context.Context) (*Dashboard, *Response, error)
}

type DashboardServiceOp

type DashboardServiceOp struct {
	// contains filtered or unexported fields
}

func (*DashboardServiceOp) Get

type ErrorResponse

type ErrorResponse struct {
	// HTTP response that caused this error
	Response *http.Response

	// Error message
	Message string `json:"message"`
}

An ErrorResponse reports the error caused by an API request

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Facts

type Facts map[string]map[string]interface{}

type FactsService

type FactsService interface {
	Get(context.Context, string, *ListOptions) (Facts, *Response, error)
}

type FactsServiceOp

type FactsServiceOp struct {
	// contains filtered or unexported fields
}

func (*FactsServiceOp) Get

func (s *FactsServiceOp) Get(ctx context.Context, hostname string, opt *ListOptions) (Facts, *Response, error)

type Host

type Host struct {
	IP                  string      `json:"ip"`
	IP6                 interface{} `json:"ip6"`
	EnvironmentID       interface{} `json:"environment_id"`
	EnvironmentName     interface{} `json:"environment_name"`
	LastReport          interface{} `json:"last_report"`
	Mac                 interface{} `json:"mac"`
	RealmID             interface{} `json:"realm_id"`
	RealmName           interface{} `json:"realm_name"`
	SpMac               interface{} `json:"sp_mac"`
	SpIP                interface{} `json:"sp_ip"`
	SpName              interface{} `json:"sp_name"`
	DomainID            interface{} `json:"domain_id"`
	DomainName          interface{} `json:"domain_name"`
	ArchitectureID      interface{} `json:"architecture_id"`
	ArchitectureName    interface{} `json:"architecture_name"`
	OperatingsystemID   interface{} `json:"operatingsystem_id"`
	OperatingsystemName interface{} `json:"operatingsystem_name"`
	SubnetID            interface{} `json:"subnet_id"`
	SubnetName          interface{} `json:"subnet_name"`
	Subnet6ID           interface{} `json:"subnet6_id"`
	Subnet6Name         interface{} `json:"subnet6_name"`
	SpSubnetID          interface{} `json:"sp_subnet_id"`
	PtableID            interface{} `json:"ptable_id"`
	PtableName          interface{} `json:"ptable_name"`
	MediumID            interface{} `json:"medium_id"`
	MediumName          interface{} `json:"medium_name"`
	Build               bool        `json:"build"`
	Comment             interface{} `json:"comment"`
	Disk                interface{} `json:"disk"`
	InstalledAt         interface{} `json:"installed_at"`
	ModelID             interface{} `json:"model_id"`
	HostgroupID         interface{} `json:"hostgroup_id"`
	OwnerID             interface{} `json:"owner_id"`
	OwnerType           interface{} `json:"owner_type"`
	Enabled             bool        `json:"enabled"`
	Managed             bool        `json:"managed"`
	UseImage            interface{} `json:"use_image"`
	ImageFile           string      `json:"image_file"`
	UUID                interface{} `json:"uuid"`
	ComputeResourceID   interface{} `json:"compute_resource_id"`
	ComputeResourceName interface{} `json:"compute_resource_name"`
	ComputeProfileID    interface{} `json:"compute_profile_id"`
	ComputeProfileName  interface{} `json:"compute_profile_name"`
	Capabilities        []string    `json:"capabilities"`
	ProvisionMethod     string      `json:"provision_method"`
	Certname            string      `json:"certname"`
	ImageID             interface{} `json:"image_id"`
	ImageName           interface{} `json:"image_name"`
	CreatedAt           string      `json:"created_at"`
	UpdatedAt           string      `json:"updated_at"`
	LastCompile         interface{} `json:"last_compile"`
	GlobalStatus        int         `json:"global_status"`
	GlobalStatusLabel   string      `json:"global_status_label"`
	OrganizationID      interface{} `json:"organization_id"`
	OrganizationName    interface{} `json:"organization_name"`
	LocationID          interface{} `json:"location_id"`
	LocationName        interface{} `json:"location_name"`
	PuppetStatus        int         `json:"puppet_status"`
	ModelName           interface{} `json:"model_name"`
	Name                string      `json:"name"`
	ID                  int         `json:"id"`
	PuppetProxyID       interface{} `json:"puppet_proxy_id"`
	PuppetProxyName     interface{} `json:"puppet_proxy_name"`
	PuppetCaProxyID     interface{} `json:"puppet_ca_proxy_id"`
	PuppetCaProxyName   interface{} `json:"puppet_ca_proxy_name"`
	PuppetProxy         interface{} `json:"puppet_proxy"`
	PuppetCaProxy       interface{} `json:"puppet_ca_proxy"`
	HostgroupName       interface{} `json:"hostgroup_name"`
	HostgroupTitle      interface{} `json:"hostgroup_title"`
}

func (Host) String

func (h Host) String() string

Convert Droplet to a string

type HostsService

type HostsService interface {
	List(context.Context, *ListOptions) ([]Host, *Response, error)
	Get(context.Context, string) (*Host, *Response, error)
}

type HostsServiceOp

type HostsServiceOp struct {
	// contains filtered or unexported fields
}

func (*HostsServiceOp) Get

func (s *HostsServiceOp) Get(ctx context.Context, hostname string) (*Host, *Response, error)

func (*HostsServiceOp) List

func (s *HostsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Host, *Response, error)

type ListOptions

type ListOptions struct {
	Page          int    `json:"page,omitempty"`
	PerPage       int    `json:"per_page,omitempty"`
	EnvironmentID string `json:"environment_id,omitempty"`
}

ListOptions

type RequestCompletionCallback

type RequestCompletionCallback func(*http.Request, *http.Response)

RequestCompletionCallback defines the type of the request callback function

type Response

type Response struct {
	*http.Response
	Meta *ResponseMeta
}

type ResponseMeta

type ResponseMeta struct {
	Total    int `json:"total,omiempty"`
	SubTotal int `json:"subtotal,omiempty"`
	Page     int `json:"page,omiempty"`
	PerPage  int `json:"per_page,omiempty"`
}

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp represents a time that can be unmarshalled from a JSON string formatted as either an RFC3339 or Unix timestamp. All exported methods of time.Time can be called on Timestamp.

func (Timestamp) Equal

func (t Timestamp) Equal(u Timestamp) bool

Equal reports whether t and u are equal based on time.Equal

func (Timestamp) String

func (t Timestamp) String() string

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Time is expected in RFC3339 or Unix format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL