Browse Source

add support to the client for setting attributes..

main
John-Mark Gurney 4 years ago
parent
commit
efe0bd2177
1 changed files with 73 additions and 0 deletions
  1. +73
    -0
      bitelab/__main__.py

+ 73
- 0
bitelab/__main__.py View File

@@ -49,6 +49,26 @@ def check_res_code(res):
print('Got status: %d, json: %s' % (res.status_code, res.json()))
sys.exit(1)

def makebool(s):
s = s.lower()
if s in { 'on', 'true', '1' }:
return True
elif s in { 'off', 'false', '0' }:
return False

raise ValueError('invalid value for bool: %s' % repr(s))

_typeconv = dict(power=makebool)
def make_attrs(*args):
'''Convert list of attributes to a dictionary.'''

r = {}
for i in args:
k, v = i.split('=', 1)
r[k] = _typeconv[k](v)

return r

async def real_main():
baseurl = os.environ['BITELAB_URL']
authkey = os.environ['BITELAB_AUTH']
@@ -74,6 +94,20 @@ async def real_main():

check_res_code(res)

brd = Board.parse_obj(res.json())
print('Name:\t%s' % brd.name)
print('Class:\t%s' % brd.brdclass)
print('Attributes:')
for i in sorted(brd.attrs):
print('\t%s\t%s' % (i, brd.attrs[i]))
elif sys.argv[1] == 'set':
board_id = sys.argv[-1]
res = await client.post('board/%s/attrs' %
urllib.parse.quote(board_id, safe=''),
auth=BiteAuth(authkey), json=make_attrs(*sys.argv[2:-1]))

check_res_code(res)

brd = Board.parse_obj(res.json())
print('Name:\t%s' % brd.name)
print('Class:\t%s' % brd.brdclass)
@@ -216,3 +250,42 @@ Attributes:
ac.assert_called_with(base_url='http://someserver/')

acp.assert_called_with('board/cora-z7s/release', auth=BiteAuth('thisisanapikey'))

@patch.dict(sys.__dict__, dict(argv=[ '', 'set', 'power=on', 'cora-z7s' ]))
def test_set(self):
ac = self.ac
acp = self.acp
acp.return_value.status_code = HTTP_200_OK
acp.return_value.json.return_value = Board(name='cora-1',
brdclass='cora-z7s', reserved=True,
attrs={
'ip': '172.20.20.5',
'power': True,
}).dict()

ret, stdout = self.runMain()

output = '''Name:\tcora-1
Class:\tcora-z7s
Attributes:
\tip\t172.20.20.5
\tpower\tTrue
'''

self.assertEqual(ret, 0)
self.assertEqual(stdout, output)

ac.assert_called_with(base_url='http://someserver/')

acp.assert_called_with('board/cora-z7s/attrs',
auth=BiteAuth('thisisanapikey'), json=dict(power=True))

def test_make_attrs(self):
self.assertEqual(make_attrs('power=on'), dict(power=True))
self.assertEqual(make_attrs('power=True'), dict(power=True))
self.assertEqual(make_attrs('power=1'), dict(power=True))
self.assertEqual(make_attrs('power=off'), dict(power=False))
self.assertEqual(make_attrs('power=FALSE'), dict(power=False))
self.assertEqual(make_attrs('power=0'), dict(power=False))

self.assertRaises(ValueError, make_attrs, 'power=bar')

Loading…
Cancel
Save