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 configuration as code support #147

Merged
merged 13 commits into from
Apr 12, 2019
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
.settings

*.idea
*.iml
*.iml
index.html
27 changes: 24 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<findbugs.excludeFilterFile>exclude-findbugs.xml</findbugs.excludeFilterFile>
<maven.javadoc.skip>true</maven.javadoc.skip>
<azure-commons.version>0.2.5</azure-commons.version>
<configuration-as-code.version>1.10</configuration-as-code.version>
</properties>

<licenses>
Expand Down Expand Up @@ -86,7 +87,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.7</version>
<version>2.1.16</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -126,6 +127,26 @@
<artifactId>blueocean-rest</artifactId>
<version>1.3.5</version>
</dependency>

<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>configuration-as-code-support</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down Expand Up @@ -163,12 +184,12 @@
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>symbol-annotation</artifactId>
<version>1.5</version>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.5</version>
<version>1.7</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

Expand Down Expand Up @@ -199,6 +200,7 @@ public StorageAccountCredential getStorageCred() {
}

@Extension
@Symbol("azureStorageAccount")
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.microsoftopentechnologies.windowsazurestorage;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.microsoftopentechnologies.windowsazurestorage.helper.AzureCredentials;
import hudson.ExtensionList;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import io.jenkins.plugins.casc.support.credentials.CredentialsRootConfigurator;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ConfigAsCodeTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_configuration_as_code() {
AzureCredentials credentials = (AzureCredentials) SystemCredentialsProvider.getInstance().getCredentials()
.get(0);

assertEquals(credentials.getScope(), CredentialsScope.GLOBAL);
assertEquals(credentials.getDescription(), "Account");
assertEquals(credentials.getStorageAccountName(), "a-storage-account");
assertEquals(credentials.getBlobEndpointURL(), "https://blob.core.windows.net/");
assertEquals(credentials.getId(), "storage-account");
assertNotNull(credentials.getStorageKey());
}

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void export_configuration() throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);

CredentialsRootConfigurator root = ExtensionList
.lookupSingleton(CredentialsRootConfigurator.class);
final CNode node = root.describe(root.getTargetComponent(context), context);

assertNotNull(node);
Mapping mapping = node.asMapping().get("system")
.asMapping()
.get("domainCredentials")
.asSequence()
.get(0)
.asMapping()
.get("credentials")
.asSequence()
.get(0)
.asMapping()
.get("azureStorageAccount")
.asMapping();

assertEquals(mapping.getScalarValue("scope"), "GLOBAL");
assertEquals(mapping.getScalarValue("description"), "Account");
assertEquals(mapping.getScalarValue("storageAccountName"), "a-storage-account");
assertEquals(mapping.getScalarValue("blobEndpointURL"), "https://blob.core.windows.net/");
assertEquals(mapping.getScalarValue("id"), "storage-account");
assertNotNull(mapping.getScalarValue("storageKey"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
credentials:
system:
domainCredentials:
- credentials:
- azureStorageAccount:
blobEndpointURL: "https://blob.core.windows.net/"
description: "Account"
id: "storage-account"
scope: GLOBAL
storageAccountName: "a-storage-account"
storageKey: "Password12"