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
0b005a39
authored
Nov 16, 2016
by
Simon János
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
actually the whole code has been refactored
parent
eed8e4b7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
83 deletions
+127
-83
orchestrator/model/resources.py
+127
-83
tests/test_resources.py
+0
-0
No files found.
orchestrator/model/resources.py
View file @
0b005a39
import
copy
import
json
import
uuid
from
enum
import
Enum
from
voluptuous
import
Schema
,
Invalid
,
MultipleInvalid
from
voluptuous
import
Schema
,
Invalid
,
MultipleInvalid
,
Any
,
ALLOW_EXTRA
,
PREVENT_EXTRA
class
Resource
(
object
):
DEFAULT_ID_LENGTH
=
8
def
__init__
(
self
,
*
args
,
**
kwargs
):
if
len
(
args
):
resource
=
args
[
0
]
if
isinstance
(
resource
,
Resource
):
self
.
__id
=
resource
.
id
self
.
__type
=
Resource
.
Type
[
resource
.
type
]
else
:
self
.
__attributes_from_dict
(
resource
)
else
:
self
.
__attributes_from_dict
(
kwargs
)
def
__attributes_from_dict
(
self
,
attributes
):
schema
=
Schema
({
'id'
:
str
,
'type'
:
Resource
.
Type
.
validate
})
ADDITIONAL_SCHEMA
=
{}
def
__new__
(
cls
,
*
args
,
**
kwargs
):
attributes
=
cls
.
__attributes_from_dict
(
args
[
0
]
if
len
(
args
)
else
kwargs
)
resource_type
=
attributes
[
'type'
]
.
value
if
resource_type
is
Resource
:
raise
InvalidResourceException
(
'Resource class is abstract'
)
cls
.
__validate_attributes
(
attributes
,
cls
.
__schema
(
resource_type
,
extend
=
True
))
instance
=
super
(
Resource
,
resource_type
)
.
__new__
(
resource_type
)
instance
.
_attributes
=
attributes
# pylint: disable=protected-access
return
instance
@classmethod
def
__attributes_from_dict
(
cls
,
attributes
):
cls
.
__validate_attributes
(
attributes
,
cls
.
__schema
(
extend
=
False
))
attributes
=
copy
.
deepcopy
(
attributes
)
attributes
[
'id'
]
=
attributes
.
get
(
'id'
,
Resource
.
__generate_random_id
())
attributes
[
'type'
]
=
ResourceType
.
validate
(
attributes
.
get
(
'type'
,
cls
))
return
attributes
@classmethod
def
__validate_attributes
(
cls
,
attributes
,
schema
):
try
:
schema
(
attributes
)
self
.
__id
=
attributes
.
get
(
'id'
,
Resource
.
_random_id
())
self
.
__type
=
Resource
.
Type
[
attributes
.
get
(
'type'
,
'null'
)]
except
MultipleInvalid
as
invalid
:
raise
InvalidResourceException
(
invalid
)
@property
def
id
(
self
):
return
self
.
__id
@classmethod
def
__schema
(
cls
,
resource_type
=
None
,
extend
=
False
):
try
:
string_validator
=
Any
(
str
,
unicode
)
except
NameError
:
string_validator
=
Any
(
str
)
schema
=
Schema
(
schema
=
{
'id'
:
string_validator
,
'type'
:
ResourceType
.
validate
},
extra
=
ALLOW_EXTRA
)
if
extend
:
additional
=
resource_type
.
ADDITIONAL_SCHEMA
if
resource_type
else
cls
.
ADDITIONAL_SCHEMA
schema
=
schema
.
extend
(
additional
,
extra
=
PREVENT_EXTRA
)
return
schema
@property
def
type
(
self
):
return
self
.
__type
.
name
return
str
(
self
.
_attributes
[
'type'
])
def
__getattr__
(
self
,
item
):
try
:
return
self
.
_attributes
[
item
]
except
KeyError
:
raise
AttributeError
(
'Attribute (
%
s) is not set on resource'
)
@staticmethod
def
_random_id
():
def
_
_generate_
random_id
():
return
uuid
.
uuid4
()
.
hex
[:
Resource
.
DEFAULT_ID_LENGTH
]
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Resource
)
and
\
self
.
id
==
other
.
id
and
\
self
.
type
==
other
.
type
try
:
for
attr
,
other_value
in
other
.
items
():
value
=
self
.
_attributes
.
get
(
attr
)
if
value
is
None
or
value
!=
other_value
:
return
False
return
True
except
AttributeError
:
return
False
def
__repr__
(
self
):
return
"
%
s(
%
s)"
%
(
self
.
__class__
.
__name__
,
self
.
id
)
return
json
.
dumps
(
self
,
default
=
Resource
.
json_encoder
)
def
__
hash
__
(
self
):
return
hash
(
self
.
__repr__
()
)
def
__
iter
__
(
self
):
return
self
.
_attributes
.
__iter__
(
)
class
Type
(
Enum
):
null
=
0
instance
=
1
def
keys
(
self
):
return
self
.
_attributes
.
keys
()
@classmethod
def
validate
(
cls
,
value
):
try
:
cls
[
value
]
except
KeyError
:
raise
Invalid
(
'The given resource type (
%
s) is not valid'
%
value
)
return
True
def
values
(
self
):
return
self
.
_attributes
.
values
()
def
items
(
self
):
return
self
.
_attributes
.
items
()
class
Instance
(
Resource
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
[
'type'
]
=
Resource
.
Type
.
instance
.
name
super
(
Instance
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
class
ResourceGroup
(
object
):
def
__init__
(
self
,
*
args
):
self
.
__resources
=
set
()
if
len
(
args
):
for
argument
in
args
:
try
:
self
.
__iadd__
(
argument
)
except
InvalidResourceException
:
for
resource
in
argument
:
self
.
__iadd__
(
resource
)
def
__getitem__
(
self
,
item
):
return
self
.
_attributes
[
item
]
def
__add__
(
self
,
other
):
return
ResourceGroup
(
resources
=
[
self
,
other
])
@staticmethod
def
json_encoder
(
data
):
if
isinstance
(
data
,
ResourceType
):
return
str
(
data
)
return
dict
(
data
)
class
ResourceGroup
(
Resource
):
ADDITIONAL_SCHEMA
=
{
'resources'
:
Any
(
list
,
set
,
tuple
)
}
def
__init__
(
self
,
*
_
,
**
__
):
self
.
__resources
=
{}
if
'resources'
in
self
.
_attributes
.
keys
():
for
resource
in
self
.
_attributes
[
'resources'
]:
self
.
add
(
resource
)
self
.
_attributes
[
'resources'
]
=
self
.
resources
def
add
(
self
,
resource
):
if
not
isinstance
(
resource
,
Resource
):
try
:
resource
=
Resource
(
resource
)
except
InvalidResourceException
:
raise
InvalidResourceException
(
'ResourceGroup can only store resources'
)
self
.
__resources
[
resource
.
id
]
=
resource
def
remove
(
self
,
resource
):
try
:
del
self
.
__resources
[
Resource
(
resource
)
.
id
]
except
InvalidResourceException
:
del
self
.
__resources
[
resource
.
id
]
@property
def
resources
(
self
):
return
list
(
self
.
__resources
)
return
list
(
self
.
__resources
.
values
()
)
def
__add__
(
self
,
other
):
if
isinstance
(
other
,
dict
):
return
ResourceGroup
(
Resource
(
other
),
*
self
.
resources
)
if
isinstance
(
other
,
Resource
):
return
ResourceGroup
(
other
,
*
self
.
resources
)
if
isinstance
(
other
,
ResourceGroup
):
resources
=
other
.
resources
+
self
.
resources
return
ResourceGroup
(
*
resources
)
raise
InvalidResourceException
()
def
__iadd__
(
self
,
other
):
if
isinstance
(
other
,
dict
):
self
.
__resources
.
add
(
Resource
(
other
))
return
self
if
isinstance
(
other
,
Resource
):
self
.
__resources
.
add
(
other
)
return
self
if
isinstance
(
other
,
ResourceGroup
):
self
.
__resources
.
union
(
other
.
resources
)
return
self
raise
InvalidResourceException
()
def
__iter__
(
self
):
return
self
.
__resources
.
__iter__
()
class
Instance
(
Resource
):
ADDITIONAL_SCHEMA
=
{
'number_of_cores'
:
int
}
def
__eq__
(
self
,
other
):
return
self
.
__resources
==
set
(
other
.
resources
)
class
ResourceType
(
Enum
):
null
=
Resource
group
=
ResourceGroup
instance
=
Instance
@classmethod
def
validate
(
cls
,
value
):
if
isinstance
(
value
,
cls
):
return
value
try
:
return
cls
[
value
]
except
KeyError
:
return
cls
(
value
)
except
ValueError
:
raise
Invalid
(
'The given resource type (
%
s) is not valid'
%
value
)
def
__str__
(
self
):
return
str
(
self
.
name
)
def
__repr__
(
self
):
return
'
%
s(
%
s)'
%
(
self
.
__class__
.
__name__
,
str
(
self
))
class
InvalidResourceException
(
Exception
):
...
...
tests/test_resources.py
View file @
0b005a39
This diff is collapsed.
Click to expand it.
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