forked from juju/juju
/
instancetypes.go
65 lines (57 loc) · 1.87 KB
/
instancetypes.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"github.com/juju/errors"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/instances"
)
func toParamsInstanceTypeResult(itypes []instances.InstanceType) []params.InstanceType {
result := make([]params.InstanceType, len(itypes))
for i, t := range itypes {
virtType := ""
if t.VirtType != nil {
virtType = *t.VirtType
}
result[i] = params.InstanceType{
Name: t.Name,
Arches: t.Arches,
CPUCores: int(t.CpuCores),
Memory: int(t.Mem),
RootDiskSize: int(t.RootDisk),
VirtType: virtType,
Deprecated: t.Deprecated,
Cost: int(t.Cost),
}
}
return result
}
// NewInstanceTypeConstraints returns an instanceTypeConstraints with the passed
// parameters.
func NewInstanceTypeConstraints(env environs.Environ, constraints constraints.Value) instanceTypeConstraints {
return instanceTypeConstraints{
environ: env,
constraints: constraints,
}
}
// instanceTypeConstraints holds necesary params to filter instance types.
type instanceTypeConstraints struct {
constraints constraints.Value
environ environs.Environ
}
// InstanceTypes returns a list of the available instance types in the provider according
// to the passed constraints.
func InstanceTypes(cons instanceTypeConstraints) (params.InstanceTypesResult, error) {
instanceTypes, err := cons.environ.InstanceTypes(cons.constraints)
if err != nil {
return params.InstanceTypesResult{}, errors.Trace(err)
}
return params.InstanceTypesResult{
InstanceTypes: toParamsInstanceTypeResult(instanceTypes.InstanceTypes),
CostUnit: instanceTypes.CostUnit,
CostCurrency: instanceTypes.CostCurrency,
CostDivisor: instanceTypes.CostDivisor,
}, nil
}