Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make grafan-kiosk compatible with grafana 9 UID vs ID #90

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ go 1.16
require (
github.com/chromedp/cdproto v0.0.0-20210625233425-810000e4a4fc
github.com/chromedp/chromedp v0.7.3
github.com/grafana/grafana-api-golang-client v0.12.0
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/smartystreets/goconvey v1.6.4
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
)

replace github.com/grafana/grafana-api-golang-client v0.12.0 => github.com/nissessenap/grafana-api-golang-client v0.0.0-20221012135911-271ce27883ab
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chromedp/chromedp v0.7.3 h1:FvgJICfjvXtDX+miuMUY0NHuY8zQvjS/TcEQEG6Ld
github.com/chromedp/chromedp v0.7.3/go.mod h1:9gC521Yzgrk078Ulv6KIgG7hJ2x9aWrxMBBobTFk30A=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b h1:/vQ+oYKu+JoyaMPDsv5FzwuL2wwWBgBbtj/YLCi4LuA=
github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b/go.mod h1:Xo4aNUOrJnVruqWQJBtW6+bTBDTniY8yZum5rF3b5jw=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
Expand All @@ -15,6 +17,8 @@ github.com/gobwas/ws v1.1.0-rc.5 h1:QOAag7FoBaBYYHRqzqkhhd8fq5RTubvI4v3Ft/gDVVQ=
github.com/gobwas/ws v1.1.0-rc.5/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand All @@ -25,6 +29,8 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/nissessenap/grafana-api-golang-client v0.0.0-20221012135911-271ce27883ab h1:ZsoTyXT0iLcufJemdBUF/x7dnTbWvA8OXml8+kNz9zQ=
github.com/nissessenap/grafana-api-golang-client v0.0.0-20221012135911-271ce27883ab/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down
43 changes: 42 additions & 1 deletion pkg/kiosk/local_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package kiosk

import (
"context"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"time"

Expand Down Expand Up @@ -50,7 +52,14 @@ func GrafanaKioskLocal(cfg *Config) {
panic(err)
}

var generatedURL = GenerateURL(cfg.Target.URL, cfg.General.Mode, cfg.General.AutoFit, cfg.Target.IsPlayList)
anURL := cfg.Target.URL
if cfg.Target.IsPlayList {
anURL, err = gatherPlayListUID(anURL, cfg.Target.Username, cfg.Target.Password, cfg.Target.IgnoreCertificateErrors)
if err != nil {
panic(err)
}
}
var generatedURL = GenerateURL(anURL, cfg.General.Mode, cfg.General.AutoFit, cfg.Target.IsPlayList)
log.Println("Navigating to ", generatedURL)
/*
Launch chrome and login with local user account
Expand All @@ -71,3 +80,35 @@ func GrafanaKioskLocal(cfg *Config) {
panic(err)
}
}

func gatherPlayListUID(anURL, username, password string, ignoreCertErrors bool) (string, error) {
client, err := NewGrafanaClient(anURL, username, password, ignoreCertErrors)
if err != nil {
log.Println("unable to create grafana Client")
return "", err
}
uid, err := GetPlayListUID(anURL, client)
if err != nil {
log.Println("")
return "", fmt.Errorf("unable to get the uid from the id defined %e", err)
}

log.Printf("Playlist uid: %s", uid)

// replace the id with uid
urlOfAnURL, err := url.Parse(anURL)
if err != nil {
return "", err
}
uidURL := UrlChangeIDtoUID(urlOfAnURL, uid)

/*
err = nil
anURL, err = ChangeIDtoUID(anURL, uid)
if err != nil {
return "", err
}
*/
log.Println("URL using uid:", uidURL.String())
return uidURL.String(), nil
}
116 changes: 116 additions & 0 deletions pkg/kiosk/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package kiosk

import (
"crypto/tls"
"log"
"net/http"
"net/url"
"path"
"strconv"
"strings"

grapi "github.com/grafana/grafana-api-golang-client"
)

// GenerateURL constructs URL with appropriate parameters for kiosk mode
Expand Down Expand Up @@ -33,3 +40,112 @@ func GenerateURL(anURL string, kioskMode string, autoFit bool, isPlayList bool)
}
return u.String()
}

