Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Simon János
/
orchestrator
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9b554083
authored
Nov 16, 2016
by
Simon János
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flatten method for resource groups (and resources)
parent
0b005a39
Pipeline
#283
passed with stage
in 45 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
orchestrator/model/resources.py
+10
-0
tests/test_resources.py
+39
-0
No files found.
orchestrator/model/resources.py
View file @
9b554083
...
...
@@ -93,6 +93,10 @@ class Resource(object):
def
__add__
(
self
,
other
):
return
ResourceGroup
(
resources
=
[
self
,
other
])
def
flatten
(
self
,
fqn
=
None
):
fqn
=
'
%
s.
%
s'
%
(
fqn
,
self
.
id
)
if
fqn
else
self
.
id
return
{
'
%
s.
%
s'
%
(
fqn
,
key
):
str
(
value
)
for
key
,
value
in
self
.
items
()}
@staticmethod
def
json_encoder
(
data
):
if
isinstance
(
data
,
ResourceType
):
...
...
@@ -130,6 +134,12 @@ class ResourceGroup(Resource):
def
resources
(
self
):
return
list
(
self
.
__resources
.
values
())
def
flatten
(
self
,
fqn
=
None
):
flat
=
dict
()
for
resource
in
self
.
resources
:
flat
.
update
(
resource
.
flatten
(
'
%
s.
%
s'
%
(
fqn
,
self
.
id
)
if
fqn
else
self
.
id
))
return
flat
class
Instance
(
Resource
):
ADDITIONAL_SCHEMA
=
{
...
...
tests/test_resources.py
View file @
9b554083
...
...
@@ -249,3 +249,42 @@ class ResourceGroupTest(TestCase):
ResourceGroup
(
id
=
'some_other_group'
))
self
.
assertNotEqual
(
ResourceGroup
(
id
=
'some_group'
,
resources
=
[
resource1
]),
ResourceGroup
(
id
=
'some_group'
,
resources
=
[
resource2
]))
def
test_resource_group_flatten
(
self
):
tree_dict
=
{
'id'
:
'root'
,
'type'
:
'group'
,
'resources'
:
[
{
'id'
:
'instance-1'
,
'type'
:
'instance'
},
{
'id'
:
'level-1'
,
'type'
:
'group'
,
'resources'
:
[
{
'id'
:
'instance-2'
,
'type'
:
'instance'
},
{
'id'
:
'instance-3'
,
'type'
:
'instance'
}
]
}
]
}
expected_flat_dict
=
{
'root.instance-1.id'
:
'instance-1'
,
'root.instance-1.type'
:
'instance'
,
'root.level-1.instance-2.id'
:
'instance-2'
,
'root.level-1.instance-2.type'
:
'instance'
,
'root.level-1.instance-3.id'
:
'instance-3'
,
'root.level-1.instance-3.type'
:
'instance'
}
tree_resource
=
Resource
(
tree_dict
)
flat_dict
=
tree_resource
.
flatten
()
self
.
assertEqual
(
expected_flat_dict
,
flat_dict
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment