/orders

NYU DevOps Orders Service Summer 2023

Primary LanguagePythonApache License 2.0Apache-2.0

NYU DevOps Project Template

Build Status codecov License Python

This is a skeleton you can use to start your projects

Overview

This project template contains starter code for your class project. The /service folder contains your models.py file for your model and a routes.py file for your service. The /tests folder has test case starter code for testing the model and the service separately. All you need to do is add your functionality. You can use the lab-flask-tdd for code examples to copy from.

Automatic Setup

The best way to use this repo is to start your own repo using it as a git template. To do this just press the green Use this template button in GitHub and this will become the source for your repository.

Manual Setup

You can also clone this repository and then copy and paste the starter code into your project repo folder on your local computer. Be careful not to copy over your own README.md file so be selective in what you copy.

There are 4 hidden files that you will need to copy manually if you use the Mac Finder or Windows Explorer to copy files from this folder into your repo folder.

These should be copied using a bash shell as follows:

    cp .gitignore  ../<your_repo_folder>/
    cp .flaskenv ../<your_repo_folder>/
    cp .gitattributes ../<your_repo_folder>/

Contents

The project contains the following:

.gitignore          - this will ignore vagrant and other metadata files
.flaskenv           - Environment variables to configure Flask
.gitattributes      - File to gix Windows CRLF issues
.devcontainers/     - Folder with support for VSCode Remote Containers
dot-env-example     - copy to .env to use environment variables
requirements.txt    - list if Python libraries required by your code
config.py           - configuration parameters

service/                   - service python package
├── __init__.py            - package initializer
├── models.py              - module with business models
├── routes.py              - module with service routes
└── common                 - common code package
    ├── error_handlers.py  - HTTP error handling code
    ├── log_handlers.py    - logging setup code
    └── status.py          - HTTP status constants

tests/              - test cases package
├── __init__.py     - package initializer
├── factories.py    - generate test data
├── test_models.py  - test suite for business models
└── test_routes.py  - test suite for service routes

RESTful routes for orders and items

Endpoint          Method       Rule
----------------  -------      -----------------------------------------------------
create_orders     POST         /orders
list_orders       GET          /orders        
get_orders        GET          /orders/<order_id>
update_orders     PUT          /orders/<order_id>
cancel_order      PUT          /orders/<order_id>/cancel
delete_orders     DELETE       /orders/<order_id>
add_items         POST         /orders/<order_id>/items
list_items        GET          /orders/<order_id>/items    
get_items         GET          /orders/<order_id>/items/<item_id>
update_items      PUT          /orders/<order_id>/items/<item_id>
delete_items      DELETE       /orders/<order_id>/items/<item_id>

API Usage

Create an order

URL : http://127.0.0.1:8000/orders

Method: POST

Example:

Request Body (JSON)

{
    "date": "2023-07-16",
    "total": 0.00,
    "payment": "CREDITCARD",
    "address": "5th Fifth Ave, NY",
    "customer_id": 2,
    "status": "OPEN",
    "items": []
  
}

Success Response: HTTP_201_CREATED

{
  "address": "5th Fifth Ave, NY",
  "customer_id": 2,
  "date": "2023-07-16",
  "id": 1,
  "items": [],
  "payment": "CREDITCARD",
  "status": "OPEN",
  "total": 0.0
}

List all orders

URL : http://127.0.0.1:8000/orders

Method: GET

Example:

Success Response : HTTP_200_OK

[
  {
    "id": 1,
    "date": 2023-07-16,
    "total": 100.00,
    "payment": "CREDITCARD",
    "address": "5th Fifth Ave, NY",
    "customer_id": 2,
    "status": "OPEN"
  }
  {
    "id": 2,
    "date": 2023-07-15,
    "total": 50.00,
    "payment": "VEMO",
    "address": "4th Fifth Ave, NY",
    "customer_id": 3,
    "status": "DELIVERED"
  }
]

Get an order

URL : http://127.0.0.1:8000/orders/<order_id>

Method: GET

Example:

Success Response : HTTP_200_OK

