Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Use correct health state in the environments
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Apr 26, 2021
1 parent 2ba1aa2 commit a45bab5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 18 deletions.
59 changes: 59 additions & 0 deletions web/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,86 @@ const KVEnvironmentsPath string = "trento/environments"
const envIndex int = 2
const landIndex int = 4

type EnvironmentHealth struct {
Health string
HealthMap map[string]string
}

func (e *EnvironmentHealth) updateHealth(n string, h string) EnvironmentHealth {
e.HealthMap[n] = h

if h == "critical" {
e.Health = h
} else if h == "warning" && e.Health != "critical" {
e.Health = h
}

return *e
}

type SAPSystem struct {
Name string
Hosts HostList
}

type SAPSystemList map[string]*SAPSystem

func (s *SAPSystem) Health() EnvironmentHealth {
var health = EnvironmentHealth{
Health: "passing",
HealthMap: make(map[string]string),
}

for _, host := range s.Hosts {
h := host.Health()
health = health.updateHealth(host.Name(), h)
}

return health
}

type Landscape struct {
Name string
SAPSystems SAPSystemList
}

type LandscapeList map[string]*Landscape

func (l *Landscape) Health() EnvironmentHealth {
var health = EnvironmentHealth{
Health: "passing",
HealthMap: make(map[string]string),
}

for _, system := range l.SAPSystems {
h := system.Health().Health
health = health.updateHealth(system.Name, h)
}

return health
}

type Environment struct {
Name string
Landscapes LandscapeList
}

type EnvironmentList map[string]*Environment

func (l *Environment) Health() EnvironmentHealth {
var health = EnvironmentHealth{
Health: "passing",
HealthMap: make(map[string]string),
}

for _, land := range l.Landscapes {
h := land.Health().Health
health = health.updateHealth(land.Name, h)
}

return health
}

