Skip to content

Commit

Permalink
[minor_change] Add model and client files for psuInstPol (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfr-6 authored Jun 29, 2023
1 parent f69ec3b commit dcdfa9b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
44 changes: 44 additions & 0 deletions client/psuInstPol_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/v2/models"
)

func (sm *ServiceManager) CreatePowerSupplyRedundancyPolicy(name string, description string, psuInstPolAttr models.PsuInstPolAttributes) (*models.PsuInstPol, error) {
rn := fmt.Sprintf(models.RnPsuInstPol, name)
psuInstPol := models.NewPowerSupplyRedundancyPolicy(rn, description, psuInstPolAttr)
err := sm.Save(psuInstPol)
return psuInstPol, err
}

func (sm *ServiceManager) ReadPowerSupplyRedundancyPolicy(name string) (*models.PsuInstPol, error) {
dn := fmt.Sprintf(models.DnPsuInstPol, name)
cont, err := sm.Get(dn)
if err != nil {
return nil, err
}
psuInstPol := models.PsuInstPolFromContainer(cont)
return psuInstPol, nil
}

func (sm *ServiceManager) DeletePowerSupplyRedundancyPolicy(name string) error {
dn := fmt.Sprintf(models.DnPsuInstPol, name)
return sm.DeleteByDn(dn, models.PsuInstPolClassName)
}

func (sm *ServiceManager) UpdatePowerSupplyRedundancyPolicy(name string, description string, psuInstPolAttr models.PsuInstPolAttributes) (*models.PsuInstPol, error) {
rn := fmt.Sprintf(models.RnPsuInstPol, name)
psuInstPol := models.NewPowerSupplyRedundancyPolicy(rn, description, psuInstPolAttr)
psuInstPol.Status = "modified"
err := sm.Save(psuInstPol)
return psuInstPol, err
}

func (sm *ServiceManager) ListPowerSupplyRedundancyPolicy() ([]*models.PsuInstPol, error) {
dnUrl := fmt.Sprintf("%s/uni/fabric/psuInstPol.json", models.BaseurlStr)
cont, err := sm.GetViaURL(dnUrl)
list := models.PsuInstPolListFromContainer(cont)
return list, err
}
88 changes: 88 additions & 0 deletions models/psu_inst_pol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package models

import (
"fmt"
"strconv"

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

const (
DnPsuInstPol = "uni/fabric/psuInstP-%s"
RnPsuInstPol = "psuInstP-%s"
ParentDnPsuInstPol = "uni/fabric"
PsuInstPolClassName = "psuInstPol"
)

type PsuInstPol struct {
BaseAttributes
PsuInstPolAttributes
}

type PsuInstPolAttributes struct {
AdminRdnM string `json:",omitempty"`
Name string `json:",omitempty"`
Annotation string `json:",omitempty"`
NameAlias string `json:",omitempty"`
}

func NewPowerSupplyRedundancyPolicy(psuInstPolRn, description string, psuInstPolAttr PsuInstPolAttributes) *PsuInstPol {
dn := fmt.Sprintf("%s/%s", ParentDnPsuInstPol, psuInstPolRn)
return &PsuInstPol{
BaseAttributes: BaseAttributes{
DistinguishedName: dn,
Description: description,
Status: "created, modified",
ClassName: PsuInstPolClassName,
Rn: psuInstPolRn,
},

PsuInstPolAttributes: psuInstPolAttr,
}
}

func (psuInstPol *PsuInstPol) ToMap() (map[string]string, error) {
psuInstPolMap, err := psuInstPol.BaseAttributes.ToMap()
if err != nil {
return nil, err
}

A(psuInstPolMap, "adminRdnM", psuInstPol.AdminRdnM)
A(psuInstPolMap, "name", psuInstPol.Name)
A(psuInstPolMap, "annotation", psuInstPol.Annotation)
A(psuInstPolMap, "nameAlias", psuInstPol.NameAlias)

return psuInstPolMap, err
}

func PsuInstPolFromContainerList(cont *container.Container, index int) *PsuInstPol {
PsuInstPolCont := cont.S("imdata").Index(index).S(PsuInstPolClassName, "attributes")
return &PsuInstPol{
BaseAttributes{
DistinguishedName: G(PsuInstPolCont, "dn"),
Description: G(PsuInstPolCont, "descr"),
Status: G(PsuInstPolCont, "status"),
ClassName: PsuInstPolClassName,
Rn: G(PsuInstPolCont, "rn"),
},
PsuInstPolAttributes{
AdminRdnM: G(PsuInstPolCont, "adminRdnM"),
Name: G(PsuInstPolCont, "name"),
Annotation: G(PsuInstPolCont, "annotation"),
NameAlias: G(PsuInstPolCont, "nameAlias"),
},
}
}

func PsuInstPolFromContainer(cont *container.Container) *PsuInstPol {
return PsuInstPolFromContainerList(cont, 0)
}

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

0 comments on commit dcdfa9b

Please sign in to comment.