forked from juju/juju
/
environ_policy.go
93 lines (78 loc) · 2.46 KB
/
environ_policy.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build !gccgo
package vsphere
import (
"github.com/juju/errors"
"github.com/juju/utils/set"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/environs/simplestreams"
)
// PrecheckInstance verifies that the provided series and constraints
// are valid for use in creating an instance in this environment.
func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
if placement != "" {
if _, err := env.parsePlacement(placement); err != nil {
return err
}
}
return nil
}
// supportedArchitectures returns the image architectures which can
// be hosted by this environment.
func (env *environ) allSupportedArchitectures() ([]string, error) {
env.archLock.Lock()
defer env.archLock.Unlock()
if env.supportedArchitectures != nil {
return env.supportedArchitectures, nil
}
archList, err := env.lookupArchitectures()
if err != nil {
return nil, errors.Trace(err)
}
env.supportedArchitectures = archList
return archList, nil
}
func (env *environ) lookupArchitectures() ([]string, error) {
// Create a filter to get all images for the
// correct stream.
imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
Stream: env.Config().ImageStream(),
})
sources, err := environs.ImageMetadataSources(env)
if err != nil {
return nil, errors.Trace(err)
}
matchingImages, err := imageMetadataFetch(sources, imageConstraint)
if err != nil {
return nil, errors.Trace(err)
}
var arches = set.NewStrings()
for _, im := range matchingImages {
arches.Add(im.Arch)
}
return arches.Values(), nil
}
var unsupportedConstraints = []string{
constraints.Tags,
constraints.VirtType,
}
// ConstraintsValidator returns a Validator value which is used to
// validate and merge constraints.
func (env *environ) ConstraintsValidator() (constraints.Validator, error) {
validator := constraints.NewValidator()
validator.RegisterUnsupported(unsupportedConstraints)
supportedArches, err := env.allSupportedArchitectures()
if err != nil {
return nil, errors.Trace(err)
}
validator.RegisterVocabulary(constraints.Arch, supportedArches)
return validator, nil
}
// SupportNetworks returns whether the environment has support to
// specify networks for applications and machines.
func (env *environ) SupportNetworks() bool {
return false
}