|
| 1 | +from cloudshell.workflow.orchestration.sandbox import Sandbox |
| 2 | +from cloudshell.api.cloudshell_api import CloudShellAPISession, ApiEditAppRequest, AppDetails, DefaultDeployment, Deployment, \ |
| 3 | + NameValuePair |
| 4 | + |
| 5 | + |
| 6 | +def edit_target_app_in_sandbox(app_name, new_app_name, api, sb_id, target_deployment_attrs): |
| 7 | + """ |
| 8 | + the target deployment attrs ignores namespacing and case sensitivity. |
| 9 | + This will work for the hdd and cpu attrs - [("hdd", "3"), ("cpu", "5")] |
| 10 | + :param str app_name: |
| 11 | + :param str new_app_name: |
| 12 | + :param CloudShellAPISession api: |
| 13 | + :param str sb_id: |
| 14 | + :param list target_deployment_attrs: example [("hdd", "3"), ("cpu", "5")] |
| 15 | + :return: |
| 16 | + """ |
| 17 | + target_deployment_attrs = target_deployment_attrs or [] |
| 18 | + # find target app to modify |
| 19 | + apps = api.GetReservationDetails(sb_id, disableCache=True).ReservationDescription.Apps |
| 20 | + if not apps: |
| 21 | + return |
| 22 | + target_app_search = [app for app in apps if app.Name == app_name] |
| 23 | + if not target_app_search: |
| 24 | + return |
| 25 | + target_app = target_app_search[0] |
| 26 | + |
| 27 | + # copy over logical resource attributes |
| 28 | + new_resource_attrs = [] |
| 29 | + for curr_attr in target_app.LogicalResource.Attributes: |
| 30 | + new_resource_attrs.append(NameValuePair(curr_attr.Name, curr_attr.Value)) |
| 31 | + |
| 32 | + default_deployment = [x for x in target_app.DeploymentPaths if x.IsDefault][0] |
| 33 | + |
| 34 | + # copy over all deployment attributes, modify target attributes |
| 35 | + new_deployment_attrs_map = {} |
| 36 | + for curr_attr in default_deployment.DeploymentService.Attributes: |
| 37 | + for update_attr_name, update_attr_value in target_deployment_attrs: |
| 38 | + if curr_attr.Name.lower().endswith(update_attr_name): |
| 39 | + new_deployment_attrs_map[curr_attr.Name] = update_attr_value |
| 40 | + break |
| 41 | + |
| 42 | + if curr_attr.Name not in new_deployment_attrs_map: |
| 43 | + new_deployment_attrs_map[curr_attr.Name] = curr_attr.Value |
| 44 | + |
| 45 | + # build out app edit request |
| 46 | + new_deployment_attrs_list = [NameValuePair(x[0], x[1]) for x in new_deployment_attrs_map.items()] |
| 47 | + new_deployment = Deployment(new_deployment_attrs_list) |
| 48 | + app_details = AppDetails(ModelName=target_app.LogicalResource.Model, Attributes=new_resource_attrs, |
| 49 | + Driver=target_app.LogicalResource.Driver) |
| 50 | + new_default_deployment = DefaultDeployment(Name=default_deployment.Name, Deployment=new_deployment) |
| 51 | + app_edit_requests = [ApiEditAppRequest(Name=app_name, |
| 52 | + NewName=new_app_name, |
| 53 | + Description="", |
| 54 | + AppDetails=app_details, |
| 55 | + DefaultDeployment=new_default_deployment)] |
| 56 | + api.EditAppsInReservation(reservationId=sb_id, editAppsRequests=app_edit_requests) |
| 57 | + |
| 58 | + |
| 59 | +def edit_apps_in_sandbox(sandbox, components): |
| 60 | + """ |
| 61 | +
|
| 62 | + :param Sandbox sandbox: |
| 63 | + :param components: |
| 64 | + :return: |
| 65 | + """ |
| 66 | + api = sandbox.automation_api |
| 67 | + sb_id = sandbox.id |
| 68 | + APP_NAME = "TEST" |
| 69 | + NEW_NAME = "NATTI" |
| 70 | + target_deployment_attrs = [("hdd", "3"), ("cpu", "5")] |
| 71 | + |
| 72 | + edit_target_app_in_sandbox(app_name=APP_NAME, |
| 73 | + new_app_name=NEW_NAME, |
| 74 | + api=api, |
| 75 | + sb_id=sb_id, |
| 76 | + target_deployment_attrs=target_deployment_attrs) |
0 commit comments