16
16
class BasicObject (object ):
17
17
_api_version = 'v1'
18
18
19
- def __init__ (self , ** kwargs ):
20
- pass
21
-
22
19
23
20
class Restful (BasicObject ):
24
- _detail_uri = None
25
- namespaced = True
21
+ is_namespaced = True
26
22
27
- def __init__ (self , ** kwargs ):
23
+ def __init__ (self , namespace = "" , ** kwargs ):
28
24
"""Simply reflect all the values in kwargs"""
29
25
for k , v in list (kwargs .items ()):
30
26
setattr (self , k , v )
27
+ if self .is_namespaced and namespace :
28
+ self ._namespace = namespace
29
+ else :
30
+ self ._namespace = dockercloud .namespace
31
+ self ._resource_uri = ""
31
32
32
33
def __addchanges__ (self , name ):
33
34
changed_attrs = self .__getchanges__ ()
@@ -38,7 +39,7 @@ def __addchanges__(self, name):
38
39
def __setattr__ (self , name , value ):
39
40
"""Keeps track of what attributes have been set"""
40
41
current_value = getattr (self , name , None )
41
- if value != current_value :
42
+ if value != current_value and not name . startswith ( "_" ) :
42
43
self .__addchanges__ (name )
43
44
super (Restful , self ).__setattr__ (name , value )
44
45
@@ -53,17 +54,10 @@ def __setchanges__(self, val):
53
54
54
55
def _loaddict (self , dict ):
55
56
"""Internal. Sets the model attributes to the dictionary values passed"""
56
- endpoint = getattr (self , 'endpoint' , None )
57
- subsystem = getattr (self , 'subsystem' , None )
58
- assert endpoint , "Endpoint not specified for %s" % self .__class__ .__name__
59
- assert subsystem , "Subsystem not specified for %s" % self .__class__ .__name__
60
57
for k , v in list (dict .items ()):
61
58
setattr (self , k , v )
62
- if self .namespaced and dockercloud .namespace :
63
- self ._detail_uri = "/" .join (["api" , subsystem , self ._api_version , dockercloud .namespace ,
64
- endpoint .strip ("/" ), self .pk ])
65
- else :
66
- self ._detail_uri = "/" .join (["api" , subsystem , self ._api_version , endpoint .strip ("/" ), self .pk ])
59
+
60
+ self ._resource_uri = getattr (self , "resource_uri" , None )
67
61
self .__setchanges__ ([])
68
62
69
63
@property
@@ -93,9 +87,9 @@ def is_dirty(self):
93
87
def _perform_action (self , action , params = None , data = {}):
94
88
"""Internal. Performs the specified action on the object remotely"""
95
89
success = False
96
- if not self ._detail_uri :
90
+ if not self ._resource_uri :
97
91
raise ApiError ("You must save the object before performing this operation" )
98
- path = "/" .join ([self ._detail_uri .rstrip ("/" ), action .lstrip ("/" )])
92
+ path = "/" .join ([self ._resource_uri .rstrip ("/" ), action .lstrip ("/" )])
99
93
json = send_request ("POST" , path , params = params , data = data )
100
94
if json :
101
95
self ._loaddict (json )
@@ -104,9 +98,9 @@ def _perform_action(self, action, params=None, data={}):
104
98
105
99
def _expand_attribute (self , attribute ):
106
100
"""Internal. Expands the given attribute from remote information"""
107
- if not self ._detail_uri :
101
+ if not self ._resource_uri :
108
102
raise ApiError ("You must save the object before performing this operation" )
109
- path = "/" .join ([self ._detail_uri , attribute ])
103
+ path = "/" .join ([self ._resource_uri , attribute ])
110
104
json = send_request ("GET" , path )
111
105
if json :
112
106
return json [attribute ]
@@ -125,39 +119,43 @@ def get_all_attributes(self):
125
119
126
120
class Immutable (Restful ):
127
121
@classmethod
128
- def fetch (cls , pk ):
129
- instance = None
122
+ def fetch (cls , pk , namespace = "" ):
130
123
endpoint = getattr (cls , 'endpoint' , None )
131
124
subsystem = getattr (cls , 'subsystem' , None )
132
125
assert endpoint , "Endpoint not specified for %s" % cls .__name__
133
126
assert subsystem , "Subsystem not specified for %s" % cls .__name__
134
- if cls .namespaced and dockercloud .namespace :
135
- detail_uri = "/" .join (["api" , subsystem , cls ._api_version , dockercloud .namespace , endpoint .strip ("/" ), pk ])
127
+
128
+ if not namespace :
129
+ namespace = dockercloud .namespace
130
+ if cls .is_namespaced and namespace :
131
+ resource_uri = "/" .join (["api" , subsystem , cls ._api_version , namespace , endpoint .strip ("/" ), pk ])
136
132
else :
137
- detail_uri = "/" .join (["api" , subsystem , cls ._api_version , endpoint .strip ("/" ), pk ])
138
- json = send_request ('GET' , detail_uri )
133
+ resource_uri = "/" .join (["api" , subsystem , cls ._api_version , endpoint .strip ("/" ), pk ])
134
+ json = send_request ('GET' , resource_uri )
139
135
if json :
140
136
instance = cls ()
141
137
instance ._loaddict (json )
142
138
return instance
143
139
144
140
@classmethod
145
- def list (cls , limit = None , ** kwargs ):
141
+ def list (cls , limit = None , namespace = "" , ** kwargs ):
146
142
restful = []
147
143
endpoint = getattr (cls , 'endpoint' , None )
148
144
subsystem = getattr (cls , 'subsystem' , None )
149
145
assert endpoint , "Endpoint not specified for %s" % cls .__name__
150
146
assert subsystem , "Subsystem not specified for %s" % cls .__name__
151
147
152
- if cls .namespaced and dockercloud .namespace :
153
- detail_uri = "/" .join (["api" , subsystem , cls ._api_version , dockercloud .namespace , endpoint .strip ("/" )])
148
+ if not namespace :
149
+ namespace = dockercloud .namespace
150
+ if cls .is_namespaced and namespace :
151
+ resource_uri = "/" .join (["api" , subsystem , cls ._api_version , namespace , endpoint .strip ("/" )])
154
152
else :
155
- detail_uri = "/" .join (["api" , subsystem , cls ._api_version , endpoint .strip ("/" )])
153
+ resource_uri = "/" .join (["api" , subsystem , cls ._api_version , endpoint .strip ("/" )])
156
154
objects = []
157
155
while True :
158
156
if limit and len (objects ) >= limit :
159
157
break
160
- json = send_request ('GET' , detail_uri , params = kwargs )
158
+ json = send_request ('GET' , resource_uri , params = kwargs )
161
159
objs = json .get ('objects' , [])
162
160
meta = json .get ('meta' , {})
163
161
next_url = meta .get ('next' , '' )
@@ -182,10 +180,10 @@ def refresh(self, force=False):
182
180
if self .is_dirty and not force :
183
181
# We have local non-committed changes - rejecting the refresh
184
182
success = False
185
- elif not self ._detail_uri :
183
+ elif not self ._resource_uri :
186
184
raise ApiError ("You must save the object before performing this operation" )
187
185
else :
188
- json = send_request ("GET" , self ._detail_uri )
186
+ json = send_request ("GET" , self ._resource_uri )
189
187
if json :
190
188
self ._loaddict (json )
191
189
success = True
@@ -202,16 +200,17 @@ def create(cls, **kwargs):
202
200
return cls (** kwargs )
203
201
204
202
def delete (self ):
205
- if not self ._detail_uri :
203
+ if not self ._resource_uri :
206
204
raise ApiError ("You must save the object before performing this operation" )
207
205
action = "DELETE"
208
- url = self ._detail_uri
206
+ url = self ._resource_uri
209
207
json = send_request (action , url )
210
208
if json :
211
209
self ._loaddict (json )
210
+ self ._resource_uri = None
212
211
else :
213
212
# Object deleted successfully and nothing came back - deleting PK reference.
214
- self ._detail_uri = None
213
+ self ._resource_uri = None
215
214
# setattr(self, self._pk_key(), None) -- doesn't work
216
215
self .__setchanges__ ([])
217
216
return True
@@ -228,15 +227,15 @@ def save(self):
228
227
assert endpoint , "Endpoint not specified for %s" % self .__class__ .__name__
229
228
assert subsystem , "Subsystem not specified for %s" % self .__class__ .__name__
230
229
# Figure out whether we should do a create or update
231
- if not self ._detail_uri :
230
+ if not self ._resource_uri :
232
231
action = "POST"
233
- if cls .namespaced and dockercloud . namespace :
234
- path = "/" .join (["api" , subsystem , self ._api_version , dockercloud . namespace , endpoint .lstrip ("/" )])
232
+ if cls .is_namespaced and self . _namespace :
233
+ path = "/" .join (["api" , subsystem , self ._api_version , self . _namespace , endpoint .lstrip ("/" )])
235
234
else :
236
235
path = "/" .join (["api" , subsystem , self ._api_version , endpoint .lstrip ("/" )])
237
236
else :
238
237
action = "PATCH"
239
- path = self ._detail_uri
238
+ path = self ._resource_uri
240
239
# Construct the necessary params
241
240
params = {}
242
241
for attr in self .__getchanges__ ():
@@ -322,13 +321,16 @@ def run_forever(self, *args, **kwargs):
322
321
323
322
324
323
class StreamingLog (StreamingAPI ):
325
- def __init__ (self , subsystem , resource , uuid , tail , follow ):
324
+ def __init__ (self , subsystem , resource , uuid , tail , follow , namespace = "" ):
326
325
endpoint = "%s/%s/logs/?follow=%s" % (resource , uuid , str (follow ).lower ())
327
326
if tail :
328
327
endpoint = "%s&tail=%d" % (endpoint , tail )
329
- if dockercloud .namespace :
328
+
329
+ if not namespace :
330
+ namespace = dockercloud .namespace
331
+ if namespace :
330
332
url = "/" .join ([dockercloud .stream_host .rstrip ("/" ), "api" , subsystem , self ._api_version ,
331
- dockercloud . namespace , endpoint .lstrip ("/" )])
333
+ self . _namespace , endpoint .lstrip ("/" )])
332
334
else :
333
335
url = "/" .join ([dockercloud .stream_host .rstrip ("/" ), "api" , subsystem , self ._api_version ,
334
336
endpoint .lstrip ("/" )])
@@ -348,11 +350,13 @@ def run_forever(self, *args, **kwargs):
348
350
349
351
350
352
class Exec (StreamingAPI ):
351
- def __init__ (self , uuid , cmd = 'sh' ):
353
+ def __init__ (self , uuid , cmd = 'sh' , namespace = "" ):
352
354
endpoint = "container/%s/exec/?command=%s" % (uuid , urllib .quote_plus (cmd ))
353
- if dockercloud .namespace :
355
+ if not namespace :
356
+ namespace = dockercloud .namespace
357
+ if namespace :
354
358
url = "/" .join ([dockercloud .stream_host .rstrip ("/" ), "api" , "app" , self ._api_version ,
355
- dockercloud . namespace , endpoint .lstrip ("/" )])
359
+ namespace , endpoint .lstrip ("/" )])
356
360
else :
357
361
url = "/" .join ([dockercloud .stream_host .rstrip ("/" ), "api" , "app" , self ._api_version , endpoint .lstrip ("/" )])
358
362
super (self .__class__ , self ).__init__ (url )
0 commit comments