Skip to content

Commit

Permalink
Fixing lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jawatts committed Jul 18, 2019
1 parent 183c2e1 commit 0801794
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 69 deletions.
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ inflection
miq-version>=0.1.6
oauth2client
ovirt-engine-sdk-python~=4.3
#openshift==0.3.4
openshift==0.8.8
packaging
pyvmomi>=6.5.0.2017.5.post1
Expand All @@ -32,7 +31,6 @@ six
tzlocal
vspk==5.3.2
wait_for
#websocket_client
websocket_client==0.56.0

# suds jurko supports python3, suds is only used on python2
Expand Down
3 changes: 2 additions & 1 deletion wrapanapi/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@

__all__ = [
'Template', 'TemplateMixin', 'Vm', 'VmState', 'VmMixin', 'Instance',
'PhysicalContainer', 'Server', 'ServerState', 'Stack', 'StackMixin'
'PhysicalContainer', 'Server', 'ServerState', 'Stack', 'StackMixin',
'Project', 'ProjectMixin'
]
24 changes: 12 additions & 12 deletions wrapanapi/entities/project.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
wrapanapi.entities.project
Methods/classes pertaining to performing actions on a template
Methods/classes pertaining to performing actions on a project
"""
import six

from abc import ABCMeta, abstractmethod, abstractproperty
from abc import ABCMeta, abstractmethod

from wrapanapi.entities.base import Entity, EntityMixin
from wrapanapi.exceptions import MultipleItemsError, NotFoundError
Expand All @@ -15,7 +15,7 @@ class Project(six.with_metaclass(ABCMeta, Entity)):
"""
Represents a project on a system
"""
@abstractproperty
@abstractmethod
def get_quota(self):
"""
Deploy a VM/instance with name 'vm_name' using this template
Expand All @@ -31,30 +31,30 @@ class ProjectMixin(six.with_metaclass(ABCMeta, EntityMixin)):
@abstractmethod
def get_project(self, name, **kwargs):
"""
Get template from system with name 'name'
Get project from system with name 'name'
This should return only ONE matching entity. If multiple entities match
the criteria, a MultipleItemsError should be raised
Returns:
wrapanapi.entities.Template if it exists
wrapanapi.entities.Project if it exists
Raises:
wrapanapi.exceptions.MultipleItemsError if multiple matches are found
"""

@abstractmethod
def create_project(self, name, **kwargs):
"""
Create template on system with name 'name'
Create project on system with name 'name'
Returns:
wrapanapi.entities.Template for newly created templated
wrapanapi.entities.Project for newly created project
"""

