Revision ae53d437

View differences:

SConstruct
58 58
    cwd+'/src/sql',
59 59
    cwd+'/src/host',
60 60
    cwd+'/src/cluster',
61
    cwd+'/src/group',
61 62
    cwd+'/src/mad',
62 63
    cwd+'/src/nebula',
63 64
    cwd+'/src/pool',
......
187 188
    'src/template/SConstruct',
188 189
    'src/host/SConstruct',
189 190
    'src/cluster/SConstruct',
191
    'src/group/SConstruct',
190 192
    'src/mad/SConstruct',
191 193
    'src/nebula/SConstruct',
192 194
    'src/pool/SConstruct',
......
233 235
        'src/common/test/SConstruct',
234 236
        'src/host/test/SConstruct',
235 237
        'src/cluster/test/SConstruct',
238
        'src/group/test/SConstruct',
236 239
        'src/image/test/SConstruct',
237 240
        'src/lcm/test/SConstruct',
238 241
        'src/pool/test/SConstruct',
include/AuthManager.h
295 295
        IMAGE,
296 296
        USER,
297 297
        CLUSTER,
298
        TEMPLATE
298
        TEMPLATE,
299
        GROUP
299 300
    };
300 301

  
301 302
    /**
include/Group.h
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
#ifndef GROUP_H_
18
#define GROUP_H_
19

  
20
#include "PoolSQL.h"
21

  
22
using namespace std;
23

  
24
/**
25
 *  The Group class.
26
 */
