|
| 1 | +package sriovfec |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/golang/glog" |
| 8 | + "github.com/openshift-kni/eco-goinfra/pkg/clients" |
| 9 | + sriovfectypes "github.com/openshift-kni/eco-goinfra/pkg/schemes/fec/fectypes" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | +) |
| 12 | + |
| 13 | +// List returns SriovFecNodeConfigList from given namespace. |
| 14 | +func List(apiClient *clients.Settings, nsname string, options ...client.ListOptions) ([]*NodeConfigBuilder, error) { |
| 15 | + if apiClient == nil { |
| 16 | + glog.V(100).Infof("SriovFecNodeConfigList 'apiClient' parameter can not be empty") |
| 17 | + |
| 18 | + return nil, fmt.Errorf("failed to list SriovFecNodeConfig, 'apiClient' parameter is empty") |
| 19 | + } |
| 20 | + |
| 21 | + err := apiClient.AttachScheme(sriovfectypes.AddToScheme) |
| 22 | + if err != nil { |
| 23 | + glog.V(100).Infof("Failed to add sriov-fec scheme to client schemes") |
| 24 | + |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + if nsname == "" { |
| 29 | + glog.V(100).Infof("SriovFecNodeConfigList 'nsname' parameter can not be empty") |
| 30 | + |
| 31 | + return nil, fmt.Errorf("failed to list SriovFecNodeConfig, 'nsname' parameter is empty") |
| 32 | + } |
| 33 | + |
| 34 | + passedOptions := client.ListOptions{} |
| 35 | + logMessage := fmt.Sprintf("Listing SriovFecNodeConfig in the namespace %s", nsname) |
| 36 | + |
| 37 | + if len(options) > 1 { |
| 38 | + glog.V(100).Infof("'options' parameter must be empty or single-valued") |
| 39 | + |
| 40 | + return nil, fmt.Errorf("error: more than one ListOptions was passed") |
| 41 | + } |
| 42 | + |
| 43 | + if len(options) == 1 { |
| 44 | + passedOptions = options[0] |
| 45 | + logMessage += fmt.Sprintf(" with the options %v", passedOptions) |
| 46 | + } |
| 47 | + |
| 48 | + glog.V(100).Infof(logMessage) |
| 49 | + |
| 50 | + sfncList := new(sriovfectypes.SriovFecNodeConfigList) |
| 51 | + err = apiClient.List(context.TODO(), sfncList, &passedOptions) |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + glog.V(100).Infof("Failed to list SriovFecNodeConfigs in the namespace %s due to %s", nsname, err.Error()) |
| 55 | + |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + var sfncBuilderList []*NodeConfigBuilder |
| 60 | + |
| 61 | + for _, sfnc := range sfncList.Items { |
| 62 | + copiedObject := sfnc |
| 63 | + sfncBuilder := &NodeConfigBuilder{ |
| 64 | + apiClient: apiClient.Client, |
| 65 | + Object: &copiedObject, |
| 66 | + Definition: &copiedObject, |
| 67 | + } |
| 68 | + |
| 69 | + sfncBuilderList = append(sfncBuilderList, sfncBuilder) |
| 70 | + } |
| 71 | + |
| 72 | + return sfncBuilderList, nil |
| 73 | +} |
0 commit comments