github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/lxdprofile/validate.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxdprofile
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	apicharms "github.com/juju/juju/api/charms"
     9  	charm "gopkg.in/juju/charm.v6"
    10  )
    11  
    12  //go:generate mockgen -package lxdprofile_test -destination lxdprofile_mock_test.go gopkg.in/juju/charm.v6 LXDProfiler
    13  
    14  // ValidateCharmLXDProfile will attempt to validate a charm.Charm
    15  // lxd profile. The LXDProfile is an optional method on the charm.Charm, so
    16  // testing to check that it conforms to a LXDProfiler first is required.
    17  // Failure to conform to the LXDProfiler will return no error.
    18  func ValidateCharmLXDProfile(ch charm.Charm) error {
    19  	// Check if the charm conforms to the LXDProfiler, as it's optional and in
    20  	// theory the charm.Charm doesn't have to provide a LXDProfile method we
    21  	// can ignore it if it's missing and assume it is therefore valid.
    22  	if profiler, ok := ch.(charm.LXDProfiler); ok {
    23  		return ValidateLXDProfile(profiler)
    24  	}
    25  	return nil
    26  }
    27  
    28  // ValidateLXDProfile will validate the profile to determin if the configuration
    29  // is valid or not before passing continuing on.
    30  func ValidateLXDProfile(profiler charm.LXDProfiler) error {
    31  	// Profile from the api could be nil, so check that it isn't
    32  	if profile := profiler.LXDProfile(); profile != nil {
    33  		err := profile.ValidateConfigDevices()
    34  		return errors.Trace(err)
    35  	}
    36  	return nil
    37  }
    38  
    39  // ValidateCharmInfoLXDProfile will validate the charm info to determin if the
    40  // information provided is valid or not.
    41  func ValidateCharmInfoLXDProfile(info *apicharms.CharmInfo) error {
    42  	if profile := info.LXDProfile; profile != nil {
    43  		err := profile.ValidateConfigDevices()
    44  		return errors.Trace(err)
    45  	}
    46  	return nil
    47  }