Skip to content

Commit

Permalink
feat(climc): feature config (#20396)
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi authored May 30, 2024
1 parent e7889a6 commit 404914b
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions cmd/climc/shell/misc/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,45 @@ import (

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
"yunion.io/x/pkg/util/sets"

"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
)

type GlobalSettingsValue struct {
SetupKeys []string `json:"setupKeys"`
SetupKeysVersion string `json:"setupKeysVersion"`
SetupOneStackInitialized bool `json:"setupOneStackInitialized"`
ProductVersion string `json:"productVersion"`
UserDefinedKeys map[string]bool `json:"userDefinedKeys"`
}

func NewGlobalSettingsValue(setupKeys []string, setupOneStack bool) *GlobalSettingsValue {
settings := &GlobalSettingsValue{
SetupOneStackInitialized: setupOneStack,
UserDefinedKeys: make(map[string]bool),
}
for _, key := range setupKeys {
settings.Switch(key, true)
}
return settings
}

func (g *GlobalSettingsValue) Switch(featureKey string, on bool) {
if g.UserDefinedKeys == nil {
g.UserDefinedKeys = map[string]bool{}
}
g.UserDefinedKeys[featureKey] = on
ss := sets.NewString(g.SetupKeys...)
if on {
ss.Insert(featureKey)
} else {
ss.Delete(featureKey)
}
g.SetupKeys = ss.List()
}

func init() {
var features = []string{
"onestack",
Expand Down Expand Up @@ -99,10 +132,7 @@ func init() {
input := map[string]interface{}{
"name": GlobalSettings,
"service_id": YunionAgent,
"value": map[string]interface{}{
"setupKeys": []string{name},
"setupOneStackInitialized": true,
},
"value": NewGlobalSettingsValue([]string{name}, true),
}
params := jsonutils.Marshal(input)
if _, err := yunionconf.Parameters.Create(s, params); err != nil {
Expand All @@ -124,33 +154,20 @@ func init() {
if err != nil {
return errors.Wrap(err, "get value")
}
setupKeys := []string{}
if value.Contains("setupKeys") {
if err := value.Unmarshal(&setupKeys, "setupKeys"); err != nil {
return errors.Wrap(err, "unmarshal setupKeys")
}
curConf := new(GlobalSettingsValue)
if err := value.Unmarshal(curConf); err != nil {
return errors.Wrapf(err, "unmarshal to GlobalSettingsValue: %s", value)
}
if !enable {
if utils.IsInStringArray(name, setupKeys) {
newKeys := []string{}
for _, key := range setupKeys {
if key != name {
newKeys = append(newKeys, key)
}
}
setupKeys = newKeys
}
} else if enable {
if !utils.IsInStringArray(name, setupKeys) {
setupKeys = append(setupKeys, name)
}
curConf.Switch(name, false)
} else {
curConf.Switch(name, true)
}
value.(*jsonutils.JSONDict).Set("setupKeys", jsonutils.Marshal(setupKeys))
id, err := ss.GetString("id")
if err != nil {
return errors.Errorf("get id from %s", ss)
}
ss.(*jsonutils.JSONDict).Set("value", value)
ss.(*jsonutils.JSONDict).Set("value", jsonutils.Marshal(curConf))
obj, err := yunionconf.Parameters.Update(s, id, ss)
if err != nil {
return errors.Wrapf(err, "update %s(%s)", GlobalSettings, id)
Expand Down

0 comments on commit 404914b

Please sign in to comment.