// SetAPIHostPorts sets the addresses of the API server instances.
// Each server is represented by one element in the top level slice.
func (st *State) SetAPIHostPorts(netHostsPorts [][]network.HostPort) error {
controllers, closer := st.getCollection(controllersC)
defer closer()
doc := apiHostPortsDoc{
APIHostPorts: fromNetworkHostsPorts(netHostsPorts),
}
buildTxn := func(attempt int) ([]txn.Op, error) {
var existingDoc apiHostPortsDoc
err := controllers.Find(bson.D{{"_id", apiHostPortsKey}}).One(&existingDoc)
if err != nil {
return nil, err
}
op := txn.Op{
C: controllersC,
Id: apiHostPortsKey,
Assert: bson.D{{
"txn-revno", existingDoc.TxnRevno,
}},
}
hostPorts := networkHostsPorts(existingDoc.APIHostPorts)
if !hostsPortsEqual(netHostsPorts, hostPorts) {
op.Update = bson.D{{
"$set", bson.D{{"apihostports", doc.APIHostPorts}},
}}
}
return []txn.Op{op}, nil
}
if err := st.run(buildTxn); err != nil {
return errors.Annotate(err, "cannot set API addresses")
}
logger.Debugf("setting API hostPorts: %v", netHostsPorts)
return nil