Skip to content

Commit

Permalink
Merge pull request #37 from BiznetGIO/api
Browse files Browse the repository at this point in the history
Bump version to 0.2.2
  • Loading branch information
anak10thn authored Feb 12, 2020
2 parents 56dc187 + 289ccb0 commit 0c9a24c
Show file tree
Hide file tree
Showing 37 changed files with 974 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: python
python:
- '3.7'
install:
- 'pip install -r requirements.txt'
- 'pip install -r obs/api/requirements.txt'
- 'pip install -r requirements-dev.txt'
- 'pip install -e .'
script:
Expand Down
12 changes: 12 additions & 0 deletions obs/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.7-alpine

RUN mkdir /app
WORKDIR /app
COPY obs/api/requirements.txt /app

RUN pip3 install -r requirements.txt \
&& pip3 install gunicorn
EXPOSE 5000

COPY . /app
WORKDIR /app/obs/api
21 changes: 21 additions & 0 deletions obs/api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import sys

sys.path.append(os.path.abspath("../../"))

from flask import Flask
from flask_cors import CORS
from . import configs


def create_app():
app = Flask(__name__)
app.config.from_object(configs.Config)

CORS(app, resources={r"/api/*": {"origins": "*"}})

from .controllers import api_blueprint

app.register_blueprint(api_blueprint)

return app
11 changes: 11 additions & 0 deletions obs/api/app/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
from dotenv import load_dotenv
from app.libs import MetaFlaskEnv

APP_ROOT = os.path.join(os.path.dirname(__file__), "../..")
dotenv_path = os.path.join(APP_ROOT, ".env")
load_dotenv(dotenv_path)


class Config(MetaFlaskEnv):
ENV_PREFIX = "FLASK_"
1 change: 1 addition & 0 deletions obs/api/app/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .api import api_blueprint
25 changes: 25 additions & 0 deletions obs/api/app/controllers/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from flask import Blueprint
from flask_restful import Api
from .bucket import *
from .admin import *

api_blueprint = Blueprint("api", __name__, url_prefix="/api")
api = Api(api_blueprint)

api.add_resource(bucket_api, "/storage/bucket/<bucket_name>")
api.add_resource(object_api, "/storage/object/<bucket_name>")
api.add_resource(download_object, "/storage/object/download/<bucket_name>")
api.add_resource(upload_object, "/storage/object/upload/<bucket_name>")
api.add_resource(move_object, "/storage/object/move/<bucket_name>")
api.add_resource(copy_object, "/storage/object/copy/<bucket_name>")
api.add_resource(presign, "/storage/presign/<bucket_name>/<object_name>")
api.add_resource(mkdir, "/storage/mkdir/<bucket_name>")
api.add_resource(acl, "/storage/acl")
api.add_resource(list, "/storage/list")
api.add_resource(usage, "/storage/usage")
api.add_resource(gmt_policy, "/storage/gmt")

api.add_resource(user_api, "/admin/user")
api.add_resource(qos_api, "/admin/qos")
api.add_resource(cred_api, "/admin/cred")
api.add_resource(user_usage, "/admin/usage")
260 changes: 260 additions & 0 deletions obs/api/app/controllers/api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
from obs.libs import qos
from obs.libs import user
from obs.libs import credential
from obs.libs import auth as client
from obs.libs import admin as admin_usage
from distutils.util import strtobool
from app.helpers.rest import response
from flask_restful import Resource, reqparse


def get_client():
return client.admin_client()


