Skip to content

Commit

Permalink
init release
Browse files Browse the repository at this point in the history
  • Loading branch information
xod442 committed Mar 28, 2023
1 parent e131399 commit 2a3ecfe
Show file tree
Hide file tree
Showing 69 changed files with 3,754 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.8
RUN apt-get update -y
RUN apt-get install -y nano

COPY requirements.txt /opt/
RUN pip3 install -r /opt/requirements.txt

WORKDIR /opt
#COPY . .

ENV FLASK_APP=app
ENV FLASK_DEBUG=1
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "5000"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Aruba, a Hewlett Packard Enterprise company
Copyright (c) 2023 Rick Kauffman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# flask_aruba_central
A flask web based application that leverages pycentral
# flask-aruba-central
Very simple flask API examples


Installation:

# must have git installed

[Install Git](https://github.com/git-guides/install-git)


# Must have docker and docker compose installed.
[Install Docker Desktop](https://www.docker.com/products/docker-desktop)

Works well with docker-desktop for macbook

```
% git clone https://github.com/xod442/flask-aruba-central.git
% cd flask-aruba-central
flask-aruba-central1% docker-compose up -d
```

# Test api directory
flask-aruba-central/tests


## Edit pycentral central_info.py file ==> utility/central_info.py
It is necessary to fill out the required fields in this dictionary.
If you do not, there will be great sadness. :-)

```
central_info = {
"username": "[email protected]",
"password": "*****************",
"client_id": "XXxXXXXXXXXXXX",
"client_secret": "XXxXXXXXXXXXXX",
"customer_id": "XXxXXXXXXXXXXX",
"base_url": "https://apigw-uswest4.central.arubanetworks.com",
"refresh_token": "XXxXXXXXXXXXXX"
}
```

Get the values from Aruba Central API page
[Here is some very good documentation on what to do]:
(https://developer.arubanetworks.com/aruba-central/docs/api-getting-started).
194 changes: 194 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
'''
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
__author__ = "@netwookie"
__credits__ = ["Rick Kauffman"]
__license__ = "Apache2"
__version__ = "0.1.1"
__maintainer__ = "Rick Kauffman"
__status__ = "Alpha"
'''
from flask import Flask, request, render_template, abort, redirect, url_for
import pymongo
import os
from jinja2 import Environment, FileSystemLoader
from bson.json_util import dumps
from bson.json_util import loads
from utility.client import get_client
from pycentral.configuration import Groups
#
app = Flask(__name__)

# A dictionary of the mongo database credentials
config = {
"username": "admin",
"password": "siesta3",
"server": "mongo",
}

# Setup database connetor
connector = "mongodb://{}:{}@{}".format(config["username"], config["password"], config["server"])
client = pymongo.MongoClient(connector)

#set mongo database
db = client["demo2"]

'''
#-------------------------------------------------------------------------------
Main Page
#-------------------------------------------------------------------------------
'''

@app.route("/", methods=('GET', 'POST'))
def login():


message = "Use the group menu"
return render_template('home.html', message=message)


'''
#-------------------------------------------------------------------------------
Home
#-------------------------------------------------------------------------------
'''

@app.route("/home/<string:message>", methods=('GET', 'POST'))
def home(message):


# message = "Operation completed successfully"
return render_template('home1.html', message=message)



'''
#-------------------------------------------------------------------------------
Group Section
#-------------------------------------------------------------------------------
'''

@app.route("/add_group", methods=('GET', 'POST'))
def add_group():
if request.method == 'POST':
central = get_client()
# Get form values
group_name = request.form['group_name']
group_password = request.form['group_password']
wired_template = False
wireless_template = False

groups = Groups()

response = groups.create_group(conn=central,
group_name=group_name,
group_password=group_password,
wired_template=wired_template,
wireless_template=wireless_template)


code = response['code']

if code != 201:
message = 'Got return code %s operation failed' % (code)

# TODO check to see record was written to database)
message = 'Group information written to database %s:' % (code)
return redirect(url_for('home', message=message))

return render_template('add_group.html')

@app.route("/get_groups", methods=('GET', 'POST'))
def get_groups():

central = get_client()

all_groups = Groups()

response = all_groups.get_groups(conn=central,limit=50)

groups = (response['msg']['data'])


# Check user credentials
return render_template('get_groups.html', groups=groups)


@app.route("/delete_group", methods=('GET', 'POST'))
def delete_group():
central = get_client()

# If HTTP POST then do this section
if request.method == 'POST':
group_name = request.form['group']

groups = Groups()

response = groups.delete_group(conn=central,group_name=group_name)

code = response['code']
message = 'The Group has been deleted %s ' % (code)
return redirect(url_for('home', message=message))

# If it is a HTTP GET do this section first
my_central = []

all_groups = Groups()

response = all_groups.get_groups(conn=central,limit=50)

groups = (response['msg']['data'])

return render_template('delete_group.html', groups=groups)

'''
#-------------------------------------------------------------------------------
Mongo Section
#-------------------------------------------------------------------------------
'''
@app.route("/groups2mongo", methods=('GET', 'POST'))
def groups2mongo():
central = get_client()

all_groups = Groups()

response = all_groups.get_groups(conn=central,limit=50)

groups = (response['msg']['data'])

for g in groups:
entry = {
'group_name': g[0]
}
response = db.groups.insert_one(entry)

message = 'Group information written to database'
return redirect(url_for('home', message=message))

@app.route("/listmongo", methods=('GET', 'POST'))
def listmongo():
# Get a list of Groupss
my_groups = []

group = db.groups.find({})

groups = loads(dumps(group))

for g in groups:
group_name = g['group_name']
my_groups.append(group_name)

return render_template('list_groups.html', my_groups=my_groups)
24 changes: 24 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.8'
services:
web:
build: .
ports:
- "5001:5000"
volumes:
- .:/opt
links:
- mongo
mongo:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: siesta3
volumes:
- mongo-data:/data2/db
- mongo-configdb:/data2/configdb


volumes:
mongo-data:
driver: local
mongo-configdb:
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
flask
pytest
PyMongo
pycentral
6 changes: 6 additions & 0 deletions static/css/bootstrap.min.css

Large diffs are not rendered by default.

Loading

0 comments on commit 2a3ecfe

Please sign in to comment.