---
title: "How to use Central Configuration util on your application?"
canonical: "https://onesaitplatform.refined.site/space/DOCT/2221606170/How%20to%20use%20Central%20Configuration%20util%20on%20your%20application%3F"
format: markdown
---
> Macro (toc)

> ℹ️ 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.**

![image](media://7972204f-9e69-445e-bc60-0ce6b9a99e7c)

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

![image](media://250fd899-a189-47bf-9328-bb9c8ffe8ce8)

When you create a new Configuration the user fill in:

![image](media://9925a180-9947-4259-be0c-9766a46954b7)

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:

![image](media://df4cd47b-5345-4cdc-ace9-b5d7bd51eb2b)

## REST API

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

![image](media://e7da46b5-f10b-4b9b-b8f3-f172105b1eac)

**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.

### <u>Config Dependency</u>

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

```xml
<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.

```
<dependency>
	  <groupId>com.minsait.onesait.platform</groupId>
	  <artifactId>onesaitplatform-java-client</artifactId>
	  <version>2.1.0-RELEASE</version>
</dependency>
```

### <u>Initialization</u>

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:

```java
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.

### <u>Create a Configuration</u>

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

```java
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:

```java
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.

### <u>Get Configuration</u>

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.

### <u>Get all Configurations</u>

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

```java
List<Configuration> configurations = manager.getConfigurations()
```

### <u>Edit a Configuration</u>

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**:

```java
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());
```

### <u>Delete a Configuration</u>

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

```java
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);
```

### <u>Test</u>

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**

```java
/**
 * 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);

	}
}
```