def build_solution(sol):
    sol_dict = {
        'id': sol.id,
        'name': sol.name,
        'description': sol.description,
        'businessUnit': sol.businessUnit,
        'costCentre': sol.costCentre,
        'ci': sol.ci,
        'cd': sol.cd,
        'sourceControl': sol.sourceControl,
        'environments': ModelTools.load_json_array(sol.environments),
        'active': sol.active,
        'favourite': sol.favourite,
        'teams': sol.teams,
        'lastUpdated': ModelTools.datetime_as_string(sol.lastUpdated)
    }

    apps = Application.query.filter(Application.solutionId == sol.id).all()
    app_arr = []
    if apps is not None:
        for ap in apps:
            app_dict = application_extension.build_application(ap)
            app_arr.append(app_dict)

    sol_dict['applications'] = app_arr
    return sol_dict
Example #2
0
def check_credentials(login_details):
    """
    Responds to a request for /api/login.
    :return:        json string of user details
    """

    app.logger.info("Login Details: {}".format(pformat(ModelTools.redact_dict(login_details))))

    username = login_details['username']
    password = login_details['password']

    if (username == '*****@*****.**' and password == 'pass1'):
      user = User.query.filter(User.email == username).one_or_none()
      schema = ExtendedUserSchema(many=False)
      if user is not None:
        app.logger.debug('LOGIN accepted!');
        schema = ExtendedUserSchema(many=False)
        data = schema.dump(user)
        return data, 200

    if (username == '*****@*****.**' and password == 'pass2'):
      user = User.query.filter(User.email == username).one_or_none()
      if user is not None:
        app.logger.debug('LOGIN accepted!');
        schema = ExtendedUserSchema(many=False)
        data = schema.dump(user)
        return data, 200

    app.logger.warning('LOGIN FAILED!');
    abort(
      401, "Unauthorised! {}".format(ModelTools.redact_dict(login_details))
    )
def build_activator(act):
    act_dict = {
        'id': act.id,
        'name': act.name,
        'type': act.type,
        'available': act.available,
        'sensitivity': act.sensitivity,
        'category': act.category,
        'envs': ModelTools.load_json_array(act.envs),
        'platforms': ModelTools.load_json_array(act.platforms),
        'lastUpdated': ModelTools.datetime_as_string(act.lastUpdated),
        'userCapacity': act.userCapacity,
        'serverCapacity': act.serverCapacity,
        'regions': ModelTools.load_json_array(act.regions),
        'hosting': ModelTools.load_json_array(act.hosting),
        'apiManagement': ModelTools.load_json_array(act.apiManagement),
        'ci': ModelTools.load_json_array(act.ci),
        'cd': ModelTools.load_json_array(act.cd),
        'sourceControl': ModelTools.load_json_array(act.sourceControl),
        'businessUnit': act.businessUnit,
        'technologyOwner': act.technologyOwner,
        'technologyOwnerEmail': act.technologyOwnerEmail,
        'billing': act.billing,
        'activator': act.activator,
        'resources': ModelTools.load_json_array(act.resources),
        'status': act.status,
        'description': act.description,
        'accessRequestedBy': act.accessRequestedBy,
        'source': act.source,
        'activatorLink': act.activatorLink
    }
    act_dict['accessRequestedBy'] = user_extension.build_user(
        act.accessRequestedBy)

    return act_dict
Example #4
0
def deployment_update(oid, solutionDeploymentDetails):
    """
    Updates an existing solutions in the solutions list with the deployed status.

    :param key:    id of the solution
    :param solutionDetails:   solution details to update
    :return:       updated solution
    """

    app.logger.debug(solutionDeploymentDetails)

    # Does the solutions exist in solutions list?
    existing_solution = Solution.query.filter(Solution.id == oid).one_or_none()

    # Does solutions exist?

    if existing_solution is not None:
        schema = SolutionSchema()
        update_solution = schema.load(solutionDeploymentDetails,
                                      session=db.session)
        update_solution.key = solutionDeploymentDetails['id']
        update_solution.lastUpdated = ModelTools.get_utc_timestamp()
        update_solution.deployed = solutionDeploymentDetails['deployed']

        db.session.merge(update_solution)
        db.session.commit()

        # return the updted solutions in the response
        data = schema.dump(update_solution)
        return data, 200

    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"Solution {oid} not found")
