Skip to content

Commit

Permalink
Documentation improvements and type refactorings (#2)
Browse files Browse the repository at this point in the history
- add typing
- add testing
---------

Signed-off-by: Marc Schöchlin <[email protected]>
  • Loading branch information
scoopex authored Nov 25, 2024
1 parent 1d36dd3 commit 8ea5815
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 121 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI Workflow
on:
push:
branches:
- '**' # Run on commits to any branch
jobs:
build:
name: CI
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Deps
run: make deps
- name: Type test
run: make test
- name: Type check
run: make type-check
- name: Lint
run: make lint
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lint: deps
.PHONY: lint

type-check: deps
${activate} && ${python} -m mypy --no-color-output --pretty src
${activate} && ${python} -m mypy --no-color-output src
.PHONY: type-check

test: deps
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ basis for later automation.
# Usage

```
$ ./openstack_workload_generator --help
usage: Create workloads on openstack installations [-h] [--log_level loglevel] [--os_cloud OS_CLOUD] [--ansible_inventory [ANSIBLE_INVENTORY]] [--config CONFIG]
(--create_domains DOMAINNAME [DOMAINNAME ...] | --delete_domains DOMAINNAME [DOMAINNAME ...])
(--create_projects PROJECTNAME [PROJECTNAME ...] | --delete_projects PROJECTNAME [PROJECTNAME ...])
(--create_machines SERVERNAME [SERVERNAME ...] | --delete_machines SERVERNAME [SERVERNAME ...])
options:
-h, --help show this help message and exit
--log_level loglevel The loglevel
--os_cloud OS_CLOUD The openstack config to use, defaults to the value of the OS_CLOUD environment variable or "admin" if the variable is not set
--ansible_inventory [ANSIBLE_INVENTORY]
Dump the created servers as an ansible inventory to the specified directory, adds a ssh proxy jump for the hosts without a floating ip
--config CONFIG The config file for environment creation, define a path to the yaml file or a subpath in the profiles folder
--create_domains DOMAINNAME [DOMAINNAME ...]
A list of domains to be created
--delete_domains DOMAINNAME [DOMAINNAME ...]
A list of domains to be deleted, all child elements are recursively deleted
--create_projects PROJECTNAME [PROJECTNAME ...]
A list of projects to be created in the created domains
--delete_projects PROJECTNAME [PROJECTNAME ...]
A list of projects to be deleted in the created domains, all child elements are recursively deleted
--create_machines SERVERNAME [SERVERNAME ...]
A list of vms to be created in the created domains
--delete_machines SERVERNAME [SERVERNAME ...]
A list of vms to be deleted in the created projects
```

# Testing Scenarios
Expand Down
2 changes: 1 addition & 1 deletion openstack_workload_generator
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

rundir="$(dirname $(readlink -f $0))/"
rundir="$(dirname $(readlink -f $0))"
cd "$rundir" || exit 1

modification_time(){
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PyYAML==6.0.1
types-pyyaml
openstacksdk==3.3.0
pytest==7.4.0
mypy==1.4.1
mypy==1.13.0
flake8==6.1.0
Empty file removed src/__init__.py
Empty file.
44 changes: 30 additions & 14 deletions src/openstack_workload_generator/__main__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#!/usr/bin/env python3

import sys
import os
import argparse
import logging
import os
import sys
import time

from entities.helpers import setup_logging, cloud_checker, item_checker
from openstack.connection import Connection
from openstack.config import loader

from entities.helpers import Config
from entities import WorkloadGeneratorDomain
# $ make type-check
# source venv/bin/activate && python3 -m mypy --no-color-output --pretty src
# src/openstack_workload_generator/__main__.py:12: error: Cannot find implementation or library
# stub for module named "entities" [import-not-found]
# from entities import WorkloadGeneratorDomain
# ^
# src/openstack_workload_generator/__main__.py:13: error: Cannot find implementation or library stub for module
# named "entities.helpers" [import-not-found]
# from entities.helpers import setup_logging, cloud_checker, item_checker, Config
# ^
# src/openstack_workload_generator/__main__.py:13: note: See
# https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
# Found 2 errors in 1 file (checked 9 source files)
# make: *** [Makefile:25: type-check] Error 1

from entities import WorkloadGeneratorDomain # type: ignore[import-not-found]
from entities.helpers import setup_logging, cloud_checker, item_checker, Config # type: ignore[import-not-found]

LOGGER = logging.getLogger()

Expand Down Expand Up @@ -48,21 +62,22 @@
exclusive_group_project = parser.add_mutually_exclusive_group(required=True)

exclusive_group_project.add_argument('--create_projects', type=item_checker, nargs="+", default=None,
metavar="PROJECTNAME",
help='A list of projects to be created in the created domains')
metavar="PROJECTNAME",
help='A list of projects to be created in the created domains')

exclusive_group_project.add_argument('--delete_projects', type=item_checker, nargs="+", default=None,
metavar="PROJECTNAME",
help='A list of projects to be deleted in the created domains, all child elements are recursively deleted')
metavar="PROJECTNAME",
help='A list of projects to be deleted in the created '
'domains, all child elements are recursively deleted')

