Beispiel #1
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")
def read_all(category=None, status=None, environment=None,
        platform=None, type=None, source=None,
        sensitivity=None, page=None, page_size=None, sort=None):
    """
    This function responds to a request for /api/activators
    with the complete lists of activators

    :return:        json string of list of activators
    """

    # Create the list of activators from our data

    # pre-process sort instructions
    if (sort==None):
        activator_query = Activator.query.order_by(Activator.id)
    else:
        try:
            sort_inst = [ si.split(":") for si in sort ]
            orderby_arr = []
            for si in sort_inst:
                si1 = si[0]
                if len(si) > 1:
                    si2 = si[1]
                else:
                    si2 = "asc"
                orderby_arr.append(f"{si1} {si2}")
            #print("orderby: {}".format(orderby_arr))
            activator_query = Activator.query.order_by(literal_column(", ".join(orderby_arr)))
        except Exception as e:
            print(e)
            activator_query = Activator.query.order_by(Activator.id)

    activator_query = activator_query.filter(
      (category==None or Activator.category==category),
      (status==None or Activator.status==status),
      (environment==None or Activator.envs.like("%\"{}\"%".format(environment))),
      (platform==None or Activator.platforms.like("%\"{}\"%".format(platform))),
      (type==None or Activator.type==type),
      (source==None or Activator.sourceControl.like("%\"{}\"%".format(source))),
      (sensitivity==None or Activator.sensitivity==sensitivity)
    )

    if (page==None or page_size==None):
      activators = activator_query.all()
    else:
      activators = activator_query.limit(page_size).offset(page * page_size).all()

    activators_arr = []
    for act in activators:
        activators_arr.append(activator_extension.build_activator(act))

    # Serialize the data for the response
    activator_schema = ExtendedActivatorSchema(many=True)
    data = activator_schema.dump(activators_arr)
    app.logger.debug("read_all")
    app.logger.debug(pformat(data))
    return data, 200
Beispiel #3
0
def read_one(id):
    """
    This function responds to a request for /api/activator/{key}
    with one matching activator from activatorss

    :param application:   key of activator to find
    :return:              activator matching key
    """

    act = (Activator.query.filter(Activator.id == id).one_or_none())
    activator = activator_extension.build_activator(act)

    if activator is not None:
        # Serialize the data for the response
        activator_schema = ExtendedActivatorSchema()
        data = activator_schema.dump(activator)
        return data
    else:
        abort(404, "Activator with id {id} not found".format(id=id))
Beispiel #4
0
def read_all(category=None,
             status=None,
             environment=None,
             platform=None,
             type=None,
             source=None,
             sensitivity=None):
    """
    This function responds to a request for /api/activators
    with the complete lists of activators

    :return:        json string of list of activators
    """

    # Create the list of activators from our data
    activators = Activator.query.filter(
        (category == None or Activator.category == category),
        (status == None or Activator.status == status),
        (environment == None
         or Activator.envs.like("%\"{}\"%".format(environment))),
        (platform == None
         or Activator.platforms.like("%\"{}\"%".format(platform))),
        (type == None or Activator.type == type),
        (source == None
         or Activator.sourceControl.like("%\"{}\"%".format(source))),
        (sensitivity == None
         or Activator.sensitivity == sensitivity)).order_by(
             Activator.id).all()
    activators_arr = []
    for act in activators:
        activators_arr.append(activator_extension.build_activator(act))

    # Serialize the data for the response
    activator_schema = ExtendedActivatorSchema(many=True)
    data = activator_schema.dump(activators_arr)
    app.logger.debug("read_all")
    app.logger.debug(pformat(data))
    return data
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