Example #5
0
def create(activator):
    """
    This function creates a new activator in the activator list
    based on the passed in activator data

    :param activator:  activator to create in activator list
    :return:        201 on success, 406 on activator exists
    """
    id = activator.get("id", None)

    # Does the activators exist already?
    existing_activator = (Activator.query.filter(
        Activator.id == id).one_or_none())

    if existing_activator is None:
        schema = ActivatorSchema()
        new_activator = schema.load(activator, session=db.session)
        new_activator.lastUpdated = ModelTools.get_utc_timestamp()
        db.session.add(new_activator)
        db.session.commit()

        # Serialize and return the newly created deployment
        # in the response
        data = schema.dump(new_activator)
        return data, 201

    # Otherwise, it already exists, that's an error
    else:
        abort(406, f"Activator id {id} already exists")
Example #6
0
def create(applicationDetails):
    """
    This function creates a new application in the application structure
    based on the passed in application data

    :param application:  application to create in application list
    :return:             201 on success, 406 on application exists
    """

    # Remove id as it's created automatically
    if 'id' in applicationDetails:
        del applicationDetails['id']

    # As discussed with Fabio, explicitly set resources to '[]' if not set
    if not type(applicationDetails.get('resources')) is list:
        applicationDetails['resources'] = []
    applicationDetails['resources'] = json.dumps(
        applicationDetails.get('resources'))

    schema = ApplicationSchema()
    new_application = schema.load(applicationDetails, session=db.session)
    new_application.lastUpdated = ModelTools.get_utc_timestamp()
    db.session.add(new_application)
    db.session.commit()

    # Serialize and return the newly created application
    # in the response
    new_application.resources = json.loads(new_application.resources)
    schema = ExtendedApplicationSchema()
    data = schema.dump(new_application)
    app.logger.debug("application data:")
    app.logger.debug(pformat(data))
    return data, 201
Example #7
0
def setActivatorStatus(activator):
    " update the activator status"

    app.logger.info(pformat(activator))
    # Does the activator to delete exist?
    existing_activator = Activator.query.filter(
        Activator.id == activator['id']).one_or_none()

    # if found?
    if existing_activator is not None:
        existing_activator.status = activator.get('status',
                                                  existing_activator.status)
        existing_activator.accessRequestedBy = activator.get(
            'accessRequestedBy', existing_activator.accessRequestedBy)
        existing_activator.lastUpdated = ModelTools.get_utc_timestamp()

        db.session.merge(existing_activator)
        db.session.commit()

        activator = activator_extension.build_activator(existing_activator)
        activator_schema = ExtendedActivatorSchema()
        data = activator_schema.dump(activator)
        return data, 200

    # Otherwise, nope, activator to update was not found
    else:
        id = activator['id']
        abort(404, f"Activator id {id} not found")
Example #8
0
def update(id, solution):
    """
    This function updates an existing solutions in the solutions list

    :param key:    key of the solutions to update in the solutions list
    :param solutions:   solutions to update
    :return:       updated solutions
    """

    app.logger.debug(solution)

    # Does the solutions exist in solutions list?
    existing_solution = Solution.query.filter(Solution.id == id).one_or_none()

    # Does solutions exist?

    if existing_solution is not None:
        schema = SolutionSchema()
        update_solution = schema.load(solution, session=db.session)
        update_solution.key = solution['id']
        update_solution.lastUpdated = ModelTools.get_utc_timestamp()

        db.session.merge(update_solution)
        db.session.commit()

        # return the updted solutions in the response
        data = schema.dump(update_solution)
        return data, 200

    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"Solution not found")
Example #9
0
def update(id, application):
    """
    This function updates an existing application in the application list

    :param id: id of the application to update in the application list
    :param application:   application to update
    :return: updated application
    """

    app.logger.debug("application: ")
    app.logger.debug(pformat(application))

    # Does the application exist in applications?
    existing_application = Application.query.filter(Application.id == id).one_or_none()

    # Does application exist?
    if existing_application is not None:
        application['lastUpdated'] = ModelTools.get_utc_timestamp()
        Application.query.filter(Application.id == id).update(application)
        db.session.commit()

        # return the updated application in the response
        schema = ApplicationSchema()
        data = schema.dump(existing_application)
        return data, 200

    # otherwise, nope, application doesn't exist, so that's an error
    else:
        abort(404, f"Application not found")