{
  "id": 1,
  "date": 2023-07-16,
  "total": 100.00,
  "payment": "CREDITCARD",
  "address": "5th Fifth Ave, NY",
  "customer_id": 2,
  "status": "OPEN"
}

Update an order

URL : http://127.0.0.1:8000/orders/<order_id>

Method: PUT

Example:

Request Body (JSON)

{
  "date": 2023-07-16,
  "total": 200.00,
  "payment": "CREDITCARD",
  "address": "5th Fifth Ave, NY",
  "customer_id": 2,
  "status": "OPEN"
}

Success Response : HTTP_200_OK

{
  "id": 1,
  "date": 2023-07-16,
  "total": 200.00,
  "payment": "CREDITCARD",
  "address": "5th Fifth Ave, NY",
  "customer_id": 2,
  "status": "OPEN"
}

Cancel an order

URL : http://127.0.0.1:8000/orders/<order_id>/cancel

Method: PUT

Example:

Success Response : HTTP_200_OK

[
  {
    "id": 1,
    "date": 2023-07-16,
    "total": 100.00,
    "payment": "CREDITCARD",
    "address": "5th Fifth Ave, NY",
    "customer_id": 2,
    "status": "CANCELLED"
  }
]

Delete an order

URL : http://127.0.0.1:8000/orders/<order_id>

Method: DELETE

Example:

Success Response: HTTP_204_NO_CONTENT

Create an item in an order

URL : http://127.0.0.1:8000/orders/<order_id>/items

Method: POST

Example:

Request Body (JSON)

{
    "product_id": 6,
    "quantity": 2,
    "total": 60.00
}

Success Response: HTTP_201_CREATED

{
  "id": 12,
  "order_id": 1,
  "product_id": 6,
  "quantity": 2,
  "total": 60.0
}

List all items in an order

URL : http://127.0.0.1:8000/orders/<order_id>/items

Method: GET

Example:

Success Response : HTTP_200_OK

[
  {
    "id": 1,
    "product_id": 6,
    "quantity": 2,
    "total": 60.00,
    "order_id": 1,
  }
  {
    "id": 2,
    "product_id": 7,
    "quantity": 1,
    "total": 40.00,
    "order_id": 1,
  }
]

Get an item in an order

URL : http://127.0.0.1:8000/orders/<order_id>/items/<item_id>

Method: GET

Example:

Success Response : HTTP_200_OK

{
  "id": 1,
  "product_id": 6,
  "quantity": 2,
  "total": 60.00,
  "order_id": 1,
}

Update an item in an order

URL : http://127.0.0.1:8000/orders/<order_id>/items/<item_id>

Method: PUT

Example:

Request Body (JSON)

{
  "product_id": 6,
  "quantity": 3,
  "total": 180.00,
  "order_id": 1,
}

Success Response : HTTP_200_OK

{
  "id": 1,
  "product_id": 6,
  "quantity": 3,
  "total": 180.00,
  "order_id": 1,
}

Delete an item in an order

URL : http://127.0.0.1:8000/orders/<order_id>/items/<item_id>

Method: DELETE

Example:

Success Response: HTTP_204_NO_CONTENT

Useful Kubernetes CLI

Please make sure you are in the designated namespace before deploying.

#create namespace and copy container registry secret to the new namespace
make namespace

#get current namespace
kc config view --minify -o jsonpath='{..namespace}'

#switch namespace
kc config set-context --current --namespace=<namespace>

#operate in specific namespace without switching
kc -n <namespace> ...

#deployw
kc apply -f <directory/yaml files>

#check pod status
kc get pods --watch

#inspect pod
kc describe <pod NAME>

#delete deployment
kc delete -f <directory/yaml files>

License

Copyright (c) John Rofrano. All rights reserved.

Licensed under the Apache License. See LICENSE

This repository is part of the NYU masters class: CSCI-GA.2820-001 DevOps and Agile Methodologies created and taught by John Rofrano, Adjunct Instructor, NYU Courant Institute, Graduate Division, Computer Science, and NYU Stern School of Business.