class user_api(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("user_type", type=str)
parser.add_argument("user_status", type=str)
parser.add_argument("limit", type=str)
parser.add_argument("userId", type=str)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
if args["userId"]:
users = user.info(get_client(), args["userId"], args["groupId"])
if "reason" in users:
return response(users["status_code"], message=users["reason"])
return response(200, data=users)

user_type = args["user_type"] if args["user_type"] else "all"
user_status = args["user_status"] if args["user_status"] else "active"
limit = args["limit"] if args["limit"] else ""

user_list = user.list_user(
get_client(), args["groupId"], user_type, user_status, limit
)
if "reason" in user_list:
return response(list["status_code"], message=list["reason"])

return response(200, data=user_list)
except Exception as exc:
return response(500, str(exc))

def post(self):
options = {
"userId": "",
"groupId": "",
"userType": "User",
"fullName": "",
"emailAddr": "",
"address1": "",
"city": "",
"state": "",
"zip": "",
"country": "",
"phone": "",
"website": "",
"active": True,
"ldapEnabled": False,
}

parser = reqparse.RequestParser()
for index, option in options.items():
if index in ["userId", "groupId", "fullName"]:
parser.add_argument(index, type=str, required=True)
parser.add_argument(index, type=str)
parser.add_argument("quotaLimit", type=int)
args = parser.parse_args()

try:
for index, option in options.items():
if args[index] not in (option, None):
options[index] = args[index]
status = user.create(get_client(), options)
if args["quotaLimit"]:
status = qos.set(
get_client(), args["userId"], args["groupId"], args["quotaLimit"]
)
if "reason" in status:
return response(status["status_code"], message=status["reason"])
else:
return response(201)
except Exception as exc:
return response(500, str(exc))

def put(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
parser.add_argument("suspend", type=str, required=True)
args = parser.parse_args()

try:
msg = "suspended" if args["suspend"] == "true" else "unsuspended"
users = user.info(get_client(), args["userId"], args["groupId"])
if users["active"] == f"{not strtobool(args['suspend'])}".lower():
return response(400, f"User already {msg}")

# canocicalUserId can't be included when updating user
del users["canonicalUserId"]
users["active"] = not strtobool(args["suspend"])
status = user.update(get_client(), users)
if "reason" in status:
return response(status["status_code"], message=status["reason"])

message = f"User has been {msg}"
return response(200, message)
except Exception as exc:
response(500, str(exc))

def delete(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
status = user.remove(get_client(), args["userId"], args["groupId"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(204)
except Exception as exc:
return response(500, str(exc))


class qos_api(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
infos = qos.info(get_client(), args["userId"], args["groupId"])
infos["Storage Limit"] = (
infos["qosLimitList"][0]["value"] * 1024
if infos["qosLimitList"][0]["value"] != -1
else "unlimited"
)

if "reason" in infos:
return response(infos["status_code"], message=infos["reason"])

return response(200, data=infos)
except Exception as exc:
return response(500, str(exc))

def post(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
parser.add_argument("limit", type=int, required=True)
args = parser.parse_args()

try:
status = qos.set(
get_client(), args["userId"], args["groupId"], args["limit"]
)
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(201)
except Exception as exc:
return response(500, str(exc))

def delete(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
status = qos.rm(get_client(), args["userId"], args["groupId"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(204)
except Exception as exc:
return response(500, str(exc))


class cred_api(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
cred_list = credential.list(get_client(), args["userId"], args["groupId"])
if "reason" in cred_list:
return response(list["status_code"], message=list["reason"])

return response(200, data=cred_list)
except Exception as exc:
return response(500, str(exc))

def post(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
status = credential.create(get_client(), args["userId"], args["groupId"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(201)
except Exception as exc:
return response(500, str(exc))

def put(self):
parser = reqparse.RequestParser()
parser.add_argument("access_key", type=str, required=True)
parser.add_argument("status", type=str, required=True)
args = parser.parse_args()

try:
status = credential.status(get_client(), args["access_key"], args["status"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(204)
except Exception as exc:
return response(500, str(exc))

def delete(self):
parser = reqparse.RequestParser()
parser.add_argument("access_key", type=str, required=True)
args = parser.parse_args()

try:
status = credential.rm(get_client(), args["access_key"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(204)
except Exception as exc:
return response(500, str(exc))


class user_usage(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("userId", type=str, required=True)
parser.add_argument("groupId", type=str, required=True)
args = parser.parse_args()

try:
status = admin_usage.usage(get_client(), args["userId"], args["groupId"])
if "reason" in status:
return response(status["status_code"], message=status["reason"])

return response(200, data=status[0])
except Exception as exc:
return response(500, str(exc))
Loading

0 comments on commit 0c9a24c

Please sign in to comment.