27
class Group : public PoolObjectSQL
28
{
29
public:
30
    /**
31
     *  Function to write a Group on an output stream
32
     */
33
     friend ostream& operator<<(ostream& os, Group& group);
34

  
35
    /**
36
     * Function to print the Group object into a string in XML format
37
     *  @param xml the resulting XML string
38
     *  @return a reference to the generated string
39
     */
40
    string& to_xml(string& xml) const;
41

  
42
    /**
43
     *  Rebuilds the object from an xml formatted string
44
     *    @param xml_str The xml-formatted string
45
     *
46
     *    @return 0 on success, -1 otherwise
47
     */
48
    int from_xml(const string &xml_str);
49

  
50
private:
51

  
52
    // -------------------------------------------------------------------------
53
    // Friends
54
    // -------------------------------------------------------------------------
55

  
56
    friend class GroupPool;
57

  
58
    // *************************************************************************
59
    // Constructor
60
    // *************************************************************************
61

  
62
    Group(int id, int uid, const string& name);
63

  
64
    virtual ~Group();
65

  
66
    // *************************************************************************
67
    // DataBase implementation (Private)
68
    // *************************************************************************
69

  
70
    static const char * db_names;
71

  
72
    static const char * db_bootstrap;
73

  
74
    static const char * table;
75

  
76
    /**
77
     *  Execute an INSERT or REPLACE Sql query.
78
     *    @param db The SQL DB
79
     *    @param replace Execute an INSERT or a REPLACE
80
     *    @return 0 one success
81
    */
82
    int insert_replace(SqlDB *db, bool replace);
83

  
84
    /**
85
     *  Bootstraps the database table(s) associated to the Group
86
     */
87
    static void bootstrap(SqlDB * db)
88
    {
89
        ostringstream oss(Group::db_bootstrap);
90

  
91
        db->exec(oss);
92
    };
93

  
94
    /**
95
     *  Writes the Group in the database.
96
     *    @param db pointer to the db
97
     *    @return 0 on success
98
     */
99
    int insert(SqlDB *db, string& error_str);
100

  
101
    /**
102
     *  Writes/updates the Group's data fields in the database.
103
     *    @param db pointer to the db
104
     *    @return 0 on success
105
     */
106
    int update(SqlDB *db);
107
};
108

  
109
#endif /*GROUP_H_*/
include/GroupPool.h
1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, 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
#ifndef GROUP_POOL_H_
18
#define GROUP_POOL_H_
19

  
20
#include "Group.h"
21
#include "SqlDB.h"
22

  
23
using namespace std;
24

  
25

  
26
class GroupPool : public PoolSQL
27
{
28
public:
29
    GroupPool(SqlDB * db);
30

  
31
    ~GroupPool(){};
32

  
33
    /**
34
     *  Cluster name for the default cluster
35
     */
36
    static const string DEFAULT_GROUP_NAME;
37

  
38
    /* ---------------------------------------------------------------------- */
39
    /* Methods for DB management                                              */
40
    /* ---------------------------------------------------------------------- */
41

  
42
    /**
43
     *  Allocates a new group, writting it in the pool database. No memory is
44
     *  allocated for the object.
45
     *    @param uid user id (the owner of the Object)
46
     *    @param name Group name
47
     *    @param oid the id assigned to the Group
48
     *    @param error_str Returns the error reason, if any
49
     *
50
     *    @return the oid assigned to the object, -1 in case of failure
51
     */
52
    int allocate(int                      uid,
53
                 string                   name,
54
                 int *                    oid,
55
                 string&                  error_str);
56

  
57
    /**
58
     *  Function to get a group from the pool, if the object is not in memory
59
     *  it is loaded from the DB
60
     *    @param oid group unique id
61
     *    @param lock locks the group mutex
62
     *    @return a pointer to the group, 0 if the group could not be loaded
63
     */
64
    Group * get(int oid, bool lock)
65
    {
66
        return static_cast<Group *>(PoolSQL::get(oid,lock));
67
    };
68

  
69
    /**
70
     *  Gets an object from the pool (if needed the object is loaded from the
71
     *  database).
72
     *   @param name of the object
73
     *   @param uid id of owner
74
     *   @param lock locks the object if true
75
     *
76
     *   @return a pointer to the object, 0 in case of failure
77
     */
78
    Group * get(const string& name, int uid, bool lock)
79
    {
80
        return static_cast<Group *>(PoolSQL::get(name,uid,lock));
81
    };
82

  
83
    /** Update a particular Group
84
     *    @param user pointer to Group
85
     *    @return 0 on success
86
     */
87
    int update(Group * group)
88
    {
89
        return group->update(db);
90
    };
91

  
92
    /**
93
     *  Drops the Group from the data base. The object mutex SHOULD be
94
     *  locked.
95
     *    @param group a pointer to the object
96
     *    @return 0 on success.
97
     */
98
    int drop(Group * group);
99

  
100
    /**
101
     *  Bootstraps the database table(s) associated to the Group pool
102
     */
103
    static void bootstrap(SqlDB * _db)
104
    {
105
        Group::bootstrap(_db);
106
    };
107

  
108
    /**
109
     *  Dumps the Group pool in XML format. A filter can be also added to the
110
     *  query
111
     *  @param oss the output stream to dump the pool contents
112
     *  @param where filter for the objects, defaults to all
113
     *
114
     *  @return 0 on success
115
     */
116
    int dump(ostringstream& oss, const string& where)
117
    {
118
        return PoolSQL::dump(oss, "GROUP_POOL", Group::table, where);
119
    };
120

  
121
private:
122

  
123
    /**
124
     *  Factory method to produce objects
125
     *    @return a pointer to the new object
126
     */
127
    PoolObjectSQL * create()
128
    {
129
        return new Group(-1,-1,"");
130
    };
131
};
132

  
133
#endif /*GROUP_POOL_H_*/
include/Nebula.h
26 26
#include "HostPool.h"
27 27
#include "UserPool.h"
28 28
#include "VMTemplatePool.h"
29
#include "GroupPool.h"
29 30

  
30 31
#include "VirtualMachineManager.h"
31 32
#include "LifeCycleManager.h"
......
82 83
        return cpool;
83 84
    };
84 85

  
86
    GroupPool * get_gpool()
87
    {
88
        return gpool;
89
    };
90

  
85 91
    VMTemplatePool * get_tpool()
86 92
    {
87 93
        return tpool;
......
240 246
    // -----------------------------------------------------------------------
241 247

  
242 248
    Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0),
243
        upool(0),ipool(0),cpool(0),tpool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),
244
        rm(0),hm(0),authm(0),imagem(0)
249
        upool(0),ipool(0),cpool(0),gpool(0),tpool(0),lcm(0),vmm(0),im(0),tm(0),
250
        dm(0),rm(0),hm(0),authm(0),imagem(0)
245 251
    {
246 252
        const char * nl = getenv("ONE_LOCATION");
247 253

  
......
306 312
            delete cpool;
307 313
        }
308 314

  
315
        if ( gpool != 0)
316
        {
317
            delete gpool;
318
        }
319

  
309 320
        if ( tpool != 0)