exclusive_group_machines = parser.add_mutually_exclusive_group(required=True)
exclusive_group_machines.add_argument('--create_machines', type=item_checker, nargs="+", default=None,
metavar="SERVERNAME",
help='A list of vms to be created in the created domains')
metavar="SERVERNAME",
help='A list of vms to be created in the created domains')

exclusive_group_machines.add_argument('--delete_machines', type=item_checker, nargs="+", default=None,
metavar="SERVERNAME",
help='A list of vms to be deleted in the created projects')
metavar="SERVERNAME",
help='A list of vms to be deleted in the created projects')

args = parser.parse_args()

Expand All @@ -77,6 +92,7 @@ def establish_connection():
cloud_config = config.get_one(args.os_cloud)
return Connection(config=cloud_config)


time_start = time.time()

Config.load_config(args.config)
Expand All @@ -102,7 +118,7 @@ def establish_connection():
workload_project.dump_inventory_hosts(args.ansible_inventory)
elif args.delete_machines:
for machine_obj in workload_project.get_machines(args.delete_machines):
machine_obj.delete_machine()
machine_obj.delete_machine()
sys.exit(0)
elif args.delete_projects:
conn = establish_connection()
Expand Down
6 changes: 2 additions & 4 deletions src/openstack_workload_generator/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from .helpers import setup_logging, cloud_checker, item_checker, Config
from .domain import WorkloadGeneratorDomain
from .project import WorkloadGeneratorProject
from .network import WorkloadGeneratorNetwork
from .user import WorkloadGeneratorTestUser



from .user import WorkloadGeneratorUser
16 changes: 10 additions & 6 deletions src/openstack_workload_generator/entities/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
from openstack.identity.v3.domain import Domain
from .project import WorkloadGeneratorProject

from .user import WorkloadGeneratorTestUser
from .user import WorkloadGeneratorUser

LOGGER = logging.getLogger()


class WorkloadGeneratorDomain:

def __init__(self, conn: Connection, domain_name: str):
self.conn = conn
self.domain_name = domain_name
self.obj: Domain = self.conn.identity.find_domain(domain_name)
if self.obj:
DomainCache.add(self.obj.id,self.obj.name)
DomainCache.add(self.obj.id, self.obj.name)
self.workload_user = WorkloadGeneratorDomain._get_user(conn, domain_name, self.obj)
self.workload_projects: dict[str, WorkloadGeneratorProject] = WorkloadGeneratorDomain._get_projects(
conn, self.obj, self.workload_user)
Expand All @@ -26,10 +27,10 @@ def __init__(self, conn: Connection, domain_name: str):
def _get_user(conn: Connection, domain_name: str, obj: Domain):
if not obj:
return None
return WorkloadGeneratorTestUser(conn, f"{domain_name}-admin", obj)
return WorkloadGeneratorUser(conn, f"{domain_name}-admin", obj)

@staticmethod
def _get_projects(conn: Connection, domain: Domain | None, user: WorkloadGeneratorTestUser | None) \
def _get_projects(conn: Connection, domain: Domain | None, user: WorkloadGeneratorUser | None) \
-> dict[str, WorkloadGeneratorProject]:
if not domain or not user:
return dict()
Expand Down Expand Up @@ -58,12 +59,15 @@ def disable_domain(self):
return domain

def get_projects(self, projects: list[str]) -> list[WorkloadGeneratorProject]:

result: list[WorkloadGeneratorProject] = []
if self.obj is None:
return []
return result

for project in projects:
if project in self.workload_projects:
yield self.workload_projects[project]
result.append(self.workload_projects[project])
return result

def delete_domain(self):
if self.obj is None:
Expand Down
Loading

0 comments on commit 8ea5815

Please sign in to comment.