mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
lbagent: use encoding/json for {,de}serialization
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
@@ -11,7 +12,12 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
const (
|
||||
CORPUS_VERSION = "v1"
|
||||
)
|
||||
|
||||
type LoadbalancerCorpus struct {
|
||||
CorpusVersion string
|
||||
*ModelSets
|
||||
ModelSetsMaxUpdatedAt *ModelSetsMaxUpdatedAt
|
||||
}
|
||||
@@ -50,10 +56,12 @@ func (b *LoadbalancerCorpus) MaxSeenUpdatedAtParams() *jsonutils.JSONDict {
|
||||
}
|
||||
|
||||
func (b *LoadbalancerCorpus) SaveDir(dir string) error {
|
||||
j := jsonutils.Marshal(b)
|
||||
d := j.String()
|
||||
d, err := json.Marshal(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := filepath.Join(dir, "corpus")
|
||||
err := ioutil.WriteFile(p, []byte(d), agentutils.FileModeFileSensitive)
|
||||
err = ioutil.WriteFile(p, d, agentutils.FileModeFileSensitive)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -63,13 +71,14 @@ func (b *LoadbalancerCorpus) LoadDir(dir string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jd, err := jsonutils.Parse(d)
|
||||
err = json.Unmarshal(d, b)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: json parse failed: %s", p, err)
|
||||
return err
|
||||
}
|
||||
err = jd.Unmarshal(b)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: json unmarshal failed: %s", p, err)
|
||||
// version for updating
|
||||
if ver := b.CorpusVersion; ver != CORPUS_VERSION {
|
||||
b.Reset()
|
||||
return fmt.Errorf("%s: corpus version %s != %s", p, ver, CORPUS_VERSION)
|
||||
}
|
||||
correct := b.join()
|
||||
if !correct {
|
||||
@@ -77,3 +86,8 @@ func (b *LoadbalancerCorpus) LoadDir(dir string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *LoadbalancerCorpus) Reset() {
|
||||
bb := NewEmptyLoadbalancerCorpus()
|
||||
*b = *bb
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user