Files
distcloud/distributedcloud/dccommon/drivers/openstack/patching_v1.py
Bin Qian 06e8c7d414 Change central cloud to access subcloud via admin endpoints
system controller is changed to access subcloud via admin endpoints,
instead of internal endpoints.
With upcoming changes that enable https on admin endpoints,
to provide secured access.

Story: 2007347
Task: 39448

Change-Id: Iaa0454acc0b3a0da4da0d51f44dc181808ea6035
Signed-off-by: Bin Qian <bin.qian@windriver.com>
2020-04-23 00:12:25 -04:00

199 lines
7.1 KiB
Python

# Copyright 2016 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.
#
# Copyright (c) 2017-2020 Wind River Systems, Inc.
#
# The right to copy, distribute, modify, or otherwise make use
# of this software may be licensed only pursuant to the terms
# of an applicable Wind River license agreement.
#
from oslo_log import log
import requests
from requests_toolbelt import MultipartEncoder
from dccommon import consts
from dccommon.drivers import base
LOG = log.getLogger(__name__)
# Patch states
PATCH_STATE_AVAILABLE = 'Available'
PATCH_STATE_APPLIED = 'Applied'
PATCH_STATE_PARTIAL_APPLY = 'Partial-Apply'
PATCH_STATE_PARTIAL_REMOVE = 'Partial-Remove'
PATCH_STATE_COMMITTED = 'Committed'
PATCH_STATE_UNKNOWN = 'n/a'
class PatchingClient(base.DriverBase):
"""Patching V1 driver."""
def __init__(self, region, session):
# Get an endpoint and token.
self.endpoint = session.get_endpoint(
service_type='patching',
region_name=region,
interface=consts.KS_ENDPOINT_ADMIN)
self.token = session.get_token()
def query(self, state=None, release=None,):
"""Query patches"""
url = self.endpoint + '/v1/query'
if state is not None:
url += "?show=%s" % state.lower()
if release is not None:
url += "&release=%s" % release
headers = {"X-Auth-Token": self.token}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "query failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "query failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def query_hosts(self):
"""Query hosts"""
url = self.endpoint + '/v1/query_hosts'
headers = {"X-Auth-Token": self.token}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "query_hosts failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('data', [])
else:
message = "query_hosts failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def apply(self, patches):
"""Apply patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/apply/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "apply failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "apply failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def remove(self, patches):
"""Remove patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/remove/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "remove failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "remove failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def delete(self, patches):
"""Delete patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/delete/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "delete failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "delete failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def commit(self, patches):
"""Commit patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/commit/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "commit failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "commit failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def upload(self, files):
"""Upload patches"""
for file in sorted(list(set(files))):
enc = MultipartEncoder(fields={'file': (file,
open(file, 'rb'),
)})
url = self.endpoint + '/v1/upload'
headers = {"X-Auth-Token": self.token,
'Content-Type': enc.content_type}
response = requests.post(url,
data=enc,
headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "upload failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "upload failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)