-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
124 lines (110 loc) · 3.5 KB
/
models.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient
import (
"io/ioutil"
"os"
"github.com/juju/errors"
"github.com/juju/utils"
"gopkg.in/yaml.v2"
"github.com/juju/juju/juju/osenv"
)
// JujuModelsPath is the location where models information is
// expected to be found.
func JujuModelsPath() string {
// TODO(axw) models.yaml should go into XDG_CACHE_HOME.
return osenv.JujuXDGDataHomePath("models.yaml")
}
// ReadModelsFile loads all models defined in a given file.
// If the file is not found, it is not an error.
func ReadModelsFile(file string) (map[string]*ControllerModels, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
if err := migrateLegacyModels(data); err != nil {
return nil, err
}
models, err := ParseModels(data)
if err != nil {
return nil, err
}
return models, nil
}
// WriteModelsFile marshals to YAML details of the given models
// and writes it to the models file.
func WriteModelsFile(models map[string]*ControllerModels) error {
data, err := yaml.Marshal(modelsCollection{models})
if err != nil {
return errors.Annotate(err, "cannot marshal models")
}
return utils.AtomicWriteFile(JujuModelsPath(), data, os.FileMode(0600))
}
// ParseModels parses the given YAML bytes into models metadata.
func ParseModels(data []byte) (map[string]*ControllerModels, error) {
var result modelsCollection
err := yaml.Unmarshal(data, &result)
if err != nil {
return nil, errors.Annotate(err, "cannot unmarshal models")
}
return result.ControllerModels, nil
}
type modelsCollection struct {
ControllerModels map[string]*ControllerModels `yaml:"controllers"`
}
// ControllerModels stores per-controller account-model information.
type ControllerModels struct {
// Models is the collection of models for the account, indexed
// by model name. This should be treated as a cache only, and
// not the complete set of models for the account.
Models map[string]ModelDetails `yaml:"models,omitempty"`
// CurrentModel is the name of the active model for the account.
CurrentModel string `yaml:"current-model,omitempty"`
}
// TODO(axw) 2016-07-14 #NNN
// Drop this code once we get to 2.0-beta13.
func migrateLegacyModels(data []byte) error {
accounts, err := ReadAccountsFile(JujuAccountsPath())
if err != nil {
return err
}
type legacyAccountModels struct {
Models map[string]ModelDetails `yaml:"models"`
CurrentModel string `yaml:"current-model,omitempty"`
}
type legacyControllerAccountModels struct {
AccountModels map[string]*legacyAccountModels `yaml:"accounts"`
}
type legacyModelsCollection struct {
ControllerAccountModels map[string]legacyControllerAccountModels `yaml:"controllers"`
}
var legacy legacyModelsCollection
if err := yaml.Unmarshal(data, &legacy); err != nil {
return errors.Annotate(err, "cannot unmarshal models")
}
result := make(map[string]*ControllerModels)
for controller, controllerAccountModels := range legacy.ControllerAccountModels {
accountDetails, ok := accounts[controller]
if !ok {
continue
}
accountModels, ok := controllerAccountModels.AccountModels[accountDetails.User]
if !ok {
continue
}
result[controller] = &ControllerModels{
accountModels.Models,
accountModels.CurrentModel,
}
}
if len(result) > 0 {
// Only write if we found at least one,
// which means the file was in legacy
// format. Otherwise leave it alone.
return WriteModelsFile(result)
}
return nil
}