Skip to content

Commit 7b2553f

Browse files
committed
DefaultApplications: use audit events v3 for getEvents()
1 parent b69b402 commit 7b2553f

File tree

3 files changed

+123
-106
lines changed

3 files changed

+123
-106
lines changed

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.function.UnaryOperator;
4141
import java.util.stream.Collectors;
4242
import org.cloudfoundry.client.CloudFoundryClient;
43-
import org.cloudfoundry.client.v2.OrderDirection;
4443
import org.cloudfoundry.client.v2.applications.AbstractApplicationResource;
4544
import org.cloudfoundry.client.v2.applications.ApplicationEntity;
4645
import org.cloudfoundry.client.v2.applications.ApplicationInstanceInfo;
@@ -68,9 +67,6 @@
6867
import org.cloudfoundry.client.v2.applications.UploadApplicationRequest;
6968
import org.cloudfoundry.client.v2.applications.UploadApplicationResponse;
7069
import org.cloudfoundry.client.v2.applications.Usage;
71-
import org.cloudfoundry.client.v2.events.EventEntity;
72-
import org.cloudfoundry.client.v2.events.EventResource;
73-
import org.cloudfoundry.client.v2.events.ListEventsRequest;
7470
import org.cloudfoundry.client.v2.organizations.ListOrganizationPrivateDomainsRequest;
7571
import org.cloudfoundry.client.v2.organizations.ListOrganizationSpacesRequest;
7672
import org.cloudfoundry.client.v2.organizations.ListOrganizationsRequest;
@@ -122,6 +118,8 @@
122118
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
123119
import org.cloudfoundry.client.v3.applications.SetApplicationCurrentDropletRequest;
124120
import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest;
121+
import org.cloudfoundry.client.v3.auditevents.AuditEventResource;
122+
import org.cloudfoundry.client.v3.auditevents.ListAuditEventsRequest;
125123
import org.cloudfoundry.client.v3.builds.BuildState;
126124
import org.cloudfoundry.client.v3.builds.CreateBuildRequest;
127125
import org.cloudfoundry.client.v3.builds.CreateBuildResponse;
@@ -356,7 +354,7 @@ public Mono<ApplicationEnvironments> getEnvironments(
356354

357355
@Override
358356
public Flux<ApplicationEvent> getEvents(GetApplicationEventsRequest request) {
359-
return getApplicationId(request.getName())
357+
return getApplicationIdV3(request.getName())
360358
.flatMapMany(
361359
applicationId ->
362360
requestEvents(applicationId)
@@ -843,20 +841,19 @@ public static BiFunction<ProcessState, ProcessState, ProcessState> collectStates
843841
};
844842
}
845843

846-
private static ApplicationEvent convertToApplicationEvent(EventResource resource) {
847-
EventEntity entity = resource.getEntity();
844+
private static ApplicationEvent convertToApplicationEvent(AuditEventResource entity) {
848845
Date timestamp = null;
849846
try {
850-
timestamp = DateUtils.parseFromIso8601(entity.getTimestamp());
847+
timestamp = DateUtils.parseFromIso8601(entity.getCreatedAt());
851848
} catch (IllegalArgumentException iae) {
852849
// do not set time
853850
}
854851
return ApplicationEvent.builder()
855-
.actor(entity.getActorName())
852+
.actor(entity.getAuditEventActor().getName())
856853
.description(
857854
eventDescription(
858855
getMetadataRequest(entity), getEntryNames(entity.getType())))
859-
.id(ResourceUtils.getId(resource))
856+
.id(entity.getId())
860857
.event(entity.getType())
861858
.time(timestamp)
862859
.build();
@@ -1210,17 +1207,14 @@ private Flux<LogMessage> getLogs(String applicationId, Boolean recent) {
12101207
}
12111208

12121209
@SuppressWarnings("unchecked")
1213-
private static Map<String, Object> getMetadataRequest(EventEntity entity) {
1214-
Map<String, Optional<Object>> metadata =
1215-
Optional.ofNullable(entity.getMetadatas()).orElse(Collections.emptyMap());
1210+
private static Map<String, Object> getMetadataRequest(AuditEventResource entity) {
1211+
Map<String, Object> metadata =
1212+
Optional.ofNullable(entity.getData()).orElse(Collections.emptyMap());
12161213

12171214
if (metadata.get("request") != null) {
1218-
return metadata.get("request")
1219-
.map(m -> (Map<String, Object>) m)
1220-
.orElse(Collections.emptyMap());
1215+
return (Map<String, Object>) metadata.getOrDefault("request", Collections.emptyMap());
12211216
} else if (metadata.get("instance") != null) {
1222-
return metadata.entrySet().stream()
1223-
.collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().orElse("")));
1217+
return Collections.unmodifiableMap(metadata);
12241218
} else {
12251219
return Collections.emptyMap();
12261220
}
@@ -1871,16 +1865,16 @@ private Mono<DeleteRouteResponse> requestDeleteRoute(String routeId) {
18711865
.delete(DeleteRouteRequest.builder().async(true).routeId(routeId).build());
18721866
}
18731867

1874-
private Flux<EventResource> requestEvents(String applicationId) {
1875-
return PaginationUtils.requestClientV2Resources(
1868+
private Flux<AuditEventResource> requestEvents(String applicationId) {
1869+
return PaginationUtils.requestClientV3Resources(
18761870
page ->
18771871
this.cloudFoundryClient
1878-
.events()
1872+
.auditEventsV3()
18791873
.list(
1880-
ListEventsRequest.builder()
1881-
.actee(applicationId)
1882-
.orderDirection(OrderDirection.DESCENDING)
1883-
.resultsPerPage(50)
1874+
ListAuditEventsRequest.builder()
1875+
.targetId(applicationId)
1876+
.orderBy("-created_at")
1877+
.perPage(50)
18841878
.page(page)
18851879
.build()));
18861880
}

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.cloudfoundry.client.v2.userprovidedserviceinstances.UserProvidedServiceInstances;
4545
import org.cloudfoundry.client.v2.users.Users;
4646
import org.cloudfoundry.client.v3.applications.ApplicationsV3;
47+
import org.cloudfoundry.client.v3.auditevents.AuditEventsV3;
4748
import org.cloudfoundry.client.v3.buildpacks.BuildpacksV3;
4849
import org.cloudfoundry.client.v3.domains.DomainsV3;
4950
import org.cloudfoundry.client.v3.jobs.JobsV3;
@@ -106,6 +107,7 @@ public abstract class AbstractOperationsTest {
106107
protected final DopplerClient dopplerClient = mock(DopplerClient.class, RETURNS_SMART_NULLS);
107108

108109
protected final Events events = mock(Events.class, RETURNS_SMART_NULLS);
110+
protected final AuditEventsV3 auditEventsV3 = mock(AuditEventsV3.class, RETURNS_SMART_NULLS);
109111

110112
protected final FeatureFlags featureFlags = mock(FeatureFlags.class, RETURNS_SMART_NULLS);
111113

@@ -182,6 +184,7 @@ public final void mockClient() {
182184
when(this.cloudFoundryClient.domains()).thenReturn(this.domains);
183185
when(this.cloudFoundryClient.domainsV3()).thenReturn(this.domainsV3);
184186
when(this.cloudFoundryClient.events()).thenReturn(this.events);
187+
when(this.cloudFoundryClient.auditEventsV3()).thenReturn(this.auditEventsV3);
185188
when(this.cloudFoundryClient.featureFlags()).thenReturn(this.featureFlags);
186189
when(this.cloudFoundryClient.jobs()).thenReturn(this.jobs);
187190
when(this.cloudFoundryClient.jobsV3()).thenReturn(this.jobsV3);

0 commit comments

Comments
 (0)