コード例 #1
0
        public static void AddGlobalModule(GlobalModule module)
        {
            if (module == null)
            {
                throw new ArgumentNullException("nativeModule");
            }
            // NOTE: Can add a new native module only when configuring the server scope
            // and NOT in the location mode

            // Native modules can only be added at the server level
            ModuleCollection        serverCollection = GetModulesCollection(null, null);
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (IsGlobalModule(module.Name, globalCollection, out element))
            {
                throw new AlreadyExistsException("module.name");
            }

            Module action = null;

            if (ExistsModule(module.Name, serverCollection, out action))
            {
                throw new AlreadyExistsException("module.name");
            }

            // Add to global modules
            element = globalCollection.Add(module);

            // NOTE: When we add a new native module we do not add it to the server modules
            // list.
        }
コード例 #2
0
 public static object GlobalModuleToJsonModelRef(GlobalModule globalModule, Fields fields = null)
 {
     if (fields == null || !fields.HasFields)
     {
         return(GlobalModuleToJsonModel(globalModule, GlobalModuleRefFields, false));
     }
     else
     {
         return(GlobalModuleToJsonModel(globalModule, fields, false));
     }
 }
コード例 #3
0
        public object Get(string id)
        {
            GlobalModuleId moduleId = GlobalModuleId.CreateFromUuid(id);

            GlobalModule module = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(moduleId.Name));

            if (module == null)
            {
                return(NotFound());
            }

            return(ModuleHelper.GlobalModuleToJsonModel(module));
        }
コード例 #4
0
        private static bool IsGlobalModule(string moduleName, GlobalModulesCollection collection,
                                           out GlobalModule element)
        {
            element = null;
            foreach (GlobalModule e in collection)
            {
                if (String.Equals(e.Name, moduleName, StringComparison.Ordinal))
                {
                    element = e;
                    return(true);
                }
            }

            return(false);
        }
コード例 #5
0
        public static GlobalModule UpdateGlobalModule(GlobalModule globalModule, dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (globalModule == null)
            {
                throw new ArgumentNullException("globalModule");
            }

            Configuration config = ManagementUnit.GetConfiguration(null, null);

            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();
            ModuleCollection        serverCollection = GetModulesCollection(null, null);

            Module action = null;

            string image = DynamicHelper.Value(model.image);

            if (image != null)
            {
                if (!File.Exists(System.Environment.ExpandEnvironmentVariables(image)))
                {
                    throw new NotFoundException("image");
                }

                globalModule.Image = image;
            }

            string preCondition = globalModule.PreCondition;

            BitnessUtility.AppendBitnessPreCondition(ref preCondition, image);
            globalModule.PreCondition = preCondition;

            // If the global module is present in the server modules list then we
            // update the precondition of that entry as well.
            if (ExistsModule(globalModule.Name, serverCollection, out action))
            {
                if (ConfigurationUtility.ShouldPersist(action.PreCondition, globalModule.PreCondition))
                {
                    action.PreCondition = globalModule.PreCondition;
                }
            }

            return(globalModule);
        }
コード例 #6
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            GlobalModuleId moduleId = GlobalModuleId.CreateFromUuid(id);

            GlobalModule module = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(moduleId.Name));

            if (module == null)
            {
                return(NotFound());
            }

            module = ModuleHelper.UpdateGlobalModule(module, model);

            ManagementUnit.Current.Commit();

            return(ModuleHelper.GlobalModuleToJsonModel(module));
        }
コード例 #7
0
        public object Post([FromBody] dynamic model)
        {
            GlobalModule module = null;

            // Create a global module
            module = ModuleHelper.CreateGlobalModule(model);

            // Save it
            ModuleHelper.AddGlobalModule(module);
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic gm = ModuleHelper.GlobalModuleToJsonModel(module);

            return(Created(ModuleHelper.GetGlobalModuleLocation(gm.id), gm));
        }
コード例 #8
0
        public void Delete(string id, [FromBody] dynamic model)
        {
            GlobalModuleId moduleId = GlobalModuleId.CreateFromUuid(id);

            GlobalModule module = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(moduleId.Name));

            if (module != null)
            {
                // Delete target global module
                ModuleHelper.DeleteGlobalModule(module);

                // Save changes
                ManagementUnit.Current.Commit();
            }

            // Success
            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
