This repository has been archived by the owner on Oct 5, 2020. It is now read-only.
/
node_instances.py
142 lines (120 loc) · 4.14 KB
/
node_instances.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
########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed 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.
__author__ = 'idanmo'
class NodeInstance(dict):
"""
Cloudify node instance.
"""
def __init__(self, node_instance):
self.update(node_instance)
@property
def id(self):
"""
:return: The identifier of the node instance.
"""
return self['id']
@property
def node_id(self):
"""
:return: The identifier of the node whom this is in instance of.
"""
return self['node_id']
@property
def relationships(self):
"""
:return: The node instance relationships.
"""
return self['relationships']
@property
def host_id(self):
"""
:return: The node instance host_id.
"""
return self['host_id']
@property
def deployment_id(self):
"""
:return: The deployment id the node instance belongs to.
"""
return self['deployment_id']
@property
def runtime_properties(self):
"""
:return: The runtime properties of the node instance.
"""
return self['runtime_properties']
@property
def state(self):
"""
:return: The current state of the node instance.
"""
return self['state']
@property
def version(self):
"""
:return: The current version of the node instance
(used for optimistic locking on update)
"""
return self['version']
class NodeInstancesClient(object):
def __init__(self, api):
self.api = api
@staticmethod
def _get_node_instance_uri(node_instance_id):
return '/node-instances/{0}'.format(node_instance_id)
def get(self, node_instance_id):
"""
Returns the node instance for the provided node instance id.
:param node_instance_id: The identifier of the node instance to get.
:return: The retrieved node instance.
"""
assert node_instance_id
uri = self._get_node_instance_uri(node_instance_id)
response = self.api.get(uri)
return NodeInstance(response)
def update(self,
node_instance_id,
state=None,
runtime_properties=None,
version=0):
"""
Update node instance with the provided state & runtime_properties.
:param node_instance_id: The identifier of the node instance to update.
:param state: The updated state.
:param runtime_properties: The updated runtime properties.
:param version: Current version value of this node instance in
Cloudify's storage (used for optimistic locking).
:return: The updated node instance.
"""
assert node_instance_id
uri = self._get_node_instance_uri(node_instance_id)
data = {'version': version}
if runtime_properties:
data['runtime_properties'] = runtime_properties
if state:
data['state'] = state
response = self.api.patch(uri, data=data)
return NodeInstance(response)
def list(self, deployment_id=None):
"""
Returns a list of node instances which belong to the deployment
identified by the provided deployment id.
:param deployment_id: The deployment's id to list node instances for.
:return: Node instances.
:rtype: list
"""
params = {'deployment_id': deployment_id} if deployment_id else None
response = self.api.get('/node-instances', params=params)
return [NodeInstance(item) for item in response]