Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #110 from cloudnautique/metadata_yml_parsing
Browse files Browse the repository at this point in the history
Add better YML parsing for metadata
  • Loading branch information
ibuildthecloud committed Oct 14, 2015
2 parents 04ea6c9 + a22c227 commit 4313936
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rancher/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (r *RancherService) createNormalService() (*rancherClient.Service, error) {

func (r *RancherService) getMetadata() map[string]interface{} {
if config, ok := r.context.RancherConfig[r.name]; ok {
return config.Metadata
return NestedMapsToMapInterface(config.Metadata)
}
return nil
}
Expand Down
49 changes: 49 additions & 0 deletions rancher/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
package rancher

import (
"fmt"
)

func NestedMapsToMapInterface(data map[string]interface{}) map[string]interface{} {
newMapInterface := map[string]interface{}{}

for k, v := range data {
switch v.(type) {
case map[interface{}]interface{}:
v = mapWalk(v.(map[interface{}]interface{}))
case []interface{}:
v = listWalk(v.([]interface{}))
}
newMapInterface[k] = v
}

return newMapInterface
}

func listWalk(val []interface{}) []interface{} {
for i, v := range val {
switch v.(type) {
case map[interface{}]interface{}:
val[i] = mapWalk(v.(map[interface{}]interface{}))
case []interface{}:
val[i] = listWalk(v.([]interface{}))
}
}
return val
}

func mapWalk(val map[interface{}]interface{}) map[string]interface{} {
newMap := map[string]interface{}{}

for k, v := range val {
strK := fmt.Sprintf("%v", k)
switch v.(type) {
case map[interface{}]interface{}:
v = mapWalk(v.(map[interface{}]interface{}))
case []interface{}:
v = listWalk(v.([]interface{}))
}
newMap[strK] = v
}

return newMap
}

func Contains(list []string, item string) bool {
for _, v := range list {
if v == item {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
web:
scale: 1
metadata:
key1:
test1:
- one
- two
test2:
name: t2name
value: t2value
test3: true
test4:
- test5:
name: t5name
- test6:
name: t6name
value: t6value
test7:
test7nest:
test7nestofnest:
- test7dot1:
name: test7dot1name
- test7dot2:
name: test7dot2name
test8:
- test8a:
- name: test8a
value: test8avalue
- name: test8ab
value: test8abvalue
- test8b:
- name: test8ba
value: test8bavalue

25 changes: 23 additions & 2 deletions tests/integration/cattletest/core/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,29 @@ def test_metadata_on_service(client, compose):
service = find_one(project.services)

assert service.name == 'web'
assert service.metadata.key1[0] == 'one'
assert service.metadata.key1[1] == 'two'
assert service.metadata.test1[0] == 'one'
assert service.metadata.test1[1] == 'two'

assert service.metadata.test2.name == "t2name"
assert service.metadata.test2.value == "t2value"

assert service.metadata.test3

assert service.metadata.test4[0].test5.name == "t5name"
assert service.metadata.test4[1].test6.name == "t6name"
assert service.metadata.test4[1].test6.value == "t6value"

assert service.metadata.test7.test7nest.test7nestofnest[0].test7dot1.name \
== "test7dot1name"
assert service.metadata.test7.test7nest.test7nestofnest[1].test7dot2.name \
== "test7dot2name"

assert service.metadata.test8[0].test8a[0].name == "test8a"
assert service.metadata.test8[0].test8a[0].value == "test8avalue"
assert service.metadata.test8[0].test8a[1].name == "test8ab"
assert service.metadata.test8[0].test8a[1].value == "test8abvalue"
assert service.metadata.test8[1].test8b[0].name == "test8ba"
assert service.metadata.test8[1].test8b[0].value == "test8bavalue"


def test_healthchecks(client, compose):
Expand Down

0 comments on commit 4313936

Please sign in to comment.