Skip to content

Commit

Permalink
OVA: Change the scanning for ova and ovf files inside the NFS directory
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed Sep 6, 2023
1 parent a1671ed commit df4831a
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions cmd/ova-provider-server/ova-provider-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
const (
invalidRequestMethodMsg = "Invalid request method"
errorProcessingOvfMsg = "Error processing OVF file"
OvaExt = ".ova"
OvfExt = ".ovf"
)

// xml struct
Expand Down Expand Up @@ -299,28 +301,56 @@ func findOVAFiles(directory string) ([]string, []string, error) {
}

var ovaFiles, ovfFiles []string

// Check for .ova files in the main directory (zero level)
for _, child := range childs {
path := filepath.Join(directory, child.Name())
if hasSuffixIgnoreCase(child.Name(), OvaExt) {
ovaFiles = append(ovaFiles, path)
}
}

// Check for .ova and .ovf files in child directories (first level)
for _, child := range childs {
if !child.IsDir() {
continue
}
newDir := directory + "/" + child.Name()
newDir := filepath.Join(directory, child.Name())
files, err := os.ReadDir(newDir)
if err != nil {
return nil, nil, err
}
for _, file := range files {
path := filepath.Join(directory, child.Name(), file.Name())
path := filepath.Join(newDir, file.Name())
switch {
case strings.HasSuffix(strings.ToLower(file.Name()), ".ova"):
case hasSuffixIgnoreCase(file.Name(), OvaExt):
ovaFiles = append(ovaFiles, path)
case strings.HasSuffix(strings.ToLower(file.Name()), ".ovf"):
case hasSuffixIgnoreCase(file.Name(), OvfExt):
ovfFiles = append(ovfFiles, path)
case file.IsDir():
// Check for .ovf files in subdirectories (second level)
subDir := filepath.Join(newDir, file.Name())
subFiles, err := os.ReadDir(subDir)
if err != nil {
continue
}
for _, subFile := range subFiles {
subPath := filepath.Join(subDir, subFile.Name())
if hasSuffixIgnoreCase(subFile.Name(), OvfExt) {
ovfFiles = append(ovfFiles, subPath)
}
}
}
}
}
return ovaFiles, ovfFiles, nil
}

// Checks if the given file has the desired extension
func hasSuffixIgnoreCase(fileName, suffix string) bool {
return strings.HasSuffix(strings.ToLower(fileName), strings.ToLower(suffix))
}

func readOVFFromOVA(ovaFile string) (*Envelope, error) {
var envelope Envelope
file, err := os.Open(ovaFile)
Expand Down

0 comments on commit df4831a

Please sign in to comment.