Statistics
| Branch: | Tag: | Revision:

one / src / group / GroupPool.cc @ ae53d437

History | View | Annotate | Download (3.49 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)             */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include "GroupPool.h"
18
#include "Nebula.h"
19
#include "NebulaLog.h"
20

    
21
#include <stdexcept>
22

    
23
const string GroupPool::DEFAULT_GROUP_NAME = "default";
24

    
25
/* -------------------------------------------------------------------------- */
26
/* -------------------------------------------------------------------------- */
27

    
28
GroupPool::GroupPool(SqlDB * db):PoolSQL(db, Group::table)
29
{
30
    // lastOID is set in PoolSQL::init_cb
31
    if (get_lastOID() == -1)
32
    {
33
        int         rc;
34
        Group *     group;
35
        string      error_str;
36

    
37
        // Build a new Group object
38
        group = new Group(0, 0, GroupPool::DEFAULT_GROUP_NAME);
39

    
40
        // Insert the Object in the pool
41
        rc = PoolSQL::allocate(group, error_str);
42

    
43
        if(rc != 0)
44
        {
45
            ostringstream oss;
46

    
47
            oss << "Error trying to create default group: " << error_str;
48
            NebulaLog::log("GROUP",Log::ERROR,oss);
49

    
50
            throw runtime_error(oss.str());
51
        }
52
    }
53
}
54

    
55
/* -------------------------------------------------------------------------- */
56
/* -------------------------------------------------------------------------- */
57

    
58
int GroupPool::allocate(int uid, string name, int * oid, string& error_str)
59
{
60
    Group *         group;
61
    ostringstream   oss;
62

    
63
    if ( name.empty() )
64
    {
65
        goto error_name;
66
    }
67

    
68
    // Check for duplicates
69
    group = get(name, uid, false);
70

    
71
    if( group != 0 )
72
    {
73
        goto error_duplicated;
74
    }
75

    
76
    // Build a new Group object
77
    group = new Group(-1, uid, name);
78

    
79
    // Insert the Object in the pool
80
    *oid = PoolSQL::allocate(group, error_str);
81

    
82
    return *oid;
83

    
84

    
85
error_name:
86
    oss << "NAME cannot be empty.";
87
    goto error_common;
88

    
89
error_duplicated:
90
    oss << "NAME is already taken by GROUP " << group->get_oid() << ".";
91

    
92
error_common:
93
    *oid = -1;
94
    error_str = oss.str();
95

    
96
    return *oid;
97
}
98

    
99
/* -------------------------------------------------------------------------- */
100
/* -------------------------------------------------------------------------- */
101

    
102
int GroupPool::drop(Group * group)
103
{
104
    int         rc;
105

    
106
    // Return error if group is 'default'
107
    if( group->get_oid() == 0 )
108
    {
109
        NebulaLog::log("GROUP",Log::WARNING,
110
                       "Default group cannot be deleted.");
111

    
112
        return -1;
113
    }
114

    
115
    rc = group->drop(db);
116

    
117
    return rc;
118
}