Skip to content

Commit

Permalink
[minor_change] Addition of new client and model files for vncCDev (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrsr authored May 13, 2022
1 parent 1566eee commit 3f8d78f
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 2 deletions.
92 changes: 92 additions & 0 deletions client/vnsCDev_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package client

import (
"fmt"

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

func (sm *ServiceManager) CreateConcreteDevice(name string, parent_dn string, nameAlias string, vnsCDevAttr models.ConcreteDeviceAttributes) (*models.ConcreteDevice, error) {
rn := fmt.Sprintf(models.RnvnsCDev, name)
vnsCDev := models.NewConcreteDevice(rn, parent_dn, nameAlias, vnsCDevAttr)
err := sm.Save(vnsCDev)
return vnsCDev, err
}

func (sm *ServiceManager) ReadConcreteDevice(name string, parent_dn string) (*models.ConcreteDevice, error) {
dn := fmt.Sprintf(parent_dn+"/"+models.RnvnsCDev, name)
cont, err := sm.Get(dn)
if err != nil {
return nil, err
}

vnsCDev := models.ConcreteDeviceFromContainer(cont)
return vnsCDev, nil
}

func (sm *ServiceManager) DeleteConcreteDevice(name string, parent_dn string) error {
dn := fmt.Sprintf(parent_dn+"/"+models.RnvnsCDev, name)
return sm.DeleteByDn(dn, models.VnscdevClassName)
}

func (sm *ServiceManager) UpdateConcreteDevice(name string, parent_dn string, nameAlias string, vnsCDevAttr models.ConcreteDeviceAttributes) (*models.ConcreteDevice, error) {
rn := fmt.Sprintf(models.RnvnsCDev, name)
vnsCDev := models.NewConcreteDevice(rn, parent_dn, nameAlias, vnsCDevAttr)
vnsCDev.Status = "modified"
err := sm.Save(vnsCDev)
return vnsCDev, err
}

func (sm *ServiceManager) ListConcreteDevice(parent_dn string) ([]*models.ConcreteDevice, error) {
dnUrl := fmt.Sprintf(models.BaseurlStr + "/" + parent_dn + "/vnsCDev.json")
cont, err := sm.GetViaURL(dnUrl)
list := models.ConcreteDeviceListFromContainer(cont)
return list, err
}

func (sm *ServiceManager) CreateRelationvnsRsCDevToCtrlrP(parentDn, annotation, tDn string) error {
dn := fmt.Sprintf("%s/rscDevToCtrlrP", parentDn)
containerJSON := []byte(fmt.Sprintf(`{
"%s": {
"attributes": {
"dn": "%s",
"annotation": "%s",
"tDn": "%s"
}
}
}`, "vnsRsCDevToCtrlrP", dn, annotation, tDn))

jsonPayload, err := container.ParseJSON(containerJSON)
if err != nil {
return err
}
req, err := sm.client.MakeRestRequest("POST", fmt.Sprintf("%s.json", sm.MOURL), jsonPayload, true)
if err != nil {
return err
}
cont, _, err := sm.client.Do(req)
if err != nil {
return err
}
fmt.Printf("%+v", cont)
return nil
}

func (sm *ServiceManager) DeleteRelationvnsRsCDevToCtrlrP(parentDn string) error {
dn := fmt.Sprintf("%s/rscDevToCtrlrP", parentDn)
return sm.DeleteByDn(dn, "vnsRsCDevToCtrlrP")
}

func (sm *ServiceManager) ReadRelationvnsRsCDevToCtrlrP(parentDn string) (interface{}, error) {
dnUrl := fmt.Sprintf("%s/%s/%s.json", models.BaseurlStr, parentDn, "vnsRsCDevToCtrlrP")
cont, err := sm.GetViaURL(dnUrl)
contList := models.ListFromContainer(cont, "vnsRsCDevToCtrlrP")

if len(contList) > 0 {
dat := models.G(contList[0], "tDn")
return dat, err
} else {
return nil, err
}
}
116 changes: 116 additions & 0 deletions models/vns_c_dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package models

import (
"fmt"
"strconv"

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

const (
DnvnsCDev = "%s/cDev-%s"
RnvnsCDev = "cDev-%s"
VnscdevClassName = "vnsCDev"
)

type ConcreteDevice struct {
BaseAttributes
NameAliasAttribute
ConcreteDeviceAttributes
}

type ConcreteDeviceAttributes struct {
CloneCount string `json:",omitempty"`
DevCtxLbl string `json:",omitempty"`
Host string `json:",omitempty"`
IsCloneOperation string `json:",omitempty"`
IsTemplate string `json:",omitempty"`
Name string `json:",omitempty"`
VcenterName string `json:",omitempty"`
VmName string `json:",omitempty"`
Annotation string `json:",omitempty"`
}

func NewConcreteDevice(vnsCDevRn, parentDn, nameAlias string, vnsCDevAttr ConcreteDeviceAttributes) *ConcreteDevice {
dn := fmt.Sprintf("%s/%s", parentDn, vnsCDevRn)
return &ConcreteDevice{
BaseAttributes: BaseAttributes{
DistinguishedName: dn,
Status: "created, modified",
ClassName: VnscdevClassName,
Rn: vnsCDevRn,
},
NameAliasAttribute: NameAliasAttribute{
NameAlias: nameAlias,
},
ConcreteDeviceAttributes: vnsCDevAttr,
}
}

func (vnsCDev *ConcreteDevice) ToMap() (map[string]string, error) {
vnsCDevMap, err := vnsCDev.BaseAttributes.ToMap()
if err != nil {
return nil, err
}

alias, err := vnsCDev.NameAliasAttribute.ToMap()
if err != nil {
return nil, err
}

for key, value := range alias {
A(vnsCDevMap, key, value)
}

A(vnsCDevMap, "cloneCount", vnsCDev.CloneCount)
A(vnsCDevMap, "annotation", vnsCDev.Annotation)
A(vnsCDevMap, "devCtxLbl", vnsCDev.DevCtxLbl)
A(vnsCDevMap, "host", vnsCDev.Host)
A(vnsCDevMap, "isCloneOperation", vnsCDev.IsCloneOperation)
A(vnsCDevMap, "isTemplate", vnsCDev.IsTemplate)
A(vnsCDevMap, "name", vnsCDev.Name)
A(vnsCDevMap, "vcenterName", vnsCDev.VcenterName)
A(vnsCDevMap, "vmName", vnsCDev.VmName)
return vnsCDevMap, err
}

func ConcreteDeviceFromContainerList(cont *container.Container, index int) *ConcreteDevice {
ConcreteDeviceCont := cont.S("imdata").Index(index).S(VnscdevClassName, "attributes")
return &ConcreteDevice{
BaseAttributes{
DistinguishedName: G(ConcreteDeviceCont, "dn"),
Status: G(ConcreteDeviceCont, "status"),
ClassName: VnscdevClassName,
Rn: G(ConcreteDeviceCont, "rn"),
},
NameAliasAttribute{
NameAlias: G(ConcreteDeviceCont, "nameAlias"),
},
ConcreteDeviceAttributes{
CloneCount: G(ConcreteDeviceCont, "cloneCount"),
Annotation: G(ConcreteDeviceCont, "annotation"),
DevCtxLbl: G(ConcreteDeviceCont, "devCtxLbl"),
Host: G(ConcreteDeviceCont, "host"),
IsCloneOperation: G(ConcreteDeviceCont, "isCloneOperation"),
IsTemplate: G(ConcreteDeviceCont, "isTemplate"),
Name: G(ConcreteDeviceCont, "name"),
VcenterName: G(ConcreteDeviceCont, "vcenterName"),
VmName: G(ConcreteDeviceCont, "vmName"),
},
}
}

func ConcreteDeviceFromContainer(cont *container.Container) *ConcreteDevice {
return ConcreteDeviceFromContainerList(cont, 0)
}

func ConcreteDeviceListFromContainer(cont *container.Container) []*ConcreteDevice {
length, _ := strconv.Atoi(G(cont, "totalCount"))
arr := make([]*ConcreteDevice, length)

for i := 0; i < length; i++ {
arr[i] = ConcreteDeviceFromContainerList(cont, i)
}

return arr
}
3 changes: 1 addition & 2 deletions models/vns_l_if.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
)

const (
DnvnsLIf = "uni/tn-%s/lDevVip-%s/lIf-%s"
DnvnsLIf = "%s/lIf-%s"
RnvnsLIf = "lIf-%s"
ParentDnvnsLIf = "uni/tn-%s/lDevVip-%s"
VnslifClassName = "vnsLIf"
)

Expand Down

0 comments on commit 3f8d78f

Please sign in to comment.