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

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/os"
     9  	"github.com/juju/os/series"
    10  	"github.com/juju/utils/set"
    11  )
    12  
    13  // ModelType indicates a model type.
    14  type ModelType string
    15  
    16  const (
    17  	// IAAS is the type for IAAS models.
    18  	IAAS ModelType = "iaas"
    19  
    20  	// CAAS is the type for CAAS models.
    21  	CAAS ModelType = "caas"
    22  )
    23  
    24  // String returns m as a string.
    25  func (m ModelType) String() string {
    26  	return string(m)
    27  }
    28  
    29  // Model represents the state of a model.
    30  type Model struct {
    31  	// Name returns the human friendly name of the model.
    32  	Name string
    33  
    34  	// UUID is the universally unique identifier of the model.
    35  	UUID string
    36  
    37  	// ModelType is the type of model.
    38  	ModelType ModelType
    39  }
    40  
    41  var caasOS = set.NewStrings(os.Kubernetes.String())
    42  
    43  // ValidateSeries ensures the charm series is valid for the model type.
    44  func ValidateSeries(modelType ModelType, charmSeries string) error {
    45  	os, err := series.GetOSFromSeries(charmSeries)
    46  	if err != nil {
    47  		return errors.Trace(err)
    48  	}
    49  	switch modelType {
    50  	case CAAS:
    51  		if !caasOS.Contains(os.String()) {
    52  			return errors.NotValidf("series %q in a kubernetes model", charmSeries)
    53  		}
    54  	case IAAS:
    55  		if caasOS.Contains(os.String()) {
    56  			return errors.NotValidf("series %q in a non container model", charmSeries)
    57  		}
    58  	}
    59  	return nil
    60  }