Created
December 29, 2015 17:59
-
-
Save ygrenzinger/ff38038b5e4b282679cc to your computer and use it in GitHub Desktop.
Migration from MongoDB to ETCD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
//"os" | |
"log" | |
//"fmt" | |
//"io/ioutil" | |
"encoding/json" | |
"time" | |
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" | |
"github.com/coreos/etcd/client" | |
"gopkg.in/mgo.v2" | |
"gopkg.in/mgo.v2/bson" | |
) | |
type jsonobject []routingSwitchType | |
type routingSwitchType struct { | |
Name string `bson:"name" json:"name"` | |
KeyURL string `bson:"keyUrl" json:"keyUrl"` | |
ConditionCount int `bson:"conditionCount" json:"conditionCount"` | |
Conditions []conditionsType `bson:"conditions" json:"conditions"` | |
} | |
type conditionsType struct { | |
Order int `json:"order"` | |
URL string `json:"url"` | |
AboProfileCall bool `json:"aboProfileCall"` | |
KeepQueryStrings bool `json:"keepQueryStrings"` | |
AddAboProfileInfo bool `json:"addAboProfileInfo"` | |
AddSubscriberHash bool `json:"addSubscriberHash"` | |
UseVectors bool `json:"useVectors"` | |
Vectors string `json:"vectors"` | |
UseAlias bool `json:"useAlias"` | |
Alias string `json:"alias"` | |
UseRules bool `json:"useRules"` | |
Rules string `json:"rules"` | |
UseMacros bool `json:"useMacros"` | |
Macros string `json:"macros"` | |
} | |
func main() { | |
session, err := mgo.Dial("mongodb://127.0.0.1:27017") | |
if err != nil { | |
panic(err) | |
} | |
defer session.Close() | |
var routingswitchs []routingSwitchType | |
switchColl := session.DB("aiguillage").C("routingswitches") | |
err = switchColl.Find(bson.M{}).All(&routingswitchs) | |
if err != nil { | |
log.Fatal(err) | |
} | |
cfg := client.Config{ | |
Endpoints: []string{"http://127.0.0.1:2379"}, | |
Transport: client.DefaultTransport, | |
// set timeout per request to fail fast when the target endpoint is unavailable | |
HeaderTimeoutPerRequest: time.Second, | |
} | |
etcdClient, err := client.New(cfg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
kapi := client.NewKeysAPI(etcdClient) | |
/* | |
file, e := ioutil.ReadFile("./routingSwitchs.json") | |
if e != nil { | |
log.Printf("File error: %v\n", e) | |
os.Exit(1) | |
} | |
var routingswitchs jsonobject | |
json.Unmarshal(file, &routingswitchs) | |
*/ | |
for _, routingSwitch := range routingswitchs { | |
log.Printf("Setting /%s key with routing switch %v", routingSwitch.KeyURL, routingSwitch) | |
value, _ := json.Marshal(&routingSwitch) | |
key := "/routingswitchs/"+routingSwitch.KeyURL | |
resp, err := kapi.Set(context.Background(), key, string(value[:]), nil) | |
if err != nil { | |
log.Fatal(err) | |
} else { | |
// print common key info | |
log.Printf("Set is done. Metadata is %q\n", resp) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment