How to use Central Configuration util on your application?

EN | ES

In versions prior to 2.3.0 this functionality is available in the Administration > Configuration Management menu and is only available for Administrators roles.

Onesait Platform has a Centralized Configuration System for many purposes. Users can define in a visual way configurations for their needs, including Deployment (Rancher & OpenShift configurations), Social networks (Twitter), Scheduled Processes, Endpoint registrations, etc. 

With this module users can define in a simple way configurations, platform uses YAML or JSON as base.

Control Panel UI

In order to create and modify Configurations, Administrator can go to menu DEV TOOLS > Configuration Management.


As you can see in the table a Configuration is related to an environment(typically default or docker), an identification and a type:

When you create a new Configuration the user fill in:

It must be taken into account that for non-administrator users (as of Platform version 2.3.0), only EXTERNAL_CONFIG configurations can be created.

For example, you can modify the Configuration for service endpoints, which is processed when the platform launches:

REST API

You can also make the operations through an existing REST API, whose endpoints start with '/controlpanel/api/configurations'

NOTE: As these Configurations use Yaml, the response from this REST service is not human-friendly

Using the Java client for configuration management

Java Client offers a Configuration Wrapper class.

Config Dependency

To use this API, you have to add the following repository to the project pom.xml

1 2 3 4 5 6 <repositories> <repository> <id>onesait platform releases</id> <url>https://nexus.onesaitplatform.com/nexus/content/repositories/releases/</url> </repository> </repositories>

And then add the following dependency:

We are going to make use of the Client ConfigurationManager, so import the class.

1 2 3 4 5 <dependency> <groupId>com.minsait.onesait.platform</groupId> <artifactId>onesaitplatform-java-client</artifactId> <version>2.1.0-RELEASE</version> </dependency>

Initialization

The first case is to create a ConfigurationManager object, in order to use the client you must enter the user, password and URL server as arguments:

1 2 3 4 5 6 private static ConfigurationManager manager; @PostConstruct public void init() throws ConfigurationManagerException, IOException { manager = new ConfigurationManager("developer", "changeIt2020!", "https://hyperblast.onesaitplatform.com"); }

Once the object is initialized, it can be used to operate with the Platform Settings.

Create a Configuration

To create a configuration, it is necessary to first create an Object of type Configuration:

1 2 3 Configuration configuration = Configuration.builder().description("Testing config sample") .environment("default").identification("testconfiguration").type(ConfigurationType.EXTERNAL_CONFIG).yml(YML_SAMPLE) .build();

Once the configuration has been created, a call must be made to the ClientManager, to the createConfiguration method:

1 String id = manager.createConfiguration(configuration);

This method, if successful, will return the id of the configuration that was just created. With this identifier the configuration can be recovered in the future, deleted or updated.

Get Configuration

There are two methods to retrieve a configuration:

  • getConfigurationById(id) 

With this one, you can retrieve a configuration by its id.

  • getConfiguration(identification, type, environment)

If you don't know the id, you can try to retrieve the configuration by parameters. These two methods will return an instance of Configuration, if it exists.

Get all Configurations

You can also retrieve a List of Configurations by user by calling the method getConfigurations:

1 List<Configuration> configurations = manager.getConfigurations()

Edit a Configuration

To edit a configuration, you have to know its id.

The best way to edit a configuration is to retrieve it, then make changes, and finally call method updateConfiguration:

1 2 3 4 5 Configuration retrievedConfig = manager.getConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT); log.info("Retrieved config from the platform by parameters"); retrievedConfig.setDescription("This is a new description"); log.info("Updating configuration with new description and identification {}", IDENTIFICATION); manager.updateConfiguration(configuration, retrievedConfig.getId());

Delete a Configuration

To delete a configuration, you call the method deleteConfiguration passing it the id of the configuration.

1 2 3 4 5 log.info("Retrieved config from the platform by id"); Configuration retrievedConfig = manager.getConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT); log.info("Retrieved config from the platform by parameters"); log.info("Deleting configuration with id {}", retrievedConfig .getId()); manager.deleteConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT);

Test

There is a test inside the library to serve as an example. It tests all basic operations that are described in this tutorial. Take a look at it if you have any doubts.

Test Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 /** * Copyright Indra Soluciones Tecnologías de la Información, S.L.U. * 2013-2019 SPAIN * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.minsait.onesait.platform.client; import java.io.IOException; import java.util.List; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; import com.minsait.onesait.platform.client.enums.ConfigurationType; import com.minsait.onesait.platform.client.exception.ConfigurationManagerException; import com.minsait.onesait.platform.client.model.Configuration; import com.minsait.onesait.platform.testing.IntegrationTest; import lombok.extern.slf4j.Slf4j; @Slf4j @Category(IntegrationTest.class) public class TestConfigurationManager { private final static String USERNAME = "developer"; private final static String PASSWORD = "Changed2019!"; private final static String SERVER = "http://localhost:18000"; private final static String IDENTIFICATION = "testing_01"; private final static String ENVIRONMENT = "default"; private final static String YML_SAMPLE = "rancher:\r\n url: https://rancher.sofia4cities.com/\r\n accessKey: 471D90A16431C2CE7158\r\n secretKey: VSoq31eGBqaFsG4vUcuvdpkQL1T64FypVPVckaDx"; private final static ConfigurationType TYPE = ConfigurationType.EXTERNAL_CONFIG; private static ConfigurationManager manager; @BeforeClass public static void startUp() throws ConfigurationManagerException { log.info("Initializing Manager"); manager = new ConfigurationManager(USERNAME, PASSWORD, SERVER); } @Test public void getAllConfigurations() throws IOException, ConfigurationManagerException { log.info("Getting Configurations from open platform"); final List<Configuration> configurations = manager.getConfigurations(); Assert.assertTrue(configurations.size() > 0); } @Test public void configurationCRUD() throws IOException, ConfigurationManagerException { @SuppressWarnings("unchecked") final Configuration configuration = Configuration.builder().description("Rancher config sample") .environment(ENVIRONMENT).identification(IDENTIFICATION).username(USERNAME).type(TYPE).yml(YML_SAMPLE) .build(); Configuration retrievedConfig = manager.getConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT); if (retrievedConfig != null) { log.info("Deleting configuration with id {}", retrievedConfig.getId()); manager.deleteConfiguration(retrievedConfig.getId()); } log.info("Creating configuration"); final String id = manager.createConfiguration(configuration); Assert.assertNotNull(id); log.info("Created configuration, id is {}", id); retrievedConfig = manager.getConfigurationById(id); Assert.assertNotNull(retrievedConfig); log.info("Retrieved config from the platform by id"); retrievedConfig = manager.getConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT); Assert.assertNotNull(retrievedConfig); log.info("Retrieved config from the platform by parameters"); configuration.setDescription("This is a new description"); log.info("Updating configuration with new description and identification {}", IDENTIFICATION); manager.updateConfiguration(configuration, id); log.info("Deleting configuration with id {}", id); manager.deleteConfiguration(IDENTIFICATION, ConfigurationType.EXTERNAL_CONFIG, ENVIRONMENT); } }