@abstractmethod
def list_project(self, **kwargs):
"""
List templates on system
List projects on system
Returns:
list of wrapanapi.entities.Template
Expand All @@ -63,19 +63,19 @@ def list_project(self, **kwargs):
@abstractmethod
def find_projects(self, name, **kwargs):
"""
Find templates on system based on name or other filters in kwargs
Find project on system based on name or other filters in kwargs
Should return an empty list if no matches were found
Returns:
list of wrapanapi.entities.Template for matches found
list of wrapanapi.entities.Project for matches found
"""

def does_project_exist(self, name):
"""
Checks if a template with 'name' exists on the system
Checks if a project with 'name' exists on the system
If multiple templates with the same name exists, this still returns 'True'
If multiple projects with the same name exists, this still returns 'True'
"""
try:
return bool(self.get_project(name))
Expand Down
107 changes: 53 additions & 54 deletions wrapanapi/systems/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import json
import re
import string
import yaml
from collections import Iterable
Expand All @@ -18,8 +19,7 @@
from openshift import client as ociclient
from wait_for import TimedOutError, wait_for

from wrapanapi.entities import (Template, Vm, VmMixin, VmState, ProjectMixin,
Project)
from wrapanapi.entities import (Template, Vm, VmMixin, VmState, ProjectMixin, Project)
from wrapanapi.systems.base import System


Expand Down Expand Up @@ -88,23 +88,31 @@ def wrap(*args, **kwargs):
return wrap


class Project(Project):
class RHOpenShiftProject(Project, Vm):

"""
We are assuming that a Project is a VM for purposes of simplicity for CFME-QE
"""

state_map = {
'Pending': VmState.PENDING,
'Running': VmState.RUNNING,
'Succeeded': VmState.SUCCEEDED,
'Failed': VmState.FAILED,
'Unknown': VmState.UNKNOWN
}

def __init__(self, system, raw=None, **kwargs):
"""
Construct a VMWareVirtualMachine instance
Construct a RHOpenShiftProject instance
Args:
system: instance of VMWareSystem
raw: pyVmomi.vim.VirtualMachine object
name: name of VM
system: instance of OpenShiftSystem
raw: openshift.dynamic.client.ResourceField
name: name of Project
"""
super(Project, self).__init__(system, raw, **kwargs)
super(RHOpenShiftProject, self).__init__(system, raw, **kwargs)
self._name = raw.metadata.name if raw else kwargs.get('name')
if not self._name:
raise ValueError("missing required kwarg 'name'")
Expand All @@ -127,7 +135,6 @@ def _does_project_exist(self):
else:
return False

@property
def get_quota(self):
return self.system.ocp_client.resources.get(api_version='v1', kind='ResourceQuota').get(
namespace=self.name)
Expand All @@ -151,7 +158,18 @@ def uuid(self):
def ip(self):
raise NotImplementedError

@property
def creation_time(self):
"""
Detect the project creation time
"""
raise NotImplementedError

def start(self):
"""
Start the CFME pods
"""
self.logger.info("starting vm/project %s", self.name)
if self._does_project_exist:
for pod in self.system.get_required_pods(self.name):
Expand All @@ -160,11 +178,8 @@ def start(self):
raise ValueError("Project with name {n} doesn't exist".format(n=self.name))

def stop(self):
"""Stops a vm.
Args:
vm_name: name of the vm to be stopped
Returns: whether vm action has been initiated properly
"""
Stop the CFME pods
"""
self.logger.info("stopping vm/project %s", self.name)
if self._does_project_exist:
Expand All @@ -187,7 +202,7 @@ def cleanup(self):
return self.delete()


