Skip to content

Commit

Permalink
Add configuration as code support (#147)
Browse files Browse the repository at this point in the history
* Bump jenkins core baseline

* Fix tests

* Configuration as code support

* Rerun CI

* Remove unused var

* Use released version of JCasC

* Restore test

* Try bump jcasc version

* Small tweaks, trying to fix windows build

* Downgrade to compatible core version
  • Loading branch information
timja authored and gavinfish committed Apr 12, 2019
1 parent feb0c47 commit 82687ac
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
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"

0 comments on commit 82687ac

Please sign in to comment.