コード例 #9
0
        public static Module AddExistingGlobalModule(GlobalModule globalModule, ModulesSection section)
        {
            if (globalModule == null)
            {
                throw new ArgumentNullException("globalModule");
            }

            ModuleCollection        collection       = section.Modules;
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (!IsGlobalModule(globalModule.Name, globalCollection, out element))
            {
                throw new Exception(ModulesErrors.ModuleNotPresentInGlobalModulesError);
            }

            Module module = null;

            if (ExistsModule(globalModule.Name, collection, out module))
            {
                throw new Exception(ModulesErrors.ModuleAlreadyPresentError);
            }

            try {
                module = collection.Add(globalModule.Name);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            if (!String.IsNullOrEmpty(element.PreCondition))
            {
                module.PreCondition = element.PreCondition;
            }

            return(module);
        }
コード例 #10
0
        internal static object GlobalModuleToJsonModel(GlobalModule globalModule, Fields fields = null, bool full = true)
        {
            if (globalModule == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj = new ExpandoObject();

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = globalModule.Name;
            }

            //
            // id
            obj.id = GlobalModuleId.CreateFromName(globalModule.Name).Uuid;

            //
            // image
            if (fields.Exists("image"))
            {
                obj.image = globalModule.Image;
            }

            //
            // precondition
            if (fields.Exists("precondition"))
            {
                obj.precondition = globalModule.PreCondition;
            }

            return(Core.Environment.Hal.Apply(Defines.GlobalModulesResource.Guid, obj, full));
        }
コード例 #11
0
        public static void AddManagedModule(Module module, ModulesSection section)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            ModuleCollection        serverCollection = section.Modules;
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (IsGlobalModule(module.Name, globalCollection, out element))
            {
                throw new ApiArgumentException("Module already exists", "name");
            }

            Module newModule = null;

            if (ExistsModule(module.Name, serverCollection, out newModule))
            {
                throw new ApiArgumentException("Module already exists", "name");
            }

            try {
                newModule = serverCollection.Add(module.Name, module.Type);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            if (!String.IsNullOrEmpty(module.PreCondition))
            {
                newModule.PreCondition = module.PreCondition;
            }
        }
コード例 #12
0
        public static GlobalModule CreateGlobalModule(dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string name  = DynamicHelper.Value(model.name);
            string image = DynamicHelper.Value(model.image);

            if (string.IsNullOrEmpty(name))
            {
                throw new ApiArgumentException("name");
            }
            if (string.IsNullOrEmpty(image))
            {
                throw new ApiArgumentException("image");
            }
            if (!File.Exists(System.Environment.ExpandEnvironmentVariables(image)))
            {
                throw new NotFoundException("image");
            }

            var globalCollection = GetGlobalModulesCollection();

            GlobalModule globalModule = globalCollection.CreateElement();

            globalModule.Name         = name;
            globalModule.Image        = image;
            globalModule.PreCondition = DynamicHelper.Value(model.precondition) ?? globalModule.PreCondition;

            // This sets the correct bitness precondition
            string preCondition = globalModule.PreCondition;

            BitnessUtility.AppendBitnessPreCondition(ref preCondition, globalModule.Image);
            globalModule.PreCondition = preCondition;

            return(globalModule);
        }
コード例 #13
0
        public static void DeleteGlobalModule(GlobalModule module)
        {
            if (String.IsNullOrEmpty(module.Name))
            {
                throw new ArgumentNullException("nativeModule.Name");
            }

            // Native modules can only be configured at the server level
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (!IsGlobalModule(module.Name, globalCollection, out element))
            {
                return;
            }

            // If the global module is in the enabled modules collection, remove it from there first
            DeleteModule(module.Name, null, null);

            // Remove the global modules from global modules collection
            globalCollection.Remove(element);
        }
コード例 #14
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.modules == null || !(model.modules is JObject))
            {
                throw new ApiArgumentException("modules");
            }

            string modulesUuid = DynamicHelper.Value(model.modules.id);

            if (modulesUuid == null)
            {
                throw new ApiArgumentException("modules.id");
            }

            // Get the feature id
            Module    module    = null;
            ModulesId modulesId = new ModulesId(modulesUuid);
            Site      site      = modulesId.SiteId == null ? null : SiteHelper.GetSite(modulesId.SiteId.Value);

            if (modulesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            ModuleHelper.EnsureValidScope(site, modulesId.Path);

            string         configPath = ManagementUnit.ResolveConfigScope(model);
            ModulesSection section    = ModuleHelper.GetModulesSection(site, modulesId.Path, configPath);

            // The post could either be creating a Managed module, or adding an existing
            // global module to the modules list.
            // This information is taken from the model's type value
            string type = DynamicHelper.Value(model.type);

            if (string.IsNullOrEmpty(type))
            {
                // The module being added is a global/native module

                string name = DynamicHelper.Value(model.name);

                if (string.IsNullOrEmpty(name))
                {
                    throw new ApiArgumentException("name");
                }

                GlobalModule existingGlobalModule = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(name));

                // Adding a global module to the modules list means it must already exist in global modules
                if (existingGlobalModule == null)
                {
                    throw new NotFoundException("name");
                }

                // Add the existing global module
                module = ModuleHelper.AddExistingGlobalModule(existingGlobalModule, section);
                ManagementUnit.Current.Commit();
            }
            else
            {
                // Module being added to enabled modules is a managed module

                // Create module from model
                module = ModuleHelper.CreateManagedModule(model, section);

                // Save it
                ModuleHelper.AddManagedModule(module, section);
                ManagementUnit.Current.Commit();
            }


            //
            // Create response
            dynamic moduleEntry = ModuleHelper.ModuleToJsonModel(module, site, modulesId.Path);

            return(Created(ModuleHelper.GetModuleEntryLocation(moduleEntry.id), moduleEntry));
        }