Statistics
| Branch: | Tag: | Revision:

one / include / VMGroup.h @ 202b47e9

History | View | Annotate | Download (5.27 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems              */
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 VMGROUP_H_
18
#define VMGROUP_H_
19

    
20
#include "PoolObjectSQL.h"
21
#include "VMGroupRole.h"
22

    
23
class VMGroupPool;
24

    
25
/**
26
 *  A VM group is a set of related VMs that may impose placement constraints.
27
 *
28
 *  Data model:
29
 *
30
 *  NAME        = "Web server"
31
 *  DESCRIPTION = "A multi-tier web server: frontend, apps servers, db"
32
 *
33
 *  ROLE = [
34
 *    NAME = "frontend",
35
 *    ID   = 0,
36
 *    VMS  = "0,1"
37
 *  ]
38
 *
39
 *  ROLE = [
40
 *    NAME = "db",
41
 *    ID   = 1,
42
 *    VMS  = "2,3,4,5"
43
 *  ]
44
 *
45
 *  ANTI_AFFINED = "db", "front_end"
46
 */
47
class VMGroup : public PoolObjectSQL
48
{
49
public:
50
    /**
51
     * Function to print the VMGroup object into a string in XML format
52
     *   @param xml the resulting XML string
53
     *   @return a reference to the generated string
54
     */
55
    string& to_xml(string& xml) const;
56

    
57
    /**
58
     *  Rebuilds the object from an xml formatted string
59
     *    @param xml_str The xml-formatted string
60
     *    @return 0 on success, -1 otherwise
61
     */
62
    int from_xml(const string &xml_str);
63

    
64
    /**
65
     *  Returns a copy of the Template
66
     *    @return A copy of the Template
67
     */
68
    Template * clone_template() const
69
    {
70
        return new Template(*obj_template);
71
    };
72

    
73
private:
74
    // -------------------------------------------------------------------------
75
    // Friends
76
    // -------------------------------------------------------------------------
77
    friend class VMGroupPool;
78

    
79
    // -------------------------------------------------------------------------
80
    // Constructor
81
    // -------------------------------------------------------------------------
82
    VMGroup(int _uid, int _gid, const string& _uname, const string& _gname,
83
            int _umask, Template * group_template);
84

    
85
    ~VMGroup();
86

    
87
    // -------------------------------------------------------------------------
88
    // Role Management
89
    // -------------------------------------------------------------------------
90
    /**
91
     *  Check if all the roles are defined in the group
92
     *    @param aname attribute with a list (comma-separated) of role names
93
     *    @param error_str if any
94
     *
95
     *    @return 0 if all roles are defined -1 otherwise
96
     */
97
    int check_affinity(const std::string& aname, std::string& error_str);
98

    
99
    // -------------------------------------------------------------------------
100
    // DataBase implementation
101
    // -------------------------------------------------------------------------
102
    static const char * db_names;
103

    
104
    static const char * db_bootstrap;
105

    
106
    static const char * table;
107

    
108
    /**
109
     *  Execute an INSERT or REPLACE Sql query.
110
     *    @param db The SQL DB
111
     *    @param replace Execute an INSERT or a REPLACE
112
     *    @param error_str Returns the error reason, if any
113
     *    @return 0 one success
114
     */
115
    int insert_replace(SqlDB *db, bool replace, string& error_str);
116

    
117
    /**
118
     *  Bootstraps the database table(s) associated to the VMGroup
119
     *    @return 0 on success
120
     */
121
    static int bootstrap(SqlDB * db)
122
    {
123
        ostringstream oss(VMGroup::db_bootstrap);
124

    
125
        return db->exec(oss);
126
    };
127

    
128
    /**
129
     *  Writes the VMGroup in the database.
130
     *    @param db pointer to the db
131
     *    @return 0 on success
132
     */
133
    int insert(SqlDB *db, string& error_str);
134

    
135
    /**
136
     *  Writes/updates the VMGroup's data fields in the database.
137
     *    @param db pointer to the db
138
     *    @return 0 on success
139
     */
140
    int update(SqlDB *db)
141
    {
142
        string error_str;
143
        return insert_replace(db, true, error_str);
144
    }
145

    
146
    /**
147
     * Checks the new roles and affined/anti-affined cross-references
148
     *    @param error string describing the error if any
149
     *    @return 0 on success
150
     */
151
    int post_update_template(string& error)
152
        {
153
                //TODO
154
                return 0;
155
        };
156

    
157
    /**
158
     *  Factory method for VMGroup templates
159
     */
160
    Template * get_new_template() const
161
    {
162
        return new Template;
163
    }
164

    
165
    // -------------------------------------------------------------------------
166
    // VMGroup attributes
167
    // -------------------------------------------------------------------------
168
        /**
169
         *  The role set
170
         */
171
        VMGroupRoles roles;
172
};
173

    
174
#endif /*VMGROUP_H_*/
175