Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aip-console-jenkins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<packaging>hpi</packaging>
<properties>
<!-- Console Tools version -->
<revision>2.11.10-SNAPSHOT</revision>
<revision>2.11.11</revision>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>2.60.3</jenkins.version>
<!-- The 2.303.3 version doesn't work properly by now, and needs slight more works for adoption -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import com.castsoftware.aip.console.tools.core.services.JobsService;
import com.castsoftware.aip.console.tools.core.services.RestApiService;
import com.castsoftware.aip.console.tools.core.services.UploadService;
import com.castsoftware.aip.console.tools.core.utils.ApiEndpointHelper;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.VersionInformation;
import com.castsoftware.aip.console.tools.core.utils.VersionObjective;
import com.castsoftware.aip.console.tools.core.utils.*;
import com.castsoftware.aip.console.tools.providers.CliLogPollingProviderImpl;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -31,6 +28,7 @@
import picocli.CommandLine;

import java.io.File;
import java.time.LocalDateTime;
import java.util.EnumSet;
import java.util.List;
import java.util.function.Function;
Expand All @@ -46,6 +44,7 @@
@Getter
@Setter
public class AddVersionCommand extends BasicCallable {
private static final VersionInformation MAX_VERSION = VersionInformation.fromVersionString("3.0.0");

@CommandLine.Mixin
private SharedOptions sharedOptions;
Expand Down Expand Up @@ -222,12 +221,14 @@ protected Integer processCallCommand() throws Exception {

// check that the application actually has versions, otherwise it's just an add version job
boolean cloneVersion = (app.isInPlaceMode() || !disableClone) && applicationService.applicationHasVersion(applicationGuid);
snapshotName = applicationService.buildSnapshotName(snapshotName);
LocalDateTime snapshotDate = applicationService.getVersionLocalDateTime(snapshotDateString);

builder = JobRequestBuilder.newInstance(applicationGuid, sourcePath, cloneVersion ? JobType.CLONE_VERSION : JobType.ADD_VERSION, app.getCaipVersion())
.nodeName(app.getTargetNode())
.versionName(versionName)
.versionReleaseDate(applicationService.getVersionDate(versionDateString))
.snapshotDate(applicationService.getVersionDate(snapshotDateString))
.releaseAndSnapshotDateStr(DateUtils.toJsonString(snapshotDate))
.snapshotName(snapshotName)
.objectives(VersionObjective.DATA_SAFETY, enableDataSafety)
.backupApplication(backupEnabled)
.backupName(backupName)
Expand All @@ -242,11 +243,6 @@ protected Integer processCallCommand() throws Exception {
builder.objectives(VersionObjective.SECURITY, enableSecurityDataflow);

applicationService.updateModuleGenerationType(applicationGuid, builder, moduleGenerationType, !cloneVersion);

if (StringUtils.isNotBlank(snapshotName)) {
builder.snapshotName(snapshotName);
}

//Snapshot required now see whether we upload application or not
boolean forcedConsolidation = processImaging || consolidation;
builder.uploadApplication(forcedConsolidation);
Expand Down Expand Up @@ -343,6 +339,10 @@ private Thread getShutdownHookForJobGuid(String jobGuid) {
protected VersionInformation getMinVersion() {
return null; // for this feature to run on all server versions
}
@Override
protected VersionInformation getMaxVersion() {
return MAX_VERSION;
}

@Override
public SharedOptions getSharedOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.castsoftware.aip.console.tools.core.services.RestApiService;
import com.castsoftware.aip.console.tools.core.services.UploadService;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.DateUtils;
import com.castsoftware.aip.console.tools.core.utils.VersionInformation;
import com.castsoftware.aip.console.tools.core.utils.VersionObjective;
import com.castsoftware.aip.console.tools.providers.CliLogPollingProviderImpl;
Expand All @@ -27,6 +28,7 @@
import picocli.CommandLine;

import java.io.File;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.function.Function;

Expand Down Expand Up @@ -212,14 +214,15 @@ protected Integer processCallCommand() throws Exception {

// Clone the version if we're in "in-place" mode or the user wants to clone the version and the application has versions
boolean cloneVersion = (app.isInPlaceMode() || !disableClone) && applicationService.applicationHasVersion(applicationGuid);
LocalDateTime versionDate = applicationService.getVersionLocalDateTime(versionDateString);

JobRequestBuilder builder = JobRequestBuilder
.newInstance(applicationGuid, sourcePath, cloneVersion ? JobType.CLONE_VERSION : JobType.ADD_VERSION, app.getCaipVersion())
.endStep(autoDeploy ? Constants.SET_CURRENT_STEP_NAME : Constants.DELIVER_VERSION)
.versionName(versionName)
.nodeName(app.getTargetNode())
.versionReleaseDate(applicationService.getVersionDate(versionDateString))
.snapshotDate(new Date())
.versionReleaseDateStr(DateUtils.toJsonString(versionDate))
.snapshotDateStr(DateUtils.toJsonString(LocalDateTime.now()))
.objectives(VersionObjective.DATA_SAFETY, enableDataSafety)
.backupApplication(backupEnabled)
.backupName(backupName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.castsoftware.aip.console.tools.core.services.RestApiService;
import com.castsoftware.aip.console.tools.core.services.UploadService;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.DateUtils;
import com.castsoftware.aip.console.tools.core.utils.SemVerUtils;
import com.castsoftware.aip.console.tools.core.utils.VersionInformation;
import com.castsoftware.aip.console.tools.providers.CliLogPollingProviderImpl;
Expand All @@ -28,6 +29,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;
Expand All @@ -51,6 +53,7 @@ public class SnapshotCommand extends BasicCallable {
private static final DateFormat RELEASE_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
@CommandLine.Mixin
private SharedOptions sharedOptions;
private static final VersionInformation MAX_VERSION = VersionInformation.fromVersionString("3.0.0");

@CommandLine.Option(names = {"-n", "--app-name"},
paramLabel = "APPLICATION_NAME",
Expand Down Expand Up @@ -134,9 +137,8 @@ protected Integer processCallCommand() throws Exception {
foundVersion = optionalVersionDto.get();
}

if (StringUtils.isBlank(snapshotName)) {
snapshotName = String.format("Snapshot-%s", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date()));
}
LocalDateTime snapshotDate = applicationService.getVersionLocalDateTime(snapshotDateString);
snapshotName = applicationService.buildSnapshotName(snapshotName);

boolean forcedConsolidation = processImaging || consolidation;
//TODO: refactor after release to get separated workflows
Expand Down Expand Up @@ -178,7 +180,7 @@ protected Integer processCallCommand() throws Exception {
.versionName(foundVersion.getName())
.snapshotName(snapshotName)
.uploadApplication(true)
.snapshotDate(applicationService.getVersionDate(snapshotDateString))
.releaseAndSnapshotDateStr(DateUtils.toJsonString(snapshotDate))
.processImaging(processImaging)
.uploadApplication(true)
.endStep(SemVerUtils.isNewerThan115(apiInfoDto.getApiVersionSemVer()) ?
Expand Down Expand Up @@ -228,6 +230,10 @@ private Thread getShutdownHookForJobGuid(String jobGuid) {
protected VersionInformation getMinVersion() {
return null;
}
@Override
protected VersionInformation getMaxVersion() {
return MAX_VERSION;
}

@Override
public SharedOptions getSharedOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.castsoftware.aip.console.tools.core.exceptions.ApplicationServiceException;
import com.castsoftware.aip.console.tools.core.exceptions.JobServiceException;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.DateUtils;
import com.castsoftware.aip.console.tools.providers.CliLogPollingProviderImpl;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
Expand All @@ -22,6 +23,8 @@
import org.springframework.test.context.junit4.SpringRunner;
import picocli.CommandLine;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -78,6 +81,48 @@ public void testSnapshotCommand_WhenAnalysisNotDone() throws ApplicationServiceE
assertThat(spec, is(notNullValue()));
assertThat(exitCode, is(Constants.RETURN_VERSION_WITH_ANALYSIS_DONE_NOT_FOUND));
}
@Test
public void testSnapshotCommand_LegacyWorkFlow() throws ApplicationServiceException, ParseException, JobServiceException {
boolean verbose = true;
String SNAPSHOT_DATE="2025-01-15T15:14:00";
String[] args = new String[]{"--apikey", TestConstants.TEST_API_KEY,
"--app-name=" + TestConstants.TEST_CREATRE_APP,
"--version-name", TestConstants.TEST_VERSION_NAME,
"--snapshot-date",SNAPSHOT_DATE,
"--process-imaging"};

ApplicationDto applicationDto = ApplicationDto.builder()
.guid(TestConstants.TEST_APP_GUID)
.name(TestConstants.TEST_CREATRE_APP)
.onboarded(false)
.schemaPrefix("ShouldHave_One").build();

when(applicationService.getApplicationFromName(TestConstants.TEST_CREATRE_APP)).thenReturn(applicationDto);
VersionDto versionDto = new VersionDto();
versionDto.setName(TestConstants.TEST_VERSION_NAME);
versionDto.setStatus(VersionStatus.ANALYSIS_DONE);
when(applicationService.getApplicationVersion(TestConstants.TEST_APP_GUID)).thenReturn(Sets.newSet(versionDto));

String snapshotDateString = SNAPSHOT_DATE + ".000Z";
Date suffixDate = JobRequestBuilder.RELEASE_DATE_FORMATTER.parse(snapshotDateString);;
when(applicationService.getVersionDate(anyString())).thenReturn(suffixDate);
when(applicationService.getVersionLocalDateTime(anyString())).thenReturn(DateUtils.parseJsonLocalDateTime(snapshotDateString));

when(jobsService.startJob(any(JobRequestBuilder.class))).thenReturn(TestConstants.TEST_JOB_GUID);
JobExecutionDto jobStatus = new JobExecutionDto();
jobStatus.setAppGuid(TestConstants.TEST_APP_GUID);
jobStatus.setState(JobState.COMPLETED);
jobStatus.setCreatedDate(new Date());
jobStatus.setAppName(TestConstants.TEST_CREATRE_APP);
when(jobsService.pollAndWaitForJobFinished(anyString(), any(Function.class), anyBoolean())).thenReturn(jobStatus);

runStringArgs(snapshotCommand, args);

CommandLine.Model.CommandSpec spec = cliToTest.getCommandSpec();
assertThat(spec, is(notNullValue()));
assertThat(snapshotCommand.getSnapshotName(), is("Snapshot-2025-01-15T15-14-00"));
assertThat(exitCode, is(Constants.RETURN_OK));
}

@Test
public void testSnapshotCommand_WhenFastScanWorkFlow_JobFailed() throws ApplicationServiceException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package com.castsoftware.aip.console.tools;

import com.castsoftware.aip.console.tools.commands.SnapshotCommand;
import com.castsoftware.aip.console.tools.core.dto.jobs.CreateJobsRequest;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobRequestBuilder;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobType;
import com.castsoftware.aip.console.tools.core.exceptions.ApplicationServiceException;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import picocli.CommandLine;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

public class SnapshotCommandTest extends AipCommandTest<SnapshotCommand> {
Expand All @@ -24,6 +42,24 @@ public void testSnapshotCommand_WithDefaultParams() {
assertEquals(true, aipCommand.getSharedOptions().isVerbose());
assertEquals(true, aipCommand.isProcessImaging());
}
@Test
public void testSnapshotCommand_WithSomeParameters() {
String[] sb = new String[]{"--apikey",
TestConstants.TEST_API_KEY, "--app-name=" + TestConstants.TEST_CREATRE_APP,
"--version-name", TestConstants.TEST_VERSION_NAME,
"--process-imaging","--snapshot-date","2025-01-15T15:14:00"
};

aipCommandLine.parseArgs(sb);
assertEquals(TestConstants.TEST_API_KEY, aipCommand.getSharedOptions().getApiKey());
assertEquals(TestConstants.TEST_CREATRE_APP, aipCommand.getApplicationName());
assertEquals(TestConstants.TEST_VERSION_NAME, aipCommand.getVersionName());
assertEquals(true, aipCommand.getSharedOptions().isVerbose());
assertEquals(true, aipCommand.isProcessImaging());

assertEquals("2025-01-15T15:14:00", aipCommand.getSnapshotDateString());

}

@Test(expected = CommandLine.MissingParameterException.class)
public void testSnapshotCommand_WithMissingRequiredParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class JobRequestBuilder {
public static final DateTimeFormatter DELIVERY_DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
public static final DateTimeFormatter DELIVERY_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public static final String RELEASE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final String RELEASE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final DateFormat RELEASE_DATE_FORMATTER = new SimpleDateFormat(RELEASE_DATE_FORMAT);
private static final DateFormat VERSION_NAME_FORMATTER = new SimpleDateFormat("yyMMdd.HHmmss");
private static final String GLOBAL_RISK_OBJECTIVE = "GLOBAL_RISK";
Expand Down Expand Up @@ -163,6 +163,13 @@ public JobRequestBuilder releaseAndSnapshotDate(Date date) {
.snapshotDateStr(dateStr);
}

public JobRequestBuilder releaseAndSnapshotDateStr(String dateString) {
if (dateString == null) {
return this;
}
return releaseDateStr(dateString)
.snapshotDateStr(dateString);
}
public JobRequestBuilder snapshotDate(Date date) {
if (date == null) {
return this;
Expand All @@ -178,6 +185,9 @@ public JobRequestBuilder versionReleaseDate(Date releaseDate) {
String dateStr = RELEASE_DATE_FORMATTER.format(releaseDate);
return releaseDateStr(dateStr);
}
public JobRequestBuilder versionReleaseDateStr(String releaseDate) {
return releaseDateStr(releaseDate);
}

public JobRequestBuilder snapshotDateStr(String snapshotDateStr) {
this.snapshotDateStr = snapshotDateStr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.castsoftware.aip.console.tools.core.exceptions.PackagePathInvalidException;
import com.castsoftware.aip.console.tools.core.exceptions.UploadException;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -50,6 +51,8 @@ public interface ApplicationService {

DomainDto getDomainFromName(String domainName) throws ApplicationServiceException;

LocalDateTime getVersionLocalDateTime(String versionDateString) throws ApplicationServiceException ;
String buildSnapshotName( String actualName ) throws ApplicationServiceException;
Date getVersionDate(String versionDateString) throws ApplicationServiceException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
import com.castsoftware.aip.console.tools.core.exceptions.UploadException;
import com.castsoftware.aip.console.tools.core.utils.ApiEndpointHelper;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.DateUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -354,6 +357,21 @@ public boolean applicationHasVersion(String applicationGuid) throws ApplicationS
!appVersions.isEmpty();
}

@Override
public LocalDateTime getVersionLocalDateTime(String versionDateString) throws ApplicationServiceException {
if (!StringUtils.isEmpty(versionDateString)) {
// Ensure format is supported if not exception will be thrown
getVersionDate(versionDateString);
}
return (StringUtils.isEmpty(versionDateString)) ?
LocalDateTime.now(): DateUtils.parseJsonLocalDateTime(versionDateString + ".000Z");
}

public String buildSnapshotName( String actualName ) throws ApplicationServiceException {
return StringUtils.isEmpty(actualName) ?
String.format("Snapshot-%s", new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss").format(getVersionDate(actualName)))
: actualName;
}
@Override
public Date getVersionDate(String versionDateString) throws ApplicationServiceException {
if (StringUtils.isEmpty(versionDateString)) {
Expand Down
Loading