Skip to content

Commit

Permalink
a little refactoring & cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
culmat committed Jan 18, 2024
1 parent 6c121b7 commit 4d5cfee
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 72 deletions.
3 changes: 1 addition & 2 deletions LEARNINGS.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ Documentation is sparse and forums (stack overflow ) out dated
Use Typed APIs for GraphQL
Monitoring / logging is far from real time
Strange exception handling / retry logic
No support for REST in Function -> it's low level HTTP
Debugging with mvn azure-functions:run -> slow start up & could get code hot swap to work
There are no standards

Open points
Use terraform?
Get all credentials out of the code -> into environment
Use spring web?
--> https://docs.spring.io/spring-cloud-function/docs/current/reference/html/azure.html
--> https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-azure-web
136 changes: 74 additions & 62 deletions src/main/java/com/baloise/azure/FunctionalOrgEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,74 +68,16 @@ public HttpResponseMessage v1(
@BindingName("team") String team
) {
try {
context.getLogger().log(Level.INFO, "unit "+ unit);
context.getLogger().log(Level.INFO, "team "+ team);

if(!"null".equals(team)) {
Map<String, Map<String,Object>> name2team = new HashMap<>();

for (Group group : graph().getTeams(unit+"-"+team)) {
Team t = Team.parse(group.displayName);
Map<String, Object> tmp = name2team.get(t.name());
if(tmp== null) {
tmp = Map.of(
"name", t.name(),
"unit", t.unit(),
"url", getPath(request)+"/"+t.name(),
"members" , loadAndMapMembers(group, t.internal())
);
name2team.put(t.name(), tmp);
} else {
List<Map<String, Object>> members = (List<Map<String, Object>>) tmp.get("members");
members.addAll(loadAndMapMembers(group, t.internal()));
}
}
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("teams", name2team.values()))
).build();
return createTeamResponse(request, unit, team);
}

if(!"null".equals(unit)) {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("teams",
graph().getTeams(unit+"-").stream()
.map(g -> Team.parse(g.displayName))
.map(t ->
Map.of(
"name", t.name(),
"unit", t.unit(),
"url", format("%s/%s/%s", getPath(request), t.unit(),t.name())
)
)
.distinct()
.collect(Collectors.toList())
))
).build();
return createUnitResponse(request, unit);
}

return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("units",
graph().getTeams().stream()
.map(g -> Team.parse(g.displayName))
.map(t ->
Map.of(
"name", t.unit(),
"url", request.getUri()+"/"+ t.unit()
)
)
.distinct()
.collect(Collectors.toList())
))
).build();
return createRootResponse(request);

} catch (JsonProcessingException e) {
context.getLogger().warning(e.getLocalizedMessage());
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getLocalizedMessage()).build();
Expand All @@ -145,6 +87,76 @@ public HttpResponseMessage v1(
}
}

private HttpResponseMessage createRootResponse(HttpRequestMessage<Optional<String>> request)
throws JsonProcessingException {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("units",
graph().getTeams().stream()
.map(g -> Team.parse(g.displayName))
.map(t ->
Map.of(
"name", t.unit(),
"url", request.getUri()+"/"+ t.unit()
)
)
.distinct()
.collect(Collectors.toList())
))
).build();
}

private HttpResponseMessage createUnitResponse(HttpRequestMessage<Optional<String>> request, String unit) throws JsonProcessingException {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("teams",
graph().getTeams(unit+"-").stream()
.map(g -> Team.parse(g.displayName))
.map(t ->
Map.of(
"name", t.name(),
"unit", t.unit(),
"url", format("%s/%s/%s", getPath(request), t.unit(),t.name())
)
)
.distinct()
.collect(Collectors.toList())
))
).build();
}

private HttpResponseMessage createTeamResponse(HttpRequestMessage<Optional<String>> request, String unit, String team) throws JsonProcessingException {
Map<String, Map<String,Object>> name2team = new HashMap<>();

for (Group group : graph().getTeams(unit+"-"+team)) {
Team t = Team.parse(group.displayName);
Map<String, Object> tmp = name2team.get(t.name());
if(tmp== null) {
tmp = Map.of(
"name", t.name(),
"unit", t.unit(),
"url", getPath(request)+"/"+t.name(),
"members" , loadAndMapMembers(group, t.internal())
);
name2team.put(t.name(), tmp);
} else {
@SuppressWarnings("unchecked")
List<Map<String, Object>> members = (List<Map<String, Object>>) tmp.get("members");
members.addAll(loadAndMapMembers(group, t.internal()));
}
}
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type","application/json; charset=UTF-8")
.body(
objectMapper.writeValueAsString(
Map.of("teams", name2team.values()))
).build();
}

private List<Map<String, Object>> loadAndMapMembers(Group group, boolean internal) {
return graph().getGroupMembers(group.id).stream().map(u-> mapUser(u,internal)).collect(Collectors.toList());
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/keyvault/secrets/quickstart/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.microsoft.graph.http.ICollectionResponse;
import com.microsoft.graph.models.DirectoryObject;
import com.microsoft.graph.models.Group;
import com.microsoft.graph.models.User;
import com.microsoft.graph.options.QueryOption;
import com.microsoft.graph.requests.GraphServiceClient;

Expand Down
10 changes: 3 additions & 7 deletions src/test/java/com/baloise/azure/DevServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -71,7 +70,7 @@ public ParameterMapping(Method method) {
public Object[] map(LinkedList<String> path, HttpExchange exg) {
return stream(parameters).map(p->{
if(p.getType().isAssignableFrom(ExecutionContextImpl.class)) {
return new ExecutionContextImpl(exg);
return new ExecutionContextImpl();
}
if(p.getType().isAssignableFrom(HttpRequestMessageImpl.class)) {
return new HttpRequestMessageImpl(exg);
Expand All @@ -94,6 +93,7 @@ public Object[] map(LinkedList<String> path, HttpExchange exg) {
private Map<Class<?>, Object> instanceMapping = new HashMap<>();
private Map<Method, ParameterMapping> parameterMappings = new HashMap<>();

@SuppressWarnings("unchecked")
public DevServer(Class<?> ... functionClasses) {
stream(functionClasses).distinct()
.map(clazz ->{
Expand All @@ -113,11 +113,6 @@ public DevServer(Class<?> ... functionClasses) {

private final class ExecutionContextImpl implements ExecutionContext {
private String invocationId = randomUUID().toString();
private HttpExchange exg;

public ExecutionContextImpl(HttpExchange exg) {
this.exg = exg;
}

@Override
public Logger getLogger() {
Expand All @@ -131,6 +126,7 @@ public String getInvocationId() {

@Override
public String getFunctionName() {
//TODO return name from annotation
return name;
}
}
Expand Down

0 comments on commit 4d5cfee

Please sign in to comment.