310 321
        {
311 322
            delete tpool;
......
403 414
    UserPool           * upool;
404 415
    ImagePool          * ipool;
405 416
    ClusterPool        * cpool;
417
    GroupPool          * gpool;
406 418
    VMTemplatePool     * tpool;
407 419

  
408 420
    // ---------------------------------------------------------------
include/RequestManager.h
25 25
#include "ImagePool.h"
26 26
#include "ClusterPool.h"
27 27
#include "VMTemplatePool.h"
28
#include "GroupPool.h"
28 29

  
29 30
#include <xmlrpc-c/base.hpp>
30 31
#include <xmlrpc-c/registry.hpp>
......
48 49
        ImagePool          *    _ipool,
49 50
        ClusterPool        *    _cpool,
50 51
        VMTemplatePool     *    _tpool,
52
        GroupPool          *    _gpool,
51 53
        int                     _port,
52 54
        string                  _xml_log_file)
53 55
            :vmpool(_vmpool),hpool(_hpool),vnpool(_vnpool),upool(_upool),
54
            ipool(_ipool),cpool(_cpool),tpool(_tpool),port(_port),socket_fd(-1),
55
            xml_log_file(_xml_log_file)
56
            ipool(_ipool),cpool(_cpool),tpool(_tpool),gpool(_gpool),port(_port),
57
            socket_fd(-1),xml_log_file(_xml_log_file)
56 58
    {
57 59
        am.addListener(this);
58 60
    };
......
142 144
    VMTemplatePool     *    tpool;
143 145

  
144 146
    /**
147
     *  Pointer to the Group Pool, to access groups
148
     */
149
    GroupPool          *    gpool;
150

  
151
    /**
145 152
     *  Port number where the connection will be open
146 153
     */
147 154
    int port;
......
988 995

  
989 996

  
990 997
    /* ---------------------------------------------------------------------- */
998
    /*                      Group Interface                                   */
999
    /* ---------------------------------------------------------------------- */
1000

  
1001
    class GroupAllocate: public xmlrpc_c::method
1002
    {
1003
    public:
1004
        GroupAllocate(
1005
            UserPool *      _upool,
1006
            GroupPool *     _gpool):
1007
                upool(_upool),
1008
                gpool(_gpool)
1009
        {
1010
            _signature="A:ss";
1011
            _help="Allocates a group in the pool";
1012
        };
1013

  
1014
        ~GroupAllocate(){};
1015

  
1016
        void execute(
1017
            xmlrpc_c::paramList const& paramList,
1018
            xmlrpc_c::value *   const  retvalP);
1019

  
1020
    private:
1021
        UserPool *      upool;
1022
        GroupPool *     gpool;
1023
    };
1024

  
1025
    /* ---------------------------------------------------------------------- */
1026

  
1027
    class GroupInfo: public xmlrpc_c::method
1028
    {
1029
    public:
1030
        GroupInfo(
1031
            UserPool *      _upool,
1032
            GroupPool *     _gpool):
1033
                upool(_upool),
1034
                gpool(_gpool)
1035
        {
1036
            _signature="A:si";
1037
            _help="Returns group information";
1038
        };
1039

  
1040
        ~GroupInfo(){};
1041

  
1042
        void execute(
1043
            xmlrpc_c::paramList const& paramList,
1044
            xmlrpc_c::value *   const  retvalP);
1045

  
1046
    private:
1047
        UserPool *      upool;
1048
        GroupPool *     gpool;
1049
    };
1050

  
1051
    /* ---------------------------------------------------------------------- */
1052

  
1053
    class GroupDelete: public xmlrpc_c::method
1054
    {
1055
    public:
1056
        GroupDelete(
1057
            UserPool *      _upool,
1058
            GroupPool *     _gpool):
1059
                upool(_upool),
1060
                gpool(_gpool)
1061
        {
1062
            _signature="A:si";
1063
            _help="Deletes a group from the pool";
1064
        };
1065

  
1066
        ~GroupDelete(){};
1067

  
1068
        void execute(
1069
            xmlrpc_c::paramList const& paramList,
1070
            xmlrpc_c::value *   const  retvalP);
1071

  
1072
    private:
1073
        UserPool *      upool;
1074
        GroupPool *     gpool;
1075
    };
1076

  
1077
    /* ---------------------------------------------------------------------- */
1078

  
1079
    class GroupPoolInfo: public xmlrpc_c::method
