def _extract_parameters_JSON(reservation_id):
    inputs_desc = helpers.get_api_session() \
        .GetReservationInputs(reservation_id)
    global_inputs_JSON = _extract_global_inputs_JSON(inputs_desc)
    required_inputs_JSON = _extract_required_inputs_JSON(inputs_desc)
    additional_inputs_JSON = _extract_addition_inputs_JSON(inputs_desc)
    return PARAMETERS_TEMPLATE.format(
        requirement_template=required_inputs_JSON,
        additional_info_template=additional_inputs_JSON,
        global_template=global_inputs_JSON)
def _get_resource_context(resource_name):
    r = helpers.get_api_session().GetResourceDetails(resource_name)
    attributes = []
    for attribute in r.ResourceAttributes:
        attribute_json = RESOURCE_ATTRIBUTES_TEMPLATE.format(
            name=attribute.Name, value=attribute.Value)
        attributes.append(attribute_json)
    resource_details = RESOURCEDETAILS_TEMPLATE.\
        format(name=r.Name, address=r.Address,
               model=r.ResourceModelName, family=r.ResourceFamilyName,
               description=r.Description, fullname=r.Name,
               attributes='{'+string.join(attributes, sep=',') + '}')
    return '{' + resource_details + '}'
def _bootstrap_data(user,
                    password,
                    domain,
                    reservation_id,
                    server_address='localhost',
                    cloudshell_api_port='8028',
                    command_parameters={},
                    resource_name=None,
                    service_name=None):
    """
    This helper is intended to make it easier to test scripts. When a script
    gets executed by CloudShell, the Execution Server sets up several variables
    as a context for the execution.
    This function simulates the same conditions so the script can be tested
    offline.

    :param str user: The user the driver should authenticate as
    :param str passwrd: Password for the specified user
    :param str reservation_id: The reservation the driver should attach to
    :param str server_address: The address of the CloudShell server  (default to localhost)
    :param str cloudshell_api_port: The API port to use (default 8028)
    :param dict[str,str] command_parameters: user parameters passed to this command
    :param str resource_name: For resource commands only specify the name of the resource
    """

    if is_dev_mode():
        # We assume that if the env. variable doesn't exist we need to bootstrap
        quali_connectivity = CONNECTIVITY_DETAILS_TEMPLATE.\
            format(cloudshell_api_port=cloudshell_api_port,
                   server_address=server_address, password=password,
                   user=user)

        # Creat an initial template for reservation details just to get more info
        reservation_details = RESERVATIONDETAILS_TEMPLATE.\
            format(id=reservation_id, domain='Global',
                   description='', environment_name='',
                   environment_path='', parameters_template='[]',
                   owner_user=user, owner_pass=password)

        os.environ['qualiConnectivityContext'] = '{' + quali_connectivity + '}'
        os.environ['reservationContext'] = '{' + reservation_details + '}'

        parameters_details = _extract_parameters_JSON(reservation_id)

        reservation_desc = helpers.get_api_session() \
                                        .GetReservationDetails(reservation_id) \
                                        .ReservationDescription

        reservation_details = RESERVATIONDETAILS_TEMPLATE.\
            format(id=reservation_id, domain=reservation_desc.DomainName,
                   description='',
                   parameters_template='{' + parameters_details + '}',
                   environmment_path=reservation_desc.Name,
                   environment_name='',
                   owner_user=user, owner_pass=password)

        # Update the reservation details again with the full info
        os.environ['reservationContext'] = '{' + reservation_details + '}'
        for parameter in command_parameters:
            os.environ[parameter.upper()] = command_parameters[parameter]
        if resource_name is not None:
            os.environ['resourceContext'] = \
                _get_resource_context(resource_name)
        if service_name is not None:
            os.environ['resourceContext'] = \
                _get_service_context(reservation_desc,service_name)