Move configuration errors to config module

Change-Id: I878ba176f72db3dba587478f17290991b0f5b3d3
This commit is contained in:
Dmitry Ukov 2020-01-13 11:02:47 +04:00
parent 48e14c3b55
commit 7089223607
7 changed files with 39 additions and 44 deletions

View File

@ -24,7 +24,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
conferrors "opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/log"
)
@ -119,7 +118,7 @@ func runSetCluster(o *config.ClusterOptions, rootSettings *environment.AirshipCT
airconfig := rootSettings.Config()
cluster, err := airconfig.GetCluster(o.Name, o.ClusterType)
if err != nil {
var cerr conferrors.ErrMissingConfig
var cerr config.ErrMissingConfig
if !errors.As(err, &cerr) {
// An error occurred, but it wasn't a "missing" config error.
return clusterWasModified, err

View File

@ -24,7 +24,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
conferrors "opendev.org/airship/airshipctl/pkg/errors"
)
var (
@ -106,14 +105,14 @@ func runSetContext(o *config.ContextOptions, airconfig *config.Config) (bool, er
contextIWant := o.Name
context, err := airconfig.GetContext(contextIWant)
if err != nil {
var cerr conferrors.ErrMissingConfig
var cerr config.ErrMissingConfig
if !errors.As(err, &cerr) {
// An error occurred, but it wasn't a "missing" config error.
return contextWasModified, err
}
if o.CurrentContext {
return contextWasModified, conferrors.ErrMissingConfig{}
return contextWasModified, config.ErrMissingConfig{}
}
// context didn't exist, create it
// ignoring the returned added context
@ -132,7 +131,7 @@ func runSetContext(o *config.ContextOptions, airconfig *config.Config) (bool, er
// Update configuration file just in time persistence approach
if err := airconfig.PersistConfig(); err != nil {
// Error that it didnt persist the changes
return contextWasModified, conferrors.ErrConfigFailed{}
return contextWasModified, config.ErrConfigFailed{}
}
return contextWasModified, nil

View File

@ -12,7 +12,6 @@ import (
"opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/util"
@ -45,7 +44,7 @@ func GenerateBootstrapIso(settings *environment.AirshipCTLSettings, args []strin
// TODO (dukov) This check should be implemented as part of the config module
if manifest == nil {
return errors.ErrMissingConfig{What: "manifest for currnet context not found"}
return config.ErrMissingConfig{What: "manifest for currnet context not found"}
}
if err = verifyInputs(cfg); err != nil {
@ -78,12 +77,12 @@ func GenerateBootstrapIso(settings *environment.AirshipCTLSettings, args []strin
func verifyInputs(cfg *config.Bootstrap) error {
if cfg.Container.Volume == "" {
log.Print("Specify volume bind for ISO builder container")
return errors.ErrWrongConfig{}
return config.ErrWrongConfig{}
}
if (cfg.Builder.UserDataFileName == "") || (cfg.Builder.NetworkConfigFileName == "") {
log.Print("UserDataFileName or NetworkConfigFileName are not specified in ISO builder config")
return errors.ErrWrongConfig{}
return config.ErrWrongConfig{}
}
vols := strings.Split(cfg.Container.Volume, ":")
@ -92,7 +91,7 @@ func verifyInputs(cfg *config.Bootstrap) error {
cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0])
case len(vols) > 2:
log.Print("Bad container volume format. Use hostPath:contPath")
return errors.ErrWrongConfig{}
return config.ErrWrongConfig{}
}
return nil
}

View File

@ -12,7 +12,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/testutil"
)
@ -133,7 +132,7 @@ func TestVerifyInputs(t *testing.T) {
cfg: &config.Bootstrap{
Container: &config.Container{},
},
expectedErr: errors.ErrWrongConfig{},
expectedErr: config.ErrWrongConfig{},
},
{
cfg: &config.Bootstrap{
@ -142,7 +141,7 @@ func TestVerifyInputs(t *testing.T) {
},
Builder: &config.Builder{},
},
expectedErr: errors.ErrWrongConfig{},
expectedErr: config.ErrWrongConfig{},
},
{
cfg: &config.Bootstrap{
@ -166,7 +165,7 @@ func TestVerifyInputs(t *testing.T) {
NetworkConfigFileName: "net-conf",
},
},
expectedErr: errors.ErrWrongConfig{},
expectedErr: config.ErrWrongConfig{},
},
}

View File

@ -32,7 +32,6 @@ import (
kubeconfig "k8s.io/client-go/tools/clientcmd/api"
conferrors "opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/util"
)
@ -391,12 +390,12 @@ func (c *Config) ContextNames() []string {
func (c *Config) GetCluster(cName, cType string) (*Cluster, error) {
_, exists := c.Clusters[cName]
if !exists {
return nil, conferrors.ErrMissingConfig{What: fmt.Sprintf("Cluster with name '%s' of type '%s'", cName, cType)}
return nil, ErrMissingConfig{What: fmt.Sprintf("Cluster with name '%s' of type '%s'", cName, cType)}
}
// Alternative to this would be enhance Cluster.String() to embedd the appropriate kubeconfig cluster information
cluster, exists := c.Clusters[cName].ClusterTypes[cType]
if !exists {
return nil, conferrors.ErrMissingConfig{What: fmt.Sprintf("Cluster with name '%s' of type '%s'", cName, cType)}
return nil, ErrMissingConfig{What: fmt.Sprintf("Cluster with name '%s' of type '%s'", cName, cType)}
}
return cluster, nil
}
@ -490,7 +489,7 @@ func (c *Config) GetClusters() ([]*Cluster, error) {
func (c *Config) GetContext(cName string) (*Context, error) {
context, exists := c.Contexts[cName]
if !exists {
return nil, conferrors.ErrMissingConfig{What: fmt.Sprintf("Context with name '%s'", cName)}
return nil, ErrMissingConfig{What: fmt.Sprintf("Context with name '%s'", cName)}
}
return context, nil
}

View File

@ -13,3 +13,28 @@ type ErrBootstrapInfoNotFound struct {
func (e ErrBootstrapInfoNotFound) Error() string {
return fmt.Sprintf("Bootstrap info %s not found", e.Name)
}
// ErrWrongConfig returned in case of incorrect configuration
type ErrWrongConfig struct {
}
func (e ErrWrongConfig) Error() string {
return "Wrong configuration"
}
// ErrMissingConfig returned in case of missing configuration
type ErrMissingConfig struct {
What string
}
func (e ErrMissingConfig) Error() string {
return "Missing configuration: " + e.What
}
// ErrConfigFailed returned in case of failure during configuration
type ErrConfigFailed struct {
}
func (e ErrConfigFailed) Error() string {
return "Configuration failed to complete."
}

View File

@ -20,28 +20,3 @@ type ErrNotImplemented struct {
func (e ErrNotImplemented) Error() string {
return "Not implemented"
}
// ErrWrongConfig returned in case of incorrect configuration
type ErrWrongConfig struct {
}
func (e ErrWrongConfig) Error() string {
return "Wrong configuration"
}
// ErrMissingConfig returned in case of missing configuration
type ErrMissingConfig struct {
What string
}
func (e ErrMissingConfig) Error() string {
return "Missing configuration: " + e.What
}
// ErrConfigFailed returned in case of failure during configuration
type ErrConfigFailed struct {
}
func (e ErrConfigFailed) Error() string {
return "Configuration failed to complete."
}