summaryrefslogtreecommitdiffstats
path: root/nova-client/src/test/java/com/woorea/openstack/nova/api/ServersResourceTest.java
blob: bc22de428bc4def0c9bfd277f2ec74013f4a8e74 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*-
 * ============LICENSE_START=======================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package com.woorea.openstack.nova.api;


import com.woorea.openstack.base.client.Entity;
import com.woorea.openstack.base.client.HttpMethod;
import com.woorea.openstack.base.client.OpenStackClient;
import com.woorea.openstack.base.client.OpenStackRequest;
import com.woorea.openstack.nova.model.Metadata;
import com.woorea.openstack.nova.model.Server;
import com.woorea.openstack.nova.model.Server.Addresses;
import com.woorea.openstack.nova.model.ServerAction.ChangePassword;
import com.woorea.openstack.nova.model.ServerAction.ConfirmResize;
import com.woorea.openstack.nova.model.ServerAction.ConsoleOutput;
import com.woorea.openstack.nova.model.ServerAction.CreateBackup;
import com.woorea.openstack.nova.model.ServerAction.CreateImage;
import com.woorea.openstack.nova.model.ServerAction.GetConsoleOutput;
import com.woorea.openstack.nova.model.ServerAction.GetVncConsole;
import com.woorea.openstack.nova.model.ServerAction.Lock;
import com.woorea.openstack.nova.model.ServerAction.Pause;
import com.woorea.openstack.nova.model.ServerAction.Reboot;
import com.woorea.openstack.nova.model.ServerAction.Rebuild;
import com.woorea.openstack.nova.model.ServerAction.Rescue;
import com.woorea.openstack.nova.model.ServerAction.Resize;
import com.woorea.openstack.nova.model.ServerAction.Resume;
import com.woorea.openstack.nova.model.ServerAction.RevertResize;
import com.woorea.openstack.nova.model.ServerAction.Start;
import com.woorea.openstack.nova.model.ServerAction.Stop;
import com.woorea.openstack.nova.model.ServerAction.Suspend;
import com.woorea.openstack.nova.model.ServerAction.Unlock;
import com.woorea.openstack.nova.model.ServerAction.Unpause;
import com.woorea.openstack.nova.model.ServerAction.Unrescue;
import com.woorea.openstack.nova.model.ServerAction.VncConsole;
import com.woorea.openstack.nova.model.ServerForCreate;
import com.woorea.openstack.nova.model.Servers;
import com.woorea.openstack.nova.model.VolumeAttachment;
import com.woorea.openstack.nova.model.VolumeAttachments;
import java.util.Map;

public class ServersResourceTest {

    private final OpenStackClient client;
    private static final String SERVERS = "/servers/";
    private static final String ACTION = "/action";

    public ServersResourceTest(OpenStackClient client) {
        this.client = client;
    }

    public List list(boolean detail) {
        return new List(detail);
    }

    public Boot boot(ServerForCreate server) {
        return new Boot(server);
    }

    public Show show(String id) {
        return new Show(id);
    }

    public ShowMetadata showMetadata(String id) {
        return new ShowMetadata(id);
    }

    public CreateOrUpdateMetadata createOrUpdateMetadata(String id, Metadata metadata) {
        return new CreateOrUpdateMetadata(id, metadata);
    }

    public ReplaceMetadata replaceMetadata(String id, Metadata metadata) {
        return new ReplaceMetadata(id, metadata);
    }

    public DeleteMetadata deleteMetadata(String id, String key) {
        return new DeleteMetadata(id, key);
    }

    public Delete delete(String id) {
        return new Delete(id);
    }

    public class List extends OpenStackRequest<Servers> {

        public List(boolean detail) {
            super(client, HttpMethod.GET, detail ? "/servers/detail" : "/servers", null, Servers.class);
        }
    }

    public class Boot extends OpenStackRequest<Server> {

        private ServerForCreate server;

        public Boot(ServerForCreate server) {
            super(client, HttpMethod.POST, "/servers", Entity.json(server), Server.class);
            this.server = server;
        }
    }

