Skip to content

Commit

Permalink
Encapsulate utils/types.go into its own package (#1704)
Browse files Browse the repository at this point in the history
* Encapsulate utils/types.go into its own package

closes models_package

* Moves utils/types.go into utils/models/types.go
* Moves utils/types_test.go into utils/models/types_test.go
* Moves Luks device related structs back into the device files under utils
* Introduces models/utils.go to contain duplicate utility functions from the utils package

* fix bad ut imports; fix formatting

* address PR comments

* update import based on comments
  • Loading branch information
vivintw committed Aug 30, 2024
1 parent 72acd71 commit 87bba02
Show file tree
Hide file tree
Showing 120 changed files with 1,542 additions and 1,325 deletions.
6 changes: 3 additions & 3 deletions cli/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package api

import (
"github.com/netapp/trident/storage"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/models"
)

type ErrorResponse struct {
Expand Down Expand Up @@ -45,11 +45,11 @@ type MultipleVolumeResponse struct {
}

type MultipleVolumePublicationResponse struct {
Items []utils.VolumePublicationExternal `json:"items"`
Items []models.VolumePublicationExternal `json:"items"`
}

type MultipleNodeResponse struct {
Items []utils.NodeExternal `json:"items"`
Items []models.NodeExternal `json:"items"`
}

type MultipleSnapshotResponse struct {
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/get_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/netapp/trident/cli/api"
"github.com/netapp/trident/frontend/rest"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/models"
)

func init() {
Expand Down Expand Up @@ -52,7 +52,7 @@ func nodeList(nodeNames []string) error {
}
}

nodes := make([]utils.NodeExternal, 0, 10)
nodes := make([]models.NodeExternal, 0, 10)

// Get the actual node objects
for _, nodeName := range nodeNames {
Expand Down Expand Up @@ -92,7 +92,7 @@ func GetNodes() ([]string, error) {
return listNodesResponse.Nodes, nil
}

func GetNode(nodeName string) (*utils.NodeExternal, error) {
func GetNode(nodeName string) (*models.NodeExternal, error) {
url := BaseURL() + "/node/" + nodeName

response, responseBody, err := api.InvokeRESTAPI("GET", url, nil)
Expand All @@ -118,7 +118,7 @@ func GetNode(nodeName string) (*utils.NodeExternal, error) {
return getNodeResponse.Node, nil
}

func WriteNodes(nodes []utils.NodeExternal) {
func WriteNodes(nodes []models.NodeExternal) {
switch OutputFormat {
case FormatJSON:
WriteJSON(api.MultipleNodeResponse{Items: nodes})
Expand All @@ -133,7 +133,7 @@ func WriteNodes(nodes []utils.NodeExternal) {
}
}

func writeNodeTable(nodes []utils.NodeExternal) {
func writeNodeTable(nodes []models.NodeExternal) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name"})

Expand All @@ -146,7 +146,7 @@ func writeNodeTable(nodes []utils.NodeExternal) {
table.Render()
}

func writeWideNodeTable(nodes []utils.NodeExternal) {
func writeWideNodeTable(nodes []models.NodeExternal) {
table := tablewriter.NewWriter(os.Stdout)

header := []string{
Expand Down Expand Up @@ -175,7 +175,7 @@ func writeWideNodeTable(nodes []utils.NodeExternal) {
table.Render()
}

func writeNodeNames(nodes []utils.NodeExternal) {
func writeNodeNames(nodes []models.NodeExternal) {
for _, n := range nodes {
fmt.Println(n.Name)
}
Expand Down
16 changes: 8 additions & 8 deletions cli/cmd/get_publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/netapp/trident/config"
"github.com/netapp/trident/frontend/rest"
. "github.com/netapp/trident/logging"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/models"
)

var (
Expand Down Expand Up @@ -64,7 +64,7 @@ var getPublicationCmd = &cobra.Command{
},
}

func GetVolumePublication(volumeName, nodeName string) (*utils.VolumePublicationExternal, error) {
func GetVolumePublication(volumeName, nodeName string) (*models.VolumePublicationExternal, error) {
var err error

url := BaseURL() + "/publication/" + volumeName + "/" + nodeName
Expand Down Expand Up @@ -93,7 +93,7 @@ func volumePublicationGet() error {
return err
}

pubs := make([]utils.VolumePublicationExternal, 0, 1)
pubs := make([]models.VolumePublicationExternal, 0, 1)
pubs = append(pubs, *pub)

WriteVolumePublications(pubs)
Expand Down Expand Up @@ -125,7 +125,7 @@ func volumePublicationsList(cmd *cobra.Command) error {
return err
}

pubs := make([]utils.VolumePublicationExternal, 0, len(listPublicationsResponse.VolumePublications))
pubs := make([]models.VolumePublicationExternal, 0, len(listPublicationsResponse.VolumePublications))
for _, pub := range listPublicationsResponse.VolumePublications {
pubs = append(pubs, *pub)
}
Expand All @@ -135,7 +135,7 @@ func volumePublicationsList(cmd *cobra.Command) error {
return nil
}

func WriteVolumePublications(pubs []utils.VolumePublicationExternal) {
func WriteVolumePublications(pubs []models.VolumePublicationExternal) {
switch OutputFormat {
case FormatJSON:
WriteJSON(api.MultipleVolumePublicationResponse{Items: pubs})
Expand All @@ -150,7 +150,7 @@ func WriteVolumePublications(pubs []utils.VolumePublicationExternal) {
}
}

func writeVolumePublicationTable(pubs []utils.VolumePublicationExternal) {
func writeVolumePublicationTable(pubs []models.VolumePublicationExternal) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Node", "Volume"})

Expand All @@ -164,7 +164,7 @@ func writeVolumePublicationTable(pubs []utils.VolumePublicationExternal) {
table.Render()
}

func writeWideVolumePublicationTable(pubs []utils.VolumePublicationExternal) {
func writeWideVolumePublicationTable(pubs []models.VolumePublicationExternal) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Node", "Volume", "ReadOnly", "Dirty", "AccessMode"})

Expand All @@ -181,7 +181,7 @@ func writeWideVolumePublicationTable(pubs []utils.VolumePublicationExternal) {
table.Render()
}

func writeVolumePublicationNames(pubs []utils.VolumePublicationExternal) {
func writeVolumePublicationNames(pubs []models.VolumePublicationExternal) {
for _, pub := range pubs {
fmt.Println(pub.Name)
}
Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/update_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/netapp/trident/frontend/rest"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/models"
)

var (
Expand Down Expand Up @@ -84,7 +85,7 @@ var updateNodeCmd = &cobra.Command{
}

func nodeUpdate(nodeName string, orchestratorReady, administratorReady, provisionerReady *bool) error {
nodeFlags := utils.NodePublicationStateFlags{
nodeFlags := models.NodePublicationStateFlags{
OrchestratorReady: orchestratorReady,
AdministratorReady: administratorReady,
ProvisionerReady: provisionerReady,
Expand Down Expand Up @@ -124,7 +125,7 @@ func nodeUpdate(nodeName string, orchestratorReady, administratorReady, provisio
return fmt.Errorf("node was empty")
}

nodes := make([]utils.NodeExternal, 0, 1)
nodes := make([]models.NodeExternal, 0, 1)
nodes = append(nodes, *node)
WriteNodes(nodes)
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/update_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/netapp/trident/cli/api"
"github.com/netapp/trident/frontend/rest"
"github.com/netapp/trident/storage"
"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/errors"
"github.com/netapp/trident/utils/models"
)

var (
Expand Down Expand Up @@ -95,7 +95,7 @@ func updateVolume(volumeName, snapDirValue string, poolLevelVal bool) error {

snapDirBool, _ := strconv.ParseBool(snapDirValue)

request := utils.VolumeUpdateInfo{
request := models.VolumeUpdateInfo{
SnapshotDirectory: strconv.FormatBool(snapDirBool),
PoolLevel: poolLevelVal,
}
Expand Down
14 changes: 7 additions & 7 deletions core/cache/node_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package cache
import (
"sync"

"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/models"
)

type NodeCache struct {
nodes map[string]*utils.Node
nodes map[string]*models.Node
m *sync.RWMutex
}

func NewNodeCache() *NodeCache {
return &NodeCache{
nodes: make(map[string]*utils.Node),
nodes: make(map[string]*models.Node),
m: &sync.RWMutex{},
}
}

// Get returns a node by name, or nil if the node is not found
func (nc *NodeCache) Get(name string) *utils.Node {
func (nc *NodeCache) Get(name string) *models.Node {
nc.m.RLock()
defer nc.m.RUnlock()

Expand All @@ -31,7 +31,7 @@ func (nc *NodeCache) Get(name string) *utils.Node {
}

// Set adds or updates node in cache. Set should only be used with the global lock.
func (nc *NodeCache) Set(name string, node *utils.Node) {
func (nc *NodeCache) Set(name string, node *models.Node) {
nc.m.Lock()
defer nc.m.Unlock()

Expand All @@ -48,11 +48,11 @@ func (nc *NodeCache) Delete(name string) {
}

// List returns nodes in cache as an unordered slice that is safe to modify.
func (nc *NodeCache) List() []*utils.Node {
func (nc *NodeCache) List() []*models.Node {
nc.m.RLock()
defer nc.m.RUnlock()

l := make([]*utils.Node, 0, len(nc.nodes))
l := make([]*models.Node, 0, len(nc.nodes))
for _, v := range nc.nodes {
l = append(l, v.Copy())
}
Expand Down
36 changes: 18 additions & 18 deletions core/cache/node_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

"github.com/stretchr/testify/assert"

"github.com/netapp/trident/utils"
"github.com/netapp/trident/utils/models"
)

func TestNodeCacheSetAndGet(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
node2 := &utils.Node{
node2 := &models.Node{
Name: "node2",
}
cache := NewNodeCache()
Expand All @@ -26,7 +26,7 @@ func TestNodeCacheSetAndGet(t *testing.T) {
}

func TestNodeCacheLen(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
cache := NewNodeCache()
Expand All @@ -37,23 +37,23 @@ func TestNodeCacheLen(t *testing.T) {
}

func TestNodeCacheList(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
node2 := &utils.Node{
node2 := &models.Node{
Name: "node2",
}
cache := NewNodeCache()
assert.Empty(t, cache.List())

cache.Set(node1.Name, node1)
cache.Set(node2.Name, node2)
assert.ElementsMatch(t, []*utils.Node{node1, node2}, cache.List())
assert.ElementsMatch(t, []*models.Node{node1, node2}, cache.List())
}

// TestNodeCacheDelete tests delete removes entry and is idempotent
func TestNodeCacheDelete(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
cache := NewNodeCache()
Expand All @@ -72,7 +72,7 @@ func TestNodeCacheDelete(t *testing.T) {
// 2. Get node
// 3. See no deadlock
func TestNodeCacheGetUnblocked(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
cache := NewNodeCache()
Expand All @@ -99,31 +99,31 @@ func TestNodeCacheAccessorsWaitForWriteLock(t *testing.T) {

tests := []struct {
name string
accessor func(cache *NodeCache, node *utils.Node)
accessor func(cache *NodeCache, node *models.Node)
}{
{
name: "Get",
accessor: func(cache *NodeCache, node *utils.Node) {
accessor: func(cache *NodeCache, node *models.Node) {
_ = cache.Get(node.Name)
},
},
{
name: "Len",
accessor: func(cache *NodeCache, node *utils.Node) {
accessor: func(cache *NodeCache, node *models.Node) {
_ = cache.Len()
},
},
{
name: "List",
accessor: func(cache *NodeCache, node *utils.Node) {
accessor: func(cache *NodeCache, node *models.Node) {
_ = cache.List()
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
cache := NewNodeCache()
Expand Down Expand Up @@ -155,25 +155,25 @@ func TestNodeCacheMutatorsWaitForReadLock(t *testing.T) {

tests := []struct {
name string
mutator func(cache *NodeCache, node *utils.Node)
mutator func(cache *NodeCache, node *models.Node)
}{
{
name: "Set",
mutator: func(cache *NodeCache, node *utils.Node) {
mutator: func(cache *NodeCache, node *models.Node) {
cache.Set(node.Name, node)
},
},
{
name: "Delete",
mutator: func(cache *NodeCache, node *utils.Node) {
mutator: func(cache *NodeCache, node *models.Node) {
cache.Delete(node.Name)
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
node1 := &utils.Node{
node1 := &models.Node{
Name: "node1",
}
cache := NewNodeCache()
Expand Down
Loading

0 comments on commit 87bba02

Please sign in to comment.