grafana

package
v0.0.0-...-bdd216b Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteDashboards

func DeleteDashboards(filenames []string, contents map[string][]byte, client *Client)

DeleteDashboards takes a slice of files' names and a map mapping a file's name to its content, and iterates over the first slice. For each file name, extract a dashboard's slug from the content, in the map, that matches the name, and will use it to send a deletion request to the Grafana API. Logs any errors encountered during an iteration, but doesn't return until all deletion requests have been performed.

func FilterIgnored

func FilterIgnored(
	filesToPush *map[string][]byte, cfg *config.Config,
) (err error)

FilterIgnored takes a map mapping files' names to their contents and remove all the files that are supposed to be ignored by the dashboard manager. An ignored file is either named "versions.json" or describing a dashboard which slug starts with a given prefix. Returns an error if the slug couldn't be tested against the prefix.

func FolderIDFromFolderUID

func FolderIDFromFolderUID(versionsFile VersionFile, folderUID string) (folderID int)

func GetFilesContents

func GetFilesContents(
	filenames []string, contents *map[string][]byte, subdir string, cfg *config.Config,
) (err error)

getFilesContents takes a slice of files' names and a map mapping a file's name to its content and appends to it the current content of all of the files for which the name appears in the slice. Returns an error if there was an issue reading a file.

func LoadFilesFromDirectory

func LoadFilesFromDirectory(cfg *config.Config, dir string, subdir string) (filenames []string, contents map[string][]byte, err error)

func Push

func Push(cfg *config.Config, fileVersionFile VersionFile, grafanaVersionFile VersionFile, files []string, contents map[string][]byte, client *Client) (err error)

func PushFiles

func PushFiles(filenames []string, contents map[string][]byte, versionsFile VersionFile, grafanaVersionFile VersionFile, client *Client)

PushFiles takes a slice of files' names and a map mapping a file's name to its content, and iterates over the first slice. For each file name, it will push to Grafana the content from the map that matches the name, as a creation or an update of an existing dashboard. Logs any errors encountered during an iteration, but doesn't return until all creation and/or update requests have been performed.

func PushFolders

func PushFolders(filenames []string, contents map[string][]byte, versionsFile VersionFile, grafanaVersionFile VersionFile, client *Client) (err error)

PushFolders takes a slice of files' names and a map mapping a file's name to its content, and iterates over the first slice. For each file name, it will push to Grafana the content from the map that matches the name, as a creation or an update of an existing dashboard. Logs any errors encountered during an iteration, but doesn't return until all creation and/or update requests have been performed.

Types

type Client

type Client struct {
	BaseURL    string
	APIKey     string
	Username   string
	Password   string
	SkipVerify bool
	// contains filtered or unexported fields
}

Client implements a Grafana API client, and contains the instance's base URL and API key, along with an HTTP client used to request the API. use either APIKey or Username/Password

func NewClient

func NewClient(baseURL string, apiKey string, username string, password string, SkipVerify bool) (c *Client)

NewClient returns a new Grafana API client from a given base URL and API key.

func (*Client) CreateFolders

func (c *Client) CreateFolders(folders []string, contents map[string][]byte) (err error)

func (*Client) CreateOrUpdateDashboard

func (c *Client) CreateOrUpdateDashboard(contentJSON []byte, folderID int) (err error)

CreateOrUpdateDashboard takes a given JSON content (as []byte) and create the dashboard if it doesn't exist on the Grafana instance, else updates the existing one. The Grafana API decides whether to create or update based on the "id" attribute in the dashboard's JSON: If it's unkown or null, it's a creation, else it's an update. Returns an error if there was an issue generating the request body, performing the request or decoding the response's body.

func (*Client) CreateOrUpdateFolder

func (c *Client) CreateOrUpdateFolder(title string, uid string) (err error)

CreateOrUpdateFolder takes a given JSON content (as []byte) and create the dashboard if it doesn't exist on the Grafana instance, else updates the existing one. The Grafana API decides whether to create or update based on the "id" attribute in the dashboard's JSON: If it's unkown or null, it's a creation, else it's an update. Returns an error if there was an issue generating the request body, performing the request or decoding the response's body.

func (*Client) DeleteDashboard

func (c *Client) DeleteDashboard(slug string) (err error)

DeleteDashboard deletes the dashboard identified by a given slug on the Grafana API. Returns an error if the process failed.

func (*Client) DeleteFolder

func (c *Client) DeleteFolder(uid string) (err error)

DeleteFolder deletes the dashboard identified by a given uid on the Grafana API. NB this also deletes all graphs stored inside! Returns an error if the process failed.

func (*Client) GetDashboard

func (c *Client) GetDashboard(URI string) (db *Dashboard, err error)

GetDashboard requests the Grafana API for a dashboard identified by a given URI (using the same format as GetDashboardsURIs). Returns the dashboard as an instance of the Dashboard structure. Returns an error if there was an issue requesting the dashboard or parsing the response body.

func (*Client) GetDashboardsURIs

func (c *Client) GetDashboardsURIs() (URIs []string, dashboardMetaBySlug map[string]DbSearchResponse, FoldersMetaByUID map[string]DbSearchResponse, Folders []DbSearchResponse, err error)

GetDashboardsURIs requests the Grafana API for the list of all dashboards, then returns the dashboards' URIs. An URI will look like "uid/[UID]". Returns an error if there was an issue requesting the URIs or parsing the response body.

type Dashboard

type Dashboard struct {
	RawJSON []byte
	Name    string
	Slug    string
	Version int
}

Dashboard represents a Grafana dashboard, with its JSON definition, slug and current version.

func (*Dashboard) UnmarshalJSON

func (d *Dashboard) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON tells the JSON parser how to unmarshal JSON data into an instance of the Dashboard structure. Returns an error if there was an issue unmarshalling the JSON.

type DashboardVersion

type DashboardVersion struct {
	Meta DbSearchResponse
}

type DbSearchResponse

type DbSearchResponse struct {
	ID        int      `json:"id"`
	Title     string   `json:"title"`
	URI       string   `json:"uri"`
	Type      string   `json:"type"`
	Tags      []string `json:"tags"`
	Starred   bool     `json:"isStarred"`
	UID       string   `json:"uid"`
	FolderUID string   `json:"folderUid,omitEmpty"`
	FolderID  int      `json:"folderId,omitEmpty"`
}

DbSearchResponse represents an element of the response to a dashboard search query

type Folder

type Folder struct {
	Title     string   `json:"title"`
	UID       string   `json:"uid"`
	URI       string   `json:"uri"`
	Tags      []string `json:"tags"`
	Starred   bool     `json:"isStarred"`
	FolderUID string   `json:"folderUid,omitEmpty"`
}

type VersionFile

type VersionFile struct {
	DashboardMetaByTitle map[string]DbSearchResponse `json:"dashboardMetaByTitle"`
	DashboardMetaBySlug  map[string]DbSearchResponse `json:"dashboardMetaBySlug"`
	DashboardBySlug      map[string]*Dashboard       `json:"-"`

	FoldersMetaByUID       map[string]DbSearchResponse `json:"foldersMetaByUID"`
	DashboardVersionBySlug map[string]int              `json:"dashboardVersionBySlug"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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