summaryrefslogtreecommitdiffstats
path: root/nova-client/src/main/java/com/woorea/openstack/nova/api/extensions/AggregatesExtension.java
blob: 0a23d8b4ee8c091a3681f2da6a7ec33550091944 (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
/*-
 * ============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.extensions;

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.HostAggregate;
import com.woorea.openstack.nova.model.HostAggregates;

public class AggregatesExtension {

    private final OpenStackClient CLIENT;

    public AggregatesExtension(OpenStackClient client) {
        CLIENT = client;
    }

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

    public ShowAggregate showAggregate(String id) {
        return new ShowAggregate(id);
    }

    public UpdateAggregateMetadata updateAggregateMetadata(String id,
                                                           String name, String availabilityZone) {
        return new UpdateAggregateMetadata(id, name, availabilityZone);
    }

    public CreateAggregate createAggregate(String aggregateName,
                                           String availabilityZoneName) {
        return new CreateAggregate(aggregateName, availabilityZoneName);
    }

    public DeleteAggregate deleteAggregate(String id) {
        return new DeleteAggregate(id);
    }

    public AddHost addHost(String aggregateId, String hostId) {
        return new AddHost(aggregateId, hostId);
    }

    public RemoveHost removeHost(String aggregateId, String hostId) {
        return new RemoveHost(hostId, aggregateId);
    }

    public SetMetadata setMetadata(String aggregateId,
                                   String key, String value) {
        return new SetMetadata(aggregateId, key, value);
    }

    public class List extends OpenStackRequest<HostAggregates> {

        public List() {
            super(CLIENT, HttpMethod.GET, "/os-aggregates",
                    null,
                    HostAggregates.class);
        }

    }

    public class ShowAggregate extends OpenStackRequest<HostAggregate> {

        public ShowAggregate(String id) {
            super(CLIENT, HttpMethod.GET,
                    new StringBuffer("/os-aggregates/").append(id),
                    null,
                    HostAggregate.class);
        }

    }

    public class UpdateAggregateMetadata extends OpenStackRequest<HostAggregate> {
        public UpdateAggregateMetadata(String id,
                                       String name, String availabilityZone) {
            super(CLIENT, HttpMethod.PUT,
                    new StringBuffer("/os-aggregates/").append(id),
                    availabilityZone == null ?
                            Entity.json("{\"aggregate\": {\"name\": \"" + name + "\" }}")
                            :
                            Entity.json("{\"aggregate\": {\"name\": \"" +
                                    name +
                                    "\", \"availability_zone\": \"" +
                                    availabilityZone +
                                    "\" }}"),
                    HostAggregate.class);
        }

    }

    public class CreateAggregate extends OpenStackRequest<HostAggregate> {

        public CreateAggregate(String name, String availabilityZone) {
            super(CLIENT, HttpMethod.POST,
                    new StringBuffer("/os-aggregates"),
                    availabilityZone == null ?
                            Entity.json("{\"aggregate\": {\"name\": \"" +
                                    name +
                                    "\", \"availability_zone\": null }}")
                            :
                            Entity.json("{\"aggregate\": {\"name\": \"" +
                                    name +
                                    "\", \"availability_zone\": \"" +
                                    availabilityZone +
                                    "\" }}"),
                    HostAggregate.class);
        }

    }

    public class DeleteAggregate extends OpenStackRequest<Void> {

        public DeleteAggregate(String id) {
            super(CLIENT, HttpMethod.DELETE,
                    new StringBuffer("/os-aggregates/").append(id),
                    null,
                    null);
        }
    }

    public class AddHost extends OpenStackRequest<HostAggregate> {

        public AddHost(String aggregateId, String hostId) {
            super(CLIENT, HttpMethod.POST,
                    new StringBuffer("/os-aggregates/")
                            .append(aggregateId).append("/action"),
                    Entity.json("{\"add_host\": {\"host\": \"" +
                            hostId + "\" }}"),
                    HostAggregate.class);
        }

    }

    public class RemoveHost extends OpenStackRequest<HostAggregate> {

        public RemoveHost(String hostId, String aggregateId) {
            super(CLIENT, HttpMethod.POST,
                    new StringBuffer("/os-aggregates/")
                            .append(aggregateId).append("/action"),
                    Entity.json("{\"remove_host\": {\"host\": \"" +
                            hostId + "\" }}"),
                    HostAggregate.class);
        }

    }

    public class SetMetadata extends OpenStackRequest<HostAggregate> {

        public SetMetadata(String aggregateId, String key, String value) {
            super(CLIENT, HttpMethod.POST,
                    new StringBuffer("/os-aggregates/")
                            .append(aggregateId).append("/action"),
                    Entity.json("{\"set_metadata\": {\"metadata\": { \"" +
                            key + "\": \"" + value + "\" }}}"),
                    HostAggregate.class);
        }
    }
}