-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nad pkg: nad list option added (#775)
Co-authored-by: Elena German <[email protected]>
- Loading branch information
1 parent
8e2c2da
commit 983ef91
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |