This repository has been archived by the owner on May 20, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from airbnb/api-updates
[api] API updates
- Loading branch information
Showing
9 changed files
with
315 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/airbnb/airpal/resources/QueriesResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.airbnb.airpal.resources; | ||
|
||
import com.airbnb.airpal.api.Job; | ||
import com.airbnb.airpal.api.JobState; | ||
import com.airbnb.airpal.core.AuthorizationUtil; | ||
import com.airbnb.airpal.core.store.JobHistoryStore; | ||
import com.airbnb.airpal.presto.PartitionedTable; | ||
import com.airbnb.airpal.presto.Table; | ||
import com.facebook.presto.client.Column; | ||
import com.google.common.base.Optional; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Ordering; | ||
import com.google.inject.Inject; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.subject.Subject; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static com.airbnb.airpal.resources.QueryResource.JOB_ORDERING; | ||
|
||
@Path("/api/queries") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public class QueriesResource | ||
{ | ||
private final JobHistoryStore jobHistoryStore; | ||
|
||
@Inject | ||
public QueriesResource(JobHistoryStore jobHistoryStore) | ||
{ | ||
this.jobHistoryStore = jobHistoryStore; | ||
} | ||
|
||
@GET | ||
public Response getQueries( | ||
@QueryParam("results") int numResults, | ||
@QueryParam("table") List<PartitionedTable> tables) | ||
{ | ||
Subject subject = SecurityUtils.getSubject(); | ||
Iterable<Job> recentlyRun; | ||
int results = Optional.of(numResults).or(200); | ||
|
||
if (tables.size() < 1) { | ||
recentlyRun = jobHistoryStore.getRecentlyRun(results); | ||
} else { | ||
recentlyRun = jobHistoryStore.getRecentlyRun( | ||
results, | ||
Iterables.transform(tables, new PartitionedTable.PartitionedTableToTable())); | ||
} | ||
|
||
ImmutableList.Builder<Job> filtered = ImmutableList.builder(); | ||
for (Job job : recentlyRun) { | ||
if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) { | ||
filtered.add(job); | ||
continue; | ||
} | ||
for (Table table : job.getTablesUsed()) { | ||
if (AuthorizationUtil.isAuthorizedRead(subject, table)) { | ||
filtered.add(new Job( | ||
job.getUser(), | ||
job.getQuery(), | ||
job.getUuid(), | ||
job.getOutput(), | ||
job.getQueryStats(), | ||
job.getState(), | ||
Collections.<Column>emptyList(), | ||
Collections.<Table>emptySet(), | ||
job.getQueryStartedDateTime(), | ||
job.getError(), | ||
job.getQueryFinishedDateTime())); | ||
} | ||
} | ||
} | ||
|
||
List<Job> sortedResult = Ordering | ||
.natural() | ||
.nullsLast() | ||
.onResultOf(JOB_ORDERING) | ||
.reverse() | ||
.immutableSortedCopy(filtered.build()); | ||
return Response.ok(sortedResult).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/airbnb/airpal/resources/UserResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.airbnb.airpal.resources; | ||
|
||
import com.airbnb.airpal.core.AirpalUser; | ||
import com.airbnb.airpal.core.AuthorizationUtil; | ||
import lombok.Value; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.subject.Subject; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/api/user") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class UserResource | ||
{ | ||
@GET | ||
public Response getUserInfo() | ||
{ | ||
Subject subject = SecurityUtils.getSubject(); | ||
AirpalUser user = subject.getPrincipals().oneByType(AirpalUser.class); | ||
|
||
if (user == null) { | ||
return Response.status(Response.Status.FORBIDDEN).build(); | ||
} else { | ||
return Response.ok( | ||
new UserInfo( | ||
user.getUserName(), | ||
new ExecutionPermissions( | ||
AuthorizationUtil.isAuthorizedWrite(subject, "hive", "airpal", "any"), | ||
true, | ||
user.getAccessLevel()) | ||
)).build(); | ||
} | ||
} | ||
|
||
@Value | ||
private static class UserInfo | ||
{ | ||
private final String name; | ||
private final ExecutionPermissions executionPermissions; | ||
} | ||
|
||
@Value | ||
public static class ExecutionPermissions | ||
{ | ||
private final boolean canCreateTable; | ||
private final boolean canCreateCsv; | ||
private final String accessLevel; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/com/airbnb/airpal/resources/UsersResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.airbnb.airpal.resources; | ||
|
||
import com.airbnb.airpal.api.Job; | ||
import com.airbnb.airpal.api.JobState; | ||
import com.airbnb.airpal.core.AirpalUser; | ||
import com.airbnb.airpal.core.AuthorizationUtil; | ||
import com.airbnb.airpal.core.store.JobHistoryStore; | ||
import com.airbnb.airpal.presto.PartitionedTable; | ||
import com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable; | ||
import com.airbnb.airpal.presto.Table; | ||
import com.airbnb.airpal.resources.UserResource.ExecutionPermissions; | ||
import com.facebook.presto.client.Column; | ||
import com.google.common.base.Optional; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Ordering; | ||
import com.google.inject.Inject; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.subject.Subject; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static com.airbnb.airpal.resources.QueryResource.JOB_ORDERING; | ||
|
||
@Path("/api/users/{id}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class UsersResource | ||
{ | ||
private final JobHistoryStore jobHistoryStore; | ||
|
||
@Inject | ||
public UsersResource(JobHistoryStore jobHistoryStore) | ||
{ | ||
this.jobHistoryStore = jobHistoryStore; | ||
} | ||
|
||
@GET | ||
@Path("permissions") | ||
public Response getUserPermissions(@PathParam("id") String userId) | ||
{ | ||
Subject subject = SecurityUtils.getSubject(); | ||
AirpalUser user = subject.getPrincipals().oneByType(AirpalUser.class); | ||
|
||
if (user == null) { | ||
return Response.status(Response.Status.FORBIDDEN).build(); | ||
} else { | ||
return Response.ok( | ||
new ExecutionPermissions( | ||
AuthorizationUtil.isAuthorizedWrite(subject, "hive", "airpal", "any"), | ||
true, | ||
user.getAccessLevel()) | ||
).build(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("queries") | ||
public Response getUserQueries( | ||
@PathParam("id") String userId, | ||
@QueryParam("results") int numResults, | ||
@QueryParam("table") List<PartitionedTable> tables) | ||
{ | ||
Subject subject = SecurityUtils.getSubject(); | ||
Iterable<Job> recentlyRun; | ||
int results = Optional.of(numResults).or(200); | ||
|
||
if (tables.size() < 1) { | ||
recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results); | ||
} else { | ||
recentlyRun = jobHistoryStore.getRecentlyRunForUser( | ||
userId, | ||
results, | ||
Iterables.transform(tables, new PartitionedTableToTable())); | ||
} | ||
|
||
ImmutableList.Builder<Job> filtered = ImmutableList.builder(); | ||
for (Job job : recentlyRun) { | ||
if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) { | ||
filtered.add(job); | ||
continue; | ||
} | ||
for (Table table : job.getTablesUsed()) { | ||
if (AuthorizationUtil.isAuthorizedRead(subject, table)) { | ||
filtered.add(new Job( | ||
job.getUser(), | ||
job.getQuery(), | ||
job.getUuid(), | ||
job.getOutput(), | ||
job.getQueryStats(), | ||
job.getState(), | ||
Collections.<Column>emptyList(), | ||
Collections.<Table>emptySet(), | ||
job.getQueryStartedDateTime(), | ||
job.getError(), | ||
job.getQueryFinishedDateTime())); | ||
} | ||
} | ||
} | ||
|
||
List<Job> sortedResult = Ordering | ||
.natural() | ||
.nullsLast() | ||
.onResultOf(JOB_ORDERING) | ||
.reverse() | ||
.immutableSortedCopy(filtered.build()); | ||
return Response.ok(sortedResult).build(); | ||
} | ||
} |
Oops, something went wrong.