Skip to content

Commit

Permalink
Add config to avoid always loading all available versions of opentofu…
Browse files Browse the repository at this point in the history
… at boot (#90)
  • Loading branch information
baixinsui authored Dec 10, 2024
1 parent a6a9eed commit 519dee7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
java-version: 21
distribution: 'temurin'
- name: Set up OpenTofu
uses: opentofu/setup-opentofu@v1
uses: opentofu/setup-opentofu@v1.0.4
with:
tofu_version: 1.6.0
tofu_version: 1.6.1
- name: Build
run: mvn --batch-mode --update-snapshots --no-transfer-progress verify
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ RUN echo "Downloading and installing OpenTofu with multiple versions $OPENTOFU_V

COPY target/tofu-maker-*.jar tofu-maker.jar
USER tofu-maker
ENTRYPOINT ["java", "-Dopentofu.install.dir=${OPENTOFU_INSTALL_PATH}", "-Dopentofu.version=${OPENTOFU_VERSIONS}", "-jar", "tofu-maker.jar"]
ENTRYPOINT ["java", "-Dopentofu.install.dir=${OPENTOFU_INSTALL_PATH}", "-Dopentofu.default.supported.versions=${OPENTOFU_VERSIONS}", "-jar", "tofu-maker.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.annotation.Resource;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

Expand All @@ -21,6 +22,8 @@
@Component
public class OpenTofuVersionsCache {

@Value("${support.default.opentofu.versions.only:true}")
private boolean getDefaultVersionsOnly;
@Resource
private OpenTofuVersionsFetcher versionsFetcher;

Expand All @@ -31,6 +34,9 @@ public class OpenTofuVersionsCache {
*/
@Cacheable(value = OPENTOFU_VERSIONS_CACHE_NAME, key = "'all'")
public Set<String> getAvailableVersions() {
if (getDefaultVersionsOnly) {
return versionsFetcher.getDefaultVersionsFromConfig();
}
try {
return versionsFetcher.fetchAvailableVersionsFromOpenTofuWebsite();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class OpenTofuVersionsFetcher {
private String openTofuGithubApiEndpoint;
@Value("${opentofu.github.repository:opentofu/opentofu}")
private String openTofuGithubRepository;
@Value("${opentofu.versions}")
@Value("${opentofu.default.supported.versions}")
private String defaultVersionsString;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spring.retry.delay-millions=1000
support.default.opentofu.versions.only=true
opentofu.install.dir=/opt/opentofu
opentofu.download.base.url=https://github.com/opentofu/opentofu/releases
opentofu.versions=1.6.0,1.7.0,1.8.0
opentofu.default.supported.versions=1.6.0,1.7.0,1.8.0
opentofu.github.api.endpoint=https://api.github.com
opentofu.github.repository=opentofu/opentofu
failed.callback.response.store.location=/tmp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@

import java.io.File;

import java.util.Set;
import org.eclipse.xpanse.tofu.maker.models.exceptions.InvalidOpenTofuToolException;
import org.eclipse.xpanse.tofu.maker.opentofu.utils.SystemCmd;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = {OpenTofuInstaller.class,
OpenTofuVersionsHelper.class,
OpenTofuVersionsCache.class,
OpenTofuVersionsFetcher.class,
SystemCmd.class})
@SpringBootTest(classes = {OpenTofuInstaller.class, OpenTofuVersionsHelper.class,
OpenTofuVersionsCache.class, OpenTofuVersionsFetcher.class, SystemCmd.class},
properties = {"support.default.opentofu.versions.only=false"}
)
class OpenTofuInstallerTest {

@Value("${opentofu.default.supported.versions:1.6.0,1.7.0,1.8.0}")
private String opentofuVersions;
@Resource
private OpenTofuInstaller installer;
@Resource
private OpenTofuVersionsHelper versionHelper;
@Resource
private OpenTofuVersionsCache versionsCache;

@Test
void testGetExecutableOpenTofuByVersion() {
Set<String> defaultVersions = Set.of(opentofuVersions.split(","));
Set<String> cachedVersions = versionsCache.getAvailableVersions();
assertTrue(cachedVersions.containsAll(defaultVersions));
assertTrue(cachedVersions.size() >= defaultVersions.size());

String requiredVersion = "";
String openTofuPath = installer.getExecutorPathThatMatchesRequiredVersion(requiredVersion);
Expand Down

0 comments on commit 519dee7

Please sign in to comment.