    public class Show extends OpenStackRequest<Server> {

        public Show(String id) {
            super(client, HttpMethod.GET, new StringBuilder(SERVERS).append(id), null, Server.class);
        }
    }

    public class ShowMetadata extends OpenStackRequest<Metadata> {

        public ShowMetadata(String id) {
            super(client, HttpMethod.GET, new StringBuilder(SERVERS).append(id).append("/metadata"), null,
                Metadata.class);
        }
    }

    public class CreateOrUpdateMetadata extends OpenStackRequest<Metadata> {

        public CreateOrUpdateMetadata(String id, Metadata metadata) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append("/metadata"),
                Entity.json(metadata), Metadata.class);
        }
    }

    public class ReplaceMetadata extends OpenStackRequest<Metadata> {

        public ReplaceMetadata(String id, Metadata metadata) {
            super(client, HttpMethod.PUT, new StringBuilder(SERVERS).append(id).append("/metadata"),
                Entity.json(metadata), Metadata.class);
        }
    }

    public class DeleteMetadata extends OpenStackRequest<Void> {

        public DeleteMetadata(String id, String key) {
            super(client, HttpMethod.DELETE, new StringBuilder(SERVERS).append(id).append("/metadata/").append(key),
                null, Void.class);
        }
    }


    public class Delete extends OpenStackRequest<Void> {

        public Delete(String id) {
            super(client, HttpMethod.DELETE, new StringBuilder(SERVERS).append(id), null, Void.class);
        }
    }

    public class ShowServerAddresses extends OpenStackRequest<Addresses> {

        public ShowServerAddresses(String id) {
            super(client, HttpMethod.GET, new StringBuilder(SERVERS).append(id).append("/ips"), null,
                Addresses.class);
        }
    }

    public class UpdateServer extends OpenStackRequest<Server> {

        private Server server;

        public UpdateServer(String id, Server server) {
            super(client, HttpMethod.PUT, new StringBuilder(SERVERS).append(id), Entity.json(server), Server.class);
            this.server = server;
        }
    }

    public UpdateServer update(String serverId, String name, String accessIPv4, String accessIPv6) {
        Server server = new Server();
        //server.setName(name);
        //server.setAccessIPv4(accessIPv4);
        //server.setAccessIPv6(accessIPv6);
        return new UpdateServer(serverId, server);
    }

    public abstract class Action<T> extends OpenStackRequest<T> {

        public Action(String id, Entity<?> entity, Class<T> returnType) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION), entity,
                returnType);
        }
    }

    public class ChangePasswordAction extends Action<Server> {

        private ChangePassword action;

        public ChangePasswordAction(String id, ChangePassword action) {
            super(id, Entity.json(action), Server.class);
        }
    }

    public ChangePasswordAction changePassword(String serverId, String adminPass) {
        ChangePassword changePassword = new ChangePassword();
        changePassword.setAdminPass(adminPass);
        return new ChangePasswordAction(serverId, changePassword);
    }

    public class RebootAction extends Action<Void> {

        private Reboot action;

        public RebootAction(String id, Reboot action) {
            super(id, Entity.json(action), Void.class);
        }
    }

    public RebootAction reboot(String serverId, String rebootType) {
        Reboot reboot = new Reboot();
        reboot.setType(rebootType);
        return new RebootAction(serverId, reboot);
    }

    public class RebuildAction extends Action<Server> {

        private Rebuild action;

        public RebuildAction(String id, Rebuild action) {
            super(id, Entity.json(action), Server.class);
        }
    }

    public RebuildAction rebuild(String serverId, Rebuild rebuild) {
        return new RebuildAction(serverId, rebuild);
    }

    public class ResizeAction extends Action<Server> {

        private Resize action;

        public ResizeAction(String id, Resize action) {
            super(id, Entity.json(action), Server.class);
        }
    }

    public ResizeAction resize(String serverId, String flavorId, String diskConfig) {
        Resize resize = new Resize();
        resize.setFlavorRef(flavorId);
        resize.setDiskConfig(diskConfig);
        return new ResizeAction(serverId, resize);
    }

    public class ConfirmResizeAction extends Action<Server> {

        public ConfirmResizeAction(String id) {
            super(id, Entity.json(new ConfirmResize()), Server.class);
        }
    }

    public ConfirmResizeAction confirmResize(String serverId) {
        return new ConfirmResizeAction(serverId);
    }

    public class RevertResizeAction extends Action<Server> {

        public RevertResizeAction(String id) {
            super(id, Entity.json(new RevertResize()), Server.class);
        }
    }

    public RevertResizeAction revertResize(String serverId) {
        return new RevertResizeAction(serverId);
    }

    public class CreateImageAction extends Action<Void> {

        public CreateImageAction(String id, CreateImage createImage) {
            super(id, Entity.json(createImage), Void.class);
        }
    }

    public CreateImageAction createImage(String serverId, String name, Map<String, String> metadata) {
        CreateImage createImage = new CreateImage();
        createImage.setName(name);
        createImage.setMetadata(metadata);
        return new CreateImageAction(serverId, createImage);
    }

    public class StartServer extends OpenStackRequest<Void> {

        private Start action;

        private String id;

        public StartServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Start()), Void.class);
        }
    }

    public class StopServer extends OpenStackRequest<Void> {

        private Stop action;

        private String id;

        public StopServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Stop()), Void.class);
        }
    }

    public StartServer start(String id) {
        return new StartServer(id);
    }

    public StopServer stop(String id) {
        return new StopServer(id);
    }

    public class GetVncConsoleServer extends OpenStackRequest<VncConsole> {

        private GetVncConsole action;

        private String id;

        public GetVncConsoleServer(String id, GetVncConsole action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), VncConsole.class);
        }
    }

    public GetVncConsoleServer getVncConsole(String id, String type) {
        GetVncConsole action = new GetVncConsole(type);
        return new GetVncConsoleServer(id, action);
    }

    public class GetConsoleOutputServer extends OpenStackRequest<ConsoleOutput> {

        public GetConsoleOutputServer(String id, GetConsoleOutput action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), ConsoleOutput.class);
        }
    }

    public GetConsoleOutputServer getConsoleOutput(String id, int length) {
        GetConsoleOutput action = new GetConsoleOutput(length);
        return new GetConsoleOutputServer(id, action);
    }

    public class PauseServer extends OpenStackRequest<Void> {

        public PauseServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Pause()), Void.class);
        }
    }

    public class UnpauseServer extends OpenStackRequest<Void> {

        public UnpauseServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Unpause()), Void.class);
        }
    }

    public class LockServer extends OpenStackRequest<Void> {

        private Lock action;

        private String id;

        public LockServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Lock()), Void.class);
        }
    }

    public class UnlockServer extends OpenStackRequest<Void> {

        private Unlock action;

        private String id;

        public UnlockServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Unlock()), Void.class);
        }
    }

    public class SuspendServer extends OpenStackRequest<Void> {

        public SuspendServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Suspend()), Void.class);
        }
    }

    public class ResumeServer extends OpenStackRequest<Void> {

        public ResumeServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Resume()), Void.class);
        }
    }

    public class CreateBackupServer extends OpenStackRequest<Void> {

        public CreateBackupServer(String id, CreateBackup action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), Void.class);
        }
    }

    public PauseServer pause(String serverId) {
        return new PauseServer(serverId);
    }

    public UnpauseServer unpause(String serverId) {
        return new UnpauseServer(serverId);
    }

    public LockServer lock(String serverId) {
        return new LockServer(serverId);
    }

    public UnlockServer unlock(String serverId) {
        return new UnlockServer(serverId);
    }

    public SuspendServer suspend(String serverId) {
        return new SuspendServer(serverId);
    }

    public ResumeServer resume(String serverId) {
        return new ResumeServer(serverId);
    }

    public CreateBackupServer createBackup(String serverId, CreateBackup action) {
        return new CreateBackupServer(serverId, action);
    }

    public class RescueServer extends OpenStackRequest<Void> {

        public RescueServer(String id, Rescue action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), Void.class);
        }
    }

    public class UnrescueServer extends OpenStackRequest<Void> {

        public UnrescueServer(String id) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(new Unrescue()), Void.class);
        }
    }

    public RescueServer rescue(String serverId, String adminPass) {
        Rescue action = new Rescue(adminPass);
        return new RescueServer(serverId, action);
    }

    public UnrescueServer unrescue(String serverId) {
        return new UnrescueServer(serverId);
    }

    public class AssociateFloatingIp extends OpenStackRequest<Void> {

        public AssociateFloatingIp(String id, com.woorea.openstack.nova.model.ServerAction.AssociateFloatingIp action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), Void.class);
        }
    }

    public class DisassociateFloatingIp extends OpenStackRequest<Void> {

        public DisassociateFloatingIp(String id,
            com.woorea.openstack.nova.model.ServerAction.DisassociateFloatingIp action) {
            super(client, HttpMethod.POST, new StringBuilder(SERVERS).append(id).append(ACTION),
                Entity.json(action), Void.class);
        }
    }

    public AssociateFloatingIp associateFloatingIp(String serverId, String floatingIpAddress) {
        com.woorea.openstack.nova.model.ServerAction.AssociateFloatingIp action = new com.woorea.openstack.nova.model.ServerAction.AssociateFloatingIp(
            floatingIpAddress);
        return new AssociateFloatingIp(serverId, action);
    }

    public DisassociateFloatingIp disassociateFloatingIp(String serverId, String floatingIpAddress) {
        com.woorea.openstack.nova.model.ServerAction.DisassociateFloatingIp action = new com.woorea.openstack.nova.model.ServerAction.DisassociateFloatingIp(
            floatingIpAddress);
        return new DisassociateFloatingIp(serverId, action);
    }

    public class AttachVolume extends OpenStackRequest<Void> {

        public AttachVolume(String serverId, final VolumeAttachment volumeAttachment) {
            super(client, HttpMethod.POST,
                new StringBuilder(SERVERS).append(serverId).append("/os-volume_attachments"),
                Entity.json(volumeAttachment), Void.class);
        }
    }

    public class DetachVolume extends OpenStackRequest<Void> {

        public DetachVolume(String serverId, String volumeId) {
            super(client, HttpMethod.DELETE,
                new StringBuilder(SERVERS).append(serverId).append("/os-volume_attachments/").append(volumeId),
                null, Void.class);
        }
    }

    public class ListVolumeAttachments extends OpenStackRequest<VolumeAttachments> {

        public ListVolumeAttachments(String serverId) {
            super(client, HttpMethod.GET,
                new StringBuilder(SERVERS).append(serverId).append("/os-volume_attachments"), null,
                VolumeAttachments.class);
        }
    }

    public class ShowVolumeAttachment extends OpenStackRequest<VolumeAttachment> {

        public ShowVolumeAttachment(String serverId, String volumeAttachmentId) {
            super(client, HttpMethod.GET,
                new StringBuilder(SERVERS).append(serverId).append("/os-volume_attachments/")
                    .append(volumeAttachmentId), null, VolumeAttachment.class);
        }
    }

    public AttachVolume attachVolume(String serverId, String volumeId, String device) {
        VolumeAttachment volumeAttachment = new VolumeAttachment();
        volumeAttachment.setVolumeId(volumeId);
        volumeAttachment.setDevice(device);
        return new AttachVolume(serverId, volumeAttachment);
    }

    public DetachVolume detachVolume(String serverId, String volumeId) {
        return new DetachVolume(serverId, volumeId);
    }

    public ListVolumeAttachments listVolumeAttachments(String serverId) {
        return new ListVolumeAttachments(serverId);
    }

    public ShowVolumeAttachment showVolumeAttachment(String serverId, String volumeAttachmentId) {
        return new ShowVolumeAttachment(serverId, volumeAttachmentId);
    }
}