1080
    {
1081
    public:
1082
        GroupPoolInfo(
1083
            UserPool *      _upool,
1084
            GroupPool *     _gpool):
1085
                upool(_upool),
1086
                gpool(_gpool)
1087
        {
1088
            _signature="A:s";
1089
            _help="Returns the group pool information";
1090
        };
1091

  
1092
        ~GroupPoolInfo(){};
1093

  
1094
        void execute(
1095
            xmlrpc_c::paramList const& paramList,
1096
            xmlrpc_c::value *   const  retvalP);
1097

  
1098
    private:
1099
        UserPool *      upool;
1100
        GroupPool *     gpool;
1101
    };
1102

  
1103

  
1104
    /* ---------------------------------------------------------------------- */
991 1105
    /*                      Virtual Network Interface                         */
992 1106
    /* ---------------------------------------------------------------------- */
993 1107

  
include/test/NebulaTest.h
45 45
    NebulaTest():mysql(false), need_host_pool(false), need_vm_pool(false),
46 46
                need_vnet_pool(false), need_image_pool(false), 
47 47
                need_user_pool(false), need_cluster_pool(false),
48
                need_template_pool(false),need_vmm(false),
48
                need_template_pool(false), need_group_pool(false),
49
                need_vmm(false),
49 50
                need_im(false), need_tm(false),
50 51
                need_lcm(false), need_dm(false),
51 52
                need_rm(false), need_hm(false),
......
66 67
    bool need_user_pool;
67 68
    bool need_cluster_pool;
68 69
    bool need_template_pool;
70
    bool need_group_pool;
69 71

  
70 72
    bool need_vmm;
71 73
    bool need_im;
......
104 106

  
105 107
    virtual VMTemplatePool* create_tpool(SqlDB* db);
106 108

  
109
    virtual GroupPool* create_gpool(SqlDB* db);
110

  
107 111
    // ------------------------------------------------------------------------
108 112
    // Managers
109 113
    // ------------------------------------------------------------------------
......
134 138
                    ImagePool          *    ipool,
135 139
                    ClusterPool        *    cpool,
136 140
                    VMTemplatePool     *    tpool,
141
                    GroupPool          *    gpool,
137 142
                    string                  log_file);
138 143

  
139 144
    virtual HookManager* create_hm(VirtualMachinePool * vmpool);
install.sh
365 365
           src/cli/oneimage \
366 366
           src/cli/onecluster \
367 367
           src/cli/onetemplate \
368
           src/cli/onegroup \
368 369
           share/scripts/one \
369 370
           src/authm_mad/oneauth"
370 371

  
......
411 412
                           src/oca/ruby/OpenNebula/ClusterPool.rb \
412 413
                           src/oca/ruby/OpenNebula/Template.rb \
413 414
                           src/oca/ruby/OpenNebula/TemplatePool.rb \
415
                           src/oca/ruby/OpenNebula/Group.rb \
416
                           src/oca/ruby/OpenNebula/GroupPool.rb \
414 417
                           src/oca/ruby/OpenNebula/XMLUtils.rb"
415 418

  
416 419

  
......
733 736
               src/cli/oneuser \
734 737
               src/cli/oneimage \
735 738
               src/cli/onecluster \
736
               src/cli/onetemplate"
739
               src/cli/onetemplate \
740
               src/cli/onegroup"
737 741

  
738 742
#-----------------------------------------------------------------------------
739 743
# Sunstone files
......
843 847
        share/man/onevm.8.gz \
844 848
        share/man/onevnet.8.gz \
845 849
        share/man/onetemplate.8.gz \
850
        share/man/onegroup.8.gz \
846 851
        share/man/econe-describe-images.8.gz \
847 852
        share/man/econe-describe-instances.8.gz \
848 853
        share/man/econe-register.8.gz \
share/test/do_tests.sh
18 18
       $TWD_DIR/um/test \
19 19
       $TWD_DIR/lcm/test \
20 20
       $TWD_DIR/pool/test \
21
       $TWD_DIR/vm_template/test"
21
       $TWD_DIR/vm_template/test \
22
       $TWD_DIR/group/test"
22 23

  
23 24
#-------------------------------------------------------------------------------
24 25
# COMMAND LINE PARSING
src/authm/AuthManager.cc
87 87
        case USER:     oss << "USER:" ; break;
88 88
        case CLUSTER:  oss << "CLUSTER:" ; break;
89 89
        case TEMPLATE: oss << "TEMPLATE:" ; break;
90
        case GROUP:    oss << "GROUP:" ; break;
90 91
    }
91 92

  
92 93
    if (op == CREATE || op == INSTANTIATE) //encode the ob_id, it is a template
src/cli/client_utilities.rb
321 321
    get_entity_id(name, OpenNebula::TemplatePool)
