github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/instance/container.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package instance
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  type ContainerType string
    11  
    12  const (
    13  	NONE = ContainerType("none")
    14  	LXC  = ContainerType("lxc")
    15  	KVM  = ContainerType("kvm")
    16  )
    17  
    18  // ContainerTypes is used to validate add-machine arguments.
    19  var ContainerTypes []ContainerType = []ContainerType{
    20  	LXC,
    21  	KVM,
    22  }
    23  
    24  // ParseContainerTypeOrNone converts the specified string into a supported
    25  // ContainerType instance or returns an error if the container type is invalid.
    26  // For this version of the function, 'none' is a valid value.
    27  func ParseContainerTypeOrNone(ctype string) (ContainerType, error) {
    28  	if ContainerType(ctype) == NONE {
    29  		return NONE, nil
    30  	}
    31  	return ParseContainerType(ctype)
    32  }
    33  
    34  // ParseContainerType converts the specified string into a supported
    35  // ContainerType instance or returns an error if the container type is invalid.
    36  func ParseContainerType(ctype string) (ContainerType, error) {
    37  	for _, supportedType := range ContainerTypes {
    38  		if ContainerType(ctype) == supportedType {
    39  			return supportedType, nil
    40  		}
    41  	}
    42  	return "", fmt.Errorf("invalid container type %q", ctype)
    43  }