diff --git a/src/main/java/it/reply/orchestrator/service/deployment/providers/ImServiceImpl.java b/src/main/java/it/reply/orchestrator/service/deployment/providers/ImServiceImpl.java index b78d19d846..49cba77b43 100644 --- a/src/main/java/it/reply/orchestrator/service/deployment/providers/ImServiceImpl.java +++ b/src/main/java/it/reply/orchestrator/service/deployment/providers/ImServiceImpl.java @@ -91,7 +91,6 @@ import org.mitre.openid.connect.client.service.impl.StaticClientConfigurationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -105,25 +104,17 @@ public class ImServiceImpl extends AbstractDeploymentProviderService { /** * Constructor of the ImServiceImpl class. */ - @Autowired - public ImServiceImpl(RestTemplateBuilder restTemplateBuilder, - CustomOAuth2TemplateFactory templateFactory, - StaticClientConfigurationService staticClientConfigurationService, - ImClientFactory imClientFactory) { - this.restTemplate = restTemplateBuilder.build(); - this.templateFactory = templateFactory; - this.staticClientConfigurationService = staticClientConfigurationService; - this.imClientFactory = imClientFactory; - } - private final RestTemplate restTemplate; + private RestTemplate restTemplate; @Autowired private IamService iamService; - private final CustomOAuth2TemplateFactory templateFactory; + @Autowired + private CustomOAuth2TemplateFactory templateFactory; - private final StaticClientConfigurationService staticClientConfigurationService; + @Autowired + private StaticClientConfigurationService staticClientConfigurationService; @Autowired private ToscaService toscaService; @@ -143,7 +134,8 @@ public ImServiceImpl(RestTemplateBuilder restTemplateBuilder, @Autowired private OAuth2TokenService oauth2TokenService; - private final ImClientFactory imClientFactory; + @Autowired + private ImClientFactory imClientFactory; private static final String VMINFO = "VirtualMachineInfo"; public static final String IAM_TOSCA_NODE_TYPE = "tosca.nodes.indigo.iam.client"; @@ -188,6 +180,7 @@ protected ArchiveRoot prepareTemplate(String template, Deployment deployment, @Override public boolean doDeploy(DeploymentMessage deploymentMessage) { + restTemplate = new RestTemplate(); Deployment deployment = getDeployment(deploymentMessage); String uuid = deployment.getId(); @@ -268,9 +261,6 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) { Map> iamTemplateInput = null; Map> iamTemplateOutput = new HashMap<>(); - // Get information about the clients related to the orchestrator - Map clients = staticClientConfigurationService.getClients(); - // Loop over the deployment resources and create an IAM client for all the // IAM_TOSCA_NODE_TYPE nodes requested for (Resource resource : resources.get(false)) { @@ -281,10 +271,13 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) { String issuerNode; String tokenCredentials = null; Map clientCreated = new HashMap<>(); + Map orchestratorClients = new HashMap<>(); - // Get properties of IAM_TOSCA_NODE_TYPE nodes from the TOSCA template + // Get properties of IAM_TOSCA_NODE_TYPE nodes from the TOSCA template and + // get information about the clients related to the orchestrator if (iamTemplateInput == null) { iamTemplateInput = toscaService.getIamProperties(ar); + orchestratorClients = staticClientConfigurationService.getClients(); } // Set the issuer of the current node @@ -352,7 +345,7 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) { resource.setMetadata(resourceMetadata); iamTemplateOutput.put(nodeName, resourceMetadata); - if (!clients.containsKey(issuerNode)) { + if (!orchestratorClients.containsKey(issuerNode)) { String errorMessage = String.format("There is no orchestrator client belonging to the " + "identity provider: %s. Impossible to set the ownership of the client with " + "client_id %s", issuerNode, clientCreated.get(CLIENT_ID)); @@ -361,7 +354,7 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) { try { // Get the orchestrator client related to the issuer of the node and extract // client_id and client_secret - RegisteredClient orchestratorClient = clients.get(issuerNode); + RegisteredClient orchestratorClient = orchestratorClients.get(issuerNode); String orchestratorClientId = orchestratorClient.getClientId(); String orchestratorClientSecret = orchestratorClient.getClientSecret(); @@ -763,6 +756,7 @@ public void cleanFailedUpdate(DeploymentMessage deploymentMessage) { @Override public boolean doUndeploy(DeploymentMessage deploymentMessage) { + restTemplate = new RestTemplate(); Deployment deployment = getDeployment(deploymentMessage); Map> resources = diff --git a/src/test/java/it/reply/orchestrator/service/deployment/providers/ImServiceTest.java b/src/test/java/it/reply/orchestrator/service/deployment/providers/ImServiceTest.java index 9c896ff9fc..feca1a3904 100644 --- a/src/test/java/it/reply/orchestrator/service/deployment/providers/ImServiceTest.java +++ b/src/test/java/it/reply/orchestrator/service/deployment/providers/ImServiceTest.java @@ -19,12 +19,10 @@ import alien4cloud.tosca.model.ArchiveRoot; import alien4cloud.tosca.parser.ParsingException; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; - import es.upv.i3m.grycap.im.InfrastructureManager; import es.upv.i3m.grycap.im.States; import es.upv.i3m.grycap.im.exceptions.ImClientErrorException; @@ -46,9 +44,9 @@ import it.reply.orchestrator.dal.repository.DeploymentRepository; import it.reply.orchestrator.dal.repository.ResourceRepository; import it.reply.orchestrator.dto.CloudProviderEndpoint; -import it.reply.orchestrator.dto.cmdb.ComputeService; import it.reply.orchestrator.dto.cmdb.CloudService; import it.reply.orchestrator.dto.cmdb.CloudServiceType; +import it.reply.orchestrator.dto.cmdb.ComputeService; import it.reply.orchestrator.dto.deployment.DeploymentMessage; import it.reply.orchestrator.dto.workflow.CloudServiceWf; import it.reply.orchestrator.dto.workflow.CloudServicesOrderedIterator; @@ -61,10 +59,10 @@ import it.reply.orchestrator.exception.service.DeploymentException; import it.reply.orchestrator.exception.service.ToscaException; import it.reply.orchestrator.function.ThrowingFunction; +import it.reply.orchestrator.service.IamService; import it.reply.orchestrator.service.ToscaServiceImpl; import it.reply.orchestrator.service.deployment.providers.factory.ImClientFactory; import it.reply.orchestrator.util.TestUtil; - import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -74,7 +72,6 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; - import org.alien4cloud.tosca.model.templates.NodeTemplate; import org.alien4cloud.tosca.model.templates.Topology; import org.assertj.core.api.AbstractThrowableAssert; @@ -87,16 +84,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.SpyBean; - -import junitparams.JUnitParamsRunner; -import junitparams.Parameters; - +import static it.reply.orchestrator.dto.cmdb.CloudService.ONEPROVIDER_STORAGE_SERVICE; +import static it.reply.orchestrator.dto.cmdb.CloudService.OPENSTACK_COMPUTE_SERVICE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static it.reply.orchestrator.dto.cmdb.CloudService.OPENSTACK_COMPUTE_SERVICE; -import static it.reply.orchestrator.dto.cmdb.CloudService.ONEPROVIDER_STORAGE_SERVICE; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; @RunWith(JUnitParamsRunner.class) public class ImServiceTest extends ToscaParserAwareTest { @@ -107,6 +104,9 @@ public class ImServiceTest extends ToscaParserAwareTest { @MockBean private ImClientFactory imClientFactory; + @MockBean + private IamService iamService; + @MockBean private DeploymentRepository deploymentRepository;