func NewEnvironmentsListHandler(client consul.Client) gin.HandlerFunc {
return func(c *gin.Context) {
environments, err := loadEnvironments(client)
Expand Down
74 changes: 62 additions & 12 deletions web/environments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,68 @@ import (
)

func setupTest() (*mocks.Client, *mocks.Catalog) {
nodes := []*consulApi.Node{
nodes1 := []*consulApi.Node{
{
Node: "foo",
Datacenter: "dc1",
Node: "node1",
Datacenter: "dc",
Address: "192.168.1.1",
Meta: map[string]string{
"trento-sap-environments": "land1",
},
},
{
Node: "bar",
Node: "node2",
Datacenter: "dc",
Address: "192.168.1.2",
Meta: map[string]string{
"trento-sap-environments": "land1",
},
},
}

nodes2 := []*consulApi.Node{
{
Node: "node3",
Datacenter: "dc1",
Address: "192.168.1.2",
Meta: map[string]string{
"trento-sap-environments": "land2",
},
},
{
Node: "node4",
Datacenter: "dc",
Address: "192.168.1.3",
Meta: map[string]string{
"trento-sap-environments": "land2",
},
},
}

node1HealthChecks := consulApi.HealthChecks{
&consulApi.HealthCheck{
Status: consulApi.HealthPassing,
},
}

node2HealthChecks := consulApi.HealthChecks{
&consulApi.HealthCheck{
Status: consulApi.HealthPassing,
},
}

node3HealthChecks := consulApi.HealthChecks{
&consulApi.HealthCheck{
Status: consulApi.HealthPassing,
},
}

node4HealthChecks := consulApi.HealthChecks{
&consulApi.HealthCheck{
Status: consulApi.HealthCritical,
},
}

filters := []string{
"trento/environments/",
"trento/environments/env1/",
Expand All @@ -52,24 +95,31 @@ func setupTest() (*mocks.Client, *mocks.Catalog) {

consul := new(mocks.Client)
catalog := new(mocks.Catalog)
health := new(mocks.Health)
kv := new(mocks.KV)

consul.On("Catalog").Return(catalog)
consul.On("Health").Return(health)
consul.On("KV").Return(kv)

kv.On("Keys", "trento/environments", "", (*consulApi.QueryOptions)(nil)).Return(filters, nil, nil)

filterSys1 := &consulApi.QueryOptions{
Filter: "(Meta[\"trento-sap-environment\"] == \"env1\") and (Meta[\"trento-sap-landscape\"] == \"land1\") and (Meta[\"trento-sap-system\"] == \"sys1\")"}
catalog.On("Nodes", (filterSys1)).Return(nodes, nil, nil)
catalog.On("Nodes", (filterSys1)).Return(nodes1, nil, nil)

filterSys2 := &consulApi.QueryOptions{
Filter: "(Meta[\"trento-sap-environment\"] == \"env1\") and (Meta[\"trento-sap-landscape\"] == \"land2\") and (Meta[\"trento-sap-system\"] == \"sys2\")"}
catalog.On("Nodes", (filterSys2)).Return(nodes, nil, nil)
catalog.On("Nodes", (filterSys2)).Return(nodes1, nil, nil)

filterSys3 := &consulApi.QueryOptions{
Filter: "(Meta[\"trento-sap-environment\"] == \"env2\") and (Meta[\"trento-sap-landscape\"] == \"land3\") and (Meta[\"trento-sap-system\"] == \"sys3\")"}
catalog.On("Nodes", (filterSys3)).Return(nodes, nil, nil)
catalog.On("Nodes", (filterSys3)).Return(nodes2, nil, nil)

health.On("Node", "node1", (*consulApi.QueryOptions)(nil)).Return(node1HealthChecks, nil, nil)
health.On("Node", "node2", (*consulApi.QueryOptions)(nil)).Return(node2HealthChecks, nil, nil)
health.On("Node", "node3", (*consulApi.QueryOptions)(nil)).Return(node3HealthChecks, nil, nil)
health.On("Node", "node4", (*consulApi.QueryOptions)(nil)).Return(node4HealthChecks, nil, nil)

return consul, catalog
}
Expand Down Expand Up @@ -110,8 +160,8 @@ func TestEnvironmentsListHandler(t *testing.T) {

assert.Equal(t, 200, resp.Code)
assert.Contains(t, minified, "Environments")
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1'\".*<td>env1</td><td>2</td><td>2</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env2'\".*<td>env2</td><td>1</td><td>1</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1'\".*<td>env1</td><td>2</td><td>2</td><td>.*passing.*</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env2'\".*<td>env2</td><td>1</td><td>1</td><td>.*critical.*</td>"), minified)
}

func TestLandscapesListHandler(t *testing.T) {
Expand Down Expand Up @@ -149,8 +199,8 @@ func TestLandscapesListHandler(t *testing.T) {
}

assert.Equal(t, 200, resp.Code)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land1'\".*<td>land1</td><td>1</td><td>2</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land2'\".*<td>land2</td><td>1</td><td>2</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land1'\".*<td>land1</td><td>1</td><td>2</td><td>.*passing.*</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land2'\".*<td>land2</td><td>1</td><td>2</td><td>.*passing.*</td>"), minified)
}

func TestSAPSystemsListHandler(t *testing.T) {
Expand Down Expand Up @@ -188,5 +238,5 @@ func TestSAPSystemsListHandler(t *testing.T) {
}

assert.Equal(t, 200, resp.Code)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land1/sapsystems/sys1'\".*<td>sys1</td>"), minified)
assert.Regexp(t, regexp.MustCompile("<tr.*onclick=\"window.location='/environments/env1/landscapes/land1/sapsystems/sys1'\".*<td>sys1</td><td>.*passing.*</td>"), minified)
}
5 changes: 3 additions & 2 deletions web/templates/environments.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
<td>{{ $SAPSystemNumber }}</td>
<td>{{ $HostsNumber }}</td>
<td>
{{- $Health := "passing" }}
<span class='badge badge-pill badge-{{ if eq $Health "passing" }}primary{{ else if eq $Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health }} (hardcoded)</span>
{{- $Health := .Health }}
{{- /* I`t would be nice to show the summary of the the health as tooltip. How many passing, critical and warning in a nice an visual way */ -}}
<span class='badge badge-pill badge-{{ if eq $Health.Health "passing" }}primary{{ else if eq $Health.Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health.Health }}</span>
</td>
</tr>
{{- end }}
Expand Down
5 changes: 3 additions & 2 deletions web/templates/landscapes.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
<td>{{ $SAPSystemNumber }}</td>
<td>{{ $HostsNumber }}</td>
<td>
{{- $Health := "passing" }}
<span class='badge badge-pill badge-{{ if eq $Health "passing" }}primary{{ else if eq $Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health }} (hardcoded)</span>
{{- $Health := .Health }}
{{- /* It would be nice to show the summary of the the health as tooltip. How many passing, critical and warning in a nice an visual way */ -}}
<span class='badge badge-pill badge-{{ if eq $Health.Health "passing" }}primary{{ else if eq $Health.Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health.Health }}</span>
</td>
</tr>
{{- end }}
Expand Down
5 changes: 3 additions & 2 deletions web/templates/sapsystems.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
<td>{{ .Name }}</td>
<td>{{ len .Hosts }}</td>
<td>
{{- $Health := "passing" }}
<span class='badge badge-pill badge-{{ if eq $Health "passing" }}primary{{ else if eq $Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health }} (hardcoded)</span>
{{- $Health := .Health }}
{{- /* It would be nice to show the summary of the the health as tooltip. How many passing, critical and warning in a nice an visual way */ -}}
<span class='badge badge-pill badge-{{ if eq $Health.Health "passing" }}primary{{ else if eq $Health.Health "warning" }}warning{{ else }}danger{{ end }}'>{{ $Health.Health }}</span>
</td>
</tr>
{{- end }}
Expand Down

0 comments on commit a45bab5

Please sign in to comment.