Search in sources :

Example 1 with ModuleModel

use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel in project ma-modules-public by infiniteautomation.

the class ModulesRestController method listModules.

@ApiOperation(value = "List Current Installed Modules", notes = "List all installed")
@RequestMapping(method = RequestMethod.GET, value = "/list")
public List<ModuleModel> listModules(@AuthenticationPrincipal PermissionHolder user) {
    permissionService.ensureAdminRole(user);
    List<ModuleModel> models = new ArrayList<ModuleModel>();
    List<Module> modules = ModuleRegistry.getModules();
    for (Module module : modules) {
        if (module instanceof CoreModule) {
            CoreModuleModel coreModel = new CoreModuleModel(ModuleRegistry.getModule(ModuleRegistry.CORE_MODULE_NAME));
            coreModel.setGuid(Providers.get(ICoreLicense.class).getGuid());
            coreModel.setInstanceDescription(SystemSettingsDao.getInstance().getValue(SystemSettingsDao.INSTANCE_DESCRIPTION));
            coreModel.setDistributor(Common.envProps.getString("distributor"));
            coreModel.setUpgradeVersionState(SystemSettingsDao.getInstance().getIntValue(SystemSettingsDao.UPGRADE_VERSION_STATE));
            coreModel.setStoreUrl(Common.envProps.getString("store.url"));
            models.add(coreModel);
        } else {
            models.add(new ModuleModel(module));
        }
    }
    // Add the unloaded modules at the end?
    List<Module> unloaded = ModuleRegistry.getUnloadedModules();
    for (Module module : unloaded) {
        ModuleModel model = new ModuleModel(module);
        model.setUnloaded(true);
        models.add(model);
    }
    return models;
}
Also used : CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ArrayList(java.util.ArrayList) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ModuleModel

use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel in project ma-modules-public by infiniteautomation.

the class ModulesRestController method markModuleForDeletion.

@ApiOperation(value = "Set Marked For Deletion state of Module", notes = "Marking a module for deletion will un-install it upon restart")
@RequestMapping(method = RequestMethod.PUT, value = "/deletion-state")
public ModuleModel markModuleForDeletion(@ApiParam(value = "Deletion State", required = true, defaultValue = "false", allowMultiple = false) @RequestParam(name = "delete", required = true) boolean delete, @ApiParam(value = "Module model") @RequestBody() ModuleModel model, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request) {
    permissionService.ensureAdminRole(user);
    Module module = ModuleRegistry.getModule(model.getName());
    if (module == null)
        throw new NotFoundException();
    module.setMarkedForDeletion(delete);
    if (module.isMarkedForDeletion() != delete)
        throw new ModuleRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.modules.error.dependencyFailure"));
    return new ModuleModel(module);
}
Also used : ModuleRestException(com.infiniteautomation.mango.rest.latest.exception.ModuleRestException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ModuleModel

use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel in project ma-modules-public by infiniteautomation.

the class ModulesRestController method getUpgradeStatus.

@ApiOperation(value = "Get Current Upgrade Task Status", notes = "")
@RequestMapping(method = RequestMethod.GET, value = "/upgrade-status")
public UpgradeStatusModel getUpgradeStatus(@AuthenticationPrincipal PermissionHolder user) {
    permissionService.ensureAdminRole(user);
    UpgradeStatus status = service.monitorDownloads();
    UpgradeStatusModel model = new UpgradeStatusModel();
    if (status.getStage() == UpgradeState.IDLE) {
        // Not running
        model.setRunning(false);
    } else {
        List<ModuleModel> modules = new ArrayList<ModuleModel>();
        List<StringStringPair> results = status.getResults();
        for (StringStringPair r : results) modules.add(new ModuleModel(r.getKey(), r.getValue()));
        model.setResults(modules);
        model.setFinished(status.isFinished());
        model.setCancelled(status.isCancelled());
        model.setWillRestart(status.isRestart());
        model.setError(status.getError());
        model.setStage(status.getStage().name());
    }
    return model;
}
Also used : StringStringPair(com.serotonin.db.pair.StringStringPair) UpgradeStatus(com.infiniteautomation.mango.spring.service.ModulesService.UpgradeStatus) ArrayList(java.util.ArrayList) UpgradeStatusModel(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeStatusModel) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ModuleModel

use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel in project ma-modules-public by infiniteautomation.

the class ModulesRestController method getUpgrades.

@ApiOperation(value = "Get Available Upgrades", notes = "Check the store for Upgrades")
@RequestMapping(method = RequestMethod.GET, value = "/upgrades-available")
public ModuleUpgradesModel getUpgrades(@AuthenticationPrincipal PermissionHolder user) {
    permissionService.ensureAdminRole(user);
    // Do the check
    try {
        JsonValue jsonResponse = null;
        // This is handled oddly in the ModulesDwr so check it here for validation
        String baseUrl = Common.envProps.getString("store.url");
        if (!StringUtils.isEmpty(baseUrl)) {
            jsonResponse = service.getAvailableUpgrades();
        } else {
            ProcessResult result = new ProcessResult();
            result.addGenericMessage("modules.versionCheck.storeNotSet");
            throw new ValidationFailedRestException(result);
        }
        if (jsonResponse == null) {
            // Indicates that the store url is not set, which we check for above so this really means the response was a null JSON value
            throw new BadRequestException(new TranslatableMessage("modules.versionCheck.storeResponseEmpty"));
        } else if (jsonResponse instanceof JsonString) {
            throw new ServerErrorException(new TranslatableMessage("common.default", jsonResponse.toString()));
        } else {
            List<ModuleUpgradeModel> upgrades = new ArrayList<>();
            List<ModuleUpgradeModel> newInstalls = new ArrayList<>();
            List<ModuleModel> unavailableModules = new ArrayList<>();
            ModuleUpgradesModel model = new ModuleUpgradesModel(upgrades, newInstalls);
            JsonObject root = jsonResponse.toJsonObject();
            JsonValue jsonUpgrades = root.get("upgrades");
            JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
            Iterator<JsonValue> it = jsonUpgradesArray.iterator();
            while (it.hasNext()) {
                JsonValue v = it.next();
                if (v.getJsonValue("name") == null) {
                    it.remove();
                    continue;
                }
                String name = v.getJsonValue("name").toString();
                Module module = ModuleRegistry.getModule(name);
                if (module == null) {
                    it.remove();
                    continue;
                }
                upgrades.add(new ModuleUpgradeModel(module, v));
            }
            JsonValue jsonInstalls = root.get("newInstalls");
            JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
            for (JsonValue v : jsonInstallsArray) {
                newInstalls.add(new ModuleUpgradeModel(v));
            }
            // Extract any unavailable modules
            if (root.containsKey("unavailableModules")) {
                JsonValue jsonUnavailableModules = root.get("unavailableModules");
                JsonArray jsonUnavailableModulesArray = jsonUnavailableModules.toJsonArray();
                List<Module> modules = ModuleRegistry.getModules();
                for (JsonValue v : jsonUnavailableModulesArray) {
                    Module unavailable = null;
                    String moduleName = v.toString();
                    for (Module module : modules) {
                        if (StringUtils.equals(module.getName(), moduleName)) {
                            unavailable = module;
                            break;
                        }
                    }
                    if (unavailable == null) {
                        // Didn't find it?  Store must be wrong?
                        LOG.warn("Store reported unavailable module " + moduleName + " but it isn't installed.");
                    } else {
                        unavailableModules.add(new ModuleModel(unavailable));
                    }
                }
            }
            model.setUnavailableModules(unavailableModules);
            return model;
        }
    } catch (SocketTimeoutException e) {
        throw new ServerErrorException(new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
    } catch (UnknownHostException e) {
        throw new ServerErrorException(new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
    } catch (IOException | JsonException | HttpException e) {
        throw new ServerErrorException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) UnknownHostException(java.net.UnknownHostException) ValidationFailedRestException(com.infiniteautomation.mango.rest.latest.exception.ValidationFailedRestException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) IOException(java.io.IOException) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) JsonArray(com.serotonin.json.type.JsonArray) ModuleUpgradeModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradeModel) SocketTimeoutException(java.net.SocketTimeoutException) ModuleUpgradesModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel) Iterator(java.util.Iterator) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) HttpException(org.apache.http.HttpException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) JsonString(com.serotonin.json.type.JsonString) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ModuleModel

