Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
implement proper handling of appName for apps (#453)
Browse files Browse the repository at this point in the history
* implement proper handling of appName for apps

---------

Co-authored-by: Christian Lechner <[email protected]>
  • Loading branch information
rui1610 and lechnerc77 authored Mar 9, 2023
1 parent 2f927a6 commit 1796a45
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
62 changes: 57 additions & 5 deletions libs/python/btp_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
executeCommandsFromUsecaseFile,
runShellCommand,
runCommandAndGetJsonResult,
runCommandFlexAndGetJsonResult,
runShellCommandFlex,
login_btp,
login_cf,
Expand Down Expand Up @@ -1733,10 +1734,37 @@ def subscribe_app_to_subaccount(btpUsecase: BTPUSECASE, app, plan, parameters):
# (Optional) The subscription plan of the multitenant application. You can omit this parameter if the multitenant application is in the current global account.
message = message + " and plan >" + plan + "<"

log.info(message)
log.success(message)


def checkIfAppIsSubscribed(btpUsecase: BTPUSECASE, appName, appPlan):
# determine the "appName" for a "commercialAppName"
def getAppNameForCommercialAppName(btpUsecase: BTPUSECASE, commercialAppName: str):
result = None
accountMetadata = btpUsecase.accountMetadata
subaccountid = accountMetadata["subaccountid"]

command = (
"btp --format json list accounts/subscription --subaccount '"
+ subaccountid
+ "'"
)
resultCommand = runCommandFlexAndGetJsonResult(
btpUsecase, command, "INFO", "get appName for commercialAppName"
)

if (
resultCommand is not None
and len(resultCommand) > 0
and resultCommand.get("applications")
):
for entry in resultCommand.get("applications"):
if entry.get("commercialAppName") == commercialAppName:
result = str(entry.get("appName"))

return result


def checkIfAppIsSubscribed(btpUsecase: BTPUSECASE, commercialAppName, appPlan):
result = False
accountMetadata = btpUsecase.accountMetadata
subaccountid = accountMetadata["subaccountid"]
Expand All @@ -1745,7 +1773,7 @@ def checkIfAppIsSubscribed(btpUsecase: BTPUSECASE, appName, appPlan):
"btp --format json get accounts/subscription --subaccount '"
+ subaccountid
+ "' --of-app '"
+ appName
+ commercialAppName
+ "'"
)

Expand All @@ -1757,7 +1785,11 @@ def checkIfAppIsSubscribed(btpUsecase: BTPUSECASE, appName, appPlan):
btpUsecase, command, "INFO", "check if app already subscribed"
)

if "state" in resultCommand and resultCommand["state"] == "SUBSCRIBED":
if (
resultCommand is not None
and "state" in resultCommand
and resultCommand["state"] == "SUBSCRIBED"
):
result = True

return result
Expand Down Expand Up @@ -1804,7 +1836,27 @@ def initiateAppSubscriptions(btpUsecase: BTPUSECASE):

# Now do all the subscriptions
for appSubscription in btpUsecase.definedAppSubscriptions:
appName = appSubscription.name
commercialAppName = appSubscription.name
# Detect whether there is a difference between appName and commercialAppName
appName = getAppNameForCommercialAppName(btpUsecase, appSubscription.name)
# In case the appName and commercialAppName differ ...
if appName != commercialAppName:
log.success(
"appName for app subscription >"
+ commercialAppName
+ "< is called >"
+ appName
+ "<"
)
# ... use from here on the appName in the tooling (overwrite the configured name)
appSubscription.name = appName
else:
log.success(
"appName and commercialAppName are the same for >"
+ commercialAppName
+ "<"
)

appPlan = appSubscription.plan
parameters = appSubscription.parameters
if appSubscription.entitleonly is False:
Expand Down
9 changes: 9 additions & 0 deletions libs/python/helperCommandExecution.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ def checkIfCfEnvironmentIsDefined(btpUsecase):
return False


def runCommandFlexAndGetJsonResult(
btpUsecase, command, format, message, exitIfError: bool = True
):
p = runShellCommandFlex(btpUsecase, command, format, message, exitIfError, False)
list = p.stdout.decode()
list = convertStringToJson(list)
return list


def runCommandAndGetJsonResult(btpUsecase, command, format, message):
p = runShellCommand(btpUsecase, command, format, message)
list = p.stdout.decode()
Expand Down
6 changes: 4 additions & 2 deletions libs/python/helperJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ def dictToJson(dict):


def convertStringToJson(string):
jsonObject = json.loads(string)
return jsonObject
result = None
if string is not None and len(string) > 0:
result = json.loads(string)
return result


def addKeyValuePair(json, key, value):
Expand Down

0 comments on commit 1796a45

Please sign in to comment.