add remove server from networks with api. Add to default network by default.

This commit is contained in:
afeiszli
2021-04-06 19:13:34 -04:00
parent 5b955f2201
commit f3f2140f23
8 changed files with 230 additions and 64 deletions

View File

@@ -24,6 +24,58 @@ import (
//Takes in an arbitrary field and value for field and checks to see if any other
//node has that value for the same field within the group
func CreateServerToken(network string) (string, error) {
var group models.Group
var accesskey models.AccessKey
group, err := GetParentGroup(network)
if err != nil {
return "", err
}
accesskey.Name = GenKeyName()
accesskey.Value = GenKey()
accesskey.Uses = 1
gconf, errG := GetGlobalConfig()
if errG != nil {
return "", errG
}
address := "localhost" + gconf.PortGRPC
accessstringdec := address + "." + network + "." + accesskey.Value
accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))
group.AccessKeys = append(group.AccessKeys, accesskey)
collection := mongoconn.Client.Database("netmaker").Collection("groups")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// Create filter
filter := bson.M{"nameid": network}
// Read update model from body request
fmt.Println("Adding key to " + group.NameID)
// prepare update model.
update := bson.D{
{"$set", bson.D{
{"accesskeys", group.AccessKeys},
}},
}
errN := collection.FindOneAndUpdate(ctx, filter, update).Decode(&group)
defer cancel()
if errN != nil {
return "", errN
}
return accesskey.AccessString, nil
}
func IsFieldUnique(group string, field string, value string) bool {
var node models.Node