Files
distcloud/distributedcloud/dccommon/drivers/openstack/software_v1.py
Hugo Brito 8fbdbec0d4 Execute software deploy delete in Software DC orchestration
This commit implements the execution of `software deploy delete`
within the finish_strategy state. Additionally, it removes outdated
committed release code and introduces custom exceptions to handle
state errors effectively.

Test Plan (patch only):
PASS - Apply a sw-deploy-strategy
- Release in deploying state after apply_vim_strategy state
- Release changed to deployed state in finish_strategy state
  - software deploy delete executed

Story: 2010676
Task: 50543

Change-Id: If40f0ec7298270fe4a2a7306c3132eba3050e62c
Signed-off-by: Hugo Brito <hugo.brito@windriver.com>
2024-07-12 20:02:00 +00:00

98 lines
3.6 KiB
Python

# Copyright (c) 2023-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from oslo_log import log
import requests
from dccommon import consts
from dccommon.drivers import base
from dccommon import exceptions
LOG = log.getLogger(__name__)
# Proposed States
ABORTING = "aborting"
AVAILABLE = "available"
COMMITTED = "committed"
DEPLOYED = "deployed"
DEPLOYING = "deploying"
REMOVING = "removing"
UNAVAILABLE = "unavailable"
REST_DEFAULT_TIMEOUT = 900
class SoftwareClient(base.DriverBase):
"""Software V1 driver."""
def __init__(self, region, session, endpoint=None):
# Get an endpoint and token.
if not endpoint:
self.endpoint = session.get_endpoint(
service_type="usm",
region_name=region,
interface=consts.KS_ENDPOINT_ADMIN,
)
else:
self.endpoint = endpoint
# The usm systemcontroller endpoint ends with a slash but the regionone
# and the subcloud endpoint don't. The slash is removed to standardize
# with the other endpoints.
self.endpoint = self.endpoint.rstrip("/") + "/v1"
self.token = session.get_token()
self.headers = {"X-Auth-Token": self.token}
def list(self, timeout=REST_DEFAULT_TIMEOUT):
"""List releases"""
url = self.endpoint + "/release"
response = requests.get(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="List")
def show(self, release, timeout=REST_DEFAULT_TIMEOUT):
"""Show release"""
url = self.endpoint + f"/release/{release}"
response = requests.get(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="Show")
def delete(self, releases, timeout=REST_DEFAULT_TIMEOUT):
"""Delete release"""
release_str = "/".join(releases)
url = self.endpoint + f"/release/{release_str}"
response = requests.delete(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="Delete")
def deploy_precheck(self, deployment, timeout=REST_DEFAULT_TIMEOUT):
"""Deploy precheck"""
url = self.endpoint + f"/deploy/{deployment}/precheck"
response = requests.post(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="Deploy precheck")
def deploy_delete(self, timeout=REST_DEFAULT_TIMEOUT):
"""Deploy delete"""
url = self.endpoint + "/deploy"
response = requests.delete(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="Deploy delete")
def commit_patch(self, releases, timeout=REST_DEFAULT_TIMEOUT):
"""Commit patch"""
release_str = "/".join(releases)
url = self.endpoint + f"/commit_patch/{release_str}"
response = requests.post(url, headers=self.headers, timeout=timeout)
return self._handle_response(response, operation="Commit patch")
def _handle_response(self, response, operation):
if response.status_code != 200:
LOG.error(f"{operation} failed with RC: {response.status_code}")
raise exceptions.ApiException(endpoint=operation, rc=response.status_code)
data = response.json()
# Data response could be a dict with an error key or a list
if isinstance(data, dict) and data.get("error"):
message = f"{operation} failed with error: {data.get('error')}"
LOG.error(message)
raise exceptions.SoftwareDataException(endpoint=response.url, error=message)
return data