1
- from cloudify import ctx
2
1
from mistclient import MistClient
2
+ from cloudify import ctx
3
3
from cloudify .exceptions import NonRecoverableError
4
4
5
5
@@ -8,47 +8,52 @@ class MistConnectionClient(object):
8
8
"""Provides functions for getting the Mist Client
9
9
"""
10
10
11
- def __init__ (self ):
11
+ def __init__ (self , ** kwargs ):
12
12
self ._client = None
13
13
self ._cloud = None
14
14
self ._machine = None
15
-
15
+ if kwargs .get ("properties" ):
16
+ self .properties = kwargs .get ("properties" )
17
+ self .ctx = False
18
+ else :
19
+ self .properties = ctx .node .properties
20
+ self .ctx = True
16
21
@property
17
22
def client (self ):
18
23
"""Represents the MistConnection Client
19
24
"""
20
25
if self ._client is None :
21
- if ctx . node .properties ['mist_config' ].get ("mist_uri" ):
22
- mist_uri = ctx . node .properties ['mist_config' ]["mist_uri" ]
26
+ if self .properties ['mist_config' ].get ("mist_uri" ):
27
+ mist_uri = self .properties ['mist_config' ]["mist_uri" ]
23
28
else :
24
29
mist_uri = "https://mist.io"
25
- if ctx . node .properties ['mist_config' ].get ("api_token" ):
26
- token = ctx . node .properties ['mist_config' ]['api_token' ]
30
+ if self .properties ['mist_config' ].get ("api_token" ):
31
+ token = self .properties ['mist_config' ]['api_token' ]
27
32
self ._client = MistClient (mist_uri = mist_uri ,
28
33
api_token = token )
29
34
else :
30
35
self ._client = MistClient (mist_uri = mist_uri ,
31
- email = ctx . node .properties ['mist_config' ]['username' ],
32
- password = ctx . node .properties ['mist_config' ]['password' ])
36
+ email = self .properties ['mist_config' ]['username' ],
37
+ password = self .properties ['mist_config' ]['password' ])
33
38
return self ._client
34
39
35
40
@property
36
41
def cloud (self ):
37
42
"""Represents the Mist Cloud
38
43
"""
39
44
if self ._cloud is None :
40
- if ctx . node .properties ['parameters' ].get ("cloud_id" ):
45
+ if self .properties ['parameters' ].get ("cloud_id" ):
41
46
self ._cloud = self .client .clouds (
42
- id = ctx . node .properties ['parameters' ]['cloud_id' ])[0 ]
43
- elif ctx . node .properties ['parameters' ].get ("cloud_name" ):
44
- cloud_search = self .client .clouds (search = ctx . node .properties ['parameters' ][
47
+ id = self .properties ['parameters' ]['cloud_id' ])[0 ]
48
+ elif self .properties ['parameters' ].get ("cloud_name" ):
49
+ cloud_search = self .client .clouds (search = self .properties ['parameters' ][
45
50
'cloud_name' ])
46
51
if len (cloud_search ) > 1 :
47
52
raise NonRecoverableError ("Found more then one cloud with name {0}" .format (
48
- ctx . node .properties ['parameters' ]['cloud_name' ]))
53
+ self .properties ['parameters' ]['cloud_name' ]))
49
54
elif len (cloud_search ) == 0 :
50
55
raise NonRecoverableError ("Did not find cloud with name {0}" .format (
51
- ctx . node .properties ['parameters' ]['cloud_name' ]))
56
+ self .properties ['parameters' ]['cloud_name' ]))
52
57
self ._cloud = cloud_search [0 ]
53
58
return self ._cloud
54
59
@@ -57,12 +62,13 @@ def machine(self):
57
62
"""Represents a Mist Machine
58
63
"""
59
64
self .cloud .update_machines ()
60
- if ctx .node .properties .get ('use_external_resource' ,'' ):
61
- ctx .logger .info ('use external resource enabled' )
62
- if not ctx .node .properties ["resource_id" ]:
65
+ if self .properties .get ('use_external_resource' ,'' ):
66
+ if self .ctx :
67
+ ctx .logger .info ('use external resource enabled' )
68
+ if not self .properties ["resource_id" ]:
63
69
raise NonRecoverableError (
64
70
"Cannot use external resource without defining resource_id" )
65
- machines = self .cloud .machines (id = ctx . node .properties ["resource_id" ])
71
+ machines = self .cloud .machines (id = self .properties ["resource_id" ])
66
72
if not len (machines ):
67
73
raise NonRecoverableError (
68
74
"External resource not found" )
@@ -71,15 +77,41 @@ def machine(self):
71
77
"External resource state {0}" .format (machines [0 ].info ["state" ]))
72
78
return machines [0 ]
73
79
80
+ if self .ctx :
81
+ if ctx .instance .runtime_properties .get ('machine_id' ):
82
+ return self .cloud .machines (id = ctx .instance .runtime_properties ['machine_id' ])[0 ]
83
+ machines = self .cloud .machines (search = self .properties ['parameters' ]["name" ])
84
+ if len (machines ) > 1 :
85
+ if self .ctx :
86
+ ctx .logger .info ('Found multiple machines with the same name' )
87
+ for m in machines :
88
+ if m .info ["state" ] in ["running" , "stopped" ]:
89
+ machines [0 ] = m
90
+ break
91
+ if self .ctx :
92
+ ctx .instance .runtime_properties ['machine_id' ]= machines [0 ].info ["id" ]
93
+ return machines [0 ]
94
+
95
+
96
+ def other_machine (self , kwargs ):
97
+ self .cloud .update_machines ()
98
+ if kwargs .get ('use_external_resource' ,'' ):
99
+ if not kwargs ["resource_id" ]:
100
+ raise NonRecoverableError (
101
+ "Cannot use external resource without defining resource_id" )
102
+ machines = self .cloud .machines (id = kwargs ["resource_id" ])
103
+ if not len (machines ):
104
+ raise NonRecoverableError (
105
+ "External resource not found" )
106
+ if machines [0 ].info ["state" ] in ["error" ,"terminated" ]:
107
+ raise NonRecoverableError (
108
+ "External resource state {0}" .format (machines [0 ].info ["state" ]))
109
+ return machines [0 ]
74
110
75
- if ctx .instance .runtime_properties .get ('machine_id' ):
76
- return self .cloud .machines (id = ctx .instance .runtime_properties ['machine_id' ])[0 ]
77
- machines = self .cloud .machines (search = ctx .node .properties ['parameters' ]["name" ])
111
+ machines = self .cloud .machines (search = kwargs ["name" ])
78
112
if len (machines ) > 1 :
79
- ctx .logger .info ('Found multiple machines with the same name' )
80
113
for m in machines :
81
114
if m .info ["state" ] in ["running" ,"stopped" ]:
82
115
machines [0 ] = m
83
116
break
84
- ctx .instance .runtime_properties ['machine_id' ]= machines [0 ].info ["id" ]
85
117
return machines [0 ]
0 commit comments