Skip to content

Commit 13621fb

Browse files
committed
DefaultApplications: use audit events v3 for getEvents()
1 parent 8427247 commit 13621fb

File tree

3 files changed

+122
-105
lines changed

3 files changed

+122
-105
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;
@@ -121,6 +117,8 @@
121117
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
122118
import org.cloudfoundry.client.v3.applications.SetApplicationCurrentDropletRequest;
123119
import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest;
120+
import org.cloudfoundry.client.v3.auditevents.AuditEventResource;
121+
import org.cloudfoundry.client.v3.auditevents.ListAuditEventsRequest;
124122
import org.cloudfoundry.client.v3.builds.BuildState;
125123
import org.cloudfoundry.client.v3.builds.CreateBuildRequest;
126124
import org.cloudfoundry.client.v3.builds.CreateBuildResponse;
@@ -355,7 +353,7 @@ public Mono<ApplicationEnvironments> getEnvironments(
355353

356354
@Override
357355
public Flux<ApplicationEvent> getEvents(GetApplicationEventsRequest request) {
358-
return getApplicationId(request.getName())
356+
return getApplicationIdV3(request.getName())
359357
.flatMapMany(
360358
applicationId ->
361359
requestEvents(applicationId)
@@ -857,20 +855,19 @@ public static BiFunction<ProcessState, ProcessState, ProcessState> collectStates
857855
};
858856
}
859857

860-
private static ApplicationEvent convertToApplicationEvent(EventResource resource) {
861-
EventEntity entity = resource.getEntity();
858+
private static ApplicationEvent convertToApplicationEvent(AuditEventResource entity) {
862859
Date timestamp = null;
863860
try {
864-
timestamp = DateUtils.parseFromIso8601(entity.getTimestamp());
861+
timestamp = DateUtils.parseFromIso8601(entity.getCreatedAt());
865862
} catch (IllegalArgumentException iae) {
866863
// do not set time
867864
}
868865
return ApplicationEvent.builder()
869-
.actor(entity.getActorName())
866+
.actor(entity.getAuditEventActor().getName())
870867
.description(
871868
eventDescription(
872869
getMetadataRequest(entity), getEntryNames(entity.getType())))
873-
.id(ResourceUtils.getId(resource))
870+
.id(entity.getId())
874871
.event(entity.getType())
875872
.time(timestamp)
876873
.build();
@@ -1224,17 +1221,14 @@ private Flux<LogMessage> getLogs(String applicationId, Boolean recent) {
12241221
}
12251222

12261223
@SuppressWarnings("unchecked")
1227-
private static Map<String, Object> getMetadataRequest(EventEntity entity) {
1228-
Map<String, Optional<Object>> metadata =
1229-
Optional.ofNullable(entity.getMetadatas()).orElse(Collections.emptyMap());
1224+
private static Map<String, Object> getMetadataRequest(AuditEventResource entity) {
1225+
Map<String, Object> metadata =
1226+
Optional.ofNullable(entity.getData()).orElse(Collections.emptyMap());
12301227

12311228
if (metadata.get("request") != null) {
1232-
return metadata.get("request")
1233-
.map(m -> (Map<String, Object>) m)
1234-
.orElse(Collections.emptyMap());
1229+
return (Map<String, Object>) metadata.getOrDefault("request", Collections.emptyMap());
12351230
} else if (metadata.get("instance") != null) {
1236-
return metadata.entrySet().stream()
1237-
.collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().orElse("")));
1231+
return Collections.unmodifiableMap(metadata);
12381232
} else {
12391233
return Collections.emptyMap();
12401234
}
@@ -1887,16 +1881,16 @@ private Mono<DeleteRouteResponse> requestDeleteRoute(String routeId) {
18871881
.delete(DeleteRouteRequest.builder().async(true).routeId(routeId).build());
18881882
}
18891883

1890-
private Flux<EventResource> requestEvents(String applicationId) {
1891-
return PaginationUtils.requestClientV2Resources(
1884+
private Flux<AuditEventResource> requestEvents(String applicationId) {
1885+
return PaginationUtils.requestClientV3Resources(
18921886
page ->
18931887
this.cloudFoundryClient
1894-
.events()
1888+
.auditEventsV3()
18951889
.list(
1896-
ListEventsRequest.builder()
1897-
.actee(applicationId)
1898-
.orderDirection(OrderDirection.DESCENDING)
1899-
.resultsPerPage(50)
1890+
ListAuditEventsRequest.builder()
1891+
.targetId(applicationId)
1892+
.orderBy("-created_at")
1893+
.perPage(50)
19001894
.page(page)
19011895
.build()));
19021896
}

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)