Skip to content

Commit

Permalink
Allow customizable BlackDuck Project and Version
Browse files Browse the repository at this point in the history
The project name used by BlackDuck can be customized by the deployer
during the _cf push_ via the __BLACK\_DUCK\_PROJECT\_NAME__ environment
variable in the _manifest.yml_. If no environment variable is specified,
the perceptor will generate a name.

The project version used by BlackDuck can be customized by the deployer
during the _cf push_ via the __BLACK\_DUCK\_PROJECT\_VERSION__
environment variable in the _manifest.yml_. If no environment variable
is specified the perceptor will generate the version.

For example:
```yml
---
applications:
- name: spring-music
  memory: 1G
  random-route: true
  path: build/libs/spring-music.jar
  env:
    BLACK_DUCK_PROJECT_VERSION: '7.1'
    BLACK_DUCK_PROJECT_NAME: 'Spring Music'
```
  • Loading branch information
Jason Fisher committed Jan 25, 2019
1 parent 361e0bf commit 367101d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public class CfResourceData {

private DropletResource dropletData;

private HubProjectParameters hubProjectParameters;

public CfResourceData() {
}

public CfResourceData(String resourceId, String bindingId, String applicationId, DropletResource dropletData) {
public CfResourceData(String resourceId, String bindingId, String applicationId, DropletResource dropletData, HubProjectParameters hubProjectParameters) {
this.resourceId = resourceId;
this.bindingId = bindingId;
this.applicationId = applicationId;
this.dropletData = dropletData;
this.hubProjectParameters = hubProjectParameters;
}

public String getResourceId() {
Expand Down Expand Up @@ -66,18 +69,31 @@ public void setDropletData(DropletResource dropletData) {
this.dropletData = dropletData;
}

public HubProjectParameters getHubProjectParameters() {
return hubProjectParameters;
}

public void setHubProjectParameters(HubProjectParameters hubProjectParameters) {
this.hubProjectParameters = hubProjectParameters;
}

public Image toImage() {
// Repository -> Blackduck Project
// Sha -> Application Id + Checksum
// Tag -> Blackduck Project Version

Image img = new Image();
img.setRepository(getApplicationId()); // This is the application Id...this is the only
// piece required to download the actual droplet
img.setSha(getDropletData().getChecksum().getValue()); // The checksum of
// the "current"
// droplet. Used to ensure read
// consistency later
// The Respository is the location where the blob data lives
img.setRepository(getApplicationId());

// The checksum of the "current" droplet. Used to ensure read consistency later
img.setSha(getDropletData().getChecksum().getValue());

// Set the custom Project Name to appear in BlackDuck if one was provided
img.setBlackDuckProjectName(getHubProjectParameters().getProjectName().orElse(null));

// Set the custom Project Version to use in BlackDuck if one was provided
img.setBlackDuckProjectVersion(getHubProjectParameters().getProjectVersion().orElse(null));

return img;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ public final class HubProjectParameters {

private static String PROJECT_NAME_ENV_NAME = "BLACK_DUCK_PROJECT_NAME";

private static String PROJECT_VERSION_ENV_NAME = "BLACK_DUCK_PROJECT_VERSION";

private Optional<String> projectName = Optional.empty();

private Optional<String> codeLocation = Optional.empty();

private Optional<String> projectVersion = Optional.empty();

@JsonCreator
public HubProjectParameters(@JsonProperty(value = "project_name", required = false) @Nullable String projectName,
@JsonProperty(value = "code_location", required = false) @Nullable String codeLocation) {
@JsonProperty(value = "code_location", required = false) @Nullable String codeLocation,
@JsonProperty(value = "project_version", required = false) @Nullable String projectVersion) {
this.projectName = Optional.ofNullable(projectName);
this.codeLocation = Optional.ofNullable(codeLocation);
this.projectVersion = Optional.ofNullable(projectVersion);
}

/**
Expand Down Expand Up @@ -81,8 +87,25 @@ public final void setCodeLocation(Optional<String> codeLocation) {
this.codeLocation = codeLocation;
}

/**
* @return the projectVersion
*/
@JsonProperty(value = "project_version")
public final Optional<String> getProjectVersion() {
return projectVersion;
}

/**
* @param projectVersion
* the projectVersion to set
*/
public final void setProjectVersion(Optional<String> projectVersion) {
this.projectVersion = projectVersion;
}

public static HubProjectParameters fromCloudFoundryEnvironment(Map<String, Object> cfEnv) {
return new HubProjectParameters(Optional.ofNullable(cfEnv.get(PROJECT_NAME_ENV_NAME)).map(String::valueOf).orElse(null),
Optional.ofNullable(cfEnv.get(CODE_LOCATION_ENV_NAME)).map(String::valueOf).orElse(null));
Optional.ofNullable(cfEnv.get(CODE_LOCATION_ENV_NAME)).map(String::valueOf).orElse(null),
Optional.ofNullable(cfEnv.get(PROJECT_VERSION_ENV_NAME)).map(String::valueOf).orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
import static com.blackducksoftware.integration.cloudfoundry.v2.util.ApiV2Utils.requestEvents;
import static com.blackducksoftware.integration.cloudfoundry.v2.util.ApiV2Utils.requestSingleServiceBinding;
import static com.blackducksoftware.integration.cloudfoundry.v3.util.ApiV3Utils.DropletResourceNotDummy;
import static com.blackducksoftware.integration.cloudfoundry.v3.util.ApiV3Utils.createGetApplicationEnvironmentRequest;
import static com.blackducksoftware.integration.cloudfoundry.v3.util.ApiV3Utils.createListApplicationStagedDropletsRequest;
import static com.blackducksoftware.integration.cloudfoundry.v3.util.ApiV3Utils.requestApplicationEnvironment;
import static com.blackducksoftware.integration.cloudfoundry.v3.util.ApiV3Utils.requestCurrentApplicationDropletRequest;

import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v2.events.ListEventsRequest;
import org.cloudfoundry.client.v2.servicebindings.ListServiceBindingsRequest;
import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -48,6 +53,7 @@
import com.blackducksoftware.integration.cloudfoundry.perceiver.IntegrationSource;
import com.blackducksoftware.integration.cloudfoundry.perceiver.PerceptorProperties;
import com.blackducksoftware.integration.cloudfoundry.perceiver.api.CfResourceData;
import com.blackducksoftware.integration.cloudfoundry.perceiver.api.HubProjectParameters;
import com.blackducksoftware.integration.cloudfoundry.perceiver.iface.IControllerService;
import com.blackducksoftware.integration.cloudfoundry.perceiver.iface.IEventMonitorService;
import com.blackducksoftware.integration.cloudfoundry.v2.model.EventType;
Expand Down Expand Up @@ -130,14 +136,19 @@ public void run() {
Flux.fromIterable(new HashSet<>(appIdsWaitingStaged))
.switchMap(appId -> Flux.zip(Mono.just(appId),
requestCurrentApplicationDropletRequest(cloudFoundryClient, createListApplicationStagedDropletsRequest(appId)),
requestSingleServiceBinding(cloudFoundryClient, ListServiceBindingsRequest.builder().applicationId(appId).build())))
requestSingleServiceBinding(cloudFoundryClient, ListServiceBindingsRequest.builder().applicationId(appId).build()),
requestApplicationEnvironment(cloudFoundryClient, createGetApplicationEnvironmentRequest(appId))))
.filter(adsb -> DropletResourceNotDummy.test(adsb.getT2()))
.map(adsb -> {
Optional<GetApplicationEnvironmentResponse> gaer = Optional.ofNullable(adsb.getT4());
HubProjectParameters hpp = HubProjectParameters.fromCloudFoundryEnvironment(
gaer.map(GetApplicationEnvironmentResponse::getEnvironmentVariables).orElse(Collections.emptyMap()));
return new CfResourceData(adsb.getT3().getEntity().getServiceInstanceId(), // service
// instance id
adsb.getT3().getMetadata().getId(), // service binding id
adsb.getT1(), // application id
adsb.getT2()); // droplet resource
adsb.getT2(), // droplet resource
hpp);
})
.subscribe(cfrd -> {
logger.debug("Processing: {}", cfrd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,25 @@ public void run() {
if (!sbResources.isEmpty()) {
cfResources = Flux.fromStream(sbResources.stream())
.flatMap(sbr -> {
String appId = sbr.getEntity().getApplicationId();
// Get the current droplet data for each application id
ListApplicationDropletsRequest listAppDropletRequest = createListApplicationStagedDropletsRequest(
sbr.getEntity().getApplicationId());
appId);
return Flux.zip(Mono.just(sbr),
requestCurrentApplicationDropletRequest(cloudFoundryClient, listAppDropletRequest)
.log());
.log(),
requestApplicationEnvironment(cloudFoundryClient, createGetApplicationEnvironmentRequest(appId)));
})
.collect(Collectors.mapping(sbrdroplet -> {
Optional<GetApplicationEnvironmentResponse> gaer = Optional.ofNullable(sbrdroplet.getT3());
HubProjectParameters hpp = HubProjectParameters.fromCloudFoundryEnvironment(
gaer.map(GetApplicationEnvironmentResponse::getEnvironmentVariables).orElse(Collections.emptyMap()));
CfResourceData cfrd = new CfResourceData();
cfrd.setResourceId(sbrdroplet.getT1().getEntity().getServiceInstanceId());
cfrd.setBindingId(sbrdroplet.getT1().getMetadata().getId());
cfrd.setApplicationId(sbrdroplet.getT1().getEntity().getApplicationId());
cfrd.setDropletData(sbrdroplet.getT2());
cfrd.setHubProjectParameters(hpp);
return cfrd;
}, Collectors.toSet())).block();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public final class Image {
@JsonProperty("Sha")
private String sha;

@JsonProperty("Priority")
private int priority;

@JsonProperty("BlackDuckProjectName")
private String blackDuckProjectName;

@JsonProperty("BlackDuckProjectVersion")
private String blackDuckProjectVersion;

public Image() {

}
Expand Down Expand Up @@ -54,4 +63,28 @@ public final String getSha() {
public final void setSha(String sha) {
this.sha = sha;
}

public final int getPriority() {
return priority;
}

public final void setPriority(int priority) {
this.priority = priority;
}

public final String getBlackDuckProjectName() {
return blackDuckProjectName;
}

public final void setBlackDuckProjectName(String blackDuckProjectName) {
this.blackDuckProjectName = blackDuckProjectName;
}

public final String getBlackDuckProjectVersion() {
return blackDuckProjectVersion;
}

public final void setBlackDuckProjectVersion(String blackDuckProjectVersion) {
this.blackDuckProjectVersion = blackDuckProjectVersion;
}
}

0 comments on commit 367101d

Please sign in to comment.