-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LITE-28130 Add capability to Install/Update product in CBC using PLM API
- Loading branch information
1 parent
bbe6dd2
commit 51e15a3
Showing
15 changed files
with
814 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
from requests import Response | ||
|
||
|
||
class ClientError(RuntimeError): | ||
def __init__( | ||
self, | ||
message: str, | ||
status_code: int = None, | ||
response: Response = None, | ||
cause: Exception = None, | ||
): | ||
self.message = message | ||
self.response = response | ||
self.status_code = status_code | ||
self.cause = cause |
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
Empty file.
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,37 @@ | ||
from connect_ext_ppr.models.cbc_extenstion import HubCredential | ||
|
||
from sqlalchemy import text | ||
|
||
|
||
HUB_CREDENTIAL_QUERY = ''' | ||
SELECT DISTINCT h.hub_id, | ||
g.app_instance_id, | ||
h.controller_uri, | ||
c.oauth_key, | ||
c.oauth_secret | ||
FROM hub_instances h | ||
INNER JOIN global_app_configuration g ON g.hub_uuid = h.extension_resource_uid | ||
INNER JOIN configuration c ON c.product_id = g.hub_uuid | ||
WHERE h.hub_id = :hub_id | ||
''' | ||
|
||
|
||
def get_hub_credentials(hub_id, db): | ||
|
||
query = text(HUB_CREDENTIAL_QUERY) | ||
|
||
query = query.columns( | ||
HubCredential.hub_id, | ||
HubCredential.app_id, | ||
HubCredential.controller_url, | ||
HubCredential.oauth_key, | ||
HubCredential.oauth_secret, | ||
) | ||
|
||
return db.query( | ||
HubCredential, | ||
).from_statement( | ||
query, | ||
).params( | ||
hub_id=hub_id, | ||
).first() |
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 functools import cached_property | ||
|
||
from connect_ext_ppr.client import CBCClient | ||
from connect_ext_ppr.client.exception import ClientError | ||
from connect_ext_ppr.models.cbc_extenstion import HubCredential | ||
|
||
|
||
class CBCService: | ||
|
||
PLM_TYPE = 'http://com.odin.platform/inhouse-products/application' | ||
SUBSCRIPTION_TYPE = 'http://parallels.com/aps/types/pa/subscription' | ||
|
||
def __init__(self, hub_credential: HubCredential, verify_certificate: bool = False): | ||
self.hub_credential = hub_credential | ||
self.__validate_hub_credentials_object() | ||
|
||
self.client = CBCClient( | ||
endpoint=hub_credential.controller_url, | ||
oauth_key=hub_credential.oauth_key, | ||
oauth_secret=hub_credential.oauth_secret, | ||
app_id=hub_credential.app_id, | ||
verify_certificate=verify_certificate, | ||
) | ||
self.__validate_client() | ||
|
||
def __validate_hub_credentials_object(self): | ||
if not self.hub_credential: | ||
raise ValueError('`hub_credential` must be not be empty.') | ||
if not isinstance(self.hub_credential, HubCredential): | ||
raise TypeError('`hub_credential` must be object of HubCredential.') | ||
if not self.hub_credential.controller_url: | ||
raise ValueError('`hub_credential.controller_url` must be not be empty.') | ||
if not self.hub_credential.oauth_key: | ||
raise ValueError('`hub_credential.oauth_key` must be not be empty.') | ||
if not self.hub_credential.oauth_secret: | ||
raise ValueError('`hub_credential.oauth_secret` must be not be empty.') | ||
|
||
def __validate_client(self): | ||
try: | ||
self.client.execute_request( | ||
method='GET', | ||
path=f'{self.hub_credential.controller_url}/aps', | ||
) | ||
except ClientError: | ||
raise ValueError('hub_credential are not valid!') | ||
|
||
@cached_property | ||
def primary_subscription_id(self): | ||
subscriptions = self.subscription_service.get( | ||
subscriptionId=1, | ||
) | ||
return subscriptions[0]['aps']['id'] | ||
|
||
@cached_property | ||
def plm_service(self): | ||
return self.client(self.PLM_TYPE) | ||
|
||
@cached_property | ||
def subscription_service(self): | ||
return self.client(self.SUBSCRIPTION_TYPE) | ||
|
||
def get_product_details(self, product_id: str): | ||
return self.plm_service.appDetails[product_id].get( | ||
fulfillmentSystem='connect', | ||
) | ||
|
||
def install_product(self, product_id: str): | ||
self.plm_service.appDetails[product_id].action( | ||
name='import', | ||
payload={ | ||
'subscriptionId': self.primary_subscription_id, | ||
'fulfillmentSystem': 'connect', | ||
}, | ||
) | ||
|
||
def update_product(self, product_id: str): | ||
return self.plm_service.appDetails[product_id].action( | ||
name='upgrade', | ||
payload={ | ||
'subscriptionId': self.primary_subscription_id, | ||
'fulfillmentSystem': 'connect', | ||
}, | ||
) |
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
Oops, something went wrong.