Skip to content

Commit

Permalink
Merge pull request #105 from ImMin5/master
Browse files Browse the repository at this point in the history
Add APIKey apis
  • Loading branch information
ImMin5 authored Dec 5, 2023
2 parents a025758 + d9386e8 commit 6059722
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/spaceone/identity/manager/api_key_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def _rollback(api_key_vo):
api_key = key_gen.generate_api_key(api_key_vo.api_key_id)
return api_key_vo, api_key

def update_api_key_by_vo(self, params: dict, api_key_vo: APIKey) -> APIKey:
def _rollback(old_data):
_LOGGER.info(
f"[update_api_key_by_vo._rollback] Revert Data : {old_data['api_key_id']}"
)
api_key_vo.update(old_data)

self.transaction.add_rollback(_rollback, api_key_vo.to_dict())

return api_key_vo.update(params)

def delete_api_key(self, api_key_id, domain_id):
api_key_vo = self.get_api_key(api_key_id, domain_id)
api_key_vo.delete()
Expand All @@ -45,24 +56,22 @@ def delete_api_key(self, api_key_id, domain_id):
def delete_api_key_by_vo(api_key_vo: APIKey) -> None:
api_key_vo.delete()

def enable_api_key(self, api_key_id, domain_id):
def enable_api_key(self, api_key_vo: APIKey) -> APIKey:
def _rollback(old_data):
_LOGGER.info(f"[enable_api_key._rollback] Revert Data: {old_data}")
api_key_vo.update(old_data)

api_key_vo: APIKey = self.get_api_key(api_key_id, domain_id)
if api_key_vo.state != "ENABLED":
self.transaction.add_rollback(_rollback, api_key_vo.to_dict())
api_key_vo.update({"state": "ENABLED"})

return api_key_vo

def disable_api_key(self, api_key_id, domain_id):
def disable_api_key(self, api_key_vo: APIKey) -> APIKey:
def _rollback(old_data):
_LOGGER.info(f"[disable_api_key._rollback] Revert Data: {old_data}")
api_key_vo.update(old_data)

api_key_vo: APIKey = self.get_api_key(api_key_id, domain_id)
if api_key_vo.state != "DISABLED":
self.transaction.add_rollback(_rollback, api_key_vo.to_dict())
api_key_vo.update({"state": "DISABLED"})
Expand Down
51 changes: 47 additions & 4 deletions src/spaceone/identity/service/api_key_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,50 @@ def create(self, params: APIKeyCreateRequest) -> Union[APIKeyResponse, dict]:
@transaction
@convert_model
def update(self, params: APIKeyUpdateRequest) -> Union[APIKeyResponse, dict]:
return {}
"""Update API Key
Args:
params (dict): {
'api_key_id': 'str', # required
'name': 'str',
'domain_id': 'str' # required
}
Returns:
APIKeyResponse:
"""
api_key_vo = self.api_key_mgr.get_api_key(params.api_key_id, params.domain_id)
api_key_vo = self.api_key_mgr.update_api_key_by_vo(params.dict(), api_key_vo)
return APIKeyResponse(**api_key_vo.to_dict())

@transaction
@convert_model
def enable(self, params: APIKeyEnableRequest) -> Union[APIKeyResponse, dict]:
return {}
"""Enable API Key
Args:
params (dict): {
'api_key_id': 'str', # required
'domain_id': 'str' # required
}
"""
api_key_vo = self.api_key_mgr.get_api_key(params.api_key_id, params.domain_id)
api_key_vo = self.api_key_mgr.enable_api_key(api_key_vo)
return APIKeyResponse(**api_key_vo.to_dict())

@transaction
@convert_model
def disable(self, params: APIKeyDisableRequest) -> Union[APIKeyResponse, dict]:
return {}
"""Disable API Key
Args:
params (dict): {
'api_key_id': 'str', # required
'domain_id': 'str' # required
}
Returns:
APIKeyResponse:
"""

api_key_vo = self.api_key_mgr.get_api_key(params.api_key_id, params.domain_id)
api_key_vo = self.api_key_mgr.disable_api_key(api_key_vo)
return APIKeyResponse(**api_key_vo.to_dict())

@transaction
@convert_model
Expand Down Expand Up @@ -116,7 +149,17 @@ def list(self, params: APIKeySearchQueryRequest) -> Union[APIKeysResponse, dict]
@transaction
@convert_model
def stat(self, params: APIKeyStatQueryRequest) -> dict:
return {}
"""Stat API Keys
Args:
params (dict): {
'query': 'dict',
'domain_id': 'str'
}
Returns:
dict:
"""
query = params.query or {}
return self.api_key_mgr.stat_api_keys(query)

@staticmethod
def _get_expired_at(expired_at: datetime) -> datetime:
Expand Down

0 comments on commit 6059722

Please sign in to comment.