mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #621 from mavenugo/vin-fixes
move Labels from array to map to be consistent with docker
This commit is contained in:
commit
242ffc0b8b
6 changed files with 27 additions and 94 deletions
|
@ -278,7 +278,11 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b
|
|||
}
|
||||
processCreateDefaults(c, &create)
|
||||
|
||||
nw, err := c.NewNetwork(create.NetworkType, create.Name, libnetwork.NetworkOptionLabels(create.Labels))
|
||||
options := []libnetwork.NetworkOption{}
|
||||
if len(create.Labels) > 0 {
|
||||
options = append(options, libnetwork.NetworkOptionLabels(create.Labels))
|
||||
}
|
||||
nw, err := c.NewNetwork(create.NetworkType, create.Name, options...)
|
||||
if err != nil {
|
||||
return "", convertNetworkError(err)
|
||||
}
|
||||
|
|
|
@ -225,9 +225,9 @@ func TestCreateDeleteNetwork(t *testing.T) {
|
|||
t.Fatalf("Expected StatusBadRequest status code, got: %v", errRsp)
|
||||
}
|
||||
|
||||
ops := []string{
|
||||
bridge.BridgeName + "=abc",
|
||||
netlabel.EnableIPv6 + "=true",
|
||||
ops := map[string]string{
|
||||
bridge.BridgeName: "abc",
|
||||
netlabel.EnableIPv6: "true",
|
||||
}
|
||||
nc := networkCreate{Name: "network_1", NetworkType: bridgeNetType, Labels: ops}
|
||||
goodBody, err := json.Marshal(nc)
|
||||
|
@ -259,9 +259,9 @@ func TestCreateDeleteNetwork(t *testing.T) {
|
|||
}
|
||||
|
||||
// Create with labels
|
||||
labels := []string{
|
||||
netlabel.EnableIPv6 + "=true",
|
||||
bridge.BridgeName + "=abc",
|
||||
labels := map[string]string{
|
||||
netlabel.EnableIPv6: "true",
|
||||
bridge.BridgeName: "abc",
|
||||
}
|
||||
nc = networkCreate{Name: "network_2", NetworkType: bridgeNetType, Labels: labels}
|
||||
goodBody, err = json.Marshal(nc)
|
||||
|
@ -294,8 +294,8 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
|
|||
}
|
||||
defer c.Stop()
|
||||
|
||||
ops := []string{
|
||||
bridge.BridgeName + "=api_test_nw",
|
||||
ops := map[string]string{
|
||||
bridge.BridgeName: "api_test_nw",
|
||||
}
|
||||
nc := networkCreate{Name: "sh", NetworkType: bridgeNetType, Labels: ops}
|
||||
body, err := json.Marshal(nc)
|
||||
|
@ -1830,10 +1830,10 @@ func TestEndToEnd(t *testing.T) {
|
|||
|
||||
handleRequest := NewHTTPHandler(c)
|
||||
|
||||
ops := []string{
|
||||
bridge.BridgeName + "=cdef",
|
||||
netlabel.EnableIPv6 + "=true",
|
||||
netlabel.DriverMTU + "=1460",
|
||||
ops := map[string]string{
|
||||
bridge.BridgeName: "cdef",
|
||||
netlabel.EnableIPv6: "true",
|
||||
netlabel.DriverMTU: "1460",
|
||||
}
|
||||
|
||||
// Create network
|
||||
|
|
|
@ -34,9 +34,9 @@ type sandboxResource struct {
|
|||
|
||||
// networkCreate is the expected body of the "create network" http request message
|
||||
type networkCreate struct {
|
||||
Name string `json:"name"`
|
||||
NetworkType string `json:"network_type"`
|
||||
Labels []string `json:"labels"`
|
||||
Name string `json:"name"`
|
||||
NetworkType string `json:"network_type"`
|
||||
Labels map[string]string `json:"labels"`
|
||||
}
|
||||
|
||||
// endpointCreate represents the body of the "create endpoint" http request message
|
||||
|
|
|
@ -104,26 +104,3 @@ func KeyValue(label string) (key string, value string) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ToMap converts a list of labels in a map of (key,value) pairs
|
||||
func ToMap(labels []string) map[string]string {
|
||||
m := make(map[string]string, len(labels))
|
||||
for _, l := range labels {
|
||||
k, v := KeyValue(l)
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// FromMap converts a map of (key,value) pairs in a lsit of labels
|
||||
func FromMap(m map[string]string) []string {
|
||||
l := make([]string, 0, len(m))
|
||||
for k, v := range m {
|
||||
s := k
|
||||
if v != "" {
|
||||
s = s + "=" + v
|
||||
}
|
||||
l = append(l, s)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
|
|
@ -28,50 +28,3 @@ func TestKeyValue(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
lista := make([]string, len(input))
|
||||
for ind, i := range input {
|
||||
lista[ind] = i.label
|
||||
}
|
||||
|
||||
mappa := ToMap(lista)
|
||||
|
||||
if len(mappa) != len(lista) {
|
||||
t.Fatalf("Incorrect map length. Expected %d. Got %d", len(lista), len(mappa))
|
||||
}
|
||||
|
||||
for _, i := range input {
|
||||
if v, ok := mappa[i.key]; !ok || v != i.value {
|
||||
t.Fatalf("Cannot find key or value for key: %s", i.key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromMap(t *testing.T) {
|
||||
var m map[string]string
|
||||
lbls := FromMap(m)
|
||||
if len(lbls) != 0 {
|
||||
t.Fatalf("unexpected lbls length")
|
||||
}
|
||||
|
||||
m = make(map[string]string, 3)
|
||||
m["peso"] = "85"
|
||||
m["statura"] = "170"
|
||||
m["maschio"] = ""
|
||||
|
||||
lbls = FromMap(m)
|
||||
if len(lbls) != 3 {
|
||||
t.Fatalf("unexpected lbls length")
|
||||
}
|
||||
|
||||
for _, l := range lbls {
|
||||
switch l {
|
||||
case "peso=85":
|
||||
case "statura=170":
|
||||
case "maschio":
|
||||
default:
|
||||
t.Fatalf("unexpected label: %s", l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ type Network interface {
|
|||
|
||||
// NetworkInfo returns operational information about the network
|
||||
type NetworkInfo interface {
|
||||
Labels() []string
|
||||
Labels() map[string]string
|
||||
Scope() string
|
||||
}
|
||||
|
||||
|
@ -472,16 +472,15 @@ func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ip
|
|||
}
|
||||
|
||||
// NetworkOptionLabels function returns an option setter for any parameter described by a map
|
||||
func NetworkOptionLabels(labels []string) NetworkOption {
|
||||
func NetworkOptionLabels(labels map[string]string) NetworkOption {
|
||||
return func(n *network) {
|
||||
if n.generic == nil {
|
||||
n.generic = make(map[string]interface{})
|
||||
}
|
||||
opts := netlabel.ToMap(labels)
|
||||
// Store the options
|
||||
n.generic[netlabel.GenericData] = opts
|
||||
n.generic[netlabel.GenericData] = labels
|
||||
// Decode and store the endpoint options of libnetwork interest
|
||||
if val, ok := opts[netlabel.EnableIPv6]; ok {
|
||||
if val, ok := labels[netlabel.EnableIPv6]; ok {
|
||||
var err error
|
||||
if n.enableIPv6, err = strconv.ParseBool(val); err != nil {
|
||||
log.Warnf("Failed to parse %s' value: %s (%s)", netlabel.EnableIPv6, val, err.Error())
|
||||
|
@ -1047,15 +1046,15 @@ func (n *network) Info() NetworkInfo {
|
|||
return n
|
||||
}
|
||||
|
||||
func (n *network) Labels() []string {
|
||||
func (n *network) Labels() map[string]string {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
if n.generic != nil {
|
||||
if m, ok := n.generic[netlabel.GenericData]; ok {
|
||||
return netlabel.FromMap(m.(map[string]string))
|
||||
return m.(map[string]string)
|
||||
}
|
||||
}
|
||||
return []string{}
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
func (n *network) Scope() string {
|
||||
|
|
Loading…
Reference in a new issue