|
| 1 | +from cloudshell.workflow.orchestration.sandbox import Sandbox |
| 2 | +from cloudshell.api.cloudshell_api import AppConfiguration, ConfigParam |
| 3 | + |
| 4 | +TARGET_RESOURCE_BOOL_ATTRIBUTE = "Update Mail" |
| 5 | +APP_CONFIG_PARAM = "USER_MAIL" |
| 6 | + |
| 7 | + |
| 8 | +def configure_sandbox_owner_mail_on_app(sandbox, components=None): |
| 9 | + """ |
| 10 | + get user email and run configure apps on apps matching boolean attribute True on resource |
| 11 | + :param Sandbox sandbox: |
| 12 | + :param components: |
| 13 | + :return: |
| 14 | + """ |
| 15 | + api = sandbox.automation_api |
| 16 | + sb_id = sandbox.id |
| 17 | + sandbox_owner = sandbox.reservation_description.Owner |
| 18 | + owner_mail = api.GetUserDetails(sandbox_owner).Email |
| 19 | + if not owner_mail: |
| 20 | + raise ValueError("No email configured for user: {}".format(sandbox_owner)) |
| 21 | + |
| 22 | + # find apps with target attribute |
| 23 | + all_resources = api.GetReservationDetails(reservationId=sb_id, disableCache=True).ReservationDescription.Resources |
| 24 | + target_resources = [] |
| 25 | + for curr_resource in all_resources: |
| 26 | + details = api.GetResourceDetails(curr_resource.Name) |
| 27 | + attrs = details.ResourceAttributes |
| 28 | + attr_search = [x for x in attrs if x.Name == TARGET_RESOURCE_BOOL_ATTRIBUTE] |
| 29 | + if attr_search: |
| 30 | + attr_value = attr_search[0].Value |
| 31 | + if attr_value.lower() == "true": |
| 32 | + target_resources.append(curr_resource) |
| 33 | + if not target_resources: |
| 34 | + api.WriteMessageToReservationOutput(sb_id, "No target resources found to set mail on") |
| 35 | + return |
| 36 | + |
| 37 | + app_configurations = [] |
| 38 | + for resource in target_resources: |
| 39 | + app_configurations.append(AppConfiguration(AppName=resource.Name, |
| 40 | + ConfigParams=[ConfigParam(APP_CONFIG_PARAM, owner_mail)])) |
| 41 | + api.WriteMessageToReservationOutput(sb_id, "Configuring Apps with forwarded sandbox owner email: {}".format(sandbox_owner)) |
| 42 | + api.ConfigureApps(reservationId=sb_id, appConfigurations=app_configurations, printOutput=True) |
0 commit comments