@Test void testCreate() { when(repository.findByNameAndAccountId(eq(CREDENTIAL_NAME), eq(ACCOUNT_ID), anyCollection(), any())) .thenReturn(Optional.empty()); when(credentialAdapter.verify(any(), anyString(), anyBoolean())).thenAnswer(i -> new CredentialVerification(i.getArgument(0), true)); when(repository.save(any())).thenAnswer(i -> i.getArgument(0)); credentialServiceUnderTest.create(CREDENTIAL, ACCOUNT_ID, USER_ID, ENVIRONMENT); verify(credentialValidator).validateCredentialCloudPlatform(eq(PLATFORM), eq(USER_ID)); verify(credentialValidator).validateParameters(any(), any()); verify(repository).save(any()); }
public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) { repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type) .map(Credential::getName) .ifPresent(name -> { throw new BadRequestException("Credential already exists with name: " + name); }); LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn); LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes())); String credentialCrn = createCRN(accountId); credential.setResourceCrn(credentialCrn); credential.setCreator(creatorUserCrn); credential.setAccountId(accountId); Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential(); if (verifiedCredential.getVerificationStatusText() != null) { throw new BadRequestException(verifiedCredential.getVerificationStatusText()); } try { Credential createdCredential = transactionService.required(() -> { Credential created = repository.save(verifiedCredential); grpcUmsClient.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId); return created; }); sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED); return createdCredential; } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during credential creation: ", e); throw new InternalServerErrorException(e); } }
CredentialService extends AbstractCredentialService implements ResourceBasedCrnProvider { public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) { repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type) .map(Credential::getName) .ifPresent(name -> { throw new BadRequestException("Credential already exists with name: " + name); }); LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn); LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes())); String credentialCrn = createCRN(accountId); credential.setResourceCrn(credentialCrn); credential.setCreator(creatorUserCrn); credential.setAccountId(accountId); Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential(); if (verifiedCredential.getVerificationStatusText() != null) { throw new BadRequestException(verifiedCredential.getVerificationStatusText()); } try { Credential createdCredential = transactionService.required(() -> { Credential created = repository.save(verifiedCredential); grpcUmsClient.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId); return created; }); sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED); return createdCredential; } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during credential creation: ", e); throw new InternalServerErrorException(e); } } }
CredentialService extends AbstractCredentialService implements ResourceBasedCrnProvider { public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) { repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type) .map(Credential::getName) .ifPresent(name -> { throw new BadRequestException("Credential already exists with name: " + name); }); LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn); LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes())); String credentialCrn = createCRN(accountId); credential.setResourceCrn(credentialCrn); credential.setCreator(creatorUserCrn); credential.setAccountId(accountId); Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential(); if (verifiedCredential.getVerificationStatusText() != null) { throw new BadRequestException(verifiedCredential.getVerificationStatusText()); } try { Credential createdCredential = transactionService.required(() -> { Credential created = repository.save(verifiedCredential); grpcUmsClient.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId); return created; }); sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED); return createdCredential; } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during credential creation: ", e); throw new InternalServerErrorException(e); } } protected  CredentialService(NotificationSender notificationSender, CloudbreakMessagesService messagesService,
            @Value("${environment.enabledplatforms}") Set<String> enabledPlatforms); }
