Skip to content
Permalink
Browse files
feat: add auth project api (#127)
  • Loading branch information
zyxxoo committed Jul 15, 2021
1 parent 37afb66 commit 8c2846088c4523a64fa0af5db29503e86a573864
Show file tree
Hide file tree
Showing 12 changed files with 498 additions and 7 deletions.
@@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-16.04
env:
TRAVIS_DIR: assembly/travis
COMMIT_ID: 9ee9fe12f3349a54bbceb161d002200d3689863c
COMMIT_ID: 379859a44102a76cf5f815
steps:
- name: Install JDK 8
uses: actions/setup-java@v2
@@ -5,7 +5,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.9.5</version>
<version>1.9.6</version>
<packaging>jar</packaging>

<name>hugegraph-client</name>
@@ -0,0 +1,94 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.api.auth;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.auth.Project;
import com.baidu.hugegraph.structure.constant.HugeType;
import com.google.common.collect.ImmutableMap;

public class ProjectAPI extends AuthAPI {

private static final String ACTION_ADD_GRAPH = "add_graph";
private static final String ACTION_REMOVE_GRAPH = "remove_graph";

public ProjectAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return HugeType.PROJECT.string();
}

public Project create(Project project) {
RestResult result = this.client.post(this.path(), project);
return result.readObject(Project.class);
}

public Project get(Object id) {
RestResult result = this.client.get(this.path(), formatEntityId(id));
return result.readObject(Project.class);
}

public List<Project> list(long limit) {
checkLimit(limit, "Limit");
Map<String, Object> params = ImmutableMap.of("limit", limit);
RestResult result = this.client.get(this.path(), params);
return result.readList(this.type(), Project.class);
}

public Project update(Project project) {
String id = formatEntityId(project.id());
RestResult result = this.client.put(this.path(), id, project);
return result.readObject(Project.class);
}

public void delete(Object id) {
this.client.delete(this.path(), formatEntityId(id));
}

public Project addGraphs(Object projectId, Set<String> graphs) {
Project project = new Project();
project.graphs(graphs);
RestResult result = this.client.put(this.path(),
formatEntityId(projectId),
project,
ImmutableMap.of("action",
ACTION_ADD_GRAPH));
return result.readObject(Project.class);
}

public Project removeGraphs(Object projectId, Set<String> graphs) {
Project project = new Project();
project.graphs(graphs);
RestResult result = this.client.put(this.path(),
formatEntityId(projectId),
project,
ImmutableMap.of("action",
ACTION_REMOVE_GRAPH));
return result.readObject(Project.class);
}
}
@@ -20,12 +20,16 @@
package com.baidu.hugegraph.driver;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.baidu.hugegraph.api.auth.AccessAPI;
import com.baidu.hugegraph.api.auth.BelongAPI;
import com.baidu.hugegraph.api.auth.GroupAPI;
import com.baidu.hugegraph.api.auth.LoginAPI;
import com.baidu.hugegraph.api.auth.LogoutAPI;
import com.baidu.hugegraph.api.auth.ProjectAPI;
import com.baidu.hugegraph.api.auth.TargetAPI;
import com.baidu.hugegraph.api.auth.TokenAPI;
import com.baidu.hugegraph.api.auth.UserAPI;
@@ -35,6 +39,7 @@
import com.baidu.hugegraph.structure.auth.Group;
import com.baidu.hugegraph.structure.auth.Login;
import com.baidu.hugegraph.structure.auth.LoginResult;
import com.baidu.hugegraph.structure.auth.Project;
import com.baidu.hugegraph.structure.auth.Target;
import com.baidu.hugegraph.structure.auth.TokenPayload;
import com.baidu.hugegraph.structure.auth.User;
@@ -47,6 +52,7 @@ public class AuthManager {
private final UserAPI userAPI;
private final AccessAPI accessAPI;
private final BelongAPI belongAPI;
private final ProjectAPI projectAPI;
private final LoginAPI loginAPI;
private final LogoutAPI logoutAPI;
private final TokenAPI tokenAPI;
@@ -57,6 +63,7 @@ public AuthManager(RestClient client, String graph) {
this.userAPI = new UserAPI(client, graph);
this.accessAPI = new AccessAPI(client, graph);
this.belongAPI = new BelongAPI(client, graph);
this.projectAPI = new ProjectAPI(client, graph);
this.loginAPI = new LoginAPI(client, graph);
this.logoutAPI = new LogoutAPI(client, graph);
this.tokenAPI = new TokenAPI(client, graph);
@@ -222,6 +229,45 @@ public void deleteAll() {
for (Target target : this.listTargets()) {
this.deleteTarget(target.id());
}
for (Project project : this.listProjects()) {
Set<String> graphs = project.graphs();
if (CollectionUtils.isNotEmpty(graphs)) {
this.projectRemoveGraphs(project.id(), graphs);
}
this.deleteProject(project.id());
}
}

public Project createProject(Project project) {
return this.projectAPI.create(project);
}

public Project getProject(Object id) {
return this.projectAPI.get(id);
}

public List<Project> listProjects() {
return this.listProject(-1);
}

public List<Project> listProject(int limit) {
return this.projectAPI.list(limit);
}

public Project updateProject(Project project) {
return this.projectAPI.update(project);
}

public Project projectAddGraphs(Object projectId, Set<String> graphs) {
return this.projectAPI.addGraphs(projectId, graphs);
}

public Project projectRemoveGraphs(Object projectId, Set<String> graphs) {
return this.projectAPI.removeGraphs(projectId, graphs);
}

public void deleteProject(Object id) {
this.projectAPI.delete(id);
}

public LoginResult login(Login login) {
@@ -97,7 +97,7 @@ private void initManagers(RestClient client, String graph) {
private void checkServerApiVersion() {
VersionUtil.Version apiVersion = VersionUtil.Version.of(
this.version.getApiVersion());
VersionUtil.check(apiVersion, "0.38", "0.64",
VersionUtil.check(apiVersion, "0.38", "0.65",
"hugegraph-api in server");
this.client.apiVersion(apiVersion);
}
@@ -34,7 +34,10 @@ public int hashCode() {

@Override
public boolean equals(Object other) {
if (!(other instanceof Element)) {
if (this == other) {
return true;
}
if (other == null || this.getClass() != other.getClass()) {
return false;
}
return Objects.equals(this.id(), ((Element) other).id());
@@ -57,6 +57,8 @@ public enum HugeResourceType {

USER_GROUP,

PROJECT,

TARGET,

METRICS,
@@ -0,0 +1,134 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.structure.auth;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.baidu.hugegraph.structure.constant.HugeType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Project extends AuthElement {

@JsonProperty("project_name")
private String name;
@JsonProperty("project_admin_group")
private String adminGroup;
@JsonProperty("project_op_group")
private String opGroup;
@JsonProperty("project_graphs")
private Set<String> graphs;
@JsonProperty("project_target")
private String target;
@JsonProperty("project_description")
private String description;

@JsonProperty("project_create")
@JsonFormat(pattern = DATE_FORMAT)
private Date create;
@JsonProperty("project_update")
@JsonFormat(pattern = DATE_FORMAT)
private Date update;
@JsonProperty("project_creator")
private String creator;

public Project() {
}

public Project(Object id) {
this(id, null, null);
}

public Project(String name) {
this(name, null);
}

public Project(String name, String description) {
this(null, name, description);
}

public Project(Object id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}

public String name() {
return this.name;
}

public void name(String name) {
this.name = name;
}

public String adminGroup() {
return this.adminGroup;
}

public String opGroup() {
return this.opGroup;
}

public Set<String> graphs() {
return this.graphs;
}

public void graphs(Set<String> graphs) {
if (graphs != null) {
this.graphs = new HashSet<>(graphs);
} else {
this.graphs = null;
}
}

public String target() {
return this.target;
}

public String description() {
return this.description;
}

public void description(String description) {
this.description = description;
}

@Override
public String type() {
return HugeType.PROJECT.string();
}

@Override
public Date createTime() {
return this.create;
}

@Override
public Date updateTime() {
return this.update;
}

@Override
public String creator() {
return this.creator;
}
}
@@ -33,9 +33,10 @@ public enum HugeType {
USER(52, "users"),
ACCESS(53, "accesses"),
BELONG(54, "belongs"),
LOGIN(55, "login"),
LOGOUT(56, "logout"),
TOKEN_VERIFY(57, "verify"),
PROJECT(55, "projects"),
LOGIN(56, "login"),
LOGOUT(57, "logout"),
TOKEN_VERIFY(58, "verify"),

// Data
VERTEX(101, "vertices"),
@@ -27,6 +27,7 @@
import com.baidu.hugegraph.api.auth.GroupApiTest;
import com.baidu.hugegraph.api.auth.LoginApiTest;
import com.baidu.hugegraph.api.auth.LogoutApiTest;
import com.baidu.hugegraph.api.auth.ProjectApiTest;
import com.baidu.hugegraph.api.auth.TargetApiTest;
import com.baidu.hugegraph.api.auth.TokenApiTest;
import com.baidu.hugegraph.api.auth.UserApiTest;
@@ -90,6 +91,7 @@
UserApiTest.class,
AccessApiTest.class,
BelongApiTest.class,
ProjectApiTest.class,
LoginApiTest.class,
LogoutApiTest.class,
TokenApiTest.class

0 comments on commit 8c28460

Please sign in to comment.