use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel in project ma-modules-public by infiniteautomation.

the class ModulesRestController method markForDeletion.

@ApiOperation(value = "Set Marked For Deletion state of Module", notes = "Marking a module for deletion will un-install it upon restart")
@RequestMapping(method = RequestMethod.PUT, value = "/deletion-state/{moduleName}")
public ResponseEntity<ModuleModel> markForDeletion(@PathVariable String moduleName, @ApiParam(value = "Deletion State", required = true, defaultValue = "false", allowMultiple = false) @RequestParam(name = "delete", required = true) boolean delete, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request) {
    Module module = ModuleRegistry.getModule(moduleName);
    if (module == null)
        throw new NotFoundException();
    module.setMarkedForDeletion(delete);
    if (module.isMarkedForDeletion() != delete)
        throw new ModuleRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.modules.error.dependencyFailure"));
    return ResponseEntity.ok(new ModuleModel(module));
}
Also used : ModuleRestException(com.infiniteautomation.mango.rest.latest.exception.ModuleRestException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CoreModuleModel (com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel)5 ModuleModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel)5 ApiOperation (io.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 InvalidModule (com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule)4 Module (com.serotonin.m2m2.module.Module)4 CoreModule (com.serotonin.m2m2.module.ModuleRegistry.CoreModule)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 ArrayList (java.util.ArrayList)3 ModuleRestException (com.infiniteautomation.mango.rest.latest.exception.ModuleRestException)2 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)2 FileNotFoundException (java.io.FileNotFoundException)2 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)1 ServerErrorException (com.infiniteautomation.mango.rest.latest.exception.ServerErrorException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.latest.exception.ValidationFailedRestException)1 ModuleUpgradeModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradeModel)1 ModuleUpgradesModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel)1 UpgradeStatusModel (com.infiniteautomation.mango.rest.latest.model.modules.UpgradeStatusModel)1 UpgradeStatus (com.infiniteautomation.mango.spring.service.ModulesService.UpgradeStatus)1 StringStringPair (com.serotonin.db.pair.StringStringPair)1