func (*diskStoreSuite) TestWriteSmallerFile(c *gc.C) { dir := c.MkDir() store, err := configstore.NewDisk(dir) c.Assert(err, jc.ErrorIsNil) info := store.CreateInfo("somemodel") endpoint := configstore.APIEndpoint{ Addresses: []string{"this", "is", "never", "validated", "here"}, Hostnames: []string{"neither", "is", "this"}, ModelUUID: testing.ModelTag.Id(), } info.SetAPIEndpoint(endpoint) err = info.Write() c.Assert(err, jc.ErrorIsNil) newInfo, err := store.ReadInfo("somemodel") c.Assert(err, jc.ErrorIsNil) // Now change the number of addresses to be shorter. endpoint.Addresses = []string{"just one"} endpoint.Hostnames = []string{"just this"} newInfo.SetAPIEndpoint(endpoint) err = newInfo.Write() c.Assert(err, jc.ErrorIsNil) // We should be able to read in in fine. yaInfo, err := store.ReadInfo("somemodel") c.Assert(err, jc.ErrorIsNil) c.Assert(yaInfo.APIEndpoint().Addresses, gc.DeepEquals, []string{"just one"}) c.Assert(yaInfo.APIEndpoint().Hostnames, gc.DeepEquals, []string{"just this"}) }
// cacheAPIInfo updates the local environment settings (.jenv file) // with the provided apiInfo, assuming we've just successfully // connected to the API server. func cacheAPIInfo(st apiState, info configstore.EnvironInfo, apiInfo *api.Info) (err error) { defer errors.DeferredAnnotatef(&err, "failed to cache API credentials") var environUUID string if names.IsValidEnvironment(apiInfo.EnvironTag.Id()) { environUUID = apiInfo.EnvironTag.Id() } else { // For backwards-compatibility, we have to allow connections // with an empty UUID. Login will work for the same reasons. logger.Warningf("ignoring invalid cached API endpoint environment UUID %v", apiInfo.EnvironTag.Id()) } hostPorts, err := network.ParseHostPorts(apiInfo.Addrs...) if err != nil { return errors.Annotatef(err, "invalid API addresses %v", apiInfo.Addrs) } addrConnectedTo, err := network.ParseHostPorts(st.Addr()) if err != nil { // Should never happen, since we've just connected with it. return errors.Annotatef(err, "invalid API address %q", st.Addr()) } addrs, hostnames, addrsChanged := PrepareEndpointsForCaching( info, [][]network.HostPort{hostPorts}, addrConnectedTo[0], ) endpoint := configstore.APIEndpoint{ CACert: string(apiInfo.CACert), EnvironUUID: environUUID, } if addrsChanged { endpoint.Addresses = addrs endpoint.Hostnames = hostnames } info.SetAPIEndpoint(endpoint) tag, ok := apiInfo.Tag.(names.UserTag) if !ok { return errors.Errorf("apiInfo.Tag was of type %T, expecting names.UserTag", apiInfo.Tag) } info.SetAPICredentials(configstore.APICredentials{ // This looks questionable. We have a tag, say "user-admin", but then only // the Id portion of the tag is recorded, "admin", so this is really a // username, not a tag, and cannot be reconstructed accurately. User: tag.Id(), Password: apiInfo.Password, }) return info.Write() }