Skip to content

Commit

Permalink
nad pkg: nad list option added (#775)
Browse files Browse the repository at this point in the history
Co-authored-by: Elena German <[email protected]>
  • Loading branch information
elenagerman and Elena German authored Nov 14, 2024
1 parent 8e2c2da commit 983ef91
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/nad/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nad

import (
"context"
"fmt"

"github.com/golang/glog"
nadV1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// List returns NADs inventory in the given namespace.
func List(apiClient *clients.Settings, nsname string) ([]*Builder, error) {
if apiClient == nil {
glog.V(100).Infof("The apiClient is empty")

return nil, fmt.Errorf("nadList 'apiClient' cannot be empty")
}

err := apiClient.AttachScheme(nadV1.AddToScheme)
if err != nil {
glog.V(100).Infof("Failed to add nad v1 scheme to client schemes")

return nil, fmt.Errorf("failed to add nad v1 scheme to client schemes")
}

if nsname == "" {
glog.V(100).Infof("nad 'nsname' parameter can not be empty")

return nil, fmt.Errorf("failed to list NADs, 'nsname' parameter is empty")
}

logMessage := fmt.Sprintf("Listing NADs in the nsname %s", nsname)

glog.V(100).Infof(logMessage)

nadList := &nadV1.NetworkAttachmentDefinitionList{}

err = apiClient.List(context.TODO(), nadList, &goclient.ListOptions{Namespace: nsname})

if err != nil {
glog.V(100).Infof("Failed to list NADs in namespace: %s due to %s",
nsname, err.Error())

return nil, err
}

var nadObjects []*Builder

for _, nadObj := range nadList.Items {
networkAttachmentDefinition := nadObj
nadBuilder := &Builder{
apiClient: apiClient.Client,
Definition: &networkAttachmentDefinition,
Object: &networkAttachmentDefinition,
}

nadObjects = append(nadObjects, nadBuilder)
}

return nadObjects, err
}
57 changes: 57 additions & 0 deletions pkg/nad/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package nad

import (
"fmt"
"testing"

"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/stretchr/testify/assert"
)

func TestListNad(t *testing.T) {
testCases := []struct {
nad []*Builder
nsName string
expectedError error
client bool
}{
{
nad: []*Builder{
buildValidNADNetworkTestBuilder(buildTestClientWithDummyObject())},
nsName: "nadnamespace",
expectedError: nil,
client: true,
},
{
nad: []*Builder{
buildValidNADNetworkTestBuilder(buildTestClientWithDummyObject())},
nsName: "",
expectedError: fmt.Errorf("failed to list NADs, 'nsname' parameter is empty"),
client: true,
},
{
nad: []*Builder{
buildValidNADNetworkTestBuilder(buildTestClientWithDummyObject())},
nsName: "nadnamespace",
expectedError: fmt.Errorf("nadList 'apiClient' cannot be empty"),
client: false,
},
}
for _, testCase := range testCases {
var testSettings *clients.Settings

if testCase.client {
testSettings = clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: buildDummyNADNetworkObject(),
SchemeAttachers: testSchemes,
})
}

nadBuilders, err := List(testSettings, testCase.nsName)
assert.Equal(t, testCase.expectedError, err)

if testCase.expectedError == nil {
assert.Equal(t, len(nadBuilders), len(testCase.nad))
}
}
}

0 comments on commit 983ef91

Please sign in to comment.