Skip to content

Commit

Permalink
Adds implementation and test for UpdateDHCPAddressConfigs.
Browse files Browse the repository at this point in the history
  • Loading branch information
manadart committed Jun 9, 2021
1 parent abd7856 commit a51fcd5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions state/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3487,5 +3487,30 @@ func TranslateK8sServiceTypes(pool *StatePool) error {
// collection with the removed "dynamic" address configuration method are
// updated to indicate the "dhcp" configuration method.
func UpdateDHCPAddressConfigs(pool *StatePool) error {
st := pool.SystemState()

col, closer := st.db().GetRawCollection(ipAddressesC)
defer closer()

iter := col.Find(bson.M{"config-method": "dynamic"}).Iter()

var ops []txn.Op
var doc bson.M
for iter.Next(&doc) {
ops = append(ops, txn.Op{
C: ipAddressesC,
Id: doc["_id"],
Assert: txn.DocExists,
Update: bson.M{"$set": bson.M{"config-method": network.ConfigDHCP}},
})
}

if err := iter.Close(); err != nil {
return errors.Trace(err)
}

if len(ops) > 0 {
return errors.Trace(st.runRawTransaction(ops))
}
return nil
}
21 changes: 21 additions & 0 deletions state/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5209,6 +5209,27 @@ func (s *upgradesSuite) TestUpdateLegacyKubernetesCloudCredentialsOAuth2Cert(c *
)
}

func (s *upgradesSuite) TestUpdateDHCPAddressConfigs(c *gc.C) {
model1 := s.makeModel(c, "model-1", coretesting.Attrs{})
defer func() { _ = model1.Close() }()

col, closer := s.state.db().GetRawCollection(ipAddressesC)
defer closer()

docs := []interface{}{
bson.M{"_id": model1.modelUUID() + ":m#0#d#eth0#ip#10.10.10.10", "config-method": "dynamic"},
bson.M{"_id": model1.modelUUID() + ":m#1#d#eth1#ip#20.20.20.20", "config-method": network.ConfigStatic},
}
err := col.Insert(docs...)
c.Assert(err, jc.ErrorIsNil)

// The first of the docs has an upgraded config method.
s.assertUpgradedData(c, UpdateDHCPAddressConfigs, upgradedData(col, []bson.M{
{"_id": model1.modelUUID() + ":m#0#d#eth0#ip#10.10.10.10", "config-method": string(network.ConfigDHCP)},
{"_id": model1.modelUUID() + ":m#1#d#eth1#ip#20.20.20.20", "config-method": string(network.ConfigStatic)},
}))
}

type docById []bson.M

func (d docById) Len() int { return len(d) }
Expand Down

0 comments on commit a51fcd5

Please sign in to comment.