Exemple #1
0
        private void OnUnprovision(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.UnprovisionRequestDebugLogMessage, ServiceDescription(), msg);

            SimpleResponse response = new SimpleResponse();

            UnprovisionRequest unprovision_req = new UnprovisionRequest();

            unprovision_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg));
            string name = unprovision_req.Name;

            // Create a service credentials object with a name set, so we can run this operation in parallel for different service instances.
            ServiceCredentials credentials = new ServiceCredentials();

            credentials.Name = name;

            credentials.ServiceWorkFactory.StartNew(
                () =>
            {
                try
                {
                    ServiceCredentials[] bindings = unprovision_req.Bindings;

                    bool result = this.Unprovision(name, bindings);

                    if (result)
                    {
                        this.NodeNats.Publish(reply, null, EncodeSuccess(response));
                    }
                    else
                    {
                        this.NodeNats.Publish(reply, null, EncodeFailure(response));
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warning(ex.ToString());
                    NodeNats.Publish(reply, null, EncodeFailure(response, ex));
                }
            });
        }
        private void OnUpdateInstance(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.OnUpdateInstanceRequestDebugLogMessage, ServiceDescription(), msg, reply);

            object[] request = new object[0];
            request = JsonConvertibleObject.DeserializeFromJsonArray(msg);

            Handle provisionCredentialsHandle = new Handle();
            provisionCredentialsHandle.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(request[0].ToString()));

            ServiceCredentials provisionCredentials = provisionCredentialsHandle.Credentials;
            Dictionary<string, object> bindingCredentialsHash = new Dictionary<string, object>();

            object[] bindingCredentialsHandle = new object[0];
            bindingCredentialsHandle = JsonConvertibleObject.DeserializeFromJsonArray(request[1].ToString());

            foreach (object obj in bindingCredentialsHandle)
            {
                Handle handle = new Handle();
                handle.FromJsonIntermediateObject(obj);
                bindingCredentialsHash.Add(handle.ServiceId, handle.Credentials);
            }

            Collection<object> responseHandles = new Collection<object>();
            provisionCredentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    try
                    {
                        object[] result = this.UpdateInstance(provisionCredentialsHandle.Credentials, bindingCredentialsHash);
                        capacity -= CapacityUnit();

                        provisionCredentials = (ServiceCredentials)result[0];
                        bindingCredentialsHash = (Dictionary<string, object>)result[1];
                        provisionCredentials.NodeId = this.nodeId;
                        provisionCredentialsHandle.Credentials = provisionCredentials;

                        responseHandles.Add(provisionCredentialsHandle.ToJson());

                        foreach (object obj in bindingCredentialsHandle)
                        {
                            Handle handle = new Handle();
                            handle.FromJsonIntermediateObject(obj);
                            handle.Credentials = (ServiceCredentials)bindingCredentialsHash[handle.ServiceId];
                            responseHandles.Add(handle.ToJson());
                        }

                        object[] response = responseHandles.ToArray();
                        nodeNats.Publish(reply, null, JsonConvertibleObject.SerializeToJson(response));
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        SimpleResponse response = new SimpleResponse();
                        nodeNats.Publish(reply, null, EncodeFailure(response, ex));
                    }
                });
        }
        private void OnUnbind(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.UnbindRequestDebugLogMessage, ServiceDescription(), msg, reply);
            SimpleResponse response = new SimpleResponse();
            UnbindRequest unbind_req = new UnbindRequest();
            unbind_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg));

            unbind_req.Credentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    try
                    {
                        bool result = this.Unbind(unbind_req.Credentials);

                        if (result)
                        {
                            this.nodeNats.Publish(reply, null, EncodeSuccess(response));
                        }
                        else
                        {
                            this.nodeNats.Publish(reply, null, EncodeFailure(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        nodeNats.Publish(reply, null, EncodeFailure(response, ex));
                    }
                });
        }
        private void OnUnprovision(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.UnprovisionRequestDebugLogMessage, ServiceDescription(), msg);

            SimpleResponse response = new SimpleResponse();

            UnprovisionRequest unprovision_req = new UnprovisionRequest();
            unprovision_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg));
            string name = unprovision_req.Name;

            // Create a service credentials object with a name set, so we can run this operation in parallel for different service instances.
            ServiceCredentials credentials = new ServiceCredentials();
            credentials.Name = name;

            credentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    try
                    {
                        ServiceCredentials[] bindings = unprovision_req.Bindings;

                        bool result = this.Unprovision(name, bindings);

                        if (result)
                        {
                            this.nodeNats.Publish(reply, null, EncodeSuccess(response));
                            this.capacity += CapacityUnit();
                        }
                        else
                        {
                            this.nodeNats.Publish(reply, null, EncodeFailure(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        nodeNats.Publish(reply, null, EncodeFailure(response, ex));
                    }
                });
        }
        private void OnRestore(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.OnRestoreDebugLogMessage, ServiceDescription(), msg, reply);
            SimpleResponse response = new SimpleResponse();
            try
            {
                RestoreRequest restore_message = new RestoreRequest();
                restore_message.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg));
                string instance_id = restore_message.InstanceId;
                string backup_path = restore_message.BackupPath;

                // TODO: vladi: need to make this parallel when we implement the actual restore for mssql
                bool result = this.Restore(instance_id, backup_path);
                if (result)
                {
                    nodeNats.Publish(reply, null, EncodeSuccess(response));
                }
                else
                {
                    nodeNats.Publish(reply, null, EncodeFailure(response));
                }
            }
            catch (Exception ex)
            {
                Logger.Warning(ex.ToString());
                nodeNats.Publish(reply, null, EncodeFailure(response, ex));
            }
        }
        private void OnPurgeOrphan(string msg, string reply, string subject)
        {
            // This may take a long time (it can unbind many services), so we run it on a different thread;
            ThreadPool.QueueUserWorkItem(
                (data) =>
                {
                    Logger.Debug(Strings.OnPurgeOrphanDebugLogMessage, ServiceDescription());
                    SimpleResponse response = new SimpleResponse();
                    try
                    {
                        PurgeOrphanRequest request = new PurgeOrphanRequest();
                        request.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg));

                        bool result = this.PurgeOrphan(request.OrphanInsList, request.OrphanBindingList);
                        if (result)
                        {
                            nodeNats.Publish(reply, null, EncodeSuccess(response));
                        }
                        else
                        {
                            nodeNats.Publish(reply, null, EncodeFailure(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        nodeNats.Publish(reply, null, EncodeFailure(response, ex));
                    }
                });
        }
        private void OnImportInstance(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.OnImportInstanceDebugLogMessage, ServiceDescription(), msg, reply);
            object[] request = new object[0];
            request = JsonConvertibleObject.DeserializeFromJsonArray(msg);

            Handle provisionCredentialsHandle = new Handle();
            provisionCredentialsHandle.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(request[0].ToString()));

            ServiceCredentials provisionCredentials = provisionCredentialsHandle.Credentials;
            Dictionary<string, object> bindingCredentialsHash = new Dictionary<string, object>();

            object[] bindingCredentialsHandle = new object[0];
            bindingCredentialsHandle = JsonConvertibleObject.DeserializeFromJsonArray(request[1].ToString());

            foreach (object obj in bindingCredentialsHandle)
            {
                Handle handle = new Handle();
                handle.FromJsonIntermediateObject(obj);
                bindingCredentialsHash.Add(handle.ServiceId, handle.Credentials);
            }

            string plan = provisionCredentialsHandle.Configuration.Plan;

            provisionCredentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    SimpleResponse response = new SimpleResponse();

                    try
                    {
                        string instance = provisionCredentials.Name;
                        string file_path = this.GetMigrationFolder(instance);

                        bool result = this.ImportInstance(provisionCredentials, bindingCredentialsHash, file_path, plan);
                        if (result)
                        {
                            nodeNats.Publish(reply, null, EncodeSuccess(response));
                        }
                        else
                        {
                            nodeNats.Publish(reply, null, EncodeFailure(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        nodeNats.Publish(reply, null, EncodeFailure(response));
                    }
                });
        }
        private void OnEnableInstance(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.OnEnableInstanceDebugMessage, ServiceDescription(), msg, reply);
            object[] request = new object[0];
            request = JsonConvertibleObject.DeserializeFromJsonArray(msg);

            Handle provisionCredentialsHandle = new Handle();
            provisionCredentialsHandle.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(request[0].ToString()));

            ServiceCredentials provisionCredentials = provisionCredentialsHandle.Credentials;
            Dictionary<string, object> bindingCredentialsHash = new Dictionary<string, object>();

            Handle[] bindingCredentialsHandle = new Handle[0];
            bindingCredentialsHandle = JsonConvertibleObject.ObjectToValue<Handle[]>(request[1]);
            foreach (Handle handle in bindingCredentialsHandle)
            {
                bindingCredentialsHash.Add(handle.ServiceId, handle.Credentials);
            }

            provisionCredentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    SimpleResponse response = new SimpleResponse();
                    try
                    {
                        bool result = this.EnableInstance(ref provisionCredentials, ref bindingCredentialsHash);

                        if (result)
                        {
                            nodeNats.Publish(reply, null, EncodeSuccess(response));
                        }
                        else
                        {
                            nodeNats.Publish(reply, null, EncodeFailure(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                    }
                });
        }
        private void OnCleanupNfs(string msg, string reply, string subject)
        {
            Logger.Debug(Strings.CleanupNfsLogMessage, ServiceDescription(), msg, reply);
            object[] request = new object[0];
            request = JsonConvertibleObject.DeserializeFromJsonArray(msg);

            Handle prov_cred = new Handle();
            prov_cred.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(request[0].ToString()));

            prov_cred.Credentials.ServiceWorkFactory.StartNew(
                () =>
                {
                    SimpleResponse response = new SimpleResponse();

                    try
                    {
                        string instance = prov_cred.ServiceId;
                        Directory.Delete(this.GetMigrationFolder(instance), true);

                        nodeNats.Publish(reply, null, EncodeSuccess(response));
                    }
                    catch (Exception ex)
                    {
                        Logger.Warning(ex.ToString());
                        nodeNats.Publish(reply, null, EncodeFailure(response, ex));
                    }
                });
        }