Skip to content

Commit

Permalink
Baselined from internal Repository
Browse files Browse the repository at this point in the history
last_commit:11ca9f88bb86b97fc3d10ddc7eeb1f51b14ba3cc
  • Loading branch information
GVE Devnet Admin committed Mar 7, 2024
0 parents commit 9198d8e
Show file tree
Hide file tree
Showing 14 changed files with 594 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DNAC_HOST=
USERNAME=
PASSWORD=
DAY0_TEMPLATE=
AUTOCLAIM_DEVICE_PID=
53 changes: 53 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Docker Build & Publish

on:
push:
branches:
- main
- master
tags:
- '*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}


jobs:
push:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf

- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
venv
.DS_STORE
templates
__pycache__
*.pyc
45 changes: 45 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as maintainers of this Cisco Sample Code pledge to making participation with our project a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Showing empathy towards other people

Examples of unacceptable behavior include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other interactions with this project that are not aligned to this Code of Conduct, or to ban temporarily or permanently any person for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project. Examples of representing a project include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the Cisco SE GitHub team at [email protected]. The team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project or Cisco SE Leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Cisco Sample Code

This project, and the code contained herein, is provided for example and/or demonstration purposes by Cisco for use by our partners and customers in working with Cisco's products and services. While Cisco's customers and partners are free to use this code pursuant to the terms set forth in the [LICENSE][LICENSE], this is not an Open Source project as we are not seeking to build a community around this project and its capabilities.


We do desire to provide functional and high-quality examples and demonstrations. If you should discover some bug, issue, or opportunity for enhancement with the code contained in this project, please do notify us by:

1. **Reviewing Open Issues** to verify that the issue hasn't already been reported.
2. **Opening a New Issue** to report the bug, issue, or enhancement opportunity.

[LICENSE]: ../LICENSE
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9-slim-buster
WORKDIR /app
COPY ./requirements.txt /app
RUN pip install -r requirements.txt
COPY . .
EXPOSE 9002
CMD ["python", "./flaskapp.py"]
Binary file added IMAGES/0image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
CISCO SAMPLE CODE LICENSE
Version 1.1
Copyright (c) 2022 Cisco and/or its affiliates