322 322
end
323 323

  
324
def get_group_id(name)
325
    get_entity_id(name, OpenNebula::GroupPool)
326
end
327

  
324 328
def str_running_time(data)
325 329
    stime=Time.at(data["STIME"].to_i)
326 330
    if data["ETIME"]=="0"
src/cli/onegroup
1
#!/usr/bin/env ruby
2

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

  
19
ONE_LOCATION=ENV["ONE_LOCATION"]
20

  
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
end
26

  
27
$: << RUBY_LIB_LOCATION
28

  
29

  
30
require 'OpenNebula'
31
require 'client_utilities'
32
require 'command_parse'
33

  
34

  
35
ShowTableUP={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for group",
39
        :size => 4,
40
        :proc => lambda {|d,e|
41
            d.id
42
        }
43
    },
44
    :uid => {
45
        :name => "OWNER",
46
        :desc => "Owner of the group",
47
        :size => 5,
48
        :proc => lambda {|d,e|
49
            d.uid
50
        }
51
    },
52
    :name => {
53
        :name => "NAME",
54
        :desc => "Name of the group",
55
        :size => 16,
56
        :proc => lambda {|d,e|
57
            d.name
58
        }
59
    },
60

  
61
    :default => [:id, :uid, :name]
62
}
63

  
64
class UPShow
65
    def initialize
66
        @grouppool=OpenNebula::GroupPool.new(get_one_client)
67
        @table=ShowTable.new(ShowTableUP)
68
    end
69

  
70
    def header_up_small
71
        scr_bold
72
        scr_underline
73
        print @table.header_str
74
        scr_restore
75
        puts ""
76
    end
77

  
78
    def list_short(options=nil)
79
        res=@grouppool.info
80
        if options
81
            @table.columns=options[:columns] if options[:columns]
82
        end
83

  
84
        if OpenNebula.is_error?(res)
85
            result=res
86
        else
87
            result=res
88
            header_up_small
89

  
90
            puts @table.data_str(@grouppool, options)
91
            result
92
        end
93
    end
94
end
95

  
96
class OneUPParse < CommandParse
97

  
98
    COMMANDS_HELP=<<-EOT
99

  
100
Description:
101

  
102
This command enables the OpenNebula administrator to manage groups.
103

  
104

  
105
Commands:
106

  
107
* create (Creates a new group)
108
    onegroup create groupname
109

  
110
* delete (Removes a group)
111
    onegroup delete <id>
112

  
113
* list (Lists all the groups in the pool)
114
    onegroup list
115

  
116
EOT
117

  
118
    def text_commands
119
        COMMANDS_HELP
120
    end
121

  
122
    def text_command_name
123
        "onegroup"
124
    end
125

  
126
    def list_options
127
        table=ShowTable.new(ShowTableUP)
128
        table.print_help
129
    end
130

  
131
end
132

  
133
oneup_opts=OneUPParse.new([:list, :xml])
134
oneup_opts.parse(ARGV)
135
ops=oneup_opts.options
136

  
137
result=[false, "Unknown error"]
138

  
139
command=ARGV.shift
140

  
141
case command
142
when "create"
143
    check_parameters("create", 1)
144
    group=OpenNebula::Group.new(OpenNebula::Group.build_xml, get_one_client)
145
    result=group.allocate(ARGV[0])
146

  
147
    if is_successful?(result)
148
        puts "ID: " + group.id.to_s if ops[:verbose]
149
        exit 0
150
    end
151

  
152
when "delete"
153
    check_parameters("delete", 1)
154
    args=expand_args(ARGV)
155

  
156
    args.each do |param|
157
        group_id=get_group_id(param)
158
        group=OpenNebula::Group.new(
159
            OpenNebula::Group.build_xml(group_id), get_one_client)
160
        result=group.delete
161
        if !OpenNebula.is_error?(result)
162
            puts "Group deleted" if ops[:verbose]
163
            break
164
        end
165
    end
166

  
167
when "list"
168
    if !ops[:xml]
169
        uplist=UPShow.new
170
        ops[:columns]=ops[:list] if ops[:list]
171
        result=uplist.list_short(ops)
172
    else
173
        grouppool=OpenNebula::GroupPool.new(get_one_client)
174
        grouppool.info
175
        puts grouppool.to_xml(true)
176
    end
177

  
178
else
179
    oneup_opts.print_help
180
    exit -1
181
end
182

  
183
if OpenNebula.is_error?(result)
184
    puts "Error: " + result.message