func NewGrafanaClient(anURL, username, password string, ignoreCertErrors bool) (*grapi.Client, error) {
userinfo := url.UserPassword(username, password)
clientConfig := grapi.Config{
APIKey: "",
BasicAuth: userinfo,
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: ignoreCertErrors,
},
},
},
OrgID: 0,
NumRetries: 0,
}

u, err := url.Parse(anURL)
if err != nil {
return nil, err
}
u.Path = ""
u.RawQuery = ""
u.Fragment = ""

grafanaClient, err := grapi.New(u.String(), clientConfig)
if err != nil {
return nil, err
}

return grafanaClient, nil
}

// getPlayListUID, get the UID of a playlist from an id
func GetPlayListUID(anURL string, client *grapi.Client) (string, error) {
idURL, err := url.Parse(anURL)
if err != nil {
return "", err
}
id := path.Base(idURL.Path)

log.Println("Playlist ID:", id)
playLists, err := client.Playlists(url.Values{})
if err != nil {
return "", err
}

intID, err := strconv.Atoi(id)
if err != nil {
return "", err
}

playList := &grapi.Playlist{}
for _, avaliablePlayList := range *playLists {
if avaliablePlayList.ID == intID {
playList, err = client.Playlist(avaliablePlayList.UID)
if err != nil {
return "", err
}
break
}
}
// TODO what to return if it's not a hit

return playList.UID, nil
}

func ChangeIDtoUID(anURL, uid string) (string, error) {
urlA, err := url.Parse(anURL)
if err != nil {
return "", err
}
urlPATH := urlA.Path

// Make the url path in to a list
urlPATH = strings.TrimSpace(urlPATH)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
urlPATH = strings.TrimPrefix(urlPATH, "/")

//This cuts off the trailing forward slash.
if strings.HasSuffix(urlPATH, "/") {
cut_off_last_char_len := len(urlPATH) - 1
urlPATH = urlPATH[:cut_off_last_char_len]
}
//We need to isolate the individual components of the path.
splitURLpath := strings.Split(urlPATH, "/")
// delete the last item in the list
splitURLpath = splitURLpath[:len(splitURLpath)-1]
splitURLpath = append(splitURLpath, uid)

// make in to string again
fixedURL := strings.Join(splitURLpath, "/")
// make a full URL again
fixedURL = urlA.Scheme + "://" + urlA.Host + "/" + fixedURL
return fixedURL, nil
}

func UrlChangeIDtoUID(anURL *url.URL, uid string) *url.URL {
splitURLpath := strings.Split(anURL.Path, "/")
// delete the last item in the list
splitURLpath = splitURLpath[:len(splitURLpath)-1]
splitURLpath = append(splitURLpath, uid)
// make in to string again
fixedURL := strings.Join(splitURLpath, "/")
anURL.Path = fixedURL

return anURL
}
47 changes: 47 additions & 0 deletions pkg/kiosk/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kiosk

import (
"net/url"
"path"
"testing"
)

func TestChangeIDtoUID(t *testing.T) {
anyURL := "https://mygrafana.com/playlists/play/1234"
uid := "myRandomUID"
idURL, err := ChangeIDtoUID(anyURL, uid)
if err != nil {
t.Fatalf("Unable to parse url %v", err)
}
parsedIdURL, err := url.Parse(idURL)
if err != nil {
t.Fatalf("Unable to parse url %v", err)
}

baseUID := path.Base(parsedIdURL.Path)
if baseUID != "myRandomUID" {
t.Fatalf("baseUID should be myRandomUID but returned %v", baseUID)
}
if idURL != "https://mygrafana.com/playlists/play/myRandomUID" {
t.Fatalf("idURL should be https://mygrafana.com/playlists/play/myRandomUID but returned %v", idURL)
}
}

func TestURLChangeIDtoUID(t *testing.T) {
anyURL := "https://mygrafana.com/playlists/play/1234"
uid := "myRandomUID"

urlA, err := url.Parse(anyURL)
if err != nil {
t.Fatalf("Unable to parse URL")
}
uidURL := UrlChangeIDtoUID(urlA, uid)

baseUID := path.Base(uidURL.Path)
if baseUID != "myRandomUID" {
t.Fatalf("baseUID should be myRandomUID but returned %v", baseUID)
}
if uidURL.String() != "https://mygrafana.com/playlists/play/myRandomUID" {
t.Fatalf("idURL should be https://mygrafana.com/playlists/play/myRandomUID but returned %v", uidURL.String())
}
}