NFV API to list current strategy type and state.

This new NFV API will list the current running
Strategy type and its state.

Test Plan:
PASSED: On a DX system, Create a strategy of any type.
API should return the strategy type and state.
PASSED: On a DX system, there are no existing strategies.
API returns none.

Story: 2011045
Task: 49580

Change-Id: I2e367ccb5f9ff42aa0d912598fb1617a4a7ae0ee
Signed-off-by: Vanathi.Selvaraju <vanathi.selvaraju@windriver.com>
This commit is contained in:
Vanathi.Selvaraju 2024-02-15 16:02:28 -05:00 committed by Vanathi Selvaraju
parent e230abb543
commit b91c86c557
5 changed files with 147 additions and 5 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2016-2023 Wind River Systems, Inc.
# Copyright (c) 2016-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@ -179,6 +179,22 @@ def _get_strategy_object_from_response(response):
return strategy
def _get_current_strategy_from_response(response):
"""
Returns Strategy Type and State
"""
list_strategy = {}
strategy_data = response.get('strategy', None)
if strategy_data is None:
return None
strategy = Strategy()
strategy.name = strategy_data['name']
strategy.state = strategy_data['state']
if strategy.name is not None:
list_strategy[strategy.name] = strategy.state
return list_strategy
def get_strategies(token_id, url, strategy_name, username=None,
user_domain_name=None, tenant=None):
"""
@ -393,3 +409,26 @@ def abort_strategy(token_id, url, strategy_name, stage_id, username=None,
return None
return _get_strategy_object_from_response(response)
def get_current_strategy(token_id, url, username=None,
user_domain_name=None, tenant=None):
"""
Get The Current Active Strategy Type And State
"""
api_cmd = url + "/api/orchestration/current-strategy/strategy"
api_cmd_headers = dict()
if username:
api_cmd_headers['X-User'] = username
if tenant:
api_cmd_headers['X-Tenant'] = tenant
if user_domain_name:
api_cmd_headers['X-User-Domain-Name'] = user_domain_name
api_cmd_headers['X-Auth-Token'] = token_id
response = rest_api.request(token_id, "GET", api_cmd, api_cmd_headers)
if response['strategy'] is not None:
return _get_current_strategy_from_response(response)
return None

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2015-2023 Wind River Systems, Inc.
# Copyright (c) 2015-2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@ -13,6 +13,7 @@ from nfv_vim.api._link import Link
from nfv_vim.api.controllers.v1.orchestration.sw_update import FwUpdateAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import KubeRootcaUpdateAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import KubeUpgradeAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import StrategyAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import SwPatchAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import SwUpgradeAPI
from nfv_vim.api.controllers.v1.orchestration.sw_update import SystemConfigUpdateAPI
@ -41,7 +42,8 @@ class OrchestrationDescription(wsme_types.Base):
url, 'orchestration/kube-rootca-update', ''),
Link.make_link('kube-upgrade',
url, 'orchestration/kube-upgrade', ''),
Link.make_link('fw-update', url, 'orchestration/fw-update', '')]
Link.make_link('fw-update', url, 'orchestration/fw-update', ''),
Link.make_link('current-strategy', url, 'orchestration/current-strategy', '')]
return description
@ -63,6 +65,8 @@ class OrchestrationAPI(rest.RestController):
return KubeRootcaUpdateAPI(), remainder
elif 'kube-upgrade' == key:
return KubeUpgradeAPI(), remainder
elif 'current-strategy' == key:
return StrategyAPI(), remainder
else:
pecan.abort(httplib.NOT_FOUND)

View File

@ -1,8 +1,9 @@
#
# Copyright (c) 2015-2023 Wind River Systems, Inc.
# Copyright (c) 2015-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from nfv_vim.api.controllers.v1.orchestration.sw_update._current_strategy import StrategyAPI # noqa: F401
from nfv_vim.api.controllers.v1.orchestration.sw_update._fw_update import FwUpdateAPI # noqa: F401
from nfv_vim.api.controllers.v1.orchestration.sw_update._kube_rootca_update import KubeRootcaUpdateAPI # noqa: F401
from nfv_vim.api.controllers.v1.orchestration.sw_update._kube_upgrade import KubeUpgradeAPI # noqa: F401

View File

@ -0,0 +1,54 @@
#
# Copyright (c) 2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import pecan
from pecan import rest
from six.moves import http_client as httplib
from wsme import types as wsme_types
import wsmeext.pecan as wsme_pecan
from nfv_common import debug
from nfv_vim.api._link import Link
from nfv_vim.api.controllers.v1.orchestration.sw_update._sw_update_strategy import CurrentStrategyAPI
DLOG = debug.debug_get_logger('nfv_vim.api.current_strategy')
class StrategyDescription(wsme_types.Base):
"""
Current Strategy Description
"""
id = wsme_types.text
links = wsme_types.wsattr([Link], name='links')
@classmethod
def convert(cls):
url = pecan.request.host_url
description = StrategyDescription()
description.id = "current-strategy"
description.links = [
Link.make_link('self', url, 'orchestration/current-strategy'),
Link.make_link('strategy', url, 'orchestration/current-strategy/strategy')]
return description
class StrategyAPI(rest.RestController):
"""
Current Strategy Rest API
"""
@pecan.expose()
def _lookup(self, key, *remainder):
if 'strategy' == key:
return CurrentStrategyAPI(), remainder
else:
pecan.abort(httplib.NOT_FOUND)
@wsme_pecan.wsexpose(StrategyDescription)
def get(self):
# NOTE: The reason why convert() is being called for every
# request is because we need to get the host url from
# the request object to make the links.
return StrategyDescription.convert()

View File

@ -1,13 +1,15 @@
#
# Copyright (c) 2015-2023 Wind River Systems, Inc.
# Copyright (c) 2015-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import json
import os
import pecan
from pecan import rest
import six
from six.moves import http_client as httplib
import subprocess
from wsme import types as wsme_types
import wsmeext.pecan as wsme_pecan
@ -44,6 +46,9 @@ MAX_PARALLEL_KUBE_UPGRADE_HOSTS = 10
MAX_PARALLEL_PATCH_HOSTS = 100
MAX_PARALLEL_SYSTEM_CONFIG_UPDATE_HOSTS = 100
MAX_PARALLEL_UPGRADE_HOSTS = 10
DB_DUMP_DIR = '/opt/platform/nfv/vim/'
SW_RELEASE_DIR = '/etc/platform/platform.conf'
DB_DUMP = 'nfv_vim_db_dump'
def _get_sw_update_type_from_path(path):
@ -382,6 +387,12 @@ class SwUpdateStrategyQueryData(wsme_types.Base):
self.convert_strategy_phase(strategy_data['abort_phase'])
self.strategy = strategy
def convert_current_strategy(self, strategy_data):
strategy = SwUpdateStrategyData()
strategy.name = strategy_data['name']
strategy.state = strategy_data['state']
self.strategy = strategy
class SwUpdateStrategyActionAPI(rest.RestController):
"""
@ -1011,3 +1022,36 @@ class KubeUpgradeStrategyAPI(SwUpdateStrategyAPI):
auth_context_dict, exc=policy.PolicyForbidden)
else:
policy.check('admin_in_system_projects', {}, auth_context_dict)
class CurrentStrategyAPI(SwUpdateStrategyAPI):
"""
Current Strategy Rest API - Gets Data from DB Dump
"""
@wsme_pecan.wsexpose(SwUpdateStrategyQueryData, status_code=httplib.OK)
def get_all(self):
cmd = 'cat %s | grep "sw_version"' % (SW_RELEASE_DIR)
sw_version = os.popen(cmd).read()
sw_release = sw_version.split('=')[1].strip('\n')
db_dump_file = DB_DUMP_DIR + DB_DUMP
if os.path.isfile(db_dump_file):
os.remove(db_dump_file)
vim_cmd = ("nfv-vim-manage db-dump-data -d %s/%s "
"-f %s" % (DB_DUMP_DIR, sw_release, db_dump_file))
subprocess.check_call([vim_cmd], shell=True)
with open(db_dump_file) as f:
data = f.read()
js = json.loads(data)
strategy_check = js['tables']['sw_updates']
query_data = SwUpdateStrategyQueryData()
if strategy_check == []:
DLOG.verbose("No strategy exists.")
return query_data
strategy_details = js['tables']['sw_updates'][0]['strategy_data']
strategy = json.loads(strategy_details)
query_data.convert_current_strategy(strategy)
return query_data