-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add create app with appagent cred
- Loading branch information
1 parent
cfb6051
commit c76bc37
Showing
10 changed files
with
275 additions
and
7 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
83 changes: 83 additions & 0 deletions
83
indykite_sdk/config/create_application_with_agent_credentials.py
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,83 @@ | ||
from indykite_sdk.model.create_application import CreateApplication | ||
from indykite_sdk.model.create_application_agent import CreateApplicationAgent | ||
from indykite_sdk.model.register_application_agent_credential import RegisterApplicationAgentCredential | ||
from indykite_sdk.model.key_type import KeyType | ||
import sys | ||
import indykite_sdk.utils.logger as logger | ||
from indykite_sdk.config import helper | ||
from datetime import datetime | ||
|
||
|
||
def create_application_with_agent_credentials(self, | ||
app_space_id, | ||
default_tenant_id, | ||
application_name, | ||
application_agent_name, | ||
application_agent_credentials_name, | ||
public_key_type, | ||
public_key=None, | ||
expire_time=None): | ||
""" | ||
create application, application agent and application credentials for a tenant in an appSpace | ||
:param self: | ||
:param app_space_id: string | ||
:param default_tenant_id: string | ||
:param application_name: string | ||
:param application_agent_name: string | ||
:param application_agent_credentials_name: string | ||
:param public_key_type: jwk | pem | ||
:param public_key: bytes | None | ||
:param expire_time: int (number of seconds from now) | None | ||
:return: | ||
""" | ||
sys.excepthook = logger.handle_excepthook | ||
try: | ||
application_name_id = helper.change_display_to_name(str(application_name)) | ||
response_application = self.create_application(app_space_id, | ||
application_name_id, | ||
str(application_name), | ||
str(application_name), | ||
[]) | ||
if isinstance(response_application, CreateApplication): | ||
application_agent_name_id = helper.change_display_to_name(str(application_agent_name)) | ||
response_application_agent = self.create_application_agent(response_application.id, | ||
application_agent_name_id, | ||
str(application_agent_name), | ||
str(application_agent_name), | ||
[]) | ||
if isinstance(response_application_agent, CreateApplicationAgent): | ||
key_types = [k.value for k in KeyType] | ||
if public_key_type not in key_types: | ||
raise TypeError("public_key_type must be a member of KeyType: jwk, pem") | ||
|
||
response_application_agent_credentials = None | ||
t = datetime.now().timestamp() | ||
if expire_time: | ||
expire_time = int(t) + int(expire_time) | ||
else: | ||
expire_time = int(t) + 3600 | ||
if public_key_type == 'jwk': | ||
response_application_agent_credentials = self.register_application_agent_credential_jwk( | ||
response_application_agent.id, | ||
application_agent_credentials_name, | ||
public_key, | ||
expire_time, | ||
str(default_tenant_id), | ||
[]) | ||
elif public_key_type == 'pem': | ||
response_application_agent_credentials = self.register_application_agent_credential_pem( | ||
response_application_agent.id, | ||
application_agent_credentials_name, | ||
public_key, | ||
expire_time, | ||
str(default_tenant_id), | ||
[]) | ||
if isinstance(response_application_agent_credentials, RegisterApplicationAgentCredential): | ||
response = {"response_application": response_application, | ||
"response_application_agent": response_application_agent, | ||
"response_application_agent_credentials": response_application_agent_credentials} | ||
return response | ||
return None | ||
|
||
except Exception as exception: | ||
return logger.logger_error(exception) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from enum import Enum | ||
|
||
|
||
class KeyType(Enum): | ||
JWK = 'jwk' | ||
PEM = 'pem' |
105 changes: 105 additions & 0 deletions
105
tests/test_create_application_with_agent_credentials.py
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,105 @@ | ||
import time | ||
|
||
from indykite_sdk.config import ConfigClient | ||
from indykite_sdk.model.create_application import CreateApplication | ||
from indykite_sdk.model.create_application_agent import CreateApplicationAgent | ||
from indykite_sdk.model.register_application_agent_credential import RegisterApplicationAgentCredential | ||
from helpers import data | ||
|
||
|
||
def test_create_application_with_agent_cred_success(capsys): | ||
client = ConfigClient() | ||
assert client is not None | ||
|
||
app_space_id = data.get_app_space_id() | ||
default_tenant_id = data.get_tenant_id() | ||
right_now = int(time.time()) | ||
application_name = "automation-application-"+str(right_now) | ||
application_agent_name = "automation-agent-"+str(right_now) | ||
application_agent_credentials_name = "automation-agent-cred-"+str(right_now) | ||
public_key_type = "jwk" | ||
expire_time = right_now + 120 | ||
|
||
response = client.create_application_with_agent_credentials( | ||
app_space_id, | ||
default_tenant_id, | ||
application_name, | ||
application_agent_name, | ||
application_agent_credentials_name, | ||
public_key_type, | ||
public_key=None, | ||
expire_time=expire_time) | ||
captured = capsys.readouterr() | ||
|
||
assert response is not None | ||
assert isinstance(response["response_application"], CreateApplication) | ||
assert isinstance(response["response_application_agent"], CreateApplicationAgent) | ||
assert isinstance(response["response_application_agent_credentials"], RegisterApplicationAgentCredential) | ||
|
||
|
||
def test_create_application_with_agent_cred_exists(capsys): | ||
client = ConfigClient() | ||
assert client is not None | ||
|
||
client = ConfigClient() | ||
assert client is not None | ||
|
||
app_space_id = data.get_app_space_id() | ||
default_tenant_id = data.get_tenant_id() | ||
right_now = int(time.time()) | ||
application_name = "automation-application-" + str(right_now) | ||
application_agent_name = "automation-agent-" + str(right_now) | ||
application_agent_credentials_name = "automation-agent-cred-" + str(right_now) | ||
public_key_type = "jwk" | ||
expire_time = right_now + 120 | ||
|
||
response = client.create_application_with_agent_credentials( | ||
app_space_id, | ||
default_tenant_id, | ||
application_name, | ||
application_agent_name, | ||
application_agent_credentials_name, | ||
public_key_type, | ||
public_key=None, | ||
expire_time=expire_time) | ||
|
||
response2 = client.create_application_with_agent_credentials( | ||
app_space_id, | ||
default_tenant_id, | ||
application_name, | ||
application_agent_name, | ||
application_agent_credentials_name, | ||
public_key_type, | ||
public_key=None, | ||
expire_time=expire_time) | ||
captured = capsys.readouterr() | ||
assert "config entity with given name already exist" in captured.err | ||
|
||
|
||
def test_create_application_with_agent_cred_no_expire_time(capsys): | ||
client = ConfigClient() | ||
assert client is not None | ||
|
||
app_space_id = data.get_app_space_id() | ||
default_tenant_id = data.get_tenant_id() | ||
right_now = int(time.time()) | ||
application_name = "automation-application-"+str(right_now) | ||
application_agent_name = "automation-agent-"+str(right_now) | ||
application_agent_credentials_name = "automation-agent-cred-"+str(right_now) | ||
public_key_type = "pem" | ||
|
||
response = client.create_application_with_agent_credentials( | ||
app_space_id, | ||
default_tenant_id, | ||
application_name, | ||
application_agent_name, | ||
application_agent_credentials_name, | ||
public_key_type, | ||
public_key=None, | ||
expire_time=None) | ||
captured = capsys.readouterr() | ||
|
||
assert response is not None | ||
assert isinstance(response["response_application"], CreateApplication) | ||
assert isinstance(response["response_application_agent"], CreateApplicationAgent) | ||
assert isinstance(response["response_application_agent_credentials"], RegisterApplicationAgentCredential) |