class Pod(Vm):
class RHOpenShiftPod(Vm):
state_map = {
'Pending': VmState.PENDING,
'Running': VmState.RUNNING,
Expand All @@ -198,14 +213,14 @@ class Pod(Vm):

def __init__(self, system, raw=None, **kwargs):
"""
Construct a VMWareVirtualMachine instance
Construct a RHOpenShiftPod instance
Args:
system: instance of VMWareSystem
raw: pyVmomi.vim.VirtualMachine object
name: name of VM
system: instance of OpenShiftSystem
raw: openshift.dynamic.client.ResourceField
name: name of Pod
"""
super(Pod, self).__init__(system, raw, **kwargs)
super(RHOpenShiftPod, self).__init__(system, raw, **kwargs)
self._name = raw.metadata.name if raw else kwargs.get('name')
self._namespace = raw.metadata.namespace if raw else kwargs.get('namespace')
if not self._name:
Expand Down Expand Up @@ -233,16 +248,15 @@ def namespace(self):

@property
def ip(self):
# TODO JUWATTS
# ipv4_re = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
ipv4_re = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
self.refresh()
try:
return self.raw.status.podIP
ip_address=self.raw.status.podIP
if not re.match(ipv4_re, ip_address) or ip_address == '127.0.0.1':
ip_address = None
return ip_address
except (AttributeError):
# AttributeError: vm doesn't have an ip address yet
# AttributeError: pod doesn't have an ip address yet
return None

def _get_state(self):
Expand Down Expand Up @@ -270,12 +284,7 @@ def cleanup(self):

@property
def creation_time(self):
"""Detect the vm_creation_time either via uptime if non-zero, or by last boot time
The API provides no sensible way to actually get this value. The only way in which
vcenter API MAY have this is by filtering through events
Return tz-naive datetime object
"""Detect the pods creation time
"""
raise NotImplementedError

Expand All @@ -284,12 +293,12 @@ class OpenShiftTemplate(Template):

def __init__(self, system, raw=None, **kwargs):
"""
Construct a VMWareVirtualMachine instance
Construct a OpenShiftTemplate instance
Args:
system: instance of VMWareSystem
raw: pyVmomi.vim.VirtualMachine object
name: name of VM
system: instance of OpenShiftSystem
raw: openshift.dynamic.client.ResourceField
name: name of Template
"""
super(OpenShiftTemplate, self).__init__(system, raw, **kwargs)
self._name = raw.metadata.name if raw else kwargs.get('name')
Expand Down Expand Up @@ -686,19 +695,6 @@ def _k8s_client_connect(self):
# Create a ApiClient with our config
return kubeclient.ApiClient(k8_configuration)

# def _connect(self):
#
# self.dyn_client = DynamicClient(self.k8s_client)

# self.ociclient = ociclient
# self.kclient = kubeclient
# self.oapi_client = ociclient.ApiClient(config=config)
# self.kapi_client = kubeclient.ApiClient(config=config)
# self.o_api = ociclient.OapiApi(api_client=self.oapi_client)
# self.k_api = kubeclient.CoreV1Api(api_client=self.kapi_client)
# self.security_api = self.ociclient.SecurityOpenshiftIoV1Api(api_client=self.oapi_client)
# self.batch_api = self.kclient.BatchV1Api(api_client=self.kapi_client) # for job api

@property
def _identifying_attrs(self):
"""
Expand Down Expand Up @@ -933,7 +929,10 @@ def get_pod(self, name, namespace=None):
else:
pod = self.get_ocp_obj(resource_type=self.v1_pod, name=name)

return Pod(system=self, name=pod.metadata.name, namespace=pod.metadata.namespace, raw=pod)
return RHOpenShiftPod(system=self,
name=pod.metadata.name,
namespace=pod.metadata.namespace,
raw=pod)

def create_vm(self, name, **kwargs):
raise NotImplementedError('This function has not yet been implemented.')
Expand All @@ -950,7 +949,8 @@ def list_pods(self, namespace=None):
list of wrapanapi.entities.Vm
"""
return [
Pod(system=self, name=pod.metadata.name, namespace=pod.metadata.namespace, raw=pod)
RHOpenShiftPod(system=self, name=pod.metadata.name, namespace=pod.metadata.namespace,
raw=pod)
for pod in self.v1_pod.get(namespace=namespace).items]

def wait_project_exist(self, name, wait=60):
Expand All @@ -976,22 +976,22 @@ def create_project(self, name, description=None, **kwargs):

project = self.v1_project.create(body=proj)
self.wait_project_exist(name=name)
return Project(system=self, name=project.metadata.name, raw=project)
return RHOpenShiftProject(system=self, name=project.metadata.name, raw=project)

def find_projects(self, *args, **kwargs):
raise NotImplementedError

def get_project(self, name):
project = self.v1_project.get(name=name)

return Project(system=self, name=project.metadata.name, raw=project)
return RHOpenShiftProject(system=self, name=project.metadata.name, raw=project)

get_vm = get_project

def list_project(self, namespace=None):

return [
Project(system=self, name=project.metadata.name, raw=project)
RHOpenShiftProject(system=self, name=project.metadata.name, raw=project)
for project in self.v1_project.get(namespace=namespace).items]

list_vms = list_project
Expand Down Expand Up @@ -1847,7 +1847,6 @@ def read_pod_log(self, namespace, name):
"""
return self.v1_pod.log.get(name=name, namespace=namespace)


def start_vm(self, vm_name):
"""Starts a vm.
Expand Down Expand Up @@ -1987,5 +1986,5 @@ def run_command(self, namespace, name, cmd, **kwargs):
"""
# there are some limitations and this code isn't robust enough due to
# https://github.com/kubernetes-client/python/issues/58
return self.v1_pod.exec.post(namespace=namespace, name=name, command=cmd, stdout=True,
return self.v1_pod.exec(namespace=namespace, name=name, command=cmd, stdout=True,
stderr=True, **kwargs)

0 comments on commit 0801794

Please sign in to comment.