def to_dict(self): context = senlin_context.get_admin_context() profile = db_api.profile_get(context, self.profile_id, project_safe=False) return { 'id': self.id, 'name': self.name, 'profile_id': self.profile_id, 'user': self.user, 'project': self.project, 'domain': self.domain, 'init_at': utils.isotime(self.init_at), 'created_at': utils.isotime(self.created_at), 'updated_at': utils.isotime(self.updated_at), 'min_size': self.min_size, 'max_size': self.max_size, 'desired_capacity': self.desired_capacity, 'timeout': self.timeout, 'status': self.status, 'status_reason': self.status_reason, 'metadata': self.metadata or {}, 'data': self.data or {}, 'dependents': self.dependents or {}, 'profile_name': profile.name, 'nodes': db_api.node_ids_by_cluster(context, self.id), 'policies': db_api.cluster_policy_ids_by_cluster(context, self.id) }
def load(cls, ctx, profile_id=None, profile=None): '''Retrieve a profile object from database.''' if profile is None: profile = db_api.profile_get(ctx, profile_id) if profile is None: raise exception.ProfileNotFound(profile=profile_id) return cls.from_db_record(profile)
def test_load_with_profile_object(self): obj = self._create_profile('test-profile-bb') profile_id = obj.store(self.ctx) profile = db_api.profile_get(self.ctx, profile_id) result = pb.Profile.load(self.ctx, profile=profile) self.assertEqual(profile.id, result.id)
def test_load_with_both(self): profile = self._create_profile('test1') profile.store(self.ctx) db_profile = db_api.profile_get(self.ctx, profile.id) res = pb.Profile.load(self.ctx, profile=db_profile, profile_id=profile.id) self.assertEqual(profile.id, res.id)
def _select_candidates(self, context, cluster_id, count): candidates = [] nodes = db_api.node_get_all_by_cluster(context, cluster_id) if count > len(nodes): count = len(nodes) err_nodes = [n for n in nodes if n.status == 'ERROR'] nodes = [n for n in nodes if n.status != 'ERROR'] if count <= len(err_nodes): return [n.id for n in err_nodes[:count]] candidates.extend([n.id for n in err_nodes]) count -= len(err_nodes) # Random selection if self.criteria == self.RANDOM: i = count while i > 0: rand = random.randrange(i) candidates.append(nodes[rand].id) nodes.remove(nodes[rand]) i = i - 1 return candidates # Node age based selection if self.criteria in [self.OLDEST_FIRST, self.YOUNGEST_FIRST]: sorted_list = sorted(nodes, key=lambda r: (r.created_time, r.name)) for i in range(count): if self.criteria == self.OLDEST_FIRST: candidates.append(sorted_list[i].id) else: # YOUNGEST_FIRST candidates.append(sorted_list[-1 - i].id) return candidates # Node profile based selection node_map = [] for node in nodes: profile = db_api.profile_get(context, node.profile_id) created_at = profile.created_time node_map.append({'id': node.id, 'created_at': created_at}) sorted_map = sorted(node_map, key=lambda m: m['created_at']) for i in range(count): candidates.append(sorted_map[i]['id']) return candidates
def _select_candidates(self, context, cluster_id, count): candidates = [] nodes = db_api.node_get_all_by_cluster(context, cluster_id) if count > len(nodes): count = len(nodes) err_nodes = [n for n in nodes if n.status == "ERROR"] nodes = [n for n in nodes if n.status != "ERROR"] if count <= len(err_nodes): return [n.id for n in err_nodes[:count]] candidates.extend([n.id for n in err_nodes]) count -= len(err_nodes) # Random selection if self.criteria == self.RANDOM: i = count while i > 0: rand = random.randrange(i) candidates.append(nodes[rand].id) nodes.remove(nodes[rand]) i = i - 1 return candidates # Node age based selection if self.criteria in [self.OLDEST_FIRST, self.YOUNGEST_FIRST]: sorted_list = sorted(nodes, key=lambda r: (r.created_time, r.name)) for i in range(count): if self.criteria == self.OLDEST_FIRST: candidates.append(sorted_list[i].id) else: # YOUNGEST_FIRST candidates.append(sorted_list[-1 - i].id) return candidates # Node profile based selection node_map = [] for node in nodes: profile = db_api.profile_get(context, node.profile_id) created_at = profile.created_time node_map.append({"id": node.id, "created_at": created_at}) sorted_map = sorted(node_map, key=lambda m: m["created_at"]) for i in range(count): candidates.append(sorted_map[i]["id"]) return candidates
def test_from_db_record(self): obj = self._create_profile('test_profile_for_record') obj.store(self.ctx) profile = db_api.profile_get(self.ctx, obj.id) result = pb.Profile.from_db_record(profile) self.assertEqual(profile.id, result.id) self.assertEqual(profile.name, result.name) self.assertEqual(profile.type, result.type) self.assertEqual(profile.user, result.user) self.assertEqual(profile.project, result.project) self.assertEqual(profile.domain, result.domain) self.assertEqual(profile.spec, result.spec) self.assertEqual(profile.meta_data, result.metadata) self.assertEqual('value1', result.properties['key1']) self.assertEqual(2, result.properties['key2']) self.assertEqual(profile.created_at, result.created_at) self.assertEqual(profile.updated_at, result.updated_at) self.assertEqual(profile.context, result.context)
def _select_candidates(self, context, cluster_id, count): candidates = [] nodes = db_api.node_get_all_by_cluster(context, cluster_id) if count > len(nodes): count = len(nodes) # Random selection if self.criteria == self.RANDOM: i = count while i > 0: rand = random.randrange(i) candidates.append(nodes[rand]) nodes.remove(nodes[rand]) i = i - 1 return candidates # Node age based selection if self.criteria in [self.OLDEST_FIRST, self.YOUNGEST_FIRST]: sorted_list = sorted(nodes, key=lambda r: (r.created_time, r.name)) for i in range(count): if self.criteria == self.OLDEST_FIRST: candidates.append(sorted_list[i]) else: # YOUNGEST_FIRST candidates.append(sorted_list[-i]) return candidates # Node profile based selection if self.criterial == self.OLDEST_PROFILE_FIRST: map = [] for node in nodes: created_at = db_api.profile_get(node.profile_id).created_time map.append({'id': node.id, 'created_at': created_at}) sorted_map = sorted(map, key=lambda m: m['created_at']) for i in range(count): candidates.append(sorted_map[i]) return candidates return []
def get(cls, context, profile_id, **kwargs): obj = db_api.profile_get(context, profile_id, **kwargs) return cls._from_db_object(context, cls(), obj)