Revert Add name argument to Sync template feature.
Extra columns are added to the kingbird database to identify the job. Hence there is no need of "name" argument in "Sync template" feature. Added test-cases for the same. Change-Id: I6a412e550c3cd1ded7088af484dd92f6f2f79e39
This commit is contained in:
parent
87687bc662
commit
7667932ab8
@ -97,6 +97,7 @@ Eg::
|
||||
target:
|
||||
- fake_target_region_1
|
||||
- fake_target_region_2
|
||||
force: "True"
|
||||
|
||||
|
||||
Sample input file for .json
|
||||
@ -112,11 +113,13 @@ Eg::
|
||||
"fake_resource_2"
|
||||
],
|
||||
"source":["fake_source_region"],
|
||||
"target":["fake_target_region_1","fake_target_region_2"]
|
||||
"target":["fake_target_region_1","fake_target_region_2"],
|
||||
"force":"True"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Note: "force" is optional in .yaml and .json files.
|
||||
|
||||
Useful Links
|
||||
============
|
||||
|
@ -83,8 +83,7 @@ class ResourceManager(object):
|
||||
json_object = json_response_key['job_status']
|
||||
resource = []
|
||||
resource.append(self.resource_class(
|
||||
self, name=json_object['name'],
|
||||
id=json_object['id'],
|
||||
self, id=json_object['id'],
|
||||
status=json_object['status'],
|
||||
created_at=json_object['created_at']))
|
||||
return resource
|
||||
@ -98,8 +97,7 @@ class ResourceManager(object):
|
||||
resource = []
|
||||
for json_object in json_objects:
|
||||
resource.append(self.resource_class(
|
||||
self, name=json_object['name'],
|
||||
id=json_object['id'],
|
||||
self, id=json_object['id'],
|
||||
status=json_object['sync_status'],
|
||||
created_at=json_object['created_at'],
|
||||
updated_at=json_object['updated_at']))
|
||||
|
@ -21,9 +21,8 @@ class Resource(base.Resource):
|
||||
|
||||
def __init__(self, manager, status, created_at, updated_at=None,
|
||||
resource_type=None, target_region=None,
|
||||
source_region=None, name=None, id=None, resource_name=None,):
|
||||
source_region=None, id=None, resource_name=None,):
|
||||
self.manager = manager
|
||||
self.name = name
|
||||
self.id = id
|
||||
self.source_region = source_region
|
||||
self.target_region = target_region
|
||||
|
@ -23,7 +23,6 @@ from kingbirdclient import exceptions
|
||||
|
||||
def format(resources=None):
|
||||
columns = (
|
||||
'NAME',
|
||||
'ID',
|
||||
'STATUS',
|
||||
'CREATED_AT',
|
||||
@ -32,7 +31,6 @@ def format(resources=None):
|
||||
|
||||
if resources:
|
||||
data = (
|
||||
resources.name,
|
||||
resources.id,
|
||||
resources.status,
|
||||
resources.created_at,
|
||||
@ -95,26 +93,6 @@ def sync_format(resources=None):
|
||||
return columns, data
|
||||
|
||||
|
||||
def template_sync_format(resources=None):
|
||||
columns = (
|
||||
'NAME',
|
||||
'STATUS',
|
||||
'CREATED_AT',
|
||||
)
|
||||
|
||||
if resources:
|
||||
data = (
|
||||
resources.name,
|
||||
resources.status,
|
||||
resources.created_at,
|
||||
)
|
||||
|
||||
else:
|
||||
data = (tuple('<none>' for _ in range(len(columns))),)
|
||||
|
||||
return columns, data
|
||||
|
||||
|
||||
class ResourceSync(base.KingbirdLister):
|
||||
"""Sync Resources from One region to other."""
|
||||
|
||||
@ -173,71 +151,65 @@ class TemplateResourceSync(base.KingbirdLister):
|
||||
"""Sync multiple resource-types to multiple regions."""
|
||||
|
||||
def _get_format_function(self):
|
||||
return template_sync_format
|
||||
return sync_format
|
||||
|
||||
def get_parser(self, parsed_args):
|
||||
parser = super(TemplateResourceSync, self).get_parser(parsed_args)
|
||||
|
||||
parser.add_argument(
|
||||
'--template',
|
||||
required=True,
|
||||
'template',
|
||||
help='Specify the name of an input file in .yaml/.yml/.json.'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
required=True,
|
||||
help='Name of the job.'
|
||||
)
|
||||
return parser
|
||||
|
||||
def _get_resources(self, parsed_args):
|
||||
kingbird_client = self.app.client_manager.sync_engine
|
||||
kwargs = dict()
|
||||
sync_template = parsed_args.template
|
||||
kwargs['name'] = parsed_args.name
|
||||
if sync_template.endswith('.yaml') or sync_template.endswith('.yml') \
|
||||
or sync_template.endswith('.json'):
|
||||
try:
|
||||
if sync_template.endswith('.json'):
|
||||
with open(sync_template) as json_data:
|
||||
data = json.load(json_data)
|
||||
try:
|
||||
with open(sync_template) as template_data:
|
||||
if sync_template.endswith('.yaml') or sync_template.endswith('.yml') \
|
||||
or sync_template.endswith('.json'):
|
||||
try:
|
||||
if sync_template.endswith('.json'):
|
||||
data = json.load(template_data)
|
||||
else:
|
||||
data = yaml.load(template_data)
|
||||
except Exception:
|
||||
raise exceptions.TemplateError(
|
||||
'Syntactical errors in the template')
|
||||
else:
|
||||
data = yaml.load(open(sync_template))
|
||||
except Exception:
|
||||
raise exceptions.TemplateError(
|
||||
'Syntactical errors in the template')
|
||||
else:
|
||||
raise exceptions.TemplateError(
|
||||
'Provide a template with a valid extension(.yaml/.yml/.json)')
|
||||
for iteration in data['Sync']:
|
||||
if 'source' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'source_region parameter is missing in template')
|
||||
if not iteration['source']:
|
||||
raise exceptions.TemplateError(
|
||||
'source_region parameter value is missing')
|
||||
if 'target' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'target_region parameter is missing in template')
|
||||
if not iteration['target']:
|
||||
raise exceptions.TemplateError(
|
||||
'target_region parameter value is missing')
|
||||
if 'resource_type' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'resource_type parameter is missing in template')
|
||||
if not iteration['resource_type']:
|
||||
raise exceptions.TemplateError(
|
||||
'resource_type parameter value is missing')
|
||||
if 'resources' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'resources parameter is missing in template')
|
||||
if not iteration['resources']:
|
||||
raise exceptions.TemplateError(
|
||||
'resources parameter value is missing')
|
||||
|
||||
raise exceptions.TemplateError(
|
||||
'Invalid extension.Provide .yaml/.yml/.json template')
|
||||
for iteration in data['Sync']:
|
||||
if 'source' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'source_region parameter is missing in template')
|
||||
if not iteration['source']:
|
||||
raise exceptions.TemplateError(
|
||||
'source_region parameter value is missing')
|
||||
if 'target' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'target_region parameter is missing in template')
|
||||
if not iteration['target']:
|
||||
raise exceptions.TemplateError(
|
||||
'target_region parameter value is missing')
|
||||
if 'resource_type' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'resource_type parameter is missing in template')
|
||||
if not iteration['resource_type']:
|
||||
raise exceptions.TemplateError(
|
||||
'resource_type parameter value is missing')
|
||||
if 'resources' not in iteration:
|
||||
raise exceptions.TemplateError(
|
||||
'resources parameter is missing in template')
|
||||
if not iteration['resources']:
|
||||
raise exceptions.TemplateError(
|
||||
'resources parameter value is missing')
|
||||
except Exception:
|
||||
raise
|
||||
kwargs.update(data)
|
||||
|
||||
return kingbird_client.sync_manager.sync_resources(**kwargs)
|
||||
|
||||
|
||||
@ -277,14 +249,14 @@ class SyncShow(base.KingbirdLister):
|
||||
parser = super(SyncShow, self).get_parser(parsed_args)
|
||||
|
||||
parser.add_argument(
|
||||
'job',
|
||||
help='ID/Name of the job to view the details.'
|
||||
'job_id',
|
||||
help='ID of the job to view the details.'
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
def _get_resources(self, parsed_args):
|
||||
job = parsed_args.job
|
||||
job = parsed_args.job_id
|
||||
kingbird_client = self.app.client_manager.sync_engine
|
||||
return kingbird_client.sync_manager.sync_job_detail(job)
|
||||
|
||||
|
@ -31,7 +31,6 @@ FAKE_RESOURCE = 'fake_item'
|
||||
FAKE_SOURCE_REGION = 'fake_region_1'
|
||||
FAKE_TARGET_REGION = 'fake_region_2'
|
||||
FAKE_RESOURCE_TYPE = 'fake_resource'
|
||||
FAKE_NAME = 'fake_name'
|
||||
|
||||
tempdef = """Sync:
|
||||
- resource_type: fake_resource_type
|
||||
@ -69,7 +68,6 @@ SOURCE_INDEX_JSON = tempdefjson.index('"source"')
|
||||
TARGET_INDEX_JSON = tempdefjson.index(",", SOURCE_INDEX_JSON)
|
||||
|
||||
RESOURCE_DICT = {
|
||||
'NAME': FAKE_NAME,
|
||||
'ID': ID,
|
||||
'STATUS': FAKE_STATUS,
|
||||
'CREATED_AT': TIME_NOW,
|
||||
@ -77,21 +75,18 @@ RESOURCE_DICT = {
|
||||
}
|
||||
|
||||
ACTIVE_RESOURCE_DICT = {
|
||||
'NAME': FAKE_NAME,
|
||||
'ID': ID,
|
||||
'STATUS': ACTIVE_FAKE_STATUS,
|
||||
'CREATED_AT': TIME_NOW,
|
||||
'UPDATED_AT': TIME_NOW
|
||||
}
|
||||
|
||||
SYNCMANAGER = sm.Resource(mock, name=FAKE_NAME,
|
||||
id=RESOURCE_DICT['ID'],
|
||||
SYNCMANAGER = sm.Resource(mock, id=RESOURCE_DICT['ID'],
|
||||
status=RESOURCE_DICT['STATUS'],
|
||||
created_at=RESOURCE_DICT['CREATED_AT'],
|
||||
updated_at=RESOURCE_DICT['UPDATED_AT'])
|
||||
|
||||
ACTIVE_SYNCMANAGER = sm.Resource(mock, name=FAKE_NAME,
|
||||
id=ACTIVE_RESOURCE_DICT['ID'],
|
||||
ACTIVE_SYNCMANAGER = sm.Resource(mock, id=ACTIVE_RESOURCE_DICT['ID'],
|
||||
status=ACTIVE_RESOURCE_DICT['STATUS'],
|
||||
created_at=ACTIVE_RESOURCE_DICT['CREATED_AT'],
|
||||
updated_at=ACTIVE_RESOURCE_DICT['UPDATED_AT'])
|
||||
@ -121,11 +116,6 @@ SYNC_RESOURCEMANAGER = sm.Resource(mock, id=RESOURCE_DICT['ID'],
|
||||
status=RESOURCE_DICT['STATUS'],
|
||||
created_at=RESOURCE_DICT['CREATED_AT'])
|
||||
|
||||
TEMPLATE_SYNC_RESOURCEMANAGER = sm.Resource(mock, name=RESOURCE_DICT['NAME'],
|
||||
status=RESOURCE_DICT['STATUS'],
|
||||
created_at=RESOURCE_DICT[
|
||||
'CREATED_AT'])
|
||||
|
||||
|
||||
class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
"""Testcases for sync command."""
|
||||
@ -133,20 +123,20 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
def test_sync_jobs_list(self):
|
||||
self.client.sync_manager.list_sync_jobs.return_value = [SYNCMANAGER]
|
||||
actual_call = self.call(sync_cmd.SyncList)
|
||||
self.assertEqual([(FAKE_NAME, ID, FAKE_STATUS, TIME_NOW, TIME_NOW)],
|
||||
self.assertEqual([(ID, FAKE_STATUS, TIME_NOW, TIME_NOW)],
|
||||
actual_call[1])
|
||||
|
||||
def test_negative_sync_jobs_list(self):
|
||||
self.client.sync_manager.list_sync_jobs.return_value = []
|
||||
actual_call = self.call(sync_cmd.SyncList)
|
||||
self.assertEqual((('<none>', '<none>', '<none>', '<none>', '<none>'),),
|
||||
self.assertEqual((('<none>', '<none>', '<none>', '<none>'),),
|
||||
actual_call[1])
|
||||
|
||||
def test_active_sync_jobs_list(self):
|
||||
self.client.sync_manager.list_sync_jobs.\
|
||||
return_value = [ACTIVE_SYNCMANAGER]
|
||||
actual_call = self.call(sync_cmd.SyncList, app_args=['--active'])
|
||||
self.assertEqual([(FAKE_NAME, ID, ACTIVE_FAKE_STATUS,
|
||||
self.assertEqual([(ID, ACTIVE_FAKE_STATUS,
|
||||
TIME_NOW, TIME_NOW)],
|
||||
actual_call[1])
|
||||
|
||||
@ -247,17 +237,21 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
'--force'])
|
||||
self.assertEqual([(ID, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
|
||||
def test_template_resource_sync_file_not_found(self):
|
||||
self.assertRaises(
|
||||
IOError, self.call,
|
||||
sync_cmd.TemplateResourceSync, app_args=['test_template.txt'])
|
||||
|
||||
def test_template_resource_sync_with_template_yaml(self):
|
||||
with open('test_template.yaml', 'w') as f:
|
||||
f.write(tempdef)
|
||||
f.close()
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [TEMPLATE_SYNC_RESOURCEMANAGER]
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
actual_call = self.call(
|
||||
sync_cmd.TemplateResourceSync, app_args=[
|
||||
'--template', 'test_template.yaml',
|
||||
'--name', FAKE_NAME])
|
||||
self.assertEqual([(FAKE_NAME, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
'test_template.yaml'])
|
||||
self.assertEqual([(ID, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
os.remove("test_template.yaml")
|
||||
|
||||
def test_template_resource_sync_with_template_yml(self):
|
||||
@ -265,12 +259,11 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
f.write(tempdef)
|
||||
f.close()
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [TEMPLATE_SYNC_RESOURCEMANAGER]
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
actual_call = self.call(
|
||||
sync_cmd.TemplateResourceSync, app_args=[
|
||||
'--template', 'test_template.yml',
|
||||
'--name', FAKE_NAME])
|
||||
self.assertEqual([(FAKE_NAME, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
'test_template.yml'])
|
||||
self.assertEqual([(ID, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
os.remove("test_template.yml")
|
||||
|
||||
def test_template_resource_sync_with_template_json(self):
|
||||
@ -278,40 +271,28 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
f.write(tempdefjson)
|
||||
f.close()
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [TEMPLATE_SYNC_RESOURCEMANAGER]
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
actual_call = self.call(
|
||||
sync_cmd.TemplateResourceSync, app_args=[
|
||||
'--template', 'test_template.json',
|
||||
'--name', FAKE_NAME])
|
||||
self.assertEqual([(FAKE_NAME, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
'test_template.json'])
|
||||
self.assertEqual([(ID, FAKE_STATUS, TIME_NOW)], actual_call[1])
|
||||
os.remove("test_template.json")
|
||||
|
||||
def test_template_resource_sync_without_arguments(self):
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
self.assertRaises(
|
||||
SystemExit, self.call, sync_cmd.TemplateResourceSync, app_args=[])
|
||||
|
||||
def test_template_resource_sync_without_template(self):
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
self.assertRaises(
|
||||
SystemExit, self.call, sync_cmd.TemplateResourceSync,
|
||||
app_args=['--name', FAKE_NAME])
|
||||
|
||||
def test_template_resource_sync_without_name(self):
|
||||
self.client.sync_manager.sync_resources.\
|
||||
return_value = [SYNC_RESOURCEMANAGER]
|
||||
self.assertRaises(
|
||||
SystemExit, self.call, sync_cmd.TemplateResourceSync, app_args=[
|
||||
'--template', 'test_template.yaml'])
|
||||
app_args=[])
|
||||
|
||||
def test_template_resource_sync_invalid_extension(self):
|
||||
with open('test_template_invalid_extension.yzx', 'w') as f:
|
||||
f.write("")
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync, app_args=[
|
||||
'--template', 'test_template.yzx',
|
||||
'--name', 'FAKE_NAME'])
|
||||
'test_template_invalid_extension.yzx'])
|
||||
os.remove("test_template_invalid_extension.yzx")
|
||||
|
||||
def test_template_resource_sync_source_missing_yaml(self):
|
||||
temp = tempdef.replace(tempdef[SOURCE_INDEX:TARGET_INDEX], "")
|
||||
@ -321,8 +302,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_source_missing_template.yaml',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_source_missing_template.yaml'])
|
||||
os.remove("test_source_missing_template.yaml")
|
||||
|
||||
def test_template_resource_sync_target_missing_yaml(self):
|
||||
@ -333,8 +313,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_target_missing_template.yaml',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_target_missing_template.yaml'])
|
||||
os.remove("test_target_missing_template.yaml")
|
||||
|
||||
def test_template_resource_sync_resource_type_missing_yaml(self):
|
||||
@ -345,9 +324,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template',
|
||||
'test_resource_type_missing_template.yaml',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_resource_type_missing_template.yaml'])
|
||||
os.remove("test_resource_type_missing_template.yaml")
|
||||
|
||||
def test_template_resource_sync_resources_missing_yaml(self):
|
||||
@ -358,8 +335,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_resource_missing_template.yaml',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_resource_missing_template.yaml'])
|
||||
os.remove("test_resource_missing_template.yaml")
|
||||
|
||||
def test_template_resource_sync_source_missing_json(self):
|
||||
@ -371,8 +347,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_source_missing_template.json',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_source_missing_template.json'])
|
||||
os.remove("test_source_missing_template.json")
|
||||
|
||||
def test_template_resource_sync_target_missing_json(self):
|
||||
@ -384,8 +359,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_target_missing_template.json',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_target_missing_template.json'])
|
||||
os.remove("test_target_missing_template.json")
|
||||
|
||||
def test_template_resource_sync_resource_type_missing_json(self):
|
||||
@ -397,9 +371,7 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template',
|
||||
'test_resource_type_missing_template.json',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_resource_type_missing_template.json'])
|
||||
os.remove("test_resource_type_missing_template.json")
|
||||
|
||||
def test_template_resource_sync_resources_missing_json(self):
|
||||
@ -411,6 +383,5 @@ class TestCLISyncManagerV1(base.BaseCommandTest):
|
||||
self.assertRaises(
|
||||
exceptions.TemplateError, self.call,
|
||||
sync_cmd.TemplateResourceSync,
|
||||
app_args=['--template', 'test_resource_missing_template.json',
|
||||
'--name', FAKE_NAME])
|
||||
app_args=['test_resource_missing_template.json'])
|
||||
os.remove("test_resource_missing_template.json")
|
||||
|
Loading…
x
Reference in New Issue
Block a user