185
    exit -1
186
end
src/cluster/test/SConstruct
40 40
    'nebula_template',
41 41
    'nebula_vm',
42 42
    'nebula_vmtemplate',
43
    'nebula_group',
43 44
    'nebula_vnm',
44 45
    'nebula_image',
45 46
    'nebula_pool',
src/group/Group.cc
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 <limits.h>
18
#include <string.h>
19

  
20
#include <iostream>
21
#include <sstream>
22

  
23
#include "Group.h"
24

  
25

  
26
const char * Group::table = "group_pool";
27

  
28
const char * Group::db_names = "oid, name, body, uid";
29

  
30
const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool ("
31
    "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
32
    "UNIQUE(name))";
33

  
34

  
35
/* ************************************************************************ */
36
/* Group :: Constructor/Destructor                                        */
37
/* ************************************************************************ */
38

  
39
Group::Group(int id, int uid, const string& name):
40
    PoolObjectSQL(id,name,uid,table){};
41

  
42
Group::~Group(){};
43

  
44
/* ************************************************************************ */
45
/* Group :: Database Access Functions                                     */
46
/* ************************************************************************ */
47

  
48
/* ------------------------------------------------------------------------ */
49
/* ------------------------------------------------------------------------ */
50

  
51
int Group::insert(SqlDB *db, string& error_str)
52
{
53
    int rc;
54

  
55
    rc = insert_replace(db, false);
56

  
57
    if ( rc != 0 )
58
    {
59
        error_str = "Error inserting Group in DB.";
60
    }
61

  
62
    return rc;
63
}
64

  
65
/* ------------------------------------------------------------------------ */
66
/* ------------------------------------------------------------------------ */
67

  
68
int Group::update(SqlDB *db)
69
{
70
    int    rc;
71

  
72
    rc = insert_replace(db, true);
73

  
74
    return rc;
75
}
76

  
77
/* ------------------------------------------------------------------------ */
78
/* ------------------------------------------------------------------------ */
79

  
80
int Group::insert_replace(SqlDB *db, bool replace)
81
{
82
    ostringstream   oss;
83

  
84
    int    rc;
85
    string xml_body;
86

  
87
    char * sql_name;
88
    char * sql_xml;
89

  
90
   // Update the Group
91

  
92
    sql_name = db->escape_str(name.c_str());
93

  
94
    if ( sql_name == 0 )
95
    {
96
        goto error_name;
97
    }
98

  
99
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
100

  
101
    if ( sql_xml == 0 )
102
    {
103
        goto error_body;
104
    }
105

  
106
    if(replace)
107
    {
108
        oss << "REPLACE";
109
    }
110
    else
111
    {
112
        oss << "INSERT";
113
    }
114

  
115
    // Construct the SQL statement to Insert or Replace
116

  
117
    oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
118
        <<          oid                 << ","
119
        << "'" <<   sql_name            << "',"
120
        << "'" <<   sql_xml             << "',"
121
        <<          uid                 << ")";
122

  
123
    rc = db->exec(oss);
124

  
125
    db->free_str(sql_name);
126
    db->free_str(sql_xml);
127

  
128
    return rc;
129

  
130
error_body:
131
    db->free_str(sql_name);
132
error_name:
133
    return -1;
134
}
135

  
136
/* ************************************************************************ */
137
/* Group :: Misc                                                          */
138
/* ************************************************************************ */
139

  
140
ostream& operator<<(ostream& os, Group& group)
141
{
142
    string group_str;
143

  
144
    os << group.to_xml(group_str);
145

  
146
    return os;
147
}
148

  
149
/* ------------------------------------------------------------------------ */
150
/* ------------------------------------------------------------------------ */
151

  
152
string& Group::to_xml(string& xml) const
153
{
154
    ostringstream   oss;
155

  
156
    oss <<
157
    "<GROUP>"  <<
158
        "<ID>"   << oid  << "</ID>"   <<
159
        "<UID>"  << uid  << "</UID>"  <<
160
        "<NAME>" << name << "</NAME>" <<
161
    "</GROUP>";
162

  
163
    xml = oss.str();
164

  
165
    return xml;
166
}
167

  
168
/* ------------------------------------------------------------------------ */
169
/* ------------------------------------------------------------------------ */
170

  
171
int Group::from_xml(const string& xml)
172
{
173
    int rc = 0;
174

  
175
    // Initialize the internal XML object
176
    update_from_str(xml);
177

  
178
    // Get class base attributes
179
    rc += xpath(oid, "/GROUP/ID",   -1);
180
    rc += xpath(uid, "/GROUP/UID",  -1);
181
    rc += xpath(name,"/GROUP/NAME", "not_found");
182

  
183
    if (rc != 0)
184
    {
185
        return -1;
186
    }
187

  
188
    return 0;
189
}
src/group/GroupPool.cc
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
}
src/group/SConstruct
1
# SConstruct for src/group
2

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

  
19
Import('env')
20

  
21
lib_name='nebula_group'
22

  
23
# Sources to generate the library
24
source_files=[
25
    'GroupPool.cc',
26
    'Group.cc'
27
]
28

  
29
# Build library
30
env.StaticLibrary(lib_name, source_files)
src/group/test/GroupPoolTest.cc
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 <string>
18
#include <iostream>
19
#include <stdlib.h>
20

  
21
#include "GroupPool.h"
22
#include "PoolTest.h"
23

  
24
using namespace std;
25

  
26
const int uids[] = {0,1,2};
27
const string names[] = {"First name", "Second name"};
28

  
29
const string xmls[] =
30
{
31
    "<GROUP><ID>1</ID><UID>0</UID><NAME>First name</NAME></GROUP>",
32
    "<GROUP><ID>2</ID><UID>1</UID><NAME>Second name</NAME></GROUP>"
33
};
34

  
35
const string group_xml_dump =
36
    "<GROUP_POOL><GROUP><ID>0</ID><UID>0</UID><NAME>default</NAME></GROUP><GROUP><ID>1</ID><UID>5</UID><NAME>group_a</NAME></GROUP><GROUP><ID>3</ID><UID>5</UID><NAME>group_c</NAME></GROUP><GROUP><ID>4</ID><UID>5</UID><NAME>group_d</NAME></GROUP></GROUP_POOL>";