Example #10
0
def create(activatorDetails):
    """
    This function creates a new activator in the activator list
    based on the passed in activator data

    :param activator:  activator to create in activator list
    :return:        201 on success, 406 on activator exists
    """

    # Remove id as it's created automatically
    if 'id' in activatorDetails:
        del activatorDetails['id']

    schema = ActivatorSchema()
    new_activator = schema.load(activatorDetails, session=db.session)
    new_activator.lastUpdated = ModelTools.get_utc_timestamp()
    new_activator.accessRequestedBy = activatorDetails.get(
        'accessRequestedBy', 0)
    db.session.add(new_activator)
    db.session.commit()

    # Serialize and return the newly created deployment
    # in the response
    data = schema.dump(new_activator)
    return data, 201
Example #11
0
def create(solution):
    """
    This function creates a new solution in the solutions list
    based on the passed in solutions data

    :param solution:  solution to create in solutions list
    :return:        201 on success, 406 on solutions exists
    """

    app.logger.debug("Before")
    app.logger.debug(pformat(solution))

    lastUpdated = ModelTools.get_utc_timestamp()

    # Defaults
    if (solution.get('active') == None):
        solution['active'] = True

    if (solution.get('favourite') == None):
        solution['favourite'] = True

    if (solution.get('teams') == None):
        solution['teams'] = 0

    # Remove applications because Solutions don't have
    # any applications when they are first created
    if ('applications' in solution):
        del solution['applications']

    # we don't need the id, the is generated automatically on the database
    if ('id' in solution):
        del solution["id"]

    solution['lastUpdated'] = ModelTools.get_utc_timestamp()

    app.logger.debug("After")
    app.logger.debug(pformat(solution))

    schema = SolutionSchema()
    new_solution = schema.load(solution, session=db.session)
    db.session.add(new_solution)
    db.session.commit()

    # Serialize and return the newly created solution
    # in the response
    data = schema.dump(new_solution)
    return data, 201
Example #12
0
def update(oid, activatorDetails):
    """
    This function updates an existing activator in the activators list

    :param key:    key of the activator to update in the activators list
    :param activator:   activator to update
    :return:       updated activator
    """

    app.logger.debug("update")
    app.logger.debug("id")
    app.logger.debug(oid)
    app.logger.debug("activator")
    app.logger.debug(pformat(activatorDetails))

    if 'id' in activatorDetails and activatorDetails['id'] != oid:
        abort(400, f"Key mismatch in path and body")

    # Does the activators exist in activators list?
    existing_activator = Activator.query.filter(
        Activator.id == oid).one_or_none()

    # Does activator exist?

    if existing_activator is not None:
        schema = ActivatorSchema()
        activatorDetails['lastUpdated'] = ModelTools.get_utc_timestamp()
        activatorDetails['accessRequestedBy'] = activatorDetails.get(
            'accessRequestedBy', existing_activator.accessRequestedBy)
        activatorDetails["ci"] = json.dumps(
            activatorDetails.get("ci", existing_activator.ci))
        activatorDetails["cd"] = json.dumps(
            activatorDetails.get("cd", existing_activator.cd))
        activatorDetails["hosting"] = json.dumps(
            activatorDetails.get("hosting", existing_activator.hosting))
        activatorDetails["envs"] = json.dumps(
            activatorDetails.get("envs", existing_activator.envs))
        activatorDetails["sourceControl"] = json.dumps(
            activatorDetails.get("sourceControl",
                                 existing_activator.sourceControl))
        activatorDetails["regions"] = json.dumps(
            activatorDetails.get("regions", existing_activator.regions))
        activatorDetails["apiManagement"] = json.dumps(
            activatorDetails.get("apiManagement",
                                 existing_activator.apiManagement))
        activatorDetails["platforms"] = json.dumps(
            activatorDetails.get("platforms", existing_activator.platforms))
        Activator.query.filter(Activator.id == oid).update(activatorDetails)
        db.session.commit()
        # return the updated activator in the response
        data = schema.dump(existing_activator)
        app.logger.debug("activator data:")
        app.logger.debug(pformat(data))
        return data, 200
    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"Activator id {oid} not found")
