Multiple playgrounds support (#215)
* Add Playground struct and basic support for creating it and retrieving it * Add missing functions in pwd mock * Get playground from request domain and validate it exists. If valid set it on the newly created session. * Move playground specific configurations to the playground struct and use it everytime we need that conf. * Don't allow to specify a duration bigger that the allowed in the playground
This commit is contained in:
committed by
GitHub
parent
3dee0d3f0b
commit
3f5b3882dd
83
pwd/types/playground.go
Normal file
83
pwd/types/playground.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PlaygroundExtras map[string]interface{}
|
||||
|
||||
func (e PlaygroundExtras) Get(name string) (interface{}, bool) {
|
||||
v, f := e[name]
|
||||
return v, f
|
||||
}
|
||||
func (e PlaygroundExtras) GetInt(name string) (int, bool) {
|
||||
v, f := e[name]
|
||||
if f {
|
||||
if r, ok := v.(int); ok {
|
||||
return r, ok
|
||||
} else if r, ok := v.(float64); ok {
|
||||
return int(r), ok
|
||||
} else if r, ok := v.(string); ok {
|
||||
if v, err := strconv.Atoi(r); err != nil {
|
||||
return 0, false
|
||||
} else {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return v.(int), f
|
||||
} else {
|
||||
return 0, f
|
||||
}
|
||||
}
|
||||
|
||||
func (e PlaygroundExtras) GetString(name string) (string, bool) {
|
||||
v, f := e[name]
|
||||
if f {
|
||||
if r, ok := v.(int); ok {
|
||||
return strconv.Itoa(r), ok
|
||||
} else if r, ok := v.(float64); ok {
|
||||
return strconv.FormatFloat(r, 'g', -1, 64), ok
|
||||
} else if r, ok := v.(bool); ok {
|
||||
return strconv.FormatBool(r), ok
|
||||
} else if r, ok := v.(string); ok {
|
||||
return r, ok
|
||||
} else {
|
||||
return "", false
|
||||
}
|
||||
} else {
|
||||
return "", f
|
||||
}
|
||||
}
|
||||
|
||||
func (e PlaygroundExtras) GetDuration(name string) (time.Duration, bool) {
|
||||
v, f := e[name]
|
||||
if f {
|
||||
if r, ok := v.(int); ok {
|
||||
return time.Duration(r), ok
|
||||
} else if r, ok := v.(float64); ok {
|
||||
return time.Duration(r), ok
|
||||
} else if r, ok := v.(string); ok {
|
||||
if d, err := time.ParseDuration(r); err != nil {
|
||||
return time.Duration(0), false
|
||||
} else {
|
||||
return d, true
|
||||
}
|
||||
} else {
|
||||
return time.Duration(0), false
|
||||
}
|
||||
} else {
|
||||
return time.Duration(0), f
|
||||
}
|
||||
}
|
||||
|
||||
type Playground struct {
|
||||
Id string `json:"id" bson:"id"`
|
||||
Domain string `json:"domain" bson:"domain"`
|
||||
DefaultDinDInstanceImage string `json:"default_dind_instance_image" bson:"default_dind_instance_image"`
|
||||
AvailableDinDInstanceImages []string `json:"available_dind_instance_images" bson:"available_dind_instance_images"`
|
||||
AllowWindowsInstances bool `json:"allow_windows_instances" bson:"allow_windows_instances"`
|
||||
DefaultSessionDuration time.Duration `json:"default_session_duration" bson:"default_session_duration"`
|
||||
L2RouterIP string `json:"l2_router_ip" bson:"l2_router_ip"`
|
||||
Extras PlaygroundExtras `json:"extras" bson:"extras"`
|
||||
}
|
||||
119
pwd/types/playground_test.go
Normal file
119
pwd/types/playground_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/twinj/uuid"
|
||||
)
|
||||
|
||||
func TestPlayground_Extras_GetInt(t *testing.T) {
|
||||
p := Playground{
|
||||
Id: uuid.NewV4().String(),
|
||||
Domain: "localhost",
|
||||
DefaultDinDInstanceImage: "franel/dind",
|
||||
AllowWindowsInstances: false,
|
||||
DefaultSessionDuration: time.Hour * 4,
|
||||
Extras: PlaygroundExtras{
|
||||
"intFromInt": 10,
|
||||
"intFromFloat": 32.0,
|
||||
"intFromString": "15",
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(p)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var p2 Playground
|
||||
json.Unmarshal(b, &p2)
|
||||
|
||||
v, found := p2.Extras.GetInt("intFromInt")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 10, v)
|
||||
|
||||
v, found = p2.Extras.GetInt("intFromFloat")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 32, v)
|
||||
|
||||
v, found = p2.Extras.GetInt("intFromString")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 15, v)
|
||||
}
|
||||
|
||||
func TestPlayground_Extras_GetString(t *testing.T) {
|
||||
p := Playground{
|
||||
Id: uuid.NewV4().String(),
|
||||
Domain: "localhost",
|
||||
DefaultDinDInstanceImage: "franel/dind",
|
||||
AllowWindowsInstances: false,
|
||||
DefaultSessionDuration: time.Hour * 4,
|
||||
Extras: PlaygroundExtras{
|
||||
"stringFromInt": 10,
|
||||
"stringFromFloat": 32.3,
|
||||
"stringFromString": "15",
|
||||
"stringFromBool": false,
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(p)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var p2 Playground
|
||||
json.Unmarshal(b, &p2)
|
||||
|
||||
v, found := p2.Extras.GetString("stringFromInt")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "10", v)
|
||||
|
||||
v, found = p2.Extras.GetString("stringFromFloat")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "32.3", v)
|
||||
|
||||
v, found = p2.Extras.GetString("stringFromString")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "15", v)
|
||||
|
||||
v, found = p2.Extras.GetString("stringFromBool")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "false", v)
|
||||
}
|
||||
|
||||
func TestPlayground_Extras_GetDuration(t *testing.T) {
|
||||
p := Playground{
|
||||
Id: uuid.NewV4().String(),
|
||||
Domain: "localhost",
|
||||
DefaultDinDInstanceImage: "franel/dind",
|
||||
AllowWindowsInstances: false,
|
||||
DefaultSessionDuration: time.Hour * 4,
|
||||
Extras: PlaygroundExtras{
|
||||
"durationFromInt": 10,
|
||||
"durationFromFloat": 32.3,
|
||||
"durationFromString": "4h",
|
||||
"durationFromDuration": time.Hour * 3,
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.Marshal(p)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var p2 Playground
|
||||
json.Unmarshal(b, &p2)
|
||||
|
||||
v, found := p2.Extras.GetDuration("durationFromInt")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, time.Duration(10), v)
|
||||
|
||||
v, found = p2.Extras.GetDuration("durationFromFloat")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, time.Duration(32), v)
|
||||
|
||||
v, found = p2.Extras.GetDuration("durationFromString")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, time.Hour*4, v)
|
||||
|
||||
v, found = p2.Extras.GetDuration("durationFromDuration")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, time.Hour*3, v)
|
||||
}
|
||||
@@ -15,4 +15,5 @@ type Session struct {
|
||||
ImageName string `json:"image_name" bson:"image_name"`
|
||||
Host string `json:"host" bson:"host"`
|
||||
UserId string `json:"user_id" bson:"user_id"`
|
||||
PlaygroundId string `json:"playground_id" bson:"playground_id"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user