Skip to content

Commit

Permalink
Display node attributes as flattened JSON paths (#29)
Browse files Browse the repository at this point in the history
* add function to generate json path

* fix S1034 lint error

* update templates
  • Loading branch information
drewhammond authored Oct 16, 2022
1 parent 188b611 commit 5808a08
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 22 deletions.
55 changes: 48 additions & 7 deletions internal/app/ui/templates/environment.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,59 @@ <h2 class="environment-headline">{{.environment.Name}}</h2>
<p class="lead">{{.environment.Description}}</p>
</div>
<div class="">
<a type="button" href="/ui/nodes?q=environment:{{.environment.Name}}" class="btn btn-outline-primary">View Nodes</a>
<a type="button" href="/ui/nodes?q=environment:{{.environment.Name}}" class="btn btn-outline-primary">View
Nodes</a>
</div>
</div>
</div>
{{ if .environment.CookbookVersions }}
<h3>Cookbook Versions</h3>
<ul class="list-unstyled">
{{ range $cookbook, $constraint := .environment.CookbookVersions}}
<li><strong>{{ $cookbook }}</strong> {{$constraint}}</li>
{{end}}
</ul>

<ul class="list-unstyled">
{{ range $cookbook, $constraint := .environment.CookbookVersions}}
<li><strong>{{ $cookbook }}</strong> {{$constraint}}</li>
{{end}}
</ul>
{{ end }}

{{ if or .environment.DefaultAttributes .environment.OverrideAttributes }}
<h4>Attributes</h4>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" data-bs-target="#default-tab-pane">Default</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" data-bs-target="#override-tab-pane">Override</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane active" id="default-tab-pane" role="tabpanel">
<div class="table-responsive">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .environment.DefaultAttributes }}
<tr>
<td><span class="environment-attribute-key">{{$index}}</span></td>
<td><span class="environment-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
<div class="tab-pane" id="override-tab-pane" role="tabpanel">
<div class="table-responsive">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .environment.OverrideAttributes }}
<tr>
<td><span class="environment-attribute-key">{{$index}}</span></td>
<td><span class="environment-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</div>
{{ end }}
{{end}}
27 changes: 14 additions & 13 deletions internal/app/ui/templates/partials/node/attributes.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!-- Attributes -->
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" data-bs-target="#merged-tab-pane">Merged</a>
<a class="nav-link disabled" data-bs-toggle="tab" data-bs-target="#merged-tab-pane">Merged</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" data-bs-target="#default-tab-pane">Default</a>
<a class="nav-link active" data-bs-toggle="tab" data-bs-target="#default-tab-pane">Default</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" data-bs-target="#normal-tab-pane">Normal</a>
Expand All @@ -17,62 +17,63 @@
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="merged-tab-pane" role="tabpanel">
<div class="tab-pane" id="merged-tab-pane" role="tabpanel">
<div class="table-responsive">
Merge functionality does not exist
<table class="table table-striped">
<tbody>
{{ range $index, $value := .node.DefaultAttributes }}
{{ range $index, $value := .node.NormalAttributes }}
<tr>
<td><span class="node-attribute-key">$.{{$index}}</span></td>
<td><span class="node-attribute-key">{{$index}}</span></td>
<td><span class="node-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
<div class="tab-pane fade show" id="default-tab-pane" role="tabpanel">
<div class="tab-pane active" id="default-tab-pane" role="tabpanel">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .node.DefaultAttributes }}
<tr>
<td><span class="node-attribute-key">$.{{$index}}</span></td>
<td><span class="node-attribute-key">{{$index}}</span></td>
<td><span class="node-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="normal-tab-pane" role="tabpanel">
<div class="tab-pane" id="normal-tab-pane" role="tabpanel">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .node.NormalAttributes }}
<tr>
<td><span class="node-attribute-key">$.{{$index}}</span></td>
<td><span class="node-attribute-key">{{$index}}</span></td>
<td><span class="node-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="override-tab-pane" role="tabpanel">
<div class="tab-pane" id="override-tab-pane" role="tabpanel">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .node.OverrideAttributes }}
<tr>
<td><span class="node-attribute-key">$.{{$index}}</span></td>
<td><span class="node-attribute-key">{{$index}}</span></td>
<td><span class="node-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="automatic-tab-pane" role="tabpanel">
<div class="tab-pane" id="automatic-tab-pane" role="tabpanel">
<table class="table table-striped">
<tbody>
{{ range $index, $value := .node.AutomaticAttributes }}
<tr>
<td><span class="node-attribute-key">$.{{$index}}</span></td>
<td><span class="node-attribute-key">{{$index}}</span></td>
<td><span class="node-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions internal/app/ui/templates/role.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h4>Attributes</h4>
<tbody>
{{ range $index, $value := .role.DefaultAttributes }}
<tr>
<td><span class="role-attribute-key">$.{{$index}}</span></td>
<td><span class="role-attribute-key">{{$index}}</span></td>
<td><span class="role-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
Expand All @@ -48,7 +48,7 @@ <h4>Attributes</h4>
<tbody>
{{ range $index, $value := .role.OverrideAttributes }}
<tr>
<td><span class="role-attribute-key">$.{{$index}}</span></td>
<td><span class="role-attribute-key">{{$index}}</span></td>
<td><span class="role-attribute-value"><code>{{$value}}</code></span></td>
</tr>
{{ end }}
Expand Down
2 changes: 2 additions & 0 deletions internal/chef/databags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chef
import (
"context"
"errors"
"github.com/drewhammond/chefbrowser/internal/util"

"github.com/go-chef/chef"
)
Expand Down Expand Up @@ -36,5 +37,6 @@ func (s Service) GetDatabagItemContent(ctx context.Context, databag string, item
return contents, ErrDatabagItemNotFound
}

