Skip to content

Commit 49b6b24

Browse files
authored
Merge pull request #593 from jmpsec/adding-tag-types
Adding types to tags
2 parents e1c6dc8 + 037a6a1 commit 49b6b24

File tree

14 files changed

+130
-41
lines changed

14 files changed

+130
-41
lines changed

admin/handlers/post.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/jmpsec/osctrl/nodes"
1414
"github.com/jmpsec/osctrl/queries"
1515
"github.com/jmpsec/osctrl/settings"
16+
"github.com/jmpsec/osctrl/tags"
1617
"github.com/jmpsec/osctrl/users"
1718
"github.com/jmpsec/osctrl/utils"
1819
"github.com/rs/zerolog/log"
@@ -1089,7 +1090,15 @@ func (h *HandlersAdmin) EnvsPOSTHandler(w http.ResponseWriter, r *http.Request)
10891090
return
10901091
}
10911092
// Create a tag for this new environment
1092-
if err := h.Tags.NewTag(env.Name, "Tag for environment "+env.Name, "", env.Icon, ctx[sessions.CtxUser], env.ID, false); err != nil {
1093+
if err := h.Tags.NewTag(
1094+
env.Name,
1095+
"Tag for environment "+env.Name,
1096+
"",
1097+
env.Icon,
1098+
ctx[sessions.CtxUser],
1099+
env.ID,
1100+
false,
1101+
tags.TagTypeEnv); err != nil {
10931102
adminErrorResponse(w, "error generating tag", http.StatusInternalServerError, err)
10941103
h.Inc(metricAdminErr)
10951104
return
@@ -1439,7 +1448,7 @@ func (h *HandlersAdmin) TagsPOSTHandler(w http.ResponseWriter, r *http.Request)
14391448
return
14401449
}
14411450
// Prepare user to create
1442-
if err := h.Tags.NewTag(t.Name, t.Description, t.Color, t.Icon, ctx[sessions.CtxUser], env.ID, false); err != nil {
1451+
if err := h.Tags.NewTag(t.Name, t.Description, t.Color, t.Icon, ctx[sessions.CtxUser], env.ID, false, t.TagType); err != nil {
14431452
adminErrorResponse(w, "error with new tag", http.StatusInternalServerError, err)
14441453
h.Inc(metricAdminErr)
14451454
return

admin/handlers/templates.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/jmpsec/osctrl/environments"
1313
"github.com/jmpsec/osctrl/nodes"
1414
"github.com/jmpsec/osctrl/settings"
15+
"github.com/jmpsec/osctrl/tags"
1516
"github.com/jmpsec/osctrl/users"
1617
"github.com/jmpsec/osctrl/utils"
1718
"github.com/rs/zerolog/log"
@@ -783,7 +784,7 @@ func (h *HandlersAdmin) CarvesDetailsHandler(w http.ResponseWriter, r *http.Requ
783784
}
784785
// Custom functions to handle formatting
785786
funcMap := template.FuncMap{
786-
"inFutureTime": utils.InFutureTime,
787+
"inFutureTime": utils.InFutureTime,
787788
}
788789
// Prepare template
789790
tempateFiles := h.NewTemplateFiles(h.TemplatesFolder, "carves-details.html").filepaths
@@ -1452,6 +1453,7 @@ func (h *HandlersAdmin) TagsGETHandler(w http.ResponseWriter, r *http.Request) {
14521453
"pastFutureTimes": utils.PastFutureTimes,
14531454
"inFutureTime": utils.InFutureTime,
14541455
"environmentFinder": environments.EnvironmentFinder,
1456+
"tagTypeDecorator": tags.TagTypeDecorator,
14551457
}
14561458
// Prepare template
14571459
tempateFiles := h.NewTemplateFiles(h.TemplatesFolder, "tags.html").filepaths

admin/handlers/types-requests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ type TagsRequest struct {
135135
Color string `json:"color"`
136136
Icon string `json:"icon"`
137137
Environment string `json:"environment"`
138+
TagType uint `json:"tagtype"`
138139
}
139140

140141
// TagNodesRequest to receive a tag for nodes

admin/static/js/tags.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ function editTag(_name) {
2424
$("#tag_description").val($("#tag_desc_" + _name).val());
2525
$("#tag_color").val($("#tag_color_" + _name).val());
2626
$("#tag_icon").val($("#tag_icon_" + _name).val());
27-
$("#tag_env").val($("#tag_env_" + _name).val());
27+
$("#tag_env")
28+
.val($("#tag_env_" + _name).val())
29+
.change();
30+
$("#tag_type")
31+
.val($("#tag_type_" + _name).val())
32+
.change();
2833
$("#createEditTagModal").modal();
2934
}
3035

@@ -36,6 +41,7 @@ function confirmCreateTag() {
3641
var _color = $("#tag_color").val();
3742
var _icon = $("#tag_icon").val();
3843
var _env = $("#tag_env").val();
44+
var _tagtype = parseInt($("#tag_type").val());
3945
var data = {
4046
csrftoken: _csrftoken,
4147
action: "add",
@@ -44,6 +50,7 @@ function confirmCreateTag() {
4450
color: _color,
4551
icon: _icon,
4652
environment: _env,
53+
tagtype: _tagtype,
4754
};
4855
sendPostRequest(data, _url, _url, false);
4956
}
@@ -56,6 +63,7 @@ function confirmEditTag() {
5663
var _color = $("#tag_color").val();
5764
var _icon = $("#tag_icon").val();
5865
var _env = $("#tag_env").val();
66+
var _tagtype = parseInt($("#tag_type").val());
5967
var data = {
6068
csrftoken: _csrftoken,
6169
action: "edit",
@@ -64,6 +72,7 @@ function confirmEditTag() {
6472
color: _color,
6573
icon: _icon,
6674
environment: _env,
75+
tagtype: _tagtype,
6776
};
6877
sendPostRequest(data, _url, _url, false);
6978
}

admin/templates/tags.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<thead>
4444
<tr>
4545
<th>Name</th>
46+
<th>Type</th>
4647
<th>Description</th>
4748
<th>Color</th>
4849
<th>Icon</th>
@@ -54,14 +55,15 @@
5455
{{range $i, $t := $.Tags}}
5556
<tr>
5657
<td><b>{{ $t.Name }}</b><input type="hidden" value="{{ $t.Name }}" id="tag_name_{{ $t.Name }}"></td>
58+
<td>{{ tagTypeDecorator $t.TagType }}<input type="hidden" value="{{ $t.TagType }}" id="tag_type_{{ $t.Name }}">
5759
<td>{{ $t.Description }}<input type="hidden" value="{{ $t.Description }}" id="tag_desc_{{ $t.Name }}"></td>
5860
<td>{{ $t.Color }}<input type="hidden" value="{{ $t.Color }}" id="tag_color_{{ $t.Name }}">
5961
<span style="color: {{ $t.Color }}; background-color: {{ $t.Color }};">##</span>
6062
</td>
6163
<td>{{ $t.Icon }} <i class="{{ $t.Icon }}"></i><input type="hidden" value="{{ $t.Icon }}" id="tag_icon_{{ $t.Name }}"></td>
6264
<td>
63-
<b>{{ environmentFinder $t.EnvironmentID $.Environments }}</b>
64-
<input type="hidden" value="{{ environmentFinder $t.EnvironmentID $.Environments }}" id="tag_env_{{ $t.Name }}"></td>
65+
<b>{{ environmentFinder $t.EnvironmentID $.Environments false }}</b>
66+
<input type="hidden" value="{{ environmentFinder $t.EnvironmentID $.Environments true }}" id="tag_env_{{ $t.Name }}"></td>
6567
<td>
6668
<button type="button" class="btn btn-sm btn-ghost-danger" onclick="confirmDeleteTag('{{ $t.Name }}');">
6769
<i class="far fa-trash-alt"></i>
@@ -125,6 +127,17 @@ <h4 id="modal_title_tag" class="modal-title"></h4>
125127
{{ end }}
126128
</select>
127129
</div>
130+
<label class="col-md-2 col-form-label" for="tag_type">Tag Type: </label>
131+
<div class="col-md-4">
132+
<select class="form-control" name="tag_type" id="tag_type">
133+
<option value=""></option>
134+
<option value="0">Environment</option>
135+
<option value="1">UUID</option>
136+
<option value="2">Platform</option>
137+
<option value="3">Localname</option>
138+
<option value="4">Custom</option>
139+
</select>
140+
</div>
128141
</div>
129142
</div>
130143
<div class="modal-footer">
@@ -167,6 +180,10 @@ <h4 id="modal_title_tag" class="modal-title"></h4>
167180
theme: "classic",
168181
width: '100%'
169182
});
183+
$('#tag_type').select2({
184+
theme: "classic",
185+
width: '100%'
186+
});
170187

171188
// Enable all tooltips
172189
$('[data-tooltip="true"]').tooltip({trigger : 'hover'});

cli/environment.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ func addEnvironment(c *cli.Context) error {
6161
return err
6262
}
6363
// Create a tag for this new environment
64-
if err := tagsmgr.NewTag(newEnv.Name, "Tag for environment "+newEnv.Name, tags.RandomColor(), newEnv.Icon, appName, newEnv.ID, false); err != nil {
64+
if err := tagsmgr.NewTag(
65+
newEnv.Name,
66+
"Tag for environment "+newEnv.Name,
67+
tags.RandomColor(),
68+
newEnv.Icon,
69+
appName,
70+
newEnv.ID,
71+
false,
72+
tags.TagTypeEnv); err != nil {
6573
return err
6674
}
6775
// Generate flags

cli/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ func init() {
11451145
Usage: "Tag an existing node",
11461146
Flags: []cli.Flag{
11471147
&cli.StringFlag{
1148-
Name: "uuid, u",
1148+
Name: "uuid",
11491149
Aliases: []string{"u"},
11501150
Usage: "Node UUID to be tagged",
11511151
},
@@ -1159,6 +1159,12 @@ func init() {
11591159
Aliases: []string{"T"},
11601160
Usage: "Tag value to be used. It will be created if does not exist",
11611161
},
1162+
&cli.StringFlag{
1163+
Name: "tag-type",
1164+
Aliases: []string{"type"},
1165+
Value: "custom",
1166+
Usage: "Tag type to be used. It can be 'env', 'uuid', 'localname' and 'custom'",
1167+
},
11621168
},
11631169
Action: cliWrapper(tagNode),
11641170
},

cli/node.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/jmpsec/osctrl/nodes"
1010
"github.com/jmpsec/osctrl/settings"
11+
"github.com/jmpsec/osctrl/tags"
1112
"github.com/olekukonko/tablewriter"
1213
"github.com/urfave/cli/v2"
1314
)
@@ -152,6 +153,16 @@ func tagNode(c *cli.Context) error {
152153
fmt.Println("❌ tag is required")
153154
os.Exit(1)
154155
}
156+
tagType := c.String("tag-type")
157+
tagTypeInt := tags.TagTypeCustom
158+
switch tagType {
159+
case "env":
160+
tagTypeInt = tags.TagTypeEnv
161+
case "uuid":
162+
tagTypeInt = tags.TagTypeUUID
163+
case "localname":
164+
tagTypeInt = tags.TagTypeLocalname
165+
}
155166
if dbFlag {
156167
e, err := envs.Get(env)
157168
if err != nil {
@@ -162,7 +173,7 @@ func tagNode(c *cli.Context) error {
162173
return fmt.Errorf("error get uuid - %s", err)
163174
}
164175
if tagsmgr.Exists(tag) {
165-
if err := tagsmgr.TagNode(tag, n, appName, false); err != nil {
176+
if err := tagsmgr.TagNode(tag, n, appName, false, tagTypeInt); err != nil {
166177
return fmt.Errorf("error tagging - %s", err)
167178
}
168179
}
@@ -172,7 +183,7 @@ func tagNode(c *cli.Context) error {
172183
}
173184
}
174185
if !silentFlag {
175-
fmt.Println("✅ node was deleted successfully")
186+
fmt.Println("✅ node was tagged successfully")
176187
}
177188
return nil
178189
}

environments/util.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ func PackageDownloadURL(env TLSEnvironment, pkg string) string {
6060
}
6161

6262
// EnvironmentFinder to find the environment and return its name based on the environment ID
63-
func EnvironmentFinder(envID uint, envs []TLSEnvironment) string {
63+
func EnvironmentFinder(envID uint, envs []TLSEnvironment, uuid bool) string {
6464
for _, env := range envs {
6565
if env.ID == envID {
66+
if uuid {
67+
return env.UUID
68+
}
6669
return env.Name
6770
}
6871
}

settings/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
require (
1919
github.com/mattn/go-colorable v0.1.14 // indirect
2020
github.com/mattn/go-isatty v0.0.20 // indirect
21-
golang.org/x/sys v0.29.0 // indirect
21+
golang.org/x/sys v0.30.0 // indirect
2222
)
2323

2424
require (

0 commit comments

Comments
 (0)