Skip to content

Commit

Permalink
Showing 2 changed files with 120 additions and 0 deletions.
44 changes: 44 additions & 0 deletions client/tagTag_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package client

import (
"fmt"

"github.com/ciscoecosystem/aci-go-client/models"
)

func (sm *ServiceManager) CreateTag(key string, parentDn string, tagTagAttr models.TagAttributes) (*models.Tag, error) {
rn := fmt.Sprintf(models.RnTagTag, key)
tagTag := models.NewTag(rn, parentDn, tagTagAttr)
err := sm.Save(tagTag)
return tagTag, err
}

func (sm *ServiceManager) ReadTag(key string, parentDn string) (*models.Tag, error) {
dn := fmt.Sprintf(models.DnTagTag, parentDn, key)
cont, err := sm.Get(dn)
if err != nil {
return nil, err
}
tagTag := models.TagFromContainer(cont)
return tagTag, nil
}

func (sm *ServiceManager) DeleteTag(key string, parentDn string) error {
dn := fmt.Sprintf(models.DnTagTag, parentDn, key)
return sm.DeleteByDn(dn, models.TagTagClassName)
}

func (sm *ServiceManager) UpdateTag(key string, parentDn string, tagTagAttr models.TagAttributes) (*models.Tag, error) {
rn := fmt.Sprintf(models.RnTagTag, key)
tagTag := models.NewTag(rn, parentDn, tagTagAttr)
tagTag.Status = "modified"
err := sm.Save(tagTag)
return tagTag, err
}

func (sm *ServiceManager) ListTag() ([]*models.Tag, error) {
dnUrl := fmt.Sprintf("%s/tagTag.json", models.BaseurlStr)
cont, err := sm.GetViaURL(dnUrl)
list := models.TagListFromContainer(cont)
return list, err
}
76 changes: 76 additions & 0 deletions models/tag_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package models

import (
"fmt"
"strconv"

"github.com/ciscoecosystem/aci-go-client/container"
)

const (
DnTagTag = "%s/tagKey-%s"
RnTagTag = "tagKey-%s"
TagTagClassName = "tagTag"
)

type Tag struct {
BaseAttributes
TagAttributes
}

type TagAttributes struct {
Key string `json:",omitempty"`
Value string `json:",omitempty"`
}

func NewTag(tagTagRn, parentDn, tagTagAttr TagAttributes) *Tag {
dn := fmt.Sprintf("%s/%s", parentDn, tagTagRn)
return &Tag{
BaseAttributes: BaseAttributes{
DistinguishedName: dn,
Status: "created, modified",
ClassName: TagTagClassName,
Rn: tagTagRn,
},
TagAttributes: tagTagAttr,
}
}

func (tagTag *Tag) ToMap() (map[string]string, error) {
tagTagMap, err := tagTag.BaseAttributes.ToMap()
if err != nil {
return nil, err
}
A(tagTagMap, "key", tagTag.Key)
A(tagTagMap, "value", tagTag.Value)
return tagTagMap, err
}

func TagFromContainerList(cont *container.Container, index int) *Tag {
TagCont := cont.S("imdata").Index(index).S(TagTagClassName, "attributes")
return &Tag{
BaseAttributes{
DistinguishedName: G(TagCont, "dn"),
Status: G(TagCont, "status"),
ClassName: TagTagClassName,
Rn: G(TagCont, "rn"),
},
TagAttributes{
Key: G(TagCont, "key"),
Value: G(TagCont, "value"),
},
}
}

func TagFromContainer(cont *container.Container) *Tag {
return TagFromContainerList(cont, 0)
}

func TagListFromContainer(cont *container.Container) []*Tag {
length, _ := strconv.Atoi(G(cont, "totalCount"))
arr := make([]*Tag, length)
for i := 0; i < length; i++ {
arr[i] = TagFromContainerList(cont, i)
}
return arr
}

0 comments on commit cec1769

Please sign in to comment.