How-to manage state¶
Juju allows charms to store state in a key-value store. Here we cover how you can use goops
to set, get, and delete state in your charms. In this simple example, we check whether a state exists for a key named my-key
. If it exists, we delete it. If it does not exist, we set it to a value of my-value
.
package charm
import (
"fmt"
"github.com/gruyaume/goops"
)
const (
StateKey = "my-key"
StateValue = "my-value"
)
func Configure() error {
_, err := goops.GetState(StateKey)
if err != nil {
goops.LogInfof("could not get state: %s", err.Error())
err := goops.SetState(StateKey, StateValue)
if err != nil {
return fmt.Errorf("could not set state: %w", err)
}
goops.LogInfof("set state: %s = %s", StateKey, StateValue)
return nil
}
goops.LogInfof("state already set: %s = %s", StateKey, StateValue)
err = goops.DeleteState(StateKey)
if err != nil {
return fmt.Errorf("could not delete state: %w", err)
}
goops.LogInfof("deleted state: %s", StateKey)
return nil
}