Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Terraform package type #354 #380

Merged
merged 5 commits into from
Nov 8, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jfrog.artifactory.client.model.repository.settings;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsGitProvider;
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsType;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface TerraformRepositorySettings extends RepositorySettings {

// Local and federated settings
enum TerraformType {
module,
provider
}

TerraformType getTerraformType();

// Remote settings
// Url is already declared in RemoteRepo

VcsType getVcsType();

VcsGitProvider getVcsGitProvider();

String getTerraformRegistryUrl();

String getTerraformProvidersUrl();

String getRemoteRepoLayoutRef();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
@JsonSubTypes.Type(value = PuppetRepositorySettingsImpl.class, name = "puppet"),
@JsonSubTypes.Type(value = HelmRepositorySettingsImpl.class, name = "helm"),
@JsonSubTypes.Type(value = GoRepositorySettingsImpl.class, name = "go"),
@JsonSubTypes.Type(value = CargoRepositorySettingsImpl.class, name = "cargo")
@JsonSubTypes.Type(value = CargoRepositorySettingsImpl.class, name = "cargo"),
@JsonSubTypes.Type(value = TerraformRepositorySettingsImpl.class, name = "terraform")
})

public abstract class RepositorySettingsMixIn {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class FederatedRepositoryBuilderImpl extends NonVirtualRepositoryBuilderBase<FederatedRepositoryBuilder, FederatedRepository> implements FederatedRepositoryBuilder {
private static Set<PackageType> federatedRepositorySupportedTypes = new HashSet<>(Arrays.asList(
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, pypi, sbt, vagrant, yum, rpm, composer, conan, chef, puppet, helm, go, cargo
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, pypi, sbt, vagrant, yum, rpm, composer, conan, chef, puppet, helm, go, cargo, terraform
));

protected List<FederatedMember> members = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class LocalRepositoryBuilderImpl extends NonVirtualRepositoryBuilderBase<LocalRepositoryBuilder, LocalRepository> implements LocalRepositoryBuilder {
private static Set<PackageType> localRepositorySupportedTypes = new HashSet<PackageType>(Arrays.asList(
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, pypi, sbt, vagrant, yum, rpm, composer, conan, chef, puppet, helm, go, cargo
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, pypi, sbt, vagrant, yum, rpm, composer, conan, chef, puppet, helm, go, cargo, terraform
));

protected LocalRepositoryBuilderImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public enum PackageTypeImpl implements PackageType {
puppet,
helm,
go,
cargo;
cargo,
terraform;

@Override
public boolean isCustom() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class RemoteRepositoryBuilderImpl extends NonVirtualRepositoryBuilderBase<RemoteRepositoryBuilder, RemoteRepository> implements RemoteRepositoryBuilder {
private static Set<PackageType> remoteRepositorySupportedTypes = new HashSet<PackageType>(Arrays.asList(
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, p2, pypi, sbt, vcs, yum, rpm, composer, conan, chef, puppet, helm, go, cargo
bower, cocoapods, cran, conda, debian, docker, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, opkg, p2, pypi, sbt, vcs, yum, rpm, composer, conan, chef, puppet, helm, go, cargo, terraform
));

private String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class VirtualRepositoryBuilderImpl extends RepositoryBuilderBase<VirtualRepositoryBuilder, VirtualRepository> implements VirtualRepositoryBuilder {
private static Set<PackageType> virtualRepositorySupportedTypes = new HashSet<PackageType>(Arrays.asList(
bower, cran, conda, docker, debian, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, p2, pypi, sbt, yum, rpm, composer, conan, chef, puppet, helm, go
bower, cran, conda, docker, debian, gems, generic, gitlfs, gradle, ivy, maven, npm, nuget, p2, pypi, sbt, yum, rpm, composer, conan, chef, puppet, helm, go, terraform
));

private Collection<String> repositories = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jfrog.artifactory.client.model.repository.settings.impl;

import org.jfrog.artifactory.client.model.PackageType;
import org.jfrog.artifactory.client.model.impl.PackageTypeImpl;
import org.jfrog.artifactory.client.model.repository.settings.AbstractRepositorySettings;
import org.jfrog.artifactory.client.model.repository.settings.TerraformRepositorySettings;
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsGitProvider;
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsType;

public class TerraformRepositorySettingsImpl extends AbstractRepositorySettings implements TerraformRepositorySettings {

public static String defaultLayout = "simple-default";
public static String moduleLayout = "terraform-module-default";
public static String providerLayout = "terraform-provider-default";
private TerraformType terraformType;
private String terraformRegistryUrl;
private String terraformProvidersUrl;
private VcsType vcsType;
private VcsGitProvider vcsGitProvider;
private String remoteRepoLayoutRef;

public TerraformRepositorySettingsImpl() {
super(defaultLayout);
}

@Override
public PackageType getPackageType() {
return PackageTypeImpl.terraform;
}

@Override
public TerraformType getTerraformType() {
return terraformType;
}

@Override
public VcsType getVcsType() {
return vcsType;
}

@Override
public VcsGitProvider getVcsGitProvider() {
return vcsGitProvider;
}

@Override
public String getTerraformRegistryUrl() {
return terraformRegistryUrl;
}

@Override
public String getTerraformProvidersUrl() {
return terraformProvidersUrl;
}

@Override
public String getRemoteRepoLayoutRef() {
return remoteRepoLayoutRef;
}

public void setTerraformType(TerraformType terraformType) {
this.terraformType = terraformType;
}

public void setVcsType(VcsType vcsType) {
this.vcsType = vcsType;
}

public void setVcsGitProvider(VcsGitProvider vcsGitProvider) {
this.vcsGitProvider = vcsGitProvider;
}

public void setTerraformRegistryUrl(String terraformRegistryUrl) {
this.terraformRegistryUrl = terraformRegistryUrl;
}

public void setTerraformProvidersUrl(String terraformProvidersUrl) {
this.terraformProvidersUrl = terraformProvidersUrl;
}

public void setRemoteRepoLayoutRef(String remoteRepoLayoutRef) {
this.remoteRepoLayoutRef = remoteRepoLayoutRef;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class BaseRepositoryTests extends ArtifactoryTestsBase {
.build()
}
if (prepareLocalRepo) {
RepositorySettings settings = getRepositorySettings()
RepositorySettings settings = getRepositorySettings(RepositoryTypeImpl.LOCAL)
localRepo = artifactory.repositories().builders().localRepositoryBuilder()
.key("$REPO_NAME_PREFIX-local-$id")
.description("local-$id")
Expand All @@ -95,12 +95,12 @@ abstract class BaseRepositoryTests extends ArtifactoryTestsBase {
List<FederatedMember> members = new ArrayList<FederatedMember>()
if(federationUrl != null && StringUtils.isNoneBlank(federationUrl)) {
if (!federationUrl.endsWith("/")) {
federationUrl += "/";
federationUrl += "/"
}
FederatedMember member = new FederatedMember( federationUrl+"$REPO_NAME_PREFIX-fed-$id", true)
members.add(member)
}
RepositorySettings settings = getRepositorySettings()
RepositorySettings settings = getRepositorySettings(RepositoryTypeImpl.FEDERATED)
federatedRepo = artifactory.repositories().builders().federatedRepositoryBuilder()
.key("$REPO_NAME_PREFIX-fed-$id")
.description("fed-$id")
Expand All @@ -118,7 +118,7 @@ abstract class BaseRepositoryTests extends ArtifactoryTestsBase {
}

if (prepareRemoteRepo) {
RepositorySettings settings = getRepositorySettings()
RepositorySettings settings = getRepositorySettings(RepositoryTypeImpl.REMOTE)
ContentSync contentSync = new ContentSyncImpl()
remoteRepo = artifactory.repositories().builders().remoteRepositoryBuilder()
.key("$REPO_NAME_PREFIX-remote-$id")
Expand Down Expand Up @@ -152,7 +152,7 @@ abstract class BaseRepositoryTests extends ArtifactoryTestsBase {
}

if (prepareVirtualRepo) {
RepositorySettings settings = getRepositorySettings()
RepositorySettings settings = getRepositorySettings(RepositoryTypeImpl.VIRTUAL)
artifactory.repositories().create(0, genericRepo)
def repos = new ArrayList<String>()
repos.add(genericRepo.getKey())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package org.jfrog.artifactory.client

import org.hamcrest.CoreMatchers
import org.jfrog.artifactory.client.model.RepositoryType
import org.jfrog.artifactory.client.model.impl.RepositoryTypeImpl
import org.jfrog.artifactory.client.model.repository.settings.RepositorySettings
import org.jfrog.artifactory.client.model.repository.settings.TerraformRepositorySettings
import org.jfrog.artifactory.client.model.repository.settings.impl.TerraformRepositorySettingsImpl
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsGitProvider
import org.jfrog.artifactory.client.model.repository.settings.vcs.VcsType
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

class TerraformPackageTypeRepositoryTests extends BaseRepositoryTests {

TerraformPackageTypeRepositoryTests() {
remoteRepoUrl = "https://github.com"
}

@Override
RepositorySettings getRepositorySettings(RepositoryType repositoryType) {
def settings = new TerraformRepositorySettingsImpl()

switch (repositoryType) {
case RepositoryTypeImpl.REMOTE:
settings.with {
repoLayout = defaultLayout
vcsType = VcsType.GIT
vcsGitProvider = VcsGitProvider.GITHUB
terraformRegistryUrl = "https://registry.terraform.io"
terraformProvidersUrl = "https://releases.hashicorp.com"
remoteRepoLayoutRef = defaultLayout
}
break
case RepositoryTypeImpl.VIRTUAL:
settings.with {
repoLayout = moduleLayout
}
break
case RepositoryTypeImpl.FEDERATED:
settings.with {
terraformType = TerraformRepositorySettings.TerraformType.module
repoLayout = moduleLayout
}
break
default:
settings = null
}

return settings

}

@BeforeMethod
protected void setUp() {
super.setUp()
}

@Test(groups = "terraformPackageTypeRepo")
void testTerraformLocalRepoModule() {
def mySettings = new TerraformRepositorySettingsImpl()
mySettings.with {
terraformType = TerraformRepositorySettings.TerraformType.module
repoLayout = moduleLayout
}

testLocalRepoWithSettings(mySettings)
}

@Test(groups = "terraformPackageTypeRepo")
void testTerraformLocalRepoProvider() {
def mySettings = new TerraformRepositorySettingsImpl()
mySettings.with {
terraformType = TerraformRepositorySettings.TerraformType.provider
repoLayout = providerLayout
}

testLocalRepoWithSettings(mySettings)
}

@Test(groups = "terraformPackageTypeRepo")
void testTerraformRemoteRepo() {
artifactory.repositories().create(0, remoteRepo)
def expectedSettings = remoteRepo.repositorySettings

def resp = artifactory.repository(remoteRepo.getKey()).get()
assertThat(resp, CoreMatchers.notNullValue())
assertThat(resp.repoLayoutRef, CoreMatchers.is(expectedSettings.repoLayout))
assertThat(resp.url, CoreMatchers.is(remoteRepoUrl))
resp.getRepositorySettings().with {
assertThat(packageType, CoreMatchers.is(expectedSettings.getPackageType()))

// remote
assertThat(vcsType, CoreMatchers.is(expectedSettings.vcsType))
assertThat(vcsGitProvider, CoreMatchers.is(expectedSettings.vcsGitProvider))
assertThat(terraformRegistryUrl, CoreMatchers.is(expectedSettings.terraformRegistryUrl))
assertThat(terraformProvidersUrl, CoreMatchers.is(expectedSettings.terraformProvidersUrl))
assertThat(remoteRepoLayoutRef, CoreMatchers.is(expectedSettings.remoteRepoLayoutRef))
}
}

@Test(groups = "terraformPackageTypeRepo")
void testTerraformVirtualRepo() {
artifactory.repositories().create(0, virtualRepo)
def expectedSettings = virtualRepo.repositorySettings

def resp = artifactory.repository(virtualRepo.getKey()).get()
assertThat(resp, CoreMatchers.notNullValue())
assertThat(resp.repoLayoutRef, CoreMatchers.is(expectedSettings.repoLayout))

resp.getRepositorySettings().with {
assertThat(packageType, CoreMatchers.is(expectedSettings.getPackageType()))
}
}

@Test(groups = "terraformPackageTypeRepo")
void testTerraformFederatedRepo() {
artifactory.repositories().create(0, federatedRepo)
def expectedSettings = federatedRepo.repositorySettings

def resp = artifactory.repository(federatedRepo.getKey()).get()
assertThat(resp, CoreMatchers.notNullValue())
assertThat(resp.repoLayoutRef, CoreMatchers.is(expectedSettings.repoLayout))

resp.getRepositorySettings().with {
assertThat(packageType, CoreMatchers.is(expectedSettings.getPackageType()))
}
}

private void testLocalRepoWithSettings(TerraformRepositorySettingsImpl mySettings) {
String id = Long.toString(repoUniqueId)
def myLocalRepo = artifactory.repositories().builders().localRepositoryBuilder()
.key("$REPO_NAME_PREFIX-local-$id")
.description("local-$id")
.notes("notes-${rnd.nextInt()}")
.archiveBrowsingEnabled(rnd.nextBoolean())
.blackedOut(rnd.nextBoolean())
.excludesPattern("org/${rnd.nextInt()}/**")
.includesPattern("org/${rnd.nextInt()}/**")
.propertySets(Collections.emptyList()) // no property sets configured
.repositorySettings(mySettings)
.xraySettings(xraySettings)
.customProperties(customProperties)
.build()
artifactory.repositories().create(0, myLocalRepo)
def expectedSettings = myLocalRepo.repositorySettings

def resp = artifactory.repository(myLocalRepo.getKey()).get()
assertThat(resp, CoreMatchers.notNullValue())
assertThat(resp.repoLayoutRef, CoreMatchers.is(expectedSettings.repoLayout))

resp.getRepositorySettings().with {
assertThat(packageType, CoreMatchers.is(expectedSettings.packageType))
assertThat(terraformType, CoreMatchers.is(expectedSettings.terraformType))
}
}
}
Loading