Example #13
0
def update(id, activator):
    """
    This function updates an existing activator in the activators list

    :param key:    key of the activator to update in the activators list
    :param activator:   activator to update
    :return:       updated activator
    """

    app.logger.debug("update")
    app.logger.debug("id")
    app.logger.debug(id)
    app.logger.debug("activator")
    app.logger.debug(pformat(activator))

    if 'id' in activator and activator['id'] != id:
        abort(400, f"Key mismatch in path and body")

    # Does the activators exist in activators list?
    existing_activator = Activator.query.filter(
        Activator.id == id).one_or_none()

    # Does activator exist?

    if existing_activator is not None:
        activator['lastUpdated'] = ModelTools.get_utc_timestamp()
        activator['ci'] = json.dumps(activator.get('ci',
                                                   existing_activator.ci))
        activator['cd'] = json.dumps(activator.get('cd',
                                                   existing_activator.cd))
        activator['resources'] = json.dumps(
            activator.get('resources', existing_activator.resources))
        activator['hosting'] = json.dumps(
            activator.get('hosting', existing_activator.hosting))
        activator['envs'] = json.dumps(
            activator.get('envs', existing_activator.envs))
        activator['sourceControl'] = json.dumps(
            activator.get('sourceControl', existing_activator.sourceControl))
        activator['regions'] = json.dumps(
            activator.get('regions', existing_activator.regions))
        activator['apiManagement'] = json.dumps(
            activator.get('apiManagement', existing_activator.apiManagement))
        activator['platforms'] = json.dumps(
            activator.get('platforms', existing_activator.platforms))
        Activator.query.filter(Activator.id == id).update(activator)
        db.session.commit()
        # return the updated activator in the response
        schema = ActivatorSchema()
        data = schema.dump(existing_activator)
        app.logger.debug("activator data:")
        app.logger.debug(pformat(data))
        return data, 200
    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"Activator id {id} not found")
def build_application(app):
    app_dict = {
        'id': app.id,
        'name': app.name,
        'env': app.env,
        'status': app.status,
        'description': app.description,
        'resources': ModelTools.load_json_array(app.resources),
        'lastUpdated': app.lastUpdated
    }
    acts = Activator.query.filter(Activator.id == app.activatorId).all()
    act_dict = {}
    for act in acts:
        act_dict = activator_extension.build_activator(act)

    app_dict['activator'] = act_dict
    return app_dict
def create(activatorDetails):
    """
    This function creates a new activator in the activator list
    based on the passed in activator data

    :param activator:  activator to create in activator list
    :return:        201 on success, 406 on activator exists
    """

    # Remove id as it's created automatically
    if 'id' in activatorDetails:
        del activatorDetails['id']

    activatorDetails['accessRequestedBy'] = activatorDetails.get('accessRequestedBy', 0)
    activatorDetails['ci'] = json.dumps(activatorDetails.get('ci', []))
    activatorDetails['cd'] = json.dumps(activatorDetails.get('cd', []))
    activatorDetails['hosting'] = json.dumps(activatorDetails.get('hosting', []))
    activatorDetails['envs'] = json.dumps(activatorDetails.get('envs', []))
    activatorDetails['sourceControl'] = json.dumps(activatorDetails.get('sourceControl', []))
    activatorDetails['regions'] = json.dumps(activatorDetails.get('regions', []))
    activatorDetails['apiManagement'] = json.dumps(activatorDetails.get('apiManagement', []))
    activatorDetails['platforms'] = json.dumps(activatorDetails.get('platforms', []))

    schema = ActivatorSchema()
    new_activator = schema.load(activatorDetails, session=db.session)
    new_activator.lastUpdated = ModelTools.get_utc_timestamp()

    db.session.add(new_activator)
    db.session.commit()

    # Serialize and return the newly created deployment
    # in the response

    new_activator.ci = json.loads(new_activator.ci or '[]')
    new_activator.cd = json.loads(new_activator.cd or '[]')
    new_activator.hosting = json.loads(new_activator.hosting or '[]')
    new_activator.envs = json.loads(new_activator.envs or '[]')
    new_activator.sourceControl = json.loads(new_activator.sourceControl or '[]')
    new_activator.regions = json.loads(new_activator.regions or '[]')
    new_activator.apiManagement = json.loads(new_activator.apiManagement or '[]')
    new_activator.platforms = json.loads(new_activator.platforms or '[]')

    schema = ExtendedActivatorSchema(many=False)
    data = schema.dump(new_activator)
    return data, 201
