-
Notifications
You must be signed in to change notification settings - Fork 9
Add distribution validation with structured error handling #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
008f7fd
151c9da
2703009
a9234f5
68876f3
7e3ef1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package imagecustomizerapi | ||
|
||
const ( | ||
DistroNameAzureLinux string = "azurelinux" | ||
DistroNameFedora string = "fedora" | ||
) | ||
|
||
func GetSupportedDistros() map[string][]string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image Customizer and Image Creator will likely have different support lists. So, it would be good to give this a name unique to Image Creator. One option would be to create an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. for now, this applies to both tools. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fedora isn't supported in Image Customizer yet. Also, the |
||
// supportedDistros defines valid distribution and version combinations | ||
return map[string][]string{ | ||
DistroNameAzureLinux: {"2.0", "3.0"}, | ||
DistroNameFedora: {"42"}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package imagecreatorlib | ||
|
||
import ( | ||
"fmt" | ||
"maps" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi" | ||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/pkg/imagecustomizerlib" | ||
) | ||
|
||
var ( | ||
// ErrUnsupportedDistribution indicates an unsupported Linux distribution | ||
ErrUnsupportedDistribution = imagecustomizerlib.NewImageCustomizerError("Distribution:UnsupportedDistribution", | ||
"unsupported distro") | ||
|
||
// ErrUnsupportedVersion indicates an unsupported version for a given distribution | ||
ErrUnsupportedVersion = imagecustomizerlib.NewImageCustomizerError("Distribution:UnsupportedVersion", | ||
"unsupported distro-version") | ||
) | ||
|
||
// distribution represents a supported Linux distribution and version combination | ||
type Distribution struct { | ||
Name string | ||
Version string | ||
} | ||
|
||
// Validate ensures the distribution and version combination is supported | ||
func (d *Distribution) Validate() error { | ||
// Get all supported distributions and their versions | ||
supportedDistros := imagecustomizerapi.GetSupportedDistros() | ||
|
||
// Check if the distribution is supported | ||
validVersions, exists := supportedDistros[d.Name] | ||
if !exists { | ||
// Get list of supported distributions | ||
distros := slices.Collect(maps.Keys(supportedDistros)) | ||
himaja-kesari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fmt.Errorf("%w (%q)\nsupported distributions are: (%s)", | ||
ErrUnsupportedDistribution, d.Name, strings.Join(distros, ", ")) | ||
} | ||
|
||
// Check if the version is supported for this distribution | ||
if slices.Contains(validVersions, d.Version) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%w (%q)\nsupported versions for %q distro are: (%s)", | ||
ErrUnsupportedVersion, d.Version, d.Name, strings.Join(validVersions, ", ")) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.