Skip to content

Commit

Permalink
Initial implementation of screenboards
Browse files Browse the repository at this point in the history
This commit is huge since I found an issue with dependencies, and
decided to migrate go-api to github based tracking, and introduce `dep`.

This is also the first commit with tests! :)

Related: #10
  • Loading branch information
amnk committed Jan 3, 2019
1 parent 6c144f1 commit a460563
Show file tree
Hide file tree
Showing 414 changed files with 237,399 additions and 18 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
# Ignore all
*

# Unignore all with extensions and templates directory
!*.*
!tmpl/
bin/
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
language: go

env:
- DEP_VERSION=0.5.0

before_install:
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
- chmod +x $GOPATH/bin/dep

go:
- 1.9
- 1.11
- master

matrix:
allow_failures:
- go: master
fast_finish: true

notifications:
Expand Down
80 changes: 80 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.2.0"

[[constraint]]
name = "github.com/spf13/pflag"
version = "1.0.3"

[[constraint]]
name = "github.com/zorkian/go-datadog-api"
version = "2.18.0"

[[constraint]]
name = "gopkg.in/zorkian/go-datadog-api.v2"
version = "2.18.0"

[prune]
go-tests = true
unused-packages = true
2 changes: 1 addition & 1 deletion dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package main

import (
"gopkg.in/zorkian/go-datadog-api.v2"
"github.com/zorkian/go-datadog-api"
)

type Dashboard struct {
Expand Down
18 changes: 13 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"text/template"

flag "github.com/spf13/pflag"
"gopkg.in/zorkian/go-datadog-api.v2"
"github.com/zorkian/go-datadog-api"
)

type LocalConfig struct {
Expand All @@ -22,7 +22,7 @@ type LocalConfig struct {
}

var config = LocalConfig{
components: []DatadogElement{Dashboard{}, Monitor{}},
components: []DatadogElement{Dashboard{}, Monitor{}, ScreenBoard{}},
}

type DatadogElement interface {
Expand All @@ -37,14 +37,18 @@ type Item struct {
d DatadogElement
}

func (i *Item) renderElement(config LocalConfig) {
log.Debugf("Entering renderElement %v", i.id)
func (i *Item) getElement(config LocalConfig) (interface{}, error) {
item, err := i.d.getElement(config.client, i.id)
if err != nil {
log.Debugf("Error while getting element %v", i.id)
log.Fatal(err)
}
return item, err

}

func (i *Item) renderElement(item interface{}, config LocalConfig) {
log.Debugf("Entering renderElement %v", i.id)
b, _ := Asset(i.d.getAsset())
t, _ := template.New("").Funcs(template.FuncMap{
"escapeCharacters": escapeCharacters,
Expand Down Expand Up @@ -160,7 +164,11 @@ func main() {
}
for _, element := range config.items {
log.Debugf("Exporting element %v", element.id)
element.renderElement(config)
fullElem, err := element.getElement(config)
if err != nil {
log.Fatal(err)
}
element.renderElement(fullElem, config)
}
os.Exit(0)

Expand Down
2 changes: 1 addition & 1 deletion monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package main

import (
"gopkg.in/zorkian/go-datadog-api.v2"
"github.com/zorkian/go-datadog-api"
)

type Monitor struct {
Expand Down
39 changes: 39 additions & 0 deletions screenboards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:generate go-bindata -o tpl.go tmpl

package main

import (
"github.com/zorkian/go-datadog-api"
)

type ScreenBoard struct {
}

func (s ScreenBoard) getElement(client datadog.Client, id int) (interface{}, error) {
elem, err := client.GetScreenboard(*datadog.Int(id))
return elem, err
}

func (s ScreenBoard) getAsset() string {
return "tmpl/screenboard.tmpl"
}

func (s ScreenBoard) getName() string {
return "screenboards"
}

func (s ScreenBoard) String() string {
return s.getName()
}

func (s ScreenBoard) getAllElements(client datadog.Client) ([]Item, error) {
var ids []Item
dashboards, err := client.GetScreenboards()
if err != nil {
return ids, err
}
for _, elem := range dashboards {
ids = append(ids, Item{id: *elem.Id, d: ScreenBoard{}})
}
return ids, nil
}
104 changes: 104 additions & 0 deletions screenboards_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//go:generate go-bindata -o tpl.go tmpl

package main

import (
"github.com/zorkian/go-datadog-api"
)

var exampleScreenboard = datadog.Screenboard{
Id: datadog.Int(1),
Title: datadog.String("Test"),
Shared: datadog.Bool(false),
Widgets: []datadog.Widget{
{
Type: datadog.String("query_value"),
X: datadog.Int(1),
Y: datadog.Int(1),
Width: datadog.Int(5),
Height: datadog.Int(5),
Title: datadog.Bool(true),
TitleText: datadog.String("Test title"),
TitleSize: datadog.Int(16),
TitleAlign: datadog.String("right"),
Legend: datadog.Bool(true),
LegendSize: datadog.String("16"),
Time: &datadog.Time{
LiveSpan: datadog.String("1d"),
},
TileDef: &datadog.TileDef{
Viz: datadog.String("query_value"),
Requests: []datadog.TileDefRequest{{
Query: datadog.String("avg:system.cpu.user{*}"),
Type: datadog.String("line"),
Style: &datadog.TileDefRequestStyle{
Palette: datadog.String("purple"),
Type: datadog.String("dashed"),
Width: datadog.String("thin"),
},
ConditionalFormats: []datadog.ConditionalFormat{
{
Comparator: datadog.String(">="),
Value: datadog.String("1"),
Palette: datadog.String("white_on_red"),
}},
Aggregator: datadog.String("max"),
}},
CustomUnit: datadog.String("%"),
Autoscale: datadog.Bool(false),
TextAlign: datadog.String("right"),
},
Logset: datadog.String("test"),
},
},
}

func Example() {
config := LocalConfig{files: false}
item := Item{id: 1, d: ScreenBoard{}}
item.renderElement(exampleScreenboard, config)

// Unordered output:
// resource "datadog_screenboard" "1" {
// title = "Test"
// shared = false
// widget {
// type = "query_value"
// x = "1"
// y = "1"
// title = "true"
// title_text = "Test title"
// title_size = "16"
// height = "5"
// width = "5"
// title_align = "right"
// time {
// live_span = "1d"
// }
// tile_def {
// viz = "query_value"
// custom_unit = "%"
// autoscale = "false"
// text_align = "right"
// request {
// q = "avg:system.cpu.user{*}"
// type = "line"
// aggregator = "max"
// conditional_format {
// palette = "white_on_red"
// comparator = ">="
// value = "1"
// }
// style {
// palette = "purple"
// type = "dashed"
// width = "thin"
// } //style
// } //request
// } //tile_def
// legend = "true"
// legend_size = "16"
// logset = "test"
// } //widget
// }
}
Loading

0 comments on commit a460563

Please sign in to comment.