From cec1769361a2d42da603712a029acfe1057ba88b Mon Sep 17 00:00:00 2001 From: Lionel Hercot Date: Fri, 10 Dec 2021 09:59:38 +0100 Subject: [PATCH] Add support for tagTag class --- client/tagTag_service.go | 44 +++++++++++++++++++++++ models/tag_tag.go | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 client/tagTag_service.go create mode 100644 models/tag_tag.go diff --git a/client/tagTag_service.go b/client/tagTag_service.go new file mode 100644 index 00000000..b5f1ff63 --- /dev/null +++ b/client/tagTag_service.go @@ -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 +} diff --git a/models/tag_tag.go b/models/tag_tag.go new file mode 100644 index 00000000..987c4b74 --- /dev/null +++ b/models/tag_tag.go @@ -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 +}