1
# Copyright 2013 IBM Corp.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
from nova.compute import utils as compute_utils
17
from nova import exception
18
from nova.objects import base
19
from nova.objects import utils
22
class Aggregate(base.NovaObject):
26
'hosts': utils.list_of_strings_or_none,
27
'metadata': utils.dict_of_strings_or_none,
30
obj_extra_fields = ['availability_zone']
33
def _from_db_object(context, aggregate, db_aggregate):
34
for key in aggregate.fields:
36
db_key = 'metadetails'
39
aggregate[key] = db_aggregate[db_key]
40
aggregate._context = context
41
aggregate.obj_reset_changes()
44
def _assert_no_hosts(self, action):
45
if 'hosts' in self.obj_what_changed():
46
raise exception.ObjectActionError(
48
reason='hosts updated inline')
50
@base.remotable_classmethod
51
def get_by_id(cls, context, aggregate_id):
52
db_aggregate = db.aggregate_get(context, aggregate_id)
53
return cls._from_db_object(context, cls(), db_aggregate)
56
def create(self, context):
57
self._assert_no_hosts('create')
59
for key in self.obj_what_changed():
60
updates[key] = self[key]
61
payload = dict(updates)
62
if 'metadata' in updates:
63
# NOTE(danms): For some reason the notification format is weird
64
payload['meta_data'] = payload.pop('metadata')
65
compute_utils.notify_about_aggregate_update(context,
68
metadata = updates.pop('metadata', None)
69
db_aggregate = db.aggregate_create(context, updates, metadata=metadata)
70
self._from_db_object(context, self, db_aggregate)
71
payload['aggregate_id'] = self.id
72
compute_utils.notify_about_aggregate_update(context,
77
def save(self, context):
78
self._assert_no_hosts('save')
80
for key in self.obj_what_changed():
81
updates[key] = self[key]
83
payload = {'aggregate_id': self.id}
84
if 'metadata' in updates:
85
payload['meta_data'] = updates['metadata']
86
compute_utils.notify_about_aggregate_update(context,
89
updates.pop('id', None)
90
db_aggregate = db.aggregate_update(context, self.id, updates)
91
compute_utils.notify_about_aggregate_update(context,
94
return self._from_db_object(context, self, db_aggregate)
97
def update_metadata(self, context, updates):
98
payload = {'aggregate_id': self.id,
100
compute_utils.notify_about_aggregate_update(context,
101
"updatemetadata.start",
104
for key, value in updates.items():
107
db.aggregate_metadata_delete(context, self.id, key)
108
except exception.AggregateMetadataNotFound:
111
self.metadata.pop(key)
116
self.metadata[key] = value
117
db.aggregate_metadata_add(context, self.id, to_add)
118
payload['meta_data'] = to_add
119
compute_utils.notify_about_aggregate_update(context,
120
"updatemetadata.end",
122
self.obj_reset_changes(fields=['metadata'])
125
def destroy(self, context):
126
db.aggregate_delete(context, self.id)
129
def add_host(self, context, host):
130
db.aggregate_host_add(context, self.id, host)
131
if self.hosts is None:
133
self.hosts.append(host)
134
self.obj_reset_changes(fields=['hosts'])
137
def delete_host(self, context, host):
138
db.aggregate_host_delete(context, self.id, host)
139
self.hosts.remove(host)
140
self.obj_reset_changes(fields=['hosts'])
143
def availability_zone(self):
144
return self.metadata.get('availability_zone', None)
147
class AggregateList(base.ObjectListBase, base.NovaObject):
148
@base.remotable_classmethod
149
def get_all(cls, context):
150
db_aggregates = db.aggregate_get_all(context)
151
return base.obj_make_list(context, AggregateList(), Aggregate,
154
@base.remotable_classmethod
155
def get_by_host(cls, context, host):
156
db_aggregates = db.aggregate_get_by_host(context, host)
157
return base.obj_make_list(context, AggregateList(), Aggregate,