Revision c523b889 src/rm/RequestManagerUpdateTemplate.cc

View differences:

src/rm/RequestManagerUpdateTemplate.cc
70 70
    return;
71 71
}
72 72

  
73
/* ------------------------------------------------------------------------- */
74
/* ------------------------------------------------------------------------- */
75

  
76
void DatastoreUpdateTemplate::request_execute(
77
        xmlrpc_c::paramList const& paramList,
78
        RequestAttributes& att)
79
{
80
    int    rc;
81
    string error_str;
82

  
83
    int    oid  = xmlrpc_c::value_int(paramList.getInt(1));
84
    string tmpl = xmlrpc_c::value_string(paramList.getString(2));
85

  
86
    Datastore * object;
87

  
88
    if ( basic_authorization(oid, att) == false )
89
    {
90
        return;
91
    }
92

  
93
    object = static_cast<DatastorePool*>(pool)->get(oid,true);
94

  
95
    if ( object == 0 )
96
    {
97
        failure_response(NO_EXISTS,
98
                get_error(object_name(auth_object),oid),
99
                att);
100

  
101
        return;
102
    }
103

  
104
    Datastore::DatastoreType prev_type = object->get_type();
105
    Datastore::DatastoreType new_type = Datastore::type_in_template(tmpl);
106

  
107
    int cluster_id = object->get_cluster_id();
108

  
109
    if ( (prev_type == Datastore::SYSTEM_DS || new_type == Datastore::SYSTEM_DS) &&
110
        prev_type != new_type &&
111
        cluster_id != ClusterPool::NONE_CLUSTER_ID )
112
    {
113
        object->unlock();
114

  
115
        Cluster *cluster = Nebula::instance().get_clpool()->get(cluster_id, true);
116

  
117
        if ( cluster == 0 )
118
        {
119
            failure_response(NO_EXISTS,
120
                    get_error(object_name(PoolObjectSQL::CLUSTER),cluster_id),
121
                    att);
122

  
123
            return;
124
        }
125

  
126
        if ( new_type == Datastore::SYSTEM_DS )
127
        {
128
            if ( cluster->get_ds_id() == DatastorePool::SYSTEM_DS_ID )
129
            {
130
                // Cluster has a new system ds
131
                cluster->set_ds_id(oid);
132
            }
133
            else
134
            {
135
                ostringstream oss;
136
                oss << "Cannot change datastore type to "
137
                    << Datastore::type_to_str(Datastore::SYSTEM_DS)
138
                    << ". Cluster " << cluster_id
139
                    << " already contains the System Datastore "
140
                    << cluster->get_ds_id() << ".";
141

  
142
                failure_response(INTERNAL,
143
                        request_error(oss.str(),error_str),
144
                        att);
145

  
146
                cluster->unlock();
147

  
148
                return;
149
            }
150
        }
151
        else if ( cluster->get_ds_id() == oid )
152
        {
153
            // This was the cluster's system DS, now it must be set to '0'
154
            cluster->set_ds_id( DatastorePool::SYSTEM_DS_ID );
155
        }
156

  
157
        Nebula::instance().get_clpool()->update(cluster);
158
        cluster->unlock();
159

  
160
        object = static_cast<DatastorePool*>(pool)->get(oid,true);
161

  
162
        if ( object == 0 )
163
        {
164
            failure_response(NO_EXISTS,
165
                    get_error(object_name(auth_object),oid),
166
                    att);
167

  
168
            return;
169
        }
170
    }
171

  
172
    rc = object->replace_template(tmpl, error_str);
173

  
174
    if ( rc != 0 )
175
    {
176
        // TODO: rollback
177

  
178
        failure_response(INTERNAL,
179
                request_error("Cannot update template",error_str),
180
                att);
181
        object->unlock();
182

  
183
        return;
184
    }
185

  
186
    pool->update(object);
187

  
188
    object->unlock();
189

  
190
    success_response(oid, att);
191

  
192
    return;
193
}
194

  
195

  

Also available in: Unified diff