update code to adhere to OS hacking guidelines
This commit is contained in:
parent
e97b0864af
commit
98b56fe53c
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
import collections
|
||||
import time
|
||||
|
||||
import pkg_resources
|
||||
|
@ -41,10 +41,8 @@ class TeethAgentStatus(encoding.Serializable):
|
|||
self.version = version
|
||||
|
||||
def serialize(self, view):
|
||||
"""
|
||||
Turn the status into a dict.
|
||||
"""
|
||||
return OrderedDict([
|
||||
"""Turn the status into a dict."""
|
||||
return collections.OrderedDict([
|
||||
('mode', self.mode),
|
||||
('started_at', self.started_at),
|
||||
('version', self.version),
|
||||
|
@ -61,9 +59,7 @@ class TeethAgent(object):
|
|||
self.api = api.TeethAgentAPIServer(self)
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
Retrieve a serializable status.
|
||||
"""
|
||||
"""Retrieve a serializable status."""
|
||||
return TeethAgentStatus(
|
||||
mode=self.mode,
|
||||
started_at=self.started_at,
|
||||
|
@ -71,15 +67,11 @@ class TeethAgent(object):
|
|||
)
|
||||
|
||||
def execute_command(self, command):
|
||||
"""
|
||||
Execute an agent command.
|
||||
"""
|
||||
"""Execute an agent command."""
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
Run the Teeth Agent.
|
||||
"""
|
||||
"""Run the Teeth Agent."""
|
||||
if self.started_at:
|
||||
raise RuntimeError('Agent was already started')
|
||||
|
||||
|
|
|
@ -14,9 +14,12 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
import collections
|
||||
|
||||
from teeth_rest import component, encoding, errors, responses
|
||||
from teeth_rest import component
|
||||
from teeth_rest import encoding
|
||||
from teeth_rest import errors
|
||||
from teeth_rest import responses
|
||||
|
||||
|
||||
class AgentCommand(encoding.Serializable):
|
||||
|
@ -26,49 +29,41 @@ class AgentCommand(encoding.Serializable):
|
|||
|
||||
@classmethod
|
||||
def deserialize(cls, obj):
|
||||
if 'name' not in obj:
|
||||
raise errors.InvalidContentError('Missing command \'name\' field.')
|
||||
if 'params' not in obj:
|
||||
raise errors.InvalidContentError('Missing command \'params\' field.')
|
||||
for field in ['name', 'params']:
|
||||
if field not in obj:
|
||||
msg = 'Missing command \'{}\' field.'.format(field)
|
||||
raise errors.InvalidContentError(msg)
|
||||
|
||||
return cls(obj['name'], obj['params'])
|
||||
|
||||
def serialize(self, view):
|
||||
"""
|
||||
Turn a command into a dictionary.
|
||||
"""
|
||||
return OrderedDict([
|
||||
"""Turn a command into a dictionary."""
|
||||
return collections.OrderedDict([
|
||||
('name', self.name),
|
||||
('params', self.params),
|
||||
])
|
||||
|
||||
|
||||
class TeethAgentAPI(component.APIComponent):
|
||||
"""
|
||||
The primary Teeth Agent API.
|
||||
"""
|
||||
"""The primary Teeth Agent API."""
|
||||
|
||||
def __init__(self, agent):
|
||||
super(TeethAgentAPI, self).__init__()
|
||||
self.agent = agent
|
||||
|
||||
def add_routes(self):
|
||||
"""
|
||||
Called during initialization. Override to map relative routes to methods.
|
||||
"""Called during initialization. Override to map relative routes to
|
||||
methods.
|
||||
"""
|
||||
self.route('GET', '/status', self.get_agent_status)
|
||||
self.route('POST', '/command', self.execute_agent_command)
|
||||
|
||||
def get_agent_status(self, request):
|
||||
"""
|
||||
Get the status of the agent.
|
||||
"""
|
||||
"""Get the status of the agent."""
|
||||
return responses.ItemResponse(self.agent.get_status())
|
||||
|
||||
def execute_agent_command(self, request):
|
||||
"""
|
||||
Execute a command on the agent.
|
||||
"""
|
||||
"""Execute a command on the agent."""
|
||||
command = AgentCommand.deserialize(self.parse_content(request))
|
||||
self.agent.execute_command(command)
|
||||
# TODO(russellhaering): implement actual responses
|
||||
|
@ -76,9 +71,7 @@ class TeethAgentAPI(component.APIComponent):
|
|||
|
||||
|
||||
class TeethAgentAPIServer(component.APIServer):
|
||||
"""
|
||||
Server for the teeth agent API.
|
||||
"""
|
||||
"""Server for the teeth agent API."""
|
||||
|
||||
def __init__(self, agent):
|
||||
super(TeethAgentAPIServer, self).__init__()
|
||||
|
|
|
@ -14,9 +14,8 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
"""
|
||||
|
||||
from teeth_agent.agent import TeethAgent
|
||||
from teeth_agent import agent
|
||||
|
||||
|
||||
def run():
|
||||
agent = TeethAgent('0.0.0.0', 9999, 'STANDBY')
|
||||
agent.run()
|
||||
agent.TeethAgent('0.0.0.0', 9999, 'STANDBY').run()
|
||||
|
|
Loading…
Reference in New Issue