mounikasreeram 90d71c0749 Add Sync template feature to KB-client.
Existing feature "sync create" is to sync single resource-type
to single target region i.e one resource off one resource-type
to one region.
Issue with "sync create":
Kingbird cannot sync more than one resource-type to multiple
target regions in a single request.
This commit from kingbird client is to enhance the existing
"sync create" job.
Working of sync template feature:
User have to provide a input file with .yaml or .yml or .json extension.
This input file should consists of
    ->resource-type
    ->resources
    ->source_region
    ->target_region.
Added testcases for the same.

Change-Id: I5fb0e4619b20201585e5398013ba25219e24dc80
2017-12-08 10:19:34 +05:30

286 lines
8.5 KiB
Python

# Copyright (c) 2017 Ericsson AB.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import yaml
from osc_lib.command import command
from kingbirdclient.commands.v1 import base
from kingbirdclient import exceptions
def format(resources=None):
columns = (
'ID',
'STATUS',
'CREATED_AT',
'UPDATED_AT',
)
if resources:
data = (
resources.id,
resources.status,
resources.created_at,
resources.updated_at,
)
else:
data = (tuple('<none>' for _ in range(len(columns))),)
return columns, data
def detail_format(resources=None):
columns = (
'RESOURCE',
'SOURCE_REGION',
'TARGET_REGION',
'RESOURCE_TYPE',
'STATUS',
'CREATED_AT',
'UPDATED_AT',
)
if resources:
data = (
resources.resource_name,
resources.source_region,
resources.target_region,
resources.resource_type,
resources.status,
resources.created_at,
resources.updated_at,
)
else:
data = (tuple('<none>' for _ in range(len(columns))),)
return columns, data
def sync_format(resources=None):
columns = (
'ID',
'STATUS',
'CREATED_AT',
)
if resources:
data = (
resources.id,
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."""
def _get_format_function(self):
return sync_format
def get_parser(self, parsed_args):
parser = super(ResourceSync, self).get_parser(parsed_args)
parser.add_argument(
'--source',
required=True,
help='Source Region from which resources have to be synced.'
)
parser.add_argument(
'--target',
action='append',
required=True,
help='Target Region to which resources have to be synced.'
)
parser.add_argument(
'--resource_type',
required=True,
help='Type of the resource to be synced.'
)
parser.add_argument(
'--resources',
action='append',
required=True,
help='Identifier of the resource',
)
parser.add_argument(
'--force',
action='store_true',
help='Overwrites existing resources on the target regions.'
)
return parser
def _get_resources(self, parsed_args):
kingbird_client = self.app.client_manager.sync_engine
kwargs = dict()
kwargs['resource_type'] = parsed_args.resource_type
kwargs['force'] = str(parsed_args.force)
kwargs['resources'] = parsed_args.resources
kwargs['source'] = parsed_args.source
kwargs['target'] = parsed_args.target
return kingbird_client.sync_manager.sync_resources(**kwargs)
class TemplateResourceSync(base.KingbirdLister):
"""Sync multiple resource-types to multiple regions."""
def _get_format_function(self):
return sync_format
def get_parser(self, parsed_args):
parser = super(TemplateResourceSync, self).get_parser(parsed_args)
parser.add_argument(
'--template',
required=True,
help='Specify the name of an input file in .yaml/.yml/.json.'
)
return parser
def _get_resources(self, parsed_args):
kingbird_client = self.app.client_manager.sync_engine
kwargs = dict()
sync_template = parsed_args.template
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)
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_region' not in iteration:
raise exceptions.TemplateError(
'source_region parameter is missing in template')
if not iteration['source_region']:
raise exceptions.TemplateError(
'source_region parameter value is missing')
if 'target_region' not in iteration:
raise exceptions.TemplateError(
'target_region parameter is missing in template')
if not iteration['target_region']:
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')
kwargs.update(data)
return kingbird_client.sync_manager.sync_resources(**kwargs)
class SyncList(base.KingbirdLister):
"""List Sync Jobs."""
def _get_format_function(self):
return format
def get_parser(self, parsed_args):
parser = super(SyncList, self).get_parser(parsed_args)
parser.add_argument(
'--active',
action='store_true',
help='View the list of active jobs.'
)
return parser
def _get_resources(self, parsed_args):
active = parsed_args.active
action = None
kingbird_client = self.app.client_manager.sync_engine
if active:
action = 'active'
return kingbird_client.sync_manager.list_sync_jobs(action)
class SyncShow(base.KingbirdLister):
"""List the details of a Sync Job."""
def _get_format_function(self):
return detail_format
def get_parser(self, parsed_args):
parser = super(SyncShow, self).get_parser(parsed_args)
parser.add_argument(
'job_id',
help='ID of Job to view the details.'
)
return parser
def _get_resources(self, parsed_args):
job_id = parsed_args.job_id
kingbird_client = self.app.client_manager.sync_engine
return kingbird_client.sync_manager.sync_job_detail(job_id)
class SyncDelete(command.Command):
"""Delete Sync Job(s) details from the database."""
def get_parser(self, prog_name):
parser = super(SyncDelete, self).get_parser(prog_name)
parser.add_argument(
'job_id',
nargs="+",
help='ID of the job to delete entries in database.'
)
return parser
def take_action(self, parsed_args):
jobs = parsed_args.job_id
kingbird_client = self.app.client_manager.sync_engine
for job in jobs:
try:
kingbird_client.sync_manager.delete_sync_job(job)
except Exception as e:
print (e)
error_msg = "Unable to delete the entries of %s" % (job)
raise exceptions.KingbirdClientException(error_msg)