Skip to content

Commit f1d0363

Browse files
authored
Merge pull request #140 from tlm/agent-version-in-root
Introduces agent version as a top level key.
2 parents d734bcd + 1f7f17b commit f1d0363

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

model.go

+43-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Model interface {
3333
HasStatus
3434
HasStatusHistory
3535

36+
// AgentVersion returns the version currently in use by the model.
37+
AgentVersion() string
38+
3639
Type() string
3740
Cloud() string
3841
CloudRegion() string
@@ -144,6 +147,9 @@ type Model interface {
144147
// ModelArgs represent the bare minimum information that is needed
145148
// to represent a model.
146149
type ModelArgs struct {
150+
// AgentVersion defines the current version in use by the model.
151+
AgentVersion string
152+
147153
Type string
148154
Owner names.UserTag
149155
Config map[string]interface{}
@@ -159,7 +165,8 @@ type ModelArgs struct {
159165
// NewModel returns a Model based on the args specified.
160166
func NewModel(args ModelArgs) Model {
161167
m := &model{
162-
Version: 10,
168+
Version: 11,
169+
AgentVersion_: args.AgentVersion,
163170
Type_: args.Type,
164171
Owner_: args.Owner.Id(),
165172
Config_: args.Config,
@@ -254,6 +261,9 @@ func parentId(machineId string) string {
254261
type model struct {
255262
Version int `yaml:"version"`
256263

264+
// AgentVersion_ defines the agent version in use by the model.
265+
AgentVersion_ string `yaml:"agent-version"`
266+
257267
Type_ string `yaml:"type"`
258268
Owner_ string `yaml:"owner"`
259269
Config_ map[string]interface{} `yaml:"config"`
@@ -314,6 +324,11 @@ type model struct {
314324
PasswordHash_ string `yaml:"password-hash,omitempty"`
315325
}
316326

327+
// AgentVersion returns the current agent version in use the by the model.
328+
func (m *model) AgentVersion() string {
329+
return m.AgentVersion_
330+
}
331+
317332
func (m *model) Type() string {
318333
return m.Type_
319334
}
@@ -1092,6 +1107,15 @@ func (m *model) Validate() error {
10921107
return errors.NotValidf("missing status")
10931108
}
10941109

1110+
if m.AgentVersion_ != "" {
1111+
agentVersion, err := version.Parse(m.AgentVersion_)
1112+
if err != nil {
1113+
return errors.Annotate(err, "agent version not parsable")
1114+
} else if agentVersion == version.Zero {
1115+
return errors.NotValidf("agent version cannot be zero")
1116+
}
1117+
}
1118+
10951119
validationCtx := newValidationContext()
10961120
for _, machine := range m.Machines_.Machines_ {
10971121
if err := m.validateMachine(validationCtx, machine); err != nil {
@@ -1522,6 +1546,7 @@ var modelDeserializationFuncs = map[int]modelDeserializationFunc{
15221546
8: newModelImporter(8, schema.FieldMap(modelV8Fields())),
15231547
9: newModelImporter(9, schema.FieldMap(modelV9Fields())),
15241548
10: newModelImporter(10, schema.FieldMap(modelV10Fields())),
1549+
11: newModelImporter(11, schema.FieldMap(modelV11Fields())),
15251550
}
15261551

15271552
func modelV1Fields() (schema.Fields, schema.Defaults) {
@@ -1635,11 +1660,17 @@ func modelV10Fields() (schema.Fields, schema.Defaults) {
16351660
return fields, defaults
16361661
}
16371662

1663+
func modelV11Fields() (schema.Fields, schema.Defaults) {
1664+
fields, defaults := modelV10Fields()
1665+
fields["agent-version"] = schema.String()
1666+
return fields, defaults
1667+
}
1668+
16381669
func newModelFromValid(valid map[string]interface{}, importVersion int) (*model, error) {
16391670
// We're always making a version 8 model, no matter what we got on
16401671
// the way in.
16411672
result := &model{
1642-
Version: 10,
1673+
Version: 11,
16431674
Type_: IAAS,
16441675
Owner_: valid["owner"].(string),
16451676
Config_: valid["config"].(map[string]interface{}),
@@ -1903,6 +1934,16 @@ func newModelFromValid(valid map[string]interface{}, importVersion int) (*model,
19031934

19041935
result.SecretBackendID_ = valid["secret-backend-id"].(string)
19051936
}
1937+
1938+
// When we are importing v11 onwards agent version will be a first class
1939+
// citizen on the model. Before this we can attempt to get the value from
1940+
// config.
1941+
if importVersion >= 11 {
1942+
result.AgentVersion_ = valid["agent-version"].(string)
1943+
} else if result.Config_ != nil && result.Config_["agent-version"] != nil {
1944+
result.AgentVersion_ = result.Config_["agent-version"].(string)
1945+
}
1946+
19061947
return result, nil
19071948
}
19081949

model_test.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ func (s *ModelSerializationSuite) TestParsingYAMLWithMissingModificationStatus(c
171171

172172
func (s *ModelSerializationSuite) testParsingYAMLWithMachine(c *gc.C, machineFn func(Model)) {
173173
args := ModelArgs{
174-
Type: IAAS,
175-
Owner: names.NewUserTag("magic"),
174+
AgentVersion: "3.1.1",
175+
Type: IAAS,
176+
Owner: names.NewUserTag("magic"),
176177
Config: map[string]interface{}{
177178
"name": "awesome",
178179
"uuid": "some-uuid",
@@ -202,6 +203,7 @@ func (s *ModelSerializationSuite) testParsingYAMLWithMachine(c *gc.C, machineFn
202203
addMinimalApplication(initial)
203204
model := s.exportImport(c, initial)
204205

206+
c.Check(model.AgentVersion(), gc.Equals, "3.1.1")
205207
c.Assert(model.Type(), gc.Equals, IAAS)
206208
c.Assert(model.Owner(), gc.Equals, args.Owner)
207209
c.Assert(model.Tag().Id(), gc.Equals, "some-uuid")
@@ -1165,7 +1167,7 @@ func (s *ModelSerializationSuite) TestSerializesToLatestVersion(c *gc.C) {
11651167
c.Assert(ok, jc.IsTrue)
11661168
version, ok := versionValue.(int)
11671169
c.Assert(ok, jc.IsTrue)
1168-
c.Assert(version, gc.Equals, 10)
1170+
c.Assert(version, gc.Equals, 11)
11691171
}
11701172

11711173
func (s *ModelSerializationSuite) TestVersion1Works(c *gc.C) {
@@ -1634,6 +1636,23 @@ func (s *ModelSerializationSuite) TestRemoteSecretsValidate(c *gc.C) {
16341636
c.Assert(err, gc.ErrorMatches, `remote secret\[0\] consumer \(foo\) not valid`)
16351637
}
16361638

1639+
func (s *ModelSerializationSuite) TestAgentVersionPre11Import(c *gc.C) {
1640+
initial := s.newModel(ModelArgs{
1641+
Config: map[string]any{
1642+
"agent-version": "3.3.3",
1643+
},
1644+
})
1645+
data := asStringMap(c, initial)
1646+
data["version"] = 10
1647+
bytes, err := yaml.Marshal(data)
1648+
c.Assert(err, jc.ErrorIsNil)
1649+
1650+
model, err := Deserialize(bytes)
1651+
c.Check(err, jc.ErrorIsNil)
1652+
1653+
c.Check(model.AgentVersion(), gc.Equals, "3.3.3")
1654+
}
1655+
16371656
// modelV1example was taken from a Juju 2.1 model dump, which is version
16381657
// 1, and among other things is missing model status, which version 2 makes
16391658
// manditory.

0 commit comments

Comments
 (0)