Skip to content

Commit

Permalink
refactor(cf): removed unused code, fix spelling and etc (#4159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy Louie committed Nov 8, 2019
1 parent 98cdf2e commit 8f1308d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static Optional<Map<String, String>> parse(String key) {
}

public static String getApplicationKey(String app) {
return ID + ":" + Namespace.APPLICATIONS + ":" + app;
return ID + ":" + Namespace.APPLICATIONS + ":" + app.toLowerCase();
}

public static String getAllLoadBalancers() {
Expand Down Expand Up @@ -132,11 +132,27 @@ public static String getLoadBalancerKey(String account, String uri, String regio
}

public static String getClusterKey(String account, String app, String name) {
return ID + ":" + Namespace.CLUSTERS + ":" + account + ":" + app + ":" + name;
return ID
+ ":"
+ Namespace.CLUSTERS
+ ":"
+ account
+ ":"
+ app.toLowerCase()
+ ":"
+ name.toLowerCase();
}

public static String getServerGroupKey(String account, String name, String region) {
return ID + ":" + Namespace.SERVER_GROUPS + ":" + account + ":" + name + ":" + region;
return ID
+ ":"
+ Namespace.SERVER_GROUPS
+ ":"
+ account
+ ":"
+ name.toLowerCase()
+ ":"
+ region;
}

public static String getInstanceKey(String account, String instanceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public List<CloudFoundryApplication> all() {
// Evict Records from `cache` that are no longer in the foundation
List<String> availableAppIds =
newCloudFoundryAppList.stream().map(Application::getGuid).collect(toList());
serverGroupCache.asMap().keySet().stream()
serverGroupCache
.asMap()
.keySet()
.forEach(
key -> {
if (!availableAppIds.contains(key)) {
Expand Down Expand Up @@ -168,7 +170,7 @@ public List<CloudFoundryApplication> all() {
@Nullable
public CloudFoundryServerGroup findServerGroupByNameAndSpaceId(String name, String spaceId) {
return Optional.ofNullable(findServerGroupId(name, spaceId))
.map(serverGroupId -> Optional.ofNullable(findById(serverGroupId)).orElse(null))
.flatMap(serverGroupId -> Optional.ofNullable(findById(serverGroupId)))
.orElse(null);
}

Expand Down Expand Up @@ -214,7 +216,7 @@ private CloudFoundryServerGroup map(Application application) {
try {
CloudFoundryPackage cfPackage =
safelyCall(() -> api.findPackagesByAppId(appId))
.map(
.flatMap(
packages ->
packages.getResources().stream()
.findFirst()
Expand All @@ -233,8 +235,7 @@ private CloudFoundryServerGroup map(Application application) {
pkg.getData().getChecksum() == null
? null
: pkg.getData().getChecksum().getValue())
.build())
.orElse(null))
.build()))
.orElse(null);

droplet =
Expand Down Expand Up @@ -304,13 +305,17 @@ private CloudFoundryServerGroup map(Application application) {
String serverGroupAppManagerUri = appsManagerUri;
if (StringUtils.isNotEmpty(appsManagerUri)) {
serverGroupAppManagerUri =
appsManagerUri
+ "/organizations/"
+ space.getOrganization().getId()
+ "/spaces/"
+ space.getId()
+ "/applications/"
+ appId;
Optional.ofNullable(space)
.map(
s ->
appsManagerUri
+ "/organizations/"
+ s.getOrganization().getId()
+ "/spaces/"
+ s.getId()
+ "/applications/"
+ appId)
.orElse("");
}

String serverGroupMetricsUri = metricsUri;
Expand Down Expand Up @@ -504,7 +509,7 @@ public void updateProcess(
throws CloudFoundryApiException {
final Process.HealthCheck healthCheck =
healthCheckType != null ? new Process.HealthCheck().setType(healthCheckType) : null;
if (healthCheckEndpoint != null && !healthCheckEndpoint.isEmpty()) {
if (healthCheckEndpoint != null && !healthCheckEndpoint.isEmpty() && healthCheck != null) {
healthCheck.setData(new Process.HealthCheckData().setEndpoint(healthCheckEndpoint));
}
safelyCall(() -> api.updateProcess(guid, new UpdateProcess(command, healthCheck)));
Expand Down Expand Up @@ -539,9 +544,9 @@ public InputStream downloadPackageBits(String packageGuid) throws CloudFoundryAp
}
}

public String uploadPackageBits(String packageGuid, File file) throws CloudFoundryApiException {
public void uploadPackageBits(String packageGuid, File file) throws CloudFoundryApiException {
TypedFile uploadFile = new TypedFile("multipart/form-data", file);
return safelyCall(() -> api.uploadPackageBits(packageGuid, uploadFile))
safelyCall(() -> api.uploadPackageBits(packageGuid, uploadFile))
.map(Package::getGuid)
.orElseThrow(
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ public CloudFoundryApiException(ErrorDescription errorCause, RetrofitError retro
super(
Optional.ofNullable(errorCause)
.map(e -> getMessage(e.getErrors().toArray(new String[0])))
.orElse(
"status: "
+ retrofitError.getResponse().getStatus()
+ ". url: "
+ retrofitError.getResponse().getUrl()
+ ". raw response body: "
+ retrofitError.getResponse().getBody()));
.orElse(getRetrofitErrorMessage(retrofitError)));
if (errorCause != null) {
this.errorCode = errorCause.getCode();
}
}

public CloudFoundryApiException(RetrofitError retrofitError) {
super(getRetrofitErrorMessage(retrofitError));
}

public CloudFoundryApiException(Throwable t, String... errors) {
super(getMessage(t, errors), t);
}
Expand All @@ -66,4 +64,13 @@ private static String getMessage(Throwable t, String... errors) {
allErrors[errors.length] = t.getMessage();
return getMessage(allErrors);
}

private static String getRetrofitErrorMessage(RetrofitError retrofitError) {
return "status: "
+ retrofitError.getResponse().getStatus()
+ ". url: "
+ retrofitError.getResponse().getUrl()
+ ". raw response body: "
+ retrofitError.getResponse().getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static <T> Optional<T> safelyCall(RetrofitCallable<T> r) throws CloudFoundryApiE
ErrorDescription errorDescription =
(ErrorDescription) retrofitError.getBodyAs(ErrorDescription.class);
if (errorDescription == null) {
throw new CloudFoundryApiException(retrofitError.getCause());
throw new CloudFoundryApiException(retrofitError);
}
throw new CloudFoundryApiException(errorDescription, retrofitError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class HttpCloudFoundryClient implements CloudFoundryClient {
private final String user;
private final String password;
private final OkHttpClient okHttpClient;
private Logger logger = LoggerFactory.getLogger(HttpCloudFoundryClient.class);

private AuthenticationService uaaService;
private AtomicLong tokenExpirationNs = new AtomicLong(System.nanoTime());
Expand All @@ -91,8 +92,6 @@ public class HttpCloudFoundryClient implements CloudFoundryClient {
private Tasks tasks;
private Logs logs;

Logger logger = LoggerFactory.getLogger(HttpCloudFoundryClient.class);

private final RequestInterceptor oauthInterceptor =
new RequestInterceptor() {
@Override
Expand Down Expand Up @@ -164,7 +163,7 @@ Response createRetryInterceptor(Interceptor.Chain chain) {
});
} catch (SocketTimeoutException e) {
throw new RetryableApiException(
"Timeout " + callName + " " + chain.request().httpUrl() + ", attempting retrying", e);
"Timeout " + callName + " " + chain.request().httpUrl() + ", attempting retry", e);
} catch (Exception e) {
final Response response = lastResponse.get();
if (response == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public DeploymentResult operate(List priorOutputs) {
return deploymentResult();
}

final Integer desiredInstanceCount = description.getApplicationAttributes().getInstances();
final int desiredInstanceCount = description.getApplicationAttributes().getInstances();
if (description.isStartApplication() && desiredInstanceCount > 0) {
client.getApplications().startApplication(serverGroup.getId());
ProcessStats.State state =
Expand Down Expand Up @@ -285,13 +285,11 @@ private static ExternalReference resolveArtifactInfo(
private static ExternalReference resolveBuildInfo(
DeployCloudFoundryServerGroupDescription description) {
Map<String, Object> buildInfo = null;
if (buildInfo == null) {
final Artifact applicationArtifact = description.getApplicationArtifact();
if (applicationArtifact != null) {
final Map<String, Object> metadata = applicationArtifact.getMetadata();
if (metadata != null) {
buildInfo = (Map<String, Object>) applicationArtifact.getMetadata().get("build");
}
final Artifact applicationArtifact = description.getApplicationArtifact();
if (applicationArtifact != null) {
final Map<String, Object> metadata = applicationArtifact.getMetadata();
if (metadata != null) {
buildInfo = (Map<String, Object>) applicationArtifact.getMetadata().get("build");
}
}
if (buildInfo == null) {
Expand Down

0 comments on commit 8f1308d

Please sign in to comment.