contents = util.MakeJSONPath(contents.(map[string]interface{}), "$")
return contents, nil
}
4 changes: 4 additions & 0 deletions internal/chef/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chef
import (
"context"
"errors"
"github.com/drewhammond/chefbrowser/internal/util"

"github.com/go-chef/chef"
)
Expand All @@ -25,5 +26,8 @@ func (s Service) GetEnvironment(ctx context.Context, name string) (*chef.Environ
return &chef.Environment{}, ErrEnvironmentNotFound
}

environment.DefaultAttributes = util.MakeJSONPath(environment.DefaultAttributes.(map[string]interface{}), "$")
environment.OverrideAttributes = util.MakeJSONPath(environment.OverrideAttributes.(map[string]interface{}), "$")

return environment, nil
}
6 changes: 6 additions & 0 deletions internal/chef/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chef
import (
"context"
"fmt"
"github.com/drewhammond/chefbrowser/internal/util"
"sort"

"github.com/go-chef/chef"
Expand Down Expand Up @@ -49,5 +50,10 @@ func (s Service) GetNode(ctx context.Context, name string) (*Node, error) {
return nil, err
}

node.AutomaticAttributes = util.MakeJSONPath(node.AutomaticAttributes, "$")
node.NormalAttributes = util.MakeJSONPath(node.NormalAttributes, "$")
node.DefaultAttributes = util.MakeJSONPath(node.DefaultAttributes, "$")
node.OverrideAttributes = util.MakeJSONPath(node.OverrideAttributes, "$")

return &Node{node}, nil
}
4 changes: 4 additions & 0 deletions internal/chef/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chef
import (
"context"
"errors"
"github.com/drewhammond/chefbrowser/internal/util"
"sort"

"github.com/go-chef/chef"
Expand All @@ -25,6 +26,9 @@ func (s Service) GetRole(ctx context.Context, name string) (*Role, error) {
return nil, ErrRoleNotFound
}

role.DefaultAttributes = util.MakeJSONPath(role.DefaultAttributes.(map[string]interface{}), "$")
role.OverrideAttributes = util.MakeJSONPath(role.OverrideAttributes.(map[string]interface{}), "$")

return &Role{role}, nil
}

Expand Down
21 changes: 21 additions & 0 deletions internal/util/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

// MakeJSONPath will take a JSON object and return flattened JSON paths
func MakeJSONPath(obj map[string]interface{}, prefix string) map[string]interface{} {
attrs := make(map[string]interface{})
var nested map[string]interface{}

for k, v := range obj {
switch V := v.(type) {
default:
attrs[prefix+"."+k] = V
case map[string]interface{}:
nested = MakeJSONPath(V, prefix+"."+k)
for k2, v2 := range nested {
attrs[k2] = v2
}
}
}

return attrs
}
39 changes: 39 additions & 0 deletions internal/util/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package util

import (
"reflect"
"testing"
)

func TestMakeJSONPath(t *testing.T) {

input := map[string]interface{}{
"stringkey": "stringval",
"somearr": []string{"one", "two", "three"},
"nested": map[string]interface{}{
"foo": "bar",
"hello": "world",
"bool": false,
"int": 123,
"deep": map[string]interface{}{
"nest": "value",
},
},
}

expected := map[string]interface{}{
"$.stringkey": "stringval",
"$.somearr": []string{"one", "two", "three"},
"$.nested.foo": "bar",
"$.nested.bool": false,
"$.nested.int": 123,
"$.nested.hello": "world",
"$.nested.deep.nest": "value",
}

actual := MakeJSONPath(input, "$")

if !reflect.DeepEqual(expected, actual) {
t.Errorf("failed, in: %v, expected: %v, actual: %v", input, expected, actual)
}
}

0 comments on commit 5808a08

Please sign in to comment.