CredentialService extends AbstractCredentialService implements ResourceBasedCrnProvider { public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) { repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type) .map(Credential::getName) .ifPresent(name -> { throw new BadRequestException("Credential already exists with name: " + name); }); LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn); LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes())); String credentialCrn = createCRN(accountId); credential.setResourceCrn(credentialCrn); credential.setCreator(creatorUserCrn); credential.setAccountId(accountId); Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential(); if (verifiedCredential.getVerificationStatusText() != null) { throw new BadRequestException(verifiedCredential.getVerificationStatusText()); } try { Credential createdCredential = transactionService.required(() -> { Credential created = repository.save(verifiedCredential); grpcUmsClient.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId); return created; }); sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED); return createdCredential; } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during credential creation: ", e); throw new InternalServerErrorException(e); } } protected  CredentialService(NotificationSender notificationSender, CloudbreakMessagesService messagesService,
            @Value("${environment.enabledplatforms}") Set<String> enabledPlatforms); Set<Credential> listAvailablesByAccountId(String accountId, CredentialType type); @Cacheable(cacheNames = "credentialCloudPlatformCache") Set<String> getValidPlatformsForAccountId(String accountId); Credential getByNameForAccountId(String name, String accountId, CredentialType type); Optional<Credential> getOptionalByNameForAccountId(String name, String accountId, CredentialType type); Credential getByCrnForAccountId(String crn, String accountId, CredentialType type); Optional<Credential> getOptionalByCrnForAccountId(String crn, String accountId, CredentialType type); Credential extractCredential(Optional<Credential> credential, String resourceIdentifier); Credential getByEnvironmentCrnAndAccountId(String environmentCrn, String accountId, CredentialType type); Credential getByEnvironmentNameAndAccountId(String environmentName, String accountId, CredentialType type); Map<String, String> interactiveLogin(String accountId, String userId, Credential credential); Credential verify(Credential credential); Credential updateByAccountId(Credential credential, String accountId, CredentialType type); @Retryable(value = BadRequestException.class, maxAttempts = 30, backoff = @Backoff(delay = 2000)) void createWithRetry(Credential credential, String accountId, String creatorUserCrn); Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type); CredentialPrerequisitesResponse getPrerequisites(String cloudPlatform, String deploymentAddress, String userCrn, CredentialType type); String initCodeGrantFlow(String accountId, @Nonnull Credential credential, String creatorUserCrn); String initCodeGrantFlow(String accountId, String name, String userId); Credential authorizeCodeGrantFlow(String code, @Nonnull String state, String accountId, @Nonnull String platform); String getCloudPlatformByCredential(String credentialName, String accountId, CredentialType type); CreateAWSCredentialRequest getCreateAWSCredentialForCli(CredentialRequest credentialRequest); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount(); @Override AuthorizationResourceType getResourceType(); }
CredentialService extends AbstractCredentialService implements ResourceBasedCrnProvider { public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) { repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type) .map(Credential::getName) .ifPresent(name -> { throw new BadRequestException("Credential already exists with name: " + name); }); LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn); LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn); credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes())); String credentialCrn = createCRN(accountId); credential.setResourceCrn(credentialCrn); credential.setCreator(creatorUserCrn); credential.setAccountId(accountId); Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential(); if (verifiedCredential.getVerificationStatusText() != null) { throw new BadRequestException(verifiedCredential.getVerificationStatusText()); } try { Credential createdCredential = transactionService.required(() -> { Credential created = repository.save(verifiedCredential); grpcUmsClient.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId); return created; }); sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED); return createdCredential; } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during credential creation: ", e); throw new InternalServerErrorException(e); } } protected  CredentialService(NotificationSender notificationSender, CloudbreakMessagesService messagesService,
            @Value("${environment.enabledplatforms}") Set<String> enabledPlatforms); Set<Credential> listAvailablesByAccountId(String accountId, CredentialType type); @Cacheable(cacheNames = "credentialCloudPlatformCache") Set<String> getValidPlatformsForAccountId(String accountId); Credential getByNameForAccountId(String name, String accountId, CredentialType type); Optional<Credential> getOptionalByNameForAccountId(String name, String accountId, CredentialType type); Credential getByCrnForAccountId(String crn, String accountId, CredentialType type); Optional<Credential> getOptionalByCrnForAccountId(String crn, String accountId, CredentialType type); Credential extractCredential(Optional<Credential> credential, String resourceIdentifier); Credential getByEnvironmentCrnAndAccountId(String environmentCrn, String accountId, CredentialType type); Credential getByEnvironmentNameAndAccountId(String environmentName, String accountId, CredentialType type); Map<String, String> interactiveLogin(String accountId, String userId, Credential credential); Credential verify(Credential credential); Credential updateByAccountId(Credential credential, String accountId, CredentialType type); @Retryable(value = BadRequestException.class, maxAttempts = 30, backoff = @Backoff(delay = 2000)) void createWithRetry(Credential credential, String accountId, String creatorUserCrn); Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type); CredentialPrerequisitesResponse getPrerequisites(String cloudPlatform, String deploymentAddress, String userCrn, CredentialType type); String initCodeGrantFlow(String accountId, @Nonnull Credential credential, String creatorUserCrn); String initCodeGrantFlow(String accountId, String name, String userId); Credential authorizeCodeGrantFlow(String code, @Nonnull String state, String accountId, @Nonnull String platform); String getCloudPlatformByCredential(String credentialName, String accountId, CredentialType type); CreateAWSCredentialRequest getCreateAWSCredentialForCli(CredentialRequest credentialRequest); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount(); @Override AuthorizationResourceType getResourceType();  }