/
model.go
78 lines (67 loc) · 1.83 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package model
import (
"github.com/juju/charm/v8"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/os"
"github.com/juju/os/series"
"github.com/juju/systems"
)
// ModelType indicates a model type.
type ModelType string
const (
// IAAS is the type for IAAS models.
IAAS ModelType = "iaas"
// CAAS is the type for CAAS models.
CAAS ModelType = "caas"
)
// String returns m as a string.
func (m ModelType) String() string {
return string(m)
}
// Model represents the state of a model.
type Model struct {
// Name returns the human friendly name of the model.
Name string
// UUID is the universally unique identifier of the model.
UUID string
// ModelType is the type of model.
ModelType ModelType
}
var caasOS = set.NewStrings(os.Kubernetes.String())
// ValidateSeries ensures the charm series is valid for the model type.
func ValidateSeries(modelType ModelType, charmSeries string, charmFormat charm.Format) error {
if charmFormat >= charm.FormatV2 {
system, err := systems.ParseSystemFromSeries(charmSeries)
if err != nil {
return errors.Trace(err)
}
if system.Resource != "" {
switch modelType {
case CAAS:
// CAAS models support using a resource as the system.
return nil
case IAAS:
return errors.NotValidf("IAAS models don't support systems referencing a resource")
}
}
} else {
os, err := series.GetOSFromSeries(charmSeries)
if err != nil {
return errors.Trace(err)
}
switch modelType {
case CAAS:
if !caasOS.Contains(os.String()) {
return errors.NotValidf("series %q in a kubernetes model", charmSeries)
}
case IAAS:
if caasOS.Contains(os.String()) {
return errors.NotValidf("series %q in a non container model", charmSeries)
}
}
}
return nil
}