These terms govern this Cisco Systems, Inc. ("Cisco"), example or demo
source code and its associated documentation (together, the "Sample
Code"). By downloading, copying, modifying, compiling, or redistributing
the Sample Code, you accept and agree to be bound by the following terms
and conditions (the "License"). If you are accepting the License on
behalf of an entity, you represent that you have the authority to do so
(either you or the entity, "you"). Sample Code is not supported by Cisco
TAC and is not tested for quality or performance. This is your only
license to the Sample Code and all rights not expressly granted are
reserved.

1. LICENSE GRANT: Subject to the terms and conditions of this License,
Cisco hereby grants to you a perpetual, worldwide, non-exclusive, non-
transferable, non-sublicensable, royalty-free license to copy and
modify the Sample Code in source code form, and compile and
redistribute the Sample Code in binary/object code or other executable
forms, in whole or in part, solely for use with Cisco products and
services. For interpreted languages like Java and Python, the
executable form of the software may include source code and
compilation is not required.

2. CONDITIONS: You shall not use the Sample Code independent of, or to
replicate or compete with, a Cisco product or service. Cisco products
and services are licensed under their own separate terms and you shall
not use the Sample Code in any way that violates or is inconsistent
with those terms (for more information, please visit:
www.cisco.com/go/terms).

3. OWNERSHIP: Cisco retains sole and exclusive ownership of the Sample
Code, including all intellectual property rights therein, except with
respect to any third-party material that may be used in or by the
Sample Code. Any such third-party material is licensed under its own
separate terms (such as an open source license) and all use must be in
full accordance with the applicable license. This License does not
grant you permission to use any trade names, trademarks, service
marks, or product names of Cisco. If you provide any feedback to Cisco
regarding the Sample Code, you agree that Cisco, its partners, and its
customers shall be free to use and incorporate such feedback into the
Sample Code, and Cisco products and services, for any purpose, and
without restriction, payment, or additional consideration of any kind.
If you initiate or participate in any litigation against Cisco, its
partners, or its customers (including cross-claims and counter-claims)
alleging that the Sample Code and/or its use infringe any patent,
copyright, or other intellectual property right, then all rights
granted to you under this License shall terminate immediately without
notice.

4. LIMITATION OF LIABILITY: CISCO SHALL HAVE NO LIABILITY IN CONNECTION
WITH OR RELATING TO THIS LICENSE OR USE OF THE SAMPLE CODE, FOR
DAMAGES OF ANY KIND, INCLUDING BUT NOT LIMITED TO DIRECT, INCIDENTAL,
AND CONSEQUENTIAL DAMAGES, OR FOR ANY LOSS OF USE, DATA, INFORMATION,
PROFITS, BUSINESS, OR GOODWILL, HOWEVER CAUSED, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

5. DISCLAIMER OF WARRANTY: SAMPLE CODE IS INTENDED FOR EXAMPLE PURPOSES
ONLY AND IS PROVIDED BY CISCO "AS IS" WITH ALL FAULTS AND WITHOUT
WARRANTY OR SUPPORT OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY
LAW, ALL EXPRESS AND IMPLIED CONDITIONS, REPRESENTATIONS, AND
WARRANTIES INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTY OR
CONDITION OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-
INFRINGEMENT, SATISFACTORY QUALITY, NON-INTERFERENCE, AND ACCURACY,
ARE HEREBY EXCLUDED AND EXPRESSLY DISCLAIMED BY CISCO. CISCO DOES NOT
WARRANT THAT THE SAMPLE CODE IS SUITABLE FOR PRODUCTION OR COMMERCIAL
USE, WILL OPERATE PROPERLY, IS ACCURATE OR COMPLETE, OR IS WITHOUT
ERROR OR DEFECT.

6. GENERAL: This License shall be governed by and interpreted in
accordance with the laws of the State of California, excluding its
conflict of laws provisions. You agree to comply with all applicable
United States export laws, rules, and regulations. If any provision of
this License is judged illegal, invalid, or otherwise unenforceable,
that provision shall be severed and the rest of the License shall
remain in full force and effect. No failure by Cisco to enforce any of
its rights related to the Sample Code or to a breach of this License
in a particular situation will act as a waiver of such rights. In the
event of any inconsistencies with any other terms, this License shall
take precedence.
163 changes: 163 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# GVE DevNet Catalyst Center PnP Automatic Claim

This repository contains sample code that automates the process of onboarding devices using Plug and Play (PnP) in Cisco Catalyst Center. Thereby, the device is automatically claimed and assigned to a specific site based on its IP address.

The scripts utilize the "Device waiting to be claimed" webhook option of Cisco Catalyst Center.
As soon as the device contacts Catalyst Center, information such as its IP address and serial number is sent via the webhook to the sample application. If the type of device aligns with the script settings, the application maps the device IP address to a subnet provided in an external CSV file. If a successful mapping is possible, further parameters are read from the file and used to configure the device with an minimal onboarding template and assign it to a site during the claiming process.

> Note: This repository is an adapted version of the [PnPAutoClaim](https://github.com/pamosima/PnPAutoClaim) repository.
The sample code was tested with Catalyst 9200/9300 and Catalyst Center version 2.3.7.4.


## Contacts
* Ramona Renner

## Solution Components
* Python
* DNA Center
* DNA Center REST API's
* Environment set up for PnP process


## Prerequisites

### Webhooks

The app needs to be reachable on port 9002 from the public internet for Catalyst Center Webhooks to be received and processed. Any option is valid, like AWS Lambda, Heroku, GCP, etc. Ngrok is used here to expose the local app for simplicity.

#### Ngrok

The Flask app runs on http://localhost:9002 by default, so it requires a Ngrok forwarding address to port 9002 to receive the webhooks.

Follow these instructions to set up ngrok:

1. Create a free account or login to [Ngrok](https://ngrok.com/).
2. Retrieve your auth token by navigating to `Getting Started > Your Authtoken` on the menu on the left-hand side. Copy the token on this page.
3. Then install the client library, depending on your OS [here](https://ngrok.com/download).
4. Once you have ngrok installed, update the ngrok configuration file with your auth token by running the following command on the terminal/command prompt:

```ngrok authtoken <yourtoken>```

replacing <yourtoken> with the authtoken you copied in Step 2.

5. Start the ngrok tunnel for port 9002 with the command:

```ngrok http https://localhost:9002```

6. Note the link under `Forwarding` with the format `http://xxxxx.ngrok-free.app` for a later step.


### Cisco DNA Center

#### Configure the Webhook Event and Notification

1. Choose **Platform > Developer Toolkit > Event Notifications > Notifications**

2. Click **+** to create a new notification

3. For **Step 1 - Select Site and Events**, search for ```Device waiting to be claimed```, select the notification and click **Next**

4. In **Step 2 - Select Channels**, select **Rest** as a notification channels and click **Next**

5. In **Step 3 - REST Settings**, click **here** to create a new setting instance.

6. On the **Destinations** page, click **Add** and provide the following information, and click **Save**
* Name: Name of the Webhook destination, e.g. PnP Sample Script
* (Optional) Description
* URL: public URL of the sample application, format: `<ngrok-url see last section>/claim`
* Authentication, e.g. `No Auth`
* Proxy, e.g. checked


7. Return to the **Step 3 - REST Settings** page, refresh the instance select, and select the created instance.

8. Click on **Next**

9. In **Step 4 - Name and Description**, provide a name and short description for your notification, and click on **Next**

10. On the Summary page, click on **Finish**

Done! Your new notification is complete.


#### Import the Onboarding Template

1. Choose **Design** > **CLI Templates** or alternative in older Catalyst Versions **Tools** > **Template Hub**

2. Click **Import** > **Import Template**

3. Select the project **Onboarding Configuration** and upload the template provided in the **work_files** folder of this repository. Click **Import**.


### Set up the Sample App

8. Make sure you have [Python 3.8.10](https://www.python.org/downloads/) and [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed.

9. (Optional) Create and activate a virtual environment for the project ([Instructions](https://docs.python.org/3/tutorial/venv.html))

10. Access the created virtual environment folder
```
cd [add name of virtual environment here]
```
11. Clone this GitHub repository into the local folder:
```
git clone [add GitHub link here]
```
* For GitHub link:
In GitHub, click on the **Code** button in the upper part of the page > click the **copy icon**
* Or simply download the repository as zip file using the 'Download ZIP' button and extract it
12. Access the downloaded folder:
```
cd gve_devnet_catalyst_center_pnp_automatic_claim
```
13. Install all dependencies:
```
pip3 install -r requirements.txt
```
14. Fill in your variables in the .env file. Feel free to leave the variable without note as is:
```python
DNAC_HOST=<Catalyst Center address, without https://>
USERNAME=<Catalyst Center Username>
PASSWORD=<Catalyst Center Password>
DAY0_TEMPLATE=<Name of imported PnP Onboarding Template, see section: Import the Onboarding Template>
AUTOCLAIM_DEVICE_PID=<PID of device, e.g. C9200L-24P-4X>
```

> Note: Mac OS hides the .env file in the finder by default. View the demo folder for example with your preferred IDE to make the file visible.
15. Fill in one row for each network, site and configuration values in the **work_files/mapping.csv** file. As soon as a device with a defined type contacts Catalyst Center its IP address is compared to each entry in the **mapping.csv** file. If the IP address is part of one of the listed networks, the device will be claimed using the provided information for the mapping network.

IP,CIDR,site,hostname
<network address>,<subnet CIDR>,<Full path of site e.g. Global/<area name>/<building name>,<Hostname of the device>

## Usage

16. Start the flask application:

```python3 flaskapp.py```

The sample script will react as soon as a new device of the defined type is waiting to be claimed.


### LICENSE

Provided under Cisco Sample Code License, for details see [LICENSE](LICENSE.md)

### CODE_OF_CONDUCT

Our code of conduct is available [here](CODE_OF_CONDUCT.md)

### CONTRIBUTING

See our contributing guidelines [here](CONTRIBUTING.md)

#### DISCLAIMER:
<b>Please note:</b> This script is meant for demo purposes only. All tools/ scripts in this repo are released for use "AS IS" without any warranties of any kind, including, but not limited to their installation, use, or performance. Any use of these scripts and tools is at your own risk. There is no guarantee that they have been through thorough testing in a comparable environment and we are not responsible for any damage or data loss incurred with their use.
You are responsible for reviewing and testing any scripts you run thoroughly before use in any non-testing environment.
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.5"

services:
catalyst_center_pnp_automatic_claim:
image: ghcr.io/gve-sw/gve_devnet_catalyst_center_pnp_automatic_claim:latest
container_name: catalyst_center_pnp_automatic_claim
environment:
- DNAC_HOST=${DNAC_HOST}
- USERNAME=${USERNAME}
- PASSWORD=${PASSWORD}
- DAY0_TEMPLATE=${DAY0_TEMPLATE}
- AUTOCLAIM_DEVICE_PID=${AUTOCLAIM_DEVICE_PID}
volumes:
- config.yaml:/app/config.yaml
restart: "always"
Loading

0 comments on commit 9198d8e

Please sign in to comment.