root / Classes / GetImagesRequest.m @ 45f2fce6
History | View | Annotate | Download (3.1 kB)
1 |
// |
---|---|
2 |
// GetImagesRequest.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 12/31/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "GetImagesRequest.h" |
10 |
#import "OpenStackAccount.h" |
11 |
#import "AccountManager.h" |
12 |
#import "Image.h" |
13 |
|
14 |
|
15 |
@implementation GetImagesRequest |
16 |
|
17 |
+ (id)request:(OpenStackAccount *)account method:(NSString *)method url:(NSURL *)url { |
18 |
GetImagesRequest *request = [[[GetImagesRequest alloc] initWithURL:url] autorelease]; |
19 |
request.account = account; |
20 |
[request setRequestMethod:method]; |
21 |
[request addRequestHeader:@"X-Auth-Token" value:[account authToken]]; |
22 |
[request addRequestHeader:@"Content-Type" value:@"application/json"]; |
23 |
[request setTimeOutSeconds:40]; |
24 |
return request; |
25 |
} |
26 |
|
27 |
+ (id)serversRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path { |
28 |
NSString *now = [[[NSDate date] description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
29 |
NSLog(@"account servers url: %@", account.serversURL); |
30 |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?now=%@", account.serversURL, path, now]]; |
31 |
return [GetImagesRequest request:account method:method url:url]; |
32 |
} |
33 |
|
34 |
+ (GetImagesRequest *)request:(OpenStackAccount *)account { |
35 |
GetImagesRequest *request = [GetImagesRequest serversRequest:account method:@"GET" path:@"/images/detail"]; |
36 |
request.account = account; |
37 |
return request; |
38 |
} |
39 |
|
40 |
- (void)requestFinished { |
41 |
|
42 |
if ([self isSuccess]) { |
43 |
// go through results and add, rather than full replace. this way we'll keep correct OS logos |
44 |
// for servers with deprecated images |
45 |
NSMutableDictionary *newImages = [[NSMutableDictionary alloc] initWithDictionary:[self images]]; |
46 |
if (!self.account.images) { |
47 |
self.account.images = [NSMutableDictionary dictionaryWithCapacity:[newImages count]]; |
48 |
} |
49 |
// set them all to unlaunchable, then we'll update them |
50 |
for (Image *image in [self.account.images allValues]) { |
51 |
if ([image isKindOfClass:[Image class]]) { |
52 |
image.canBeLaunched = NO; |
53 |
} |
54 |
} |
55 |
for (Image *image in [newImages allValues]) { |
56 |
Image *newImage = [self.account.images objectForKey:image.identifier]; |
57 |
if (newImage) { |
58 |
if ([newImage isKindOfClass:[Image class]]) { |
59 |
newImage.canBeLaunched = YES; |
60 |
} |
61 |
} else { |
62 |
if ([image isKindOfClass:[Image class]]) { |
63 |
image.canBeLaunched = YES; |
64 |
[self.account.images setObject:image forKey:image.identifier]; |
65 |
} |
66 |
} |
67 |
} |
68 |
[newImages release]; |
69 |
[self.account persist]; |
70 |
[self.account.manager notify:@"getImagesSucceeded" request:self object:self.account]; |
71 |
} else { |
72 |
[self.account.manager notify:@"getImagesFailed" request:self object:self.account]; |
73 |
} |
74 |
|
75 |
[super requestFinished]; |
76 |
} |
77 |
|
78 |
- (void)failWithError:(NSError *)theError { |
79 |
[self.account.manager notify:@"getImagesFailed" request:self object:self.account]; |
80 |
[super failWithError:theError]; |
81 |
} |
82 |
|
83 |
|
84 |
@end |