Skip to content

Commit

Permalink
associate tags to network level
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek9686 committed Sep 22, 2024
1 parent db224ba commit c64dc85
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
44 changes: 29 additions & 15 deletions controllers/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -63,6 +64,25 @@ func createTag(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
// check if tag network exists
_, err = logic.GetNetwork(req.Network.String())
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failed to get network details for "+req.Network.String()), "badrequest"))
return
}
// check if tag exists
tag := models.Tag{
ID: models.TagID(fmt.Sprintf("%s.%s", req.Network, req.TagName)),
TagName: req.TagName,
Network: req.Network,
CreatedBy: user.UserName,
CreatedAt: time.Now(),
}
err = logic.InsertTag(tag)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
go func() {
for _, hostID := range req.TaggedHosts {
h, err := logic.GetHost(hostID)
Expand All @@ -72,21 +92,11 @@ func createTag(w http.ResponseWriter, r *http.Request) {
if h.Tags == nil {
h.Tags = make(map[models.TagID]struct{})
}
h.Tags[req.ID] = struct{}{}
h.Tags[tag.ID] = struct{}{}
logic.UpsertHost(h)
}
}()

tag := models.Tag{
ID: req.ID,
CreatedBy: user.UserName,
CreatedAt: time.Now(),
}
err = logic.InsertTag(tag)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
logic.ReturnSuccessResponseWithJson(w, r, req, "created tag successfully")
}

Expand All @@ -105,21 +115,25 @@ func updateTag(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

tag, err := logic.GetTag(updateTag.ID)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
updateTag.NewID = models.TagID(strings.TrimSpace(updateTag.NewID.String()))
if updateTag.NewID.String() != "" {
tag.ID = updateTag.NewID
updateTag.NewName = strings.TrimSpace(updateTag.NewName)
var newID models.TagID
if updateTag.NewName != "" {
newID = models.TagID(fmt.Sprintf("%s.%s", tag.Network, updateTag.NewName))
tag.ID = newID
tag.TagName = updateTag.NewName
err = logic.InsertTag(tag)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
}
go logic.UpdateTag(updateTag)
go logic.UpdateTag(updateTag, newID)
logic.ReturnSuccessResponse(w, r, "updating tags")
}

Expand Down
6 changes: 3 additions & 3 deletions logic/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func ListTags() ([]models.Tag, error) {
}

// UpdateTag - updates and syncs hosts with tag update
func UpdateTag(req models.UpdateTagReq) {
func UpdateTag(req models.UpdateTagReq, newID models.TagID) {
tagMutex.Lock()
defer tagMutex.Unlock()
tagHostsMap := GetHostsWithTag(req.ID)
Expand All @@ -122,12 +122,12 @@ func UpdateTag(req models.UpdateTagReq) {
UpsertHost(&deletedTaggedHost)
}
go func(req models.UpdateTagReq) {
if req.NewID != "" {
if newID != "" {
tagHostsMap = GetHostsWithTag(req.ID)
for _, hostI := range tagHostsMap {
hostI := hostI
delete(hostI.Tags, req.ID)
hostI.Tags[req.NewID] = struct{}{}
hostI.Tags[newID] = struct{}{}
UpsertHost(&hostI)
}
}
Expand Down
18 changes: 14 additions & 4 deletions models/tags.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package models

import "time"
import (
"fmt"
"time"
)

type TagID string

func (id TagID) String() string {
return string(id)
}

func (t Tag) GetIDFromName() string {
return fmt.Sprintf("%s.%s", t.Network, t.TagName)
}

type Tag struct {
ID TagID `json:"id"`
TagName string `json:"tag_name"`
Network NetworkID `json:"network"`
CreatedBy string `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
}

type CreateTagReq struct {
ID TagID `json:"id"`
TaggedHosts []string `json:"tagged_hosts"`
TagName string `json:"tag_name"`
Network NetworkID `json:"network"`
TaggedHosts []string `json:"tagged_hosts"`
}

type TagListResp struct {
Expand All @@ -27,6 +37,6 @@ type TagListResp struct {

type UpdateTagReq struct {
Tag
NewID TagID `json:"new_id"`
NewName string `json:"new_name"`
TaggedHosts []string `json:"tagged_hosts"`
}

0 comments on commit c64dc85

Please sign in to comment.