def update(oid, solutionDetails):
    """
    Updates an existing solutions in the solutions list.

    :param key:    key of the solutions to update in the solutions list
    :param solutions:   solutions to update
    :return:       updated solutions
    """

    app.logger.debug(solutionDetails)

    # Does the solutions exist in solutions list?
    existing_solution = Solution.query.filter(Solution.id == oid).one_or_none()

    # Does solutions exist?

    if existing_solution is not None:
        solutionDetails['environments'] = json.dumps(
            solutionDetails.get('environments')
            or existing_solution.environments)
        schema = SolutionSchema()
        update_solution = schema.load(solutionDetails, session=db.session)
        update_solution.key = solutionDetails.get('id', oid)
        update_solution.lastUpdated = ModelTools.get_utc_timestamp()

        db.session.merge(update_solution)
        db.session.commit()

        # return the updted solutions in the response
        schema = ExtendedSolutionSchema(many=False)
        print(">>>>>  " + pformat(solutionDetails))
        solutionDetails['environments'] = json.loads(
            solutionDetails['environments'])
        data = schema.dump(solutionDetails)
        return data, 200

    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"Solution not found")
Example #17
0
def update(oid, applicationDetails):
    """
    This function updates an existing application in the application list

    :param id: id of the application to update in the application list
    :param application:   application to update
    :return: updated application
    """

    app.logger.debug("application: ")
    app.logger.debug(pformat(applicationDetails))

    # Does the application exist in applications?
    existing_application = Application.query.filter(
        Application.id == oid).one_or_none()

    # Does application exist?
    if existing_application is not None:
        applicationDetails['lastUpdated'] = ModelTools.get_utc_timestamp()
        applicationDetails["resources"] = json.dumps(
            applicationDetails.get("resources",
                                   existing_application.resources))
        Application.query.filter(
            Application.id == oid).update(applicationDetails)
        db.session.commit()

        applicationDetails['resources'] = json.loads(
            existing_application.resources)
        # return the updated application in the response
        schema = ExtendedApplicationSchema()
        data = schema.dump(applicationDetails)
        return data, 200

    # otherwise, nope, application doesn't exist, so that's an error
    else:
        abort(404, f"Application {oid} not found")
Example #18
0
def create(application):
    """
    This function creates a new application in the application structure
    based on the passed in application data

    :param application:  application to create in application list
    :return:             201 on success, 406 on application exists
    """

    pprint(application)
    
    schema = ApplicationSchema()
    new_application = schema.load(application, session=db.session)
    new_application.lastUpdated = ModelTools.get_utc_timestamp()
    db.session.add(new_application)
    db.session.commit()

    # Serialize and return the newly created application
    # in the response
    data = schema.dump(new_application)
    app.logger.debug("application data:")
    app.logger.debug(pformat(data))

    return data, 201
def create(solutionDetails):
    """
    This function creates a new solution in the solutions list
    based on the passed in solutions data

    :param solution:  solution to create in solutions list
    :return:        201 on success, 406 on solutions exists
    """

    # Defaults
    if (solutionDetails.get('active') == None):
        solutionDetails['active'] = True

    if (solutionDetails.get('favourite') == None):
        solutionDetails['favourite'] = True

    if (solutionDetails.get('teams') == None):
        solutionDetails['teams'] = 0

    if (solutionDetails.get('deployed') == None):
        solutionDetails['deployed'] = False

    if (solutionDetails.get('deploymentState') == None):
        solutionDetails['deploymentState'] = ""

    if (solutionDetails.get('statusId') == None):
        solutionDetails['statusId'] = 0

    if (solutionDetails.get('statusCode') == None):
        solutionDetails['statusCode'] = ""

    if (solutionDetails.get('statusMessage') == None):
        solutionDetails['statusMessage'] = ""

    # Remove applications because Solutions don't have
    # any applications when they are first created
    if ('applications' in solutionDetails):
        del solutionDetails['applications']

    # we don't need the id, the is generated automatically on the database
    if ('id' in solutionDetails):
        del solutionDetails["id"]

    solutionDetails['lastUpdated'] = ModelTools.get_utc_timestamp()
    solutionDetails['environments'] = json.dumps(
        solutionDetails.get('environments') or [])

    print("Create name 2: " + solutionDetails['name'])

    schema = SolutionSchema(many=False)
    new_solution = schema.load(solutionDetails, session=db.session)
    db.session.add(new_solution)
    db.session.commit()

    print("Create name 3: " + new_solution.name)

    # Serialize and return the newly created solution
    # in the response

    print(pformat(solutionDetails['environments']))

    print("create solution")
    print(pformat(new_solution))
    print(pformat(new_solution.environments))

    schema = ExtendedSolutionSchema()
    data = schema.dump(new_solution)
    return data, 201