Permalink
Browse files

added KV crud support for VLAN/NET/SITE objects

  • Loading branch information...
1 parent 8cbd9f5 commit 35e2d3cc83e33937d9304eba170eb9f5f9903440 @uberj uberj committed Aug 6, 2013
View
12 invtool/dispatch.py
@@ -123,14 +123,18 @@ def create(self, nas):
url = "{0}{1}".format(REMOTE, self.create_url(nas))
return self.action(nas, url, requests.post, data)
- def action(self, nas, url, method, data):
+ def action(self, nas, url, method, data, form_encode=True):
headers = {'content-type': 'application/json'}
- data = json.dumps(data, indent=2)
+ if form_encode:
+ wire_data = json.dumps(data, indent=2)
+ else:
+ wire_data = data
+
if nas.DEBUG:
sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
- method.__name__, url, data
+ method.__name__, url, json.dumps(data, indent=2)
))
- resp = method(url, headers=headers, data=data, auth=auth)
+ resp = method(url, headers=headers, data=wire_data, auth=auth)
return self.handle_resp(nas, data, resp)
def get_create_data(self, nas):
View
112 invtool/kv/kv_core_dispatch.py
@@ -1,68 +1,116 @@
from invtool.lib.registrar import registrar
-from invtool.lib.kv_options import kvlist_pk_argument
-from invtool.kv.kv_dispatch import DispatchKV, build_kvlist_parser
-from invtool.lib.dns_options import detail_pk_argument
-from invtool.lib.parser import build_detail_parser
-
-
-class CoreDispatchKV(DispatchKV):
- NO_TEST = True
-
- def build_parser(self, base_parser):
- record_base_parser = base_parser.add_parser(
- self.dtype,
- help="The interface for CRUDing {0} Key Value "
- "pairs".format(self.dtype),
- add_help=True
- )
- action_parser = record_base_parser.add_subparsers(
- help="{0} record actions".format(self.dtype),
- dest='action'
- )
- build_detail_parser(
- self, action_parser, help="Detail a {0} KV pair".format(self.dtype)
- )
- build_kvlist_parser(self, action_parser)
-
- def do_test_setup(self, test_case):
- return False
-
-
-class NetworkKV(CoreDispatchKV):
+from invtool.tests.utils import (
+ call_to_json, test_method_to_params, EXEC, TestKVSetupMixin
+)
+from invtool.kv.kv_dispatch import DispatchKV
+from invtool.lib.kv_options import (
+ key_argument, value_argument, update_pk_argument,
+ delete_pk_argument, detail_pk_argument, kvlist_pk_argument,
+ create_pk_argument
+)
+from invtool.core_dispatch import DispatchNetwork, DispatchVlan, DispatchSite
+
+
+class NetworkKV(DispatchKV, TestKVSetupMixin):
kv_class = 'networkkeyvalue'
dtype = 'NET_kv'
dgroup = 'kv'
+ create_args = [
+ key_argument('key'),
+ value_argument('value'),
+ create_pk_argument('obj_pk', dtype)
+ ]
+
+ update_args = [
+ key_argument('key'),
+ value_argument('value'),
+ update_pk_argument('kv_pk', dtype)
+ ]
+
+ delete_args = [
+ delete_pk_argument('kv_pk', dtype)
+ ]
detail_args = [detail_pk_argument('kv_pk', dtype)]
kvlist_args = [kvlist_pk_argument('obj_pk', dtype)]
+ def do_test_setup(self, test_case):
+ command = [EXEC, DispatchNetwork.dtype, 'create']
+ for add_arg, extract_arg, tm in DispatchNetwork.create_args:
+ if tm()[0] in ('site-pk', 'vlan-pk'):
+ continue
+ command.append(test_method_to_params(tm()))
+ command_str = ' '.join(command)
+ ret, errors, rc = call_to_json(command_str)
+ if errors:
+ test_case.fail(errors)
+ return ret['pk']
+
registrar.register(NetworkKV())
-class SiteKV(CoreDispatchKV):
+class SiteKV(DispatchKV, TestKVSetupMixin):
kv_class = 'sitekeyvalue'
dtype = 'SITE_kv'
dgroup = 'kv'
+ create_args = [
+ key_argument('key'),
+ value_argument('value'),
+ create_pk_argument('obj_pk', dtype)
+ ]
+
+ update_args = [
+ key_argument('key'),
+ value_argument('value'),
+ update_pk_argument('kv_pk', dtype)
+ ]
+
+ delete_args = [
+ delete_pk_argument('kv_pk', dtype)
+ ]
+
detail_args = [detail_pk_argument('kv_pk', dtype)]
kvlist_args = [kvlist_pk_argument('obj_pk', dtype)]
+ def do_test_setup(self, test_case):
+ return self.do_setup(DispatchSite, test_case)
+
registrar.register(SiteKV())
-class VlanKV(CoreDispatchKV):
+class VlanKV(DispatchKV, TestKVSetupMixin):
kv_class = 'vlankeyvalue'
dtype = 'VLAN_kv'
dgroup = 'kv'
+ create_args = [
+ key_argument('key'),
+ value_argument('value'),
+ create_pk_argument('obj_pk', dtype)
+ ]
+
+ update_args = [
+ key_argument('key'),
+ value_argument('value'),
+ update_pk_argument('kv_pk', dtype)
+ ]
+
+ delete_args = [
+ delete_pk_argument('kv_pk', dtype)
+ ]
+
detail_args = [detail_pk_argument('kv_pk', dtype)]
kvlist_args = [kvlist_pk_argument('obj_pk', dtype)]
+ def do_test_setup(self, test_case):
+ return self.do_setup(DispatchVlan, test_case)
+
registrar.register(VlanKV())
View
8 invtool/kv/kv_dispatch.py
@@ -2,12 +2,10 @@
import simplejson as json
except ImportError:
import json
-import sys
import requests
from invtool.dispatch import Dispatch
-from invtool.lib.config import REMOTE, auth
-
+from invtool.lib.config import REMOTE
from invtool.lib.parser import (
build_create_parser, build_update_parser, build_delete_parser,
@@ -53,6 +51,10 @@ def build_parser(self, base_parser):
)
build_kvlist_parser(self, action_parser)
+ def action(self, nas, url, method, data, **kwargs):
+ kwargs.update(**{'form_encode': False})
+ return super(DispatchKV, self).action(nas, url, method, data, **kwargs)
+
def update(self, nas):
# We have to override this because the default update uses PATCH, we
# need to use POST because this dispatch isn't for a tastypie endpoint
View
18 invtool/kv/kv_sreg_dispatch.py
@@ -3,27 +3,15 @@
from invtool.tests.utils import call_to_json, test_method_to_params, EXEC
from invtool.kv.kv_dispatch import DispatchKV
+from invtool.tests.utils import TestKVSetupMixin
from invtool.lib.kv_options import (
key_argument, value_argument, update_pk_argument,
delete_pk_argument, detail_pk_argument, kvlist_pk_argument,
create_pk_argument
)
-class TestSetupMixin(object):
- def do_setup(self, DispatchType, test_case):
- """Create an object and return it's pk so the KV api can test itself"""
- command = [EXEC, DispatchType.dtype, 'create']
- for add_arg, extract_arg, tm in DispatchType.create_args:
- command.append(test_method_to_params(tm()))
- command_str = ' '.join(command)
- ret, errors, rc = call_to_json(command_str)
- if errors:
- test_case.fail(errors)
- return ret['pk']
-
-
-class StaticRegKV(DispatchKV, TestSetupMixin):
+class StaticRegKV(DispatchKV, TestKVSetupMixin):
kv_class = 'staticregkeyvalue'
dtype = 'SREG_kv'
dgroup = 'kv'
@@ -51,7 +39,7 @@ def do_test_setup(self, test_case):
return self.do_setup(DispatchSREG, test_case)
-class HWAdapterKV(DispatchKV, TestSetupMixin):
+class HWAdapterKV(DispatchKV, TestKVSetupMixin):
kv_class = 'hwadapterkeyvalue'
dtype = 'HW_kv'
dgroup = 'kv'
View
2 invtool/tests/kv_tests.py
@@ -149,7 +149,7 @@ def build_testcases(dispatch):
test_cases = []
# Build KV test cases
for dispatch in registrar.dispatches:
- if dispatch.dgroup == 'kv' and not dispatch.NO_TEST:
+ if dispatch.dgroup == 'kv':
tc = build_testcases(dispatch)
test_cases.append(tc)
View
14 invtool/tests/utils.py
@@ -23,7 +23,6 @@ def call_to_json(command_str):
Given a string, this function will shell out, execute the command
and parse the json returned by that command
"""
- print command_str
p = subprocess.Popen(shlex.split(command_str),
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
@@ -37,3 +36,16 @@ def call_to_json(command_str):
return (None,
"Ret was: {0}. Got error: {1}".format(stdout, str(e)),
p.returncode)
+
+
+class TestKVSetupMixin(object):
+ def do_setup(self, DispatchType, test_case):
+ """Create an object and return it's pk so the KV api can test itself"""
+ command = [EXEC, DispatchType.dtype, 'create']
+ for add_arg, extract_arg, tm in DispatchType.create_args:
+ command.append(test_method_to_params(tm()))
+ command_str = ' '.join(command)
+ ret, errors, rc = call_to_json(command_str)
+ if errors:
+ test_case.fail(errors)
+ return ret['pk']

0 comments on commit 35e2d3c

Please sign in to comment.