37

  
38
/* ************************************************************************* */
39
/* ************************************************************************* */
40

  
41

  
42
#include "NebulaTest.h"
43

  
44
class NebulaTestGroup: public NebulaTest
45
{
46
public:
47
    NebulaTestGroup():NebulaTest()
48
    {
49
        NebulaTest::the_tester = this;
50

  
51
        need_group_pool= true;
52
    }
53
};
54

  
55
class GroupPoolTest : public PoolTest
56
{
57
    CPPUNIT_TEST_SUITE (GroupPoolTest);
58

  
59
    // Not all tests from PoolTest can be used. Because
60
    // of the initial default group added to the DB, the
61
    // oid_assignment would fail.
62
    CPPUNIT_TEST (get_from_cache);
63
    CPPUNIT_TEST (get_from_db);
64
    CPPUNIT_TEST (wrong_get);
65
    CPPUNIT_TEST (drop_and_get);
66

  
67
    CPPUNIT_TEST (duplicates);
68
    CPPUNIT_TEST (dump);
69

  
70
    CPPUNIT_TEST_SUITE_END ();
71

  
72
protected:
73

  
74
    NebulaTestGroup * tester;
75
    GroupPool *       gpool;
76

  
77

  
78

  
79
    void bootstrap(SqlDB* db)
80
    {
81
        // setUp overwritten
82
    };
83

  
84
    PoolSQL* create_pool(SqlDB* db)
85
    {
86
        // setUp overwritten
87
        return gpool;
88
    };
89

  
90
    int allocate(int index)
91
    {
92
        int    oid;
93
        string err;
94

  
95
        return gpool->allocate(uids[index], names[index], &oid, err);
96
    };
97

  
98
    void check(int index, PoolObjectSQL* obj)
99
    {
100
        Group * group = static_cast<Group *>(obj);
101

  
102
        CPPUNIT_ASSERT( obj != 0 );
103

  
104
        string xml_str = "";
105
        string name = group->get_name();
106

  
107
        CPPUNIT_ASSERT( name == names[index] );
108

  
109
        // Get the xml
110
        group->to_xml(xml_str);
111

  
112
//  A little help for debugging
113
/*
114
        if( xml_str != xmls[index] )
115
        {
116
            cout << endl << xml_str << endl << "========"
117
                 << endl << xmls[index];
118
        }
119
//*/
120
        CPPUNIT_ASSERT( xml_str == xmls[index]);
121
    };
122

  
123
private:
124

  
125

  
126
public:
127
    GroupPoolTest()
128
    {
129
        xmlInitParser();
130
    };
131

  
132
    ~GroupPoolTest()
133
    {
134
        xmlCleanupParser();
135
    };
136

  
137
    void setUp()
138
    {
139
        create_db();
140

  
141
        tester = new NebulaTestGroup();
142

  
143
        Nebula& neb = Nebula::instance();
144
        neb.start();
145

  
146
        gpool   = neb.get_gpool();
147

  
148
        pool    = gpool;
149
    };
150

  
151
    void tearDown()
152
    {
153
        delete_db();
154

  
155
        delete tester;
156
    };
157

  
158
    /* ********************************************************************* */
159
    /* ********************************************************************* */
160

  
161
    void duplicates()
162
    {
163
        int         rc, oid;
164
        string      err;
165
        GroupPool * gpool = static_cast<GroupPool*>(pool);
166

  
167
        // Allocate a group
168
        rc = gpool->allocate(uids[0], names[0], &oid, err);
169
        CPPUNIT_ASSERT( oid == 1 );
170
        CPPUNIT_ASSERT( oid == rc );
171

  
172
        // Try to allocate twice the same group, should fail
173
        rc = gpool->allocate(uids[0], names[0], &oid, err);
174
        CPPUNIT_ASSERT( rc  == -1 );
175
        CPPUNIT_ASSERT( oid == rc );
176

  
177
        // Try again, this time with different uid. Should fail, groups can't
178
        // repeat names
179
        rc = gpool->allocate(uids[1], names[0], &oid, err);
180
        CPPUNIT_ASSERT( rc  == -1 );
181
        CPPUNIT_ASSERT( oid == rc );
182
    }
183

  
184
    /* ********************************************************************* */
185

  
186
    void dump()
187
    {
188
        Group *         group;
189
        int             oid, rc;
190
        ostringstream   oss;
191
        string          err;
192

  
193
        // Allocate some groups
194
        rc = gpool->allocate(5, "group_a", &oid, err);
195
        CPPUNIT_ASSERT( rc == 1 );
196

  
197
        rc = gpool->allocate(5, "group_b", &oid, err);
198
        CPPUNIT_ASSERT( rc == 2 );
199

  
200
        rc = gpool->allocate(5, "group_c", &oid, err);
201
        CPPUNIT_ASSERT( rc == 3 );
202

  
203
        rc = gpool->allocate(5, "group_d", &oid, err);
204
        CPPUNIT_ASSERT( rc == 4 );
205

  
206
        // Drop one of them
207
        group = gpool->get(2, false);
208
        CPPUNIT_ASSERT( group != 0 );
209

  
210
        rc = gpool->drop(group);
211
        CPPUNIT_ASSERT( rc == 0 );
212

  
213
        // dump the pool
214
        rc = gpool->dump(oss,"");
215
/*
216
        if( oss.str() != group_xml_dump )
217
        {
218
            cout << endl << oss.str() << endl << "========"
219
                 << endl << group_xml_dump;
220
        }
221
//*/
222
        CPPUNIT_ASSERT( oss.str() == group_xml_dump );
223

  
224
    }
225
};
226

  
227
/* ************************************************************************* */
228
/* ************************************************************************* */
229

  
230
int main(int argc, char ** argv)
231
{
232
    return PoolTest::main(argc, argv, GroupPoolTest::suite());
233
}
src/group/test/SConstruct
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
Import('env')
18

  
19
env.Prepend(LIBS=[
20
    'nebula_cluster',
21
    'nebula_host',
22
    'nebula_pool',
23
    'nebula_template',
24
    'nebula_xml',
25
    'nebula_log',
26
    'nebula_common',
27
    'nebula_sql',
28

  
29
### TODO: delete not needed
30
    'nebula_core_test',
31
    'nebula_host',
32
    'nebula_cluster',
33
    'nebula_xml',
34
    'nebula_vmm',
35
    'nebula_im',
36
    'nebula_rm',
37
    'nebula_tm',
38
    'nebula_um',
39
    'nebula_mad',
40
    'nebula_template',
41
    'nebula_vm',
42
    'nebula_vmtemplate',
43
    'nebula_group',
44
    'nebula_vnm',
45
    'nebula_image',
46
    'nebula_pool',
47
    'nebula_hm',
48
    'nebula_authm',
49
    'nebula_common',
50
    'nebula_lcm',
51
    'nebula_dm',
52
    'nebula_sql',
53
    'nebula_log',
54
    'crypto'
55
])
56

  
57
env.Program('test','GroupPoolTest.cc')
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff