Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding vapi client #19

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""
import sqlalchemy as sa
import sqlmodel

from alembic import op

# revision identifiers, used by Alembic.
Expand Down
2 changes: 1 addition & 1 deletion zpodcli/src/zpodcli/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show_error(obj):
print(f"Error: {obj}")
return

if 'detail' in obj_json:
if "detail" in obj_json:
print(f"Error: {obj_json['detail']}")
else:
print(f"Error: {obj_json}")
54 changes: 54 additions & 0 deletions zpodcommon/src/zpodcommon/lib/vapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import requests
from rich.console import Console
from vmware.vapi.lib.connect import get_requests_connector
from vmware.vapi.security.session import create_session_security_context
from vmware.vapi.security.user_password import create_user_password_security_context
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
from vmware.vapi.vsphere.client import Session, create_vsphere_client

console = Console()


class VAPIClient:
def __init__(self, hostname, username, password):
self.hostname = hostname
self.username = username
self.password = password
self.stub_config = self.get_stub_config()

def get_stub_config(self, vclient_only: bool = False):
api_url = f"https://{self.hostname}/api"
requests.packages.urllib3.disable_warnings()
session = requests.session()
session.verify = False

# return vcenter client
if vclient_only:
return create_vsphere_client(
username=self.username,
password=self.password,
server=self.hostname,
session=session,
)

security_context = create_user_password_security_context(
self.username, self.password
)
connector = get_requests_connector(
session=session,
url=api_url,
)
connector.set_security_context(security_context)
session_service = Session(
StubConfigurationFactory.new_std_configuration(connector)
)

session_id = session_service.create()
security_context = create_session_security_context(session_id)

connector = get_requests_connector(
session=session,
url=api_url,
)
connector.set_security_context(security_context)
return StubConfigurationFactory.new_std_configuration(connector)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, List

from sqlmodel import Field, Relationship

from zpodcommon.models.model_base import ModelBase

if TYPE_CHECKING:
Expand Down
57 changes: 31 additions & 26 deletions zpodengine/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zpodengine/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ python-dotenv = "1.0.0"
sqlmodel = "0.0.8"
pyvmomi = "^8.0.1.0"
jinja2 = "^3.1.2"
cryptography = "40.0.2"

[tool.poetry.group.dev.dependencies]
black = "^23.1.0"
Expand Down
1 change: 1 addition & 0 deletions zpodengine/scripts/create_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
create_deployment(flow="instance_deploy")
create_deployment(flow="instance_destroy")
create_deployment(flow="instance_component_add")
create_deployment(flow="maintenance_operations")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from prefect import task

from zpodcommon import models as M
from zpodcommon.lib.network import create_dnsmasq_config, MgmtIp
from zpodcommon.lib.network import MgmtIp, create_dnsmasq_config
from zpodengine.lib import database


Expand Down
20 changes: 10 additions & 10 deletions zpodengine/src/zpodengine/lib/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

def create_deployment(*, flow: str | Flow, **kwargs):
"""
* flow=[module].[method_name]
ex.
flow="instance_deploy.flow_instance_deploy.flow_instance_deploy"
* flow=[deployment_name]
ex.
flow="instance_deploy". This is equal to "instance_deploy.flow_instance_deploy.flow_instance_deploy".
* flow=method
ex.
from zpodengine.instance_deploy.flow_instance_deploy import flow_instance_deploy
flow=flow_instance_deploy
* flow=[module].[method_name]
ex.
flow="instance_deploy.flow_instance_deploy.flow_instance_deploy"
* flow=[deployment_name]
ex.
flow="instance_deploy". This is equal to "instance_deploy.flow_instance_deploy.flow_instance_deploy".
* flow=method
ex.
from zpodengine.instance_deploy.flow_instance_deploy import flow_instance_deploy
flow=flow_instance_deploy
""" # noqa: E501, B950

def get_flow(flow_):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

import com.vmware.vapi.std.errors_client as vapi_exceptions
from com.vmware.appliance.update_client import Pending
from prefect import flow, get_run_logger, task

from zpodcommon.lib.vapi import VAPIClient


@task(task_run_name="parse VCenter Updates object")
def parse_vc_update_object(updates: list):
if not updates:
return "no updates found"
return [
{
"version": update.version,
"name": update.name,
"description": {
"id": update.description.id,
"default_message": update.description.default_message,
"args": update.description.args,
"params": update.description.params,
"localized": update.description.localized,
},
"severity": update.severity,
"priority": update.priority,
"release_date": update.release_date,
"reboot_required": update.reboot_required,
"category": update.update_type,
"size": update.size,
}
for update in updates
]


@task(task_run_name="Get VCenter Updates")
def get_vcenter_updates():
hostname = os.getenv("VCENTER_HOSTNAME")
username = os.getenv("VCENTER_USERNAME")
password = os.getenv("VCENTER_PASSWORD")

vc = VAPIClient(hostname=hostname, username=username, password=password)

stub_config = vc.get_stub_config()

logger = get_run_logger()
pending_client = Pending(stub_config)
source_type = pending_client.SourceType.LOCAL_AND_ONLINE

try:
logger.info("Checking for VCenter updates")
return pending_client.list(source_type)
except vapi_exceptions.NotFound:
logger.info("No applicable update found")
return []


@flow(name="maintenance_operations", log_prints=True)
def flow_maintenance_operations():
updates = get_vcenter_updates()
vc_parsed_updates = parse_vc_update_object(updates)
print(vc_parsed_updates)


if __name__ == "__main__":
flow_maintenance_operations()