This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
/
service_instance.py
1945 lines (1548 loc) · 68.1 KB
/
service_instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=too-many-lines, no-self-argument, no-member, abstract-method
from sqlalchemy import (
Column,
Text,
Integer,
Enum,
Boolean
)
from sqlalchemy import DateTime
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.ext.orderinglist import ordering_list
from .mixins import InstanceModelMixin
from ..orchestrator import execution_plugin
from ..parser import validation
from ..parser.consumption import ConsumptionContext
from ..utils import (
collections,
formatting,
console
)
from . import (
relationship,
utils,
types as modeling_types
)
class ServiceBase(InstanceModelMixin):
"""
A service is usually an instance of a :class:`ServiceTemplate`.
You will usually not create it programmatically, but instead instantiate it from a service
template.
:ivar name: Name (unique for this ARIA installation)
:vartype name: basestring
:ivar service_template: Template from which this service was instantiated (optional)
:vartype service_template: :class:`ServiceTemplate`
:ivar description: Human-readable description
:vartype description: string
:ivar meta_data: Custom annotations
:vartype meta_data: {basestring: :class:`Metadata`}
:ivar node: Nodes
:vartype node: {basestring: :class:`Node`}
:ivar groups: Groups of nodes
:vartype groups: {basestring: :class:`Group`}
:ivar policies: Policies
:vartype policies: {basestring: :class:`Policy`]}
:ivar substitution: The entire service can appear as a node
:vartype substitution: :class:`Substitution`
:ivar inputs: Externally provided parameters
:vartype inputs: {basestring: :class:`Input`}
:ivar outputs: These parameters are filled in after service installation
:vartype outputs: {basestring: :class:`Output`}
:ivar workflows: Custom workflows that can be performed on the service
:vartype workflows: {basestring: :class:`Operation`}
:ivar plugins: Plugins used by the service
:vartype plugins: {basestring: :class:`Plugin`}
:ivar created_at: Creation timestamp
:vartype created_at: :class:`datetime.datetime`
:ivar updated_at: Update timestamp
:vartype updated_at: :class:`datetime.datetime`
:ivar modifications: Modifications of this service
:vartype modifications: [:class:`ServiceModification`]
:ivar updates: Updates of this service
:vartype updates: [:class:`ServiceUpdate`]
:ivar executions: Executions on this service
:vartype executions: [:class:`Execution`]
"""
__tablename__ = 'service'
__private_fields__ = ['substitution_fk',
'service_template_fk']
# region foreign keys
@declared_attr
def substitution_fk(cls):
"""Service one-to-one to Substitution"""
return relationship.foreign_key('substitution', nullable=True)
@declared_attr
def service_template_fk(cls):
"""For Service many-to-one to ServiceTemplate"""
return relationship.foreign_key('service_template', nullable=True)
# endregion
# region association proxies
@declared_attr
def service_template_name(cls):
"""Required for use by SQLAlchemy queries"""
return association_proxy('service_template', 'name')
# endregion
# region one_to_one relationships
@declared_attr
def substitution(cls):
return relationship.one_to_one(cls, 'substitution', back_populates=relationship.NO_BACK_POP)
# endregion
# region one_to_many relationships
@declared_attr
def outputs(cls):
return relationship.one_to_many(cls, 'output', dict_key='name')
@declared_attr
def inputs(cls):
return relationship.one_to_many(cls, 'input', dict_key='name')
@declared_attr
def updates(cls):
return relationship.one_to_many(cls, 'service_update')
@declared_attr
def modifications(cls):
return relationship.one_to_many(cls, 'service_modification')
@declared_attr
def executions(cls):
return relationship.one_to_many(cls, 'execution')
@declared_attr
def nodes(cls):
return relationship.one_to_many(cls, 'node', dict_key='name')
@declared_attr
def groups(cls):
return relationship.one_to_many(cls, 'group', dict_key='name')
@declared_attr
def policies(cls):
return relationship.one_to_many(cls, 'policy', dict_key='name')
@declared_attr
def workflows(cls):
return relationship.one_to_many(cls, 'operation', dict_key='name')
# endregion
# region many_to_one relationships
@declared_attr
def service_template(cls):
return relationship.many_to_one(cls, 'service_template')
# endregion
# region many_to_many relationships
@declared_attr
def meta_data(cls):
# Warning! We cannot use the attr name "metadata" because it's used by SQLAlchemy!
return relationship.many_to_many(cls, 'metadata', dict_key='name')
@declared_attr
def plugins(cls):
return relationship.many_to_many(cls, 'plugin', dict_key='name')
# endregion
description = Column(Text)
created_at = Column(DateTime, nullable=False, index=True)
updated_at = Column(DateTime)
def satisfy_requirements(self):
satisfied = True
for node in self.nodes.itervalues():
if not node.satisfy_requirements():
satisfied = False
return satisfied
def validate_capabilities(self):
satisfied = True
for node in self.nodes.itervalues():
if not node.validate_capabilities():
satisfied = False
return satisfied
def find_hosts(self):
for node in self.nodes.itervalues():
node.find_host()
def configure_operations(self):
for node in self.nodes.itervalues():
node.configure_operations()
for group in self.groups.itervalues():
group.configure_operations()
for operation in self.workflows.itervalues():
operation.configure()
def is_node_a_target(self, target_node):
for node in self.nodes.itervalues():
if self._is_node_a_target(node, target_node):
return True
return False
def _is_node_a_target(self, source_node, target_node):
if source_node.outbound_relationships:
for relationship_model in source_node.outbound_relationships:
if relationship_model.target_node.name == target_node.name:
return True
else:
node = relationship_model.target_node
if node is not None:
if self._is_node_a_target(node, target_node):
return True
return False
@property
def as_raw(self):
return collections.OrderedDict((
('description', self.description),
('metadata', formatting.as_raw_dict(self.meta_data)),
('nodes', formatting.as_raw_list(self.nodes)),
('groups', formatting.as_raw_list(self.groups)),
('policies', formatting.as_raw_list(self.policies)),
('substitution', formatting.as_raw(self.substitution)),
('inputs', formatting.as_raw_dict(self.inputs)),
('outputs', formatting.as_raw_dict(self.outputs)),
('workflows', formatting.as_raw_list(self.workflows))))
def validate(self):
utils.validate_dict_values(self.meta_data)
utils.validate_dict_values(self.nodes)
utils.validate_dict_values(self.groups)
utils.validate_dict_values(self.policies)
if self.substitution is not None:
self.substitution.validate()
utils.validate_dict_values(self.inputs)
utils.validate_dict_values(self.outputs)
utils.validate_dict_values(self.workflows)
def coerce_values(self, report_issues):
utils.coerce_dict_values(self.meta_data, report_issues)
utils.coerce_dict_values(self.nodes, report_issues)
utils.coerce_dict_values(self.groups, report_issues)
utils.coerce_dict_values(self.policies, report_issues)
if self.substitution is not None:
self.substitution.coerce_values(report_issues)
utils.coerce_dict_values(self.inputs, report_issues)
utils.coerce_dict_values(self.outputs, report_issues)
utils.coerce_dict_values(self.workflows, report_issues)
def dump(self):
context = ConsumptionContext.get_thread_local()
if self.description is not None:
console.puts(context.style.meta(self.description))
utils.dump_dict_values(self.meta_data, 'Metadata')
for node in self.nodes.itervalues():
node.dump()
for group in self.groups.itervalues():
group.dump()
for policy in self.policies.itervalues():
policy.dump()
if self.substitution is not None:
self.substitution.dump()
utils.dump_dict_values(self.inputs, 'Inputs')
utils.dump_dict_values(self.outputs, 'Outputs')
utils.dump_dict_values(self.workflows, 'Workflows')
def dump_graph(self):
for node in self.nodes.itervalues():
if not self.is_node_a_target(node):
self._dump_graph_node(node)
def _dump_graph_node(self, node, capability=None):
context = ConsumptionContext.get_thread_local()
console.puts(context.style.node(node.name))
if capability is not None:
console.puts('{0} ({1})'.format(context.style.property(capability.name),
context.style.type(capability.type.name)))
if node.outbound_relationships:
with context.style.indent:
for relationship_model in node.outbound_relationships:
relationship_name = context.style.property(relationship_model.name)
if relationship_model.type is not None:
console.puts('-> {0} ({1})'.format(relationship_name,
context.style.type(
relationship_model.type.name)))
else:
console.puts('-> {0}'.format(relationship_name))
with console.indent(3):
self._dump_graph_node(relationship_model.target_node,
relationship_model.target_capability)
class NodeBase(InstanceModelMixin):
"""
Usually an instance of a :class:`NodeTemplate`.
Nodes may have zero or more :class:`Relationship` instances to other nodes.
:ivar name: Name (unique for this service)
:vartype name: basestring
:ivar node_template: Template from which this node was instantiated (optional)
:vartype node_template: :class:`NodeTemplate`
:ivar type: Node type
:vartype type: :class:`Type`
:ivar description: Human-readable description
:vartype description: string
:ivar properties: Associated parameters
:vartype properties: {basestring: :class:`Property`}
:ivar interfaces: Bundles of operations
:vartype interfaces: {basestring: :class:`Interface`}
:ivar artifacts: Associated files
:vartype artifacts: {basestring: :class:`Artifact`}
:ivar capabilities: Exposed capabilities
:vartype capabilities: {basestring: :class:`Capability`}
:ivar outbound_relationships: Relationships to other nodes
:vartype outbound_relationships: [:class:`Relationship`]
:ivar inbound_relationships: Relationships from other nodes
:vartype inbound_relationships: [:class:`Relationship`]
:ivar host: Host node (can be self)
:vartype host: :class:`Node`
:ivar state: The state of the node, according to to the TOSCA-defined node states
:vartype state: string
:ivar version: Used by `aria.storage.instrumentation`
:vartype version: int
:ivar service: Containing service
:vartype service: :class:`Service`
:ivar groups: We are a member of these groups
:vartype groups: [:class:`Group`]
:ivar policies: Policies enacted on this node
:vartype policies: [:class:`Policy`]
:ivar substitution_mapping: Our contribution to service substitution
:vartype substitution_mapping: :class:`SubstitutionMapping`
:ivar tasks: Tasks for this node
:vartype tasks: [:class:`Task`]
:ivar hosted_tasks: Tasks on this node
:vartype hosted_tasks: [:class:`Task`]
"""
__tablename__ = 'node'
__private_fields__ = ['type_fk',
'host_fk',
'service_fk',
'node_template_fk']
INITIAL = 'initial'
CREATING = 'creating'
CREATED = 'created'
CONFIGURING = 'configuring'
CONFIGURED = 'configured'
STARTING = 'starting'
STARTED = 'started'
STOPPING = 'stopping'
DELETING = 'deleting'
# 'deleted' isn't actually part of the tosca spec, since according the description of the
# 'deleting' state: "Node is transitioning from its current state to one where it is deleted and
# its state is no longer tracked by the instance model."
# However, we prefer to be able to retrieve information about deleted nodes, so we chose to add
# this 'deleted' state to enable us to do so.
DELETED = 'deleted'
ERROR = 'error'
STATES = [INITIAL, CREATING, CREATED, CONFIGURING, CONFIGURED, STARTING, STARTED, STOPPING,
DELETING, DELETED, ERROR]
_op_to_state = {'create': {'transitional': CREATING, 'finished': CREATED},
'configure': {'transitional': CONFIGURING, 'finished': CONFIGURED},
'start': {'transitional': STARTING, 'finished': STARTED},
'stop': {'transitional': STOPPING, 'finished': CONFIGURED},
'delete': {'transitional': DELETING, 'finished': DELETED}}
@classmethod
def determine_state(cls, op_name, is_transitional):
""" :returns the state the node should be in as a result of running the
operation on this node.
e.g. if we are running tosca.interfaces.node.lifecycle.Standard.create, then
the resulting state should either 'creating' (if the task just started) or 'created'
(if the task ended).
If the operation is not a standard tosca lifecycle operation, then we return None"""
state_type = 'transitional' if is_transitional else 'finished'
try:
return cls._op_to_state[op_name][state_type]
except KeyError:
return None
def is_available(self):
return self.state not in (self.INITIAL, self.DELETED, self.ERROR)
# region foreign_keys
@declared_attr
def type_fk(cls):
"""For Node many-to-one to Type"""
return relationship.foreign_key('type')
@declared_attr
def host_fk(cls):
"""For Node one-to-one to Node"""
return relationship.foreign_key('node', nullable=True)
@declared_attr
def service_fk(cls):
"""For Service one-to-many to Node"""
return relationship.foreign_key('service')
@declared_attr
def node_template_fk(cls):
"""For Node many-to-one to NodeTemplate"""
return relationship.foreign_key('node_template')
# endregion
# region association proxies
@declared_attr
def service_name(cls):
"""Required for use by SQLAlchemy queries"""
return association_proxy('service', 'name')
@declared_attr
def node_template_name(cls):
"""Required for use by SQLAlchemy queries"""
return association_proxy('node_template', 'name')
# endregion
# region one_to_one relationships
@declared_attr
def host(cls): # pylint: disable=method-hidden
return relationship.one_to_one_self(cls, 'host_fk')
# endregion
# region one_to_many relationships
@declared_attr
def tasks(cls):
return relationship.one_to_many(cls, 'task')
@declared_attr
def interfaces(cls):
return relationship.one_to_many(cls, 'interface', dict_key='name')
@declared_attr
def properties(cls):
return relationship.one_to_many(cls, 'property', dict_key='name')
@declared_attr
def attributes(cls):
return relationship.one_to_many(cls, 'attribute', dict_key='name')
@declared_attr
def artifacts(cls):
return relationship.one_to_many(cls, 'artifact', dict_key='name')
@declared_attr
def capabilities(cls):
return relationship.one_to_many(cls, 'capability', dict_key='name')
@declared_attr
def outbound_relationships(cls):
return relationship.one_to_many(
cls, 'relationship', other_fk='source_node_fk', back_populates='source_node',
rel_kwargs=dict(
order_by='Relationship.source_position',
collection_class=ordering_list('source_position', count_from=0)
)
)
@declared_attr
def inbound_relationships(cls):
return relationship.one_to_many(
cls, 'relationship', other_fk='target_node_fk', back_populates='target_node',
rel_kwargs=dict(
order_by='Relationship.target_position',
collection_class=ordering_list('target_position', count_from=0)
)
)
# endregion
# region many_to_one relationships
@declared_attr
def service(cls):
return relationship.many_to_one(cls, 'service')
@declared_attr
def node_template(cls):
return relationship.many_to_one(cls, 'node_template')
@declared_attr
def type(cls):
return relationship.many_to_one(cls, 'type', back_populates=relationship.NO_BACK_POP)
# endregion
description = Column(Text)
state = Column(Enum(*STATES, name='node_state'), nullable=False, default=INITIAL)
version = Column(Integer, default=1)
__mapper_args__ = {'version_id_col': version} # Enable SQLAlchemy automatic version counting
@property
def host_address(self):
if self.host and self.host.attributes:
attribute = self.host.attributes.get('ip')
return attribute.value if attribute else None
return None
def satisfy_requirements(self):
node_template = self.node_template
satisfied = True
for requirement_template in node_template.requirement_templates:
# Find target template
target_node_template, target_node_capability = \
requirement_template.find_target(node_template)
if target_node_template is not None:
satisfied = self._satisfy_capability(target_node_capability,
target_node_template,
requirement_template)
else:
context = ConsumptionContext.get_thread_local()
context.validation.report('requirement "{0}" of node "{1}" has no target node '
'template'.format(requirement_template.name, self.name),
level=validation.Issue.BETWEEN_INSTANCES)
satisfied = False
return satisfied
def _satisfy_capability(self, target_node_capability, target_node_template,
requirement_template):
from . import models
context = ConsumptionContext.get_thread_local()
# Find target nodes
target_nodes = target_node_template.nodes
if target_nodes:
target_node = None
target_capability = None
if target_node_capability is not None:
# Relate to the first target node that has capacity
for node in target_nodes:
a_target_capability = node.capabilities.get(target_node_capability.name)
if a_target_capability.relate():
target_node = node
target_capability = a_target_capability
break
else:
# Use first target node
target_node = target_nodes[0]
if target_node is not None:
if requirement_template.relationship_template is not None:
relationship_model = \
requirement_template.relationship_template.instantiate(self)
else:
relationship_model = models.Relationship()
relationship_model.name = requirement_template.name
relationship_model.requirement_template = requirement_template
relationship_model.target_node = target_node
relationship_model.target_capability = target_capability
self.outbound_relationships.append(relationship_model)
return True
else:
context.validation.report('requirement "{0}" of node "{1}" targets node '
'template "{2}" but its instantiated nodes do not '
'have enough capacity'.format(
requirement_template.name,
self.name,
target_node_template.name),
level=validation.Issue.BETWEEN_INSTANCES)
return False
else:
context.validation.report('requirement "{0}" of node "{1}" targets node template '
'"{2}" but it has no instantiated nodes'.format(
requirement_template.name,
self.name,
target_node_template.name),
level=validation.Issue.BETWEEN_INSTANCES)
return False
def validate_capabilities(self):
context = ConsumptionContext.get_thread_local()
satisfied = False
for capability in self.capabilities.itervalues():
if not capability.has_enough_relationships:
context.validation.report('capability "{0}" of node "{1}" requires at least {2:d} '
'relationships but has {3:d}'.format(
capability.name,
self.name,
capability.min_occurrences,
capability.occurrences),
level=validation.Issue.BETWEEN_INSTANCES)
satisfied = False
return satisfied
def find_host(self):
def _find_host(node):
if node.type.role == 'host':
return node
for the_relationship in node.outbound_relationships:
if (the_relationship.target_capability is not None) and \
the_relationship.target_capability.type.role == 'host':
host = _find_host(the_relationship.target_node)
if host is not None:
return host
for the_relationship in node.inbound_relationships:
if (the_relationship.target_capability is not None) and \
the_relationship.target_capability.type.role == 'feature':
host = _find_host(the_relationship.source_node)
if host is not None:
return host
return None
self.host = _find_host(self)
def configure_operations(self):
for interface in self.interfaces.itervalues():
interface.configure_operations()
for the_relationship in self.outbound_relationships:
the_relationship.configure_operations()
@property
def as_raw(self):
return collections.OrderedDict((
('name', self.name),
('type_name', self.type.name),
('properties', formatting.as_raw_dict(self.properties)),
('attributes', formatting.as_raw_dict(self.properties)),
('interfaces', formatting.as_raw_list(self.interfaces)),
('artifacts', formatting.as_raw_list(self.artifacts)),
('capabilities', formatting.as_raw_list(self.capabilities)),
('relationships', formatting.as_raw_list(self.outbound_relationships))))
def validate(self):
context = ConsumptionContext.get_thread_local()
if len(self.name) > context.modeling.id_max_length:
context.validation.report('"{0}" has an ID longer than the limit of {1:d} characters: '
'{2:d}'.format(
self.name,
context.modeling.id_max_length,
len(self.name)),
level=validation.Issue.BETWEEN_INSTANCES)
# TODO: validate that node template is of type?
utils.validate_dict_values(self.properties)
utils.validate_dict_values(self.attributes)
utils.validate_dict_values(self.interfaces)
utils.validate_dict_values(self.artifacts)
utils.validate_dict_values(self.capabilities)
utils.validate_list_values(self.outbound_relationships)
def coerce_values(self, report_issues):
utils.coerce_dict_values(self.properties, report_issues)
utils.coerce_dict_values(self.attributes, report_issues)
utils.coerce_dict_values(self.interfaces, report_issues)
utils.coerce_dict_values(self.artifacts, report_issues)
utils.coerce_dict_values(self.capabilities, report_issues)
utils.coerce_list_values(self.outbound_relationships, report_issues)
def dump(self):
context = ConsumptionContext.get_thread_local()
console.puts('Node: {0}'.format(context.style.node(self.name)))
with context.style.indent:
console.puts('Type: {0}'.format(context.style.type(self.type.name)))
console.puts('Template: {0}'.format(context.style.node(self.node_template.name)))
utils.dump_dict_values(self.properties, 'Properties')
utils.dump_dict_values(self.attributes, 'Attributes')
utils.dump_interfaces(self.interfaces)
utils.dump_dict_values(self.artifacts, 'Artifacts')
utils.dump_dict_values(self.capabilities, 'Capabilities')
utils.dump_list_values(self.outbound_relationships, 'Relationships')
class GroupBase(InstanceModelMixin):
"""
Usually an instance of a :class:`GroupTemplate`.
:ivar name: Name (unique for this service)
:vartype name: basestring
:ivar group_template: Template from which this group was instantiated (optional)
:vartype group_template: :class:`GroupTemplate`
:ivar type: Group type
:vartype type: :class:`Type`
:ivar description: Human-readable description
:vartype description: string
:ivar nodes: Members of this group
:vartype nodes: [:class:`Node`]
:ivar properties: Associated parameters
:vartype properties: {basestring: :class:`Property`}
:ivar interfaces: Bundles of operations
:vartype interfaces: {basestring: :class:`Interface`}
:ivar service: Containing service
:vartype service: :class:`Service`
:ivar policies: Policies enacted on this group
:vartype policies: [:class:`Policy`]
"""
__tablename__ = 'group'
__private_fields__ = ['type_fk', 'service_fk', 'group_template_fk']
# region foreign_keys
@declared_attr
def type_fk(cls):
"""For Group many-to-one to Type"""
return relationship.foreign_key('type')
@declared_attr
def service_fk(cls):
"""For Service one-to-many to Group"""
return relationship.foreign_key('service')
@declared_attr
def group_template_fk(cls):
"""For Group many-to-one to GroupTemplate"""
return relationship.foreign_key('group_template', nullable=True)
# endregion
# region association proxies
# endregion
# region one_to_one relationships
# endregion
# region one_to_many relationships
@declared_attr
def properties(cls):
return relationship.one_to_many(cls, 'property', dict_key='name')
@declared_attr
def interfaces(cls):
return relationship.one_to_many(cls, 'interface', dict_key='name')
# endregion
# region many_to_one relationships
@declared_attr
def service(cls):
return relationship.many_to_one(cls, 'service')
@declared_attr
def group_template(cls):
return relationship.many_to_one(cls, 'group_template')
@declared_attr
def type(cls):
return relationship.many_to_one(cls, 'type', back_populates=relationship.NO_BACK_POP)
# endregion
# region many_to_many relationships
@declared_attr
def nodes(cls):
return relationship.many_to_many(cls, 'node')
# endregion
description = Column(Text)
def configure_operations(self):
for interface in self.interfaces.itervalues():
interface.configure_operations()
@property
def as_raw(self):
return collections.OrderedDict((
('name', self.name),
('properties', formatting.as_raw_dict(self.properties)),
('interfaces', formatting.as_raw_list(self.interfaces))))
def validate(self):
utils.validate_dict_values(self.properties)
utils.validate_dict_values(self.interfaces)
def coerce_values(self, report_issues):
utils.coerce_dict_values(self.properties, report_issues)
utils.coerce_dict_values(self.interfaces, report_issues)
def dump(self):
context = ConsumptionContext.get_thread_local()
console.puts('Group: {0}'.format(context.style.node(self.name)))
with context.style.indent:
console.puts('Type: {0}'.format(context.style.type(self.type.name)))
utils.dump_dict_values(self.properties, 'Properties')
utils.dump_interfaces(self.interfaces)
if self.nodes:
console.puts('Member nodes:')
with context.style.indent:
for node in self.nodes:
console.puts(context.style.node(node.name))
class PolicyBase(InstanceModelMixin):
"""
Usually an instance of a :class:`PolicyTemplate`.
:ivar name: Name (unique for this service)
:vartype name: basestring
:ivar policy_template: Template from which this policy was instantiated (optional)
:vartype policy_template: :class:`PolicyTemplate`
:ivar type: Policy type
:vartype type: :class:`Type`
:ivar description: Human-readable description
:vartype description: string
:ivar nodes: Policy will be enacted on all these nodes
:vartype nodes: [:class:`Node`]
:ivar groups: Policy will be enacted on all nodes in these groups
:vartype groups: [:class:`Group`]
:ivar properties: Associated parameters
:vartype properties: {basestring: :class:`Property`}
:ivar service: Containing service
:vartype service: :class:`Service`
"""
__tablename__ = 'policy'
__private_fields__ = ['type_fk', 'service_fk', 'policy_template_fk']
# region foreign_keys
@declared_attr
def type_fk(cls):
"""For Policy many-to-one to Type"""
return relationship.foreign_key('type')
@declared_attr
def service_fk(cls):
"""For Service one-to-many to Policy"""
return relationship.foreign_key('service')
@declared_attr
def policy_template_fk(cls):
"""For Policy many-to-one to PolicyTemplate"""
return relationship.foreign_key('policy_template', nullable=True)
# endregion
# region association proxies
# endregion
# region one_to_one relationships
# endregion
# region one_to_many relationships
@declared_attr
def properties(cls):
return relationship.one_to_many(cls, 'property', dict_key='name')
# endregion
# region many_to_one relationships
@declared_attr
def service(cls):
return relationship.many_to_one(cls, 'service')
@declared_attr
def policy_template(cls):
return relationship.many_to_one(cls, 'policy_template')
@declared_attr
def type(cls):
return relationship.many_to_one(cls, 'type', back_populates=relationship.NO_BACK_POP)
# endregion
# region many_to_many relationships
@declared_attr
def nodes(cls):
return relationship.many_to_many(cls, 'node')
@declared_attr
def groups(cls):
return relationship.many_to_many(cls, 'group')
# endregion
description = Column(Text)
@property
def as_raw(self):
return collections.OrderedDict((
('name', self.name),
('type_name', self.type.name),
('properties', formatting.as_raw_dict(self.properties))))
def validate(self):
utils.validate_dict_values(self.properties)
def coerce_values(self, report_issues):
utils.coerce_dict_values(self.properties, report_issues)
def dump(self):
context = ConsumptionContext.get_thread_local()
console.puts('Policy: {0}'.format(context.style.node(self.name)))
with context.style.indent:
console.puts('Type: {0}'.format(context.style.type(self.type.name)))
utils.dump_dict_values(self.properties, 'Properties')
if self.nodes:
console.puts('Target nodes:')
with context.style.indent:
for node in self.nodes:
console.puts(context.style.node(node.name))
if self.groups:
console.puts('Target groups:')
with context.style.indent:
for group in self.groups:
console.puts(context.style.node(group.name))
class SubstitutionBase(InstanceModelMixin):
"""
Used to substitute a single node for the entire deployment.
Usually an instance of a :class:`SubstitutionTemplate`.
:ivar substitution_template: Template from which this substitution was instantiated (optional)
:vartype substitution_template: :class:`SubstitutionTemplate`
:ivar node_type: Exposed node type
:vartype node_type: :class:`Type`
:ivar mappings: Requirement and capability mappings
:vartype mappings: {basestring: :class:`SubstitutionTemplate`}
:ivar service: Containing service
:vartype service: :class:`Service`
"""
__tablename__ = 'substitution'
__private_fields__ = ['node_type_fk',
'substitution_template_fk']
# region foreign_keys
@declared_attr
def node_type_fk(cls):
"""For Substitution many-to-one to Type"""
return relationship.foreign_key('type')
@declared_attr
def substitution_template_fk(cls):
"""For Substitution many-to-one to SubstitutionTemplate"""
return relationship.foreign_key('substitution_template', nullable=True)
# endregion
# region association proxies
# endregion
# region one_to_one relationships
# endregion
# region one_to_many relationships
@declared_attr
def mappings(cls):
return relationship.one_to_many(cls, 'substitution_mapping', dict_key='name')
# endregion
# region many_to_one relationships
@declared_attr