5 // Created by Mike Mayo on 12/31/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "GetImagesRequest.h"
10 #import "OpenStackAccount.h"
11 #import "AccountManager.h"
15 @implementation GetImagesRequest
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];
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];
34 + (GetImagesRequest *)request:(OpenStackAccount *)account {
35 GetImagesRequest *request = [GetImagesRequest serversRequest:account method:@"GET" path:@"/images/detail"];
36 request.account = account;
40 - (void)requestFinished {
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]];
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;
55 for (Image *image in [newImages allValues]) {
56 Image *newImage = [self.account.images objectForKey:image.identifier];
58 if ([newImage isKindOfClass:[Image class]]) {
59 newImage.canBeLaunched = YES;
62 if ([image isKindOfClass:[Image class]]) {
63 image.canBeLaunched = YES;
64 [self.account.images setObject:image forKey:image.identifier];
69 [self.account persist];
70 [self.account.manager notify:@"getImagesSucceeded" request:self object:self.account];
72 [self.account.manager notify:@"getImagesFailed" request:self object:self.account];
75 [super requestFinished];
78 - (void)failWithError:(NSError *)theError {
79 [self.account.manager notify:@"getImagesFailed" request:self object:self.account];
80 [super failWithError:theError];