Remove entries related to host-based snmp

According to the host-based SNMP removal, remove community
and trapdest related processes

Story: 2008132
Task: 41394
Change-Id: Icad7f82d5e20149a49cbb1540486b882856ac189
Depends-On: https://review.opendev.org/765381
Signed-off-by: Takamasa Takenaka <takamasa.takenaka@windriver.com>
This commit is contained in:
Takamasa Takenaka 2020-12-23 09:10:33 -03:00
parent 181bd0c86d
commit ec8dba8a53
6 changed files with 4 additions and 385 deletions

View File

@ -22,12 +22,7 @@
import hashlib
import os
from cgtsclient.exc import HTTPConflict
from cgtsclient.exc import HTTPNotFound
from cgtsclient.v1.icommunity import CREATION_ATTRIBUTES \
as SNMP_COMMUNITY_CREATION_ATTRIBUTES
from cgtsclient.v1.itrapdest import CREATION_ATTRIBUTES \
as SNMP_TRAPDEST_CREATION_ATTRIBUTES
from oslo_log import log
from dccommon import consts
@ -385,137 +380,6 @@ class SysinvClient(base.DriverBase):
return idns
def snmp_trapdest_list(self):
"""Get the trapdest list for this region
:return: itrapdests list of itrapdest
"""
itrapdests = self.sysinv_client.itrapdest.list()
return itrapdests
def snmp_trapdest_create(self, trapdest_dict):
"""Add the trapdest for this region
:param: trapdest_payload dictionary
:return: itrapdest
"""
# Example trapdest_dict:
# {"ip_address": "10.10.10.12", "community": "cgcs"}
itrapdest = None
trapdest_create_dict = {}
for k, v in trapdest_dict.items():
if k in SNMP_TRAPDEST_CREATION_ATTRIBUTES:
trapdest_create_dict[str(k)] = v
LOG.info("snmp_trapdest_create driver region={}"
"trapdest_create_dict={}".format(
self.region_name, trapdest_create_dict))
try:
itrapdest = self.sysinv_client.itrapdest.create(
**trapdest_create_dict)
except HTTPConflict:
LOG.info("snmp_trapdest_create exists region={}"
"trapdest_dict={}".format(
self.region_name, trapdest_dict))
# Retrieve the existing itrapdest
trapdests = self.snmp_trapdest_list()
for trapdest in trapdests:
if trapdest.ip_address == trapdest_dict.get('ip_address'):
LOG.info("snmp_trapdest_create found existing {}"
"for region: {}".format(
trapdest, self.region_name))
itrapdest = trapdest
break
except Exception as e:
LOG.error("snmp_trapdest_create exception={}".format(e))
raise e
return itrapdest
def snmp_trapdest_delete(self, trapdest_ip_address):
"""Delete the trapdest for this region
:param: trapdest_ip_address
"""
try:
LOG.info("snmp_trapdest_delete region {} ip_address: {}".format(
self.region_name, trapdest_ip_address))
self.sysinv_client.itrapdest.delete(trapdest_ip_address)
except HTTPNotFound:
LOG.info("snmp_trapdest_delete NotFound {} for region: {}".format(
trapdest_ip_address, self.region_name))
raise exceptions.TrapDestNotFound(region_name=self.region_name,
ip_address=trapdest_ip_address)
except Exception as e:
LOG.error("snmp_trapdest_delete exception={}".format(e))
raise e
def snmp_community_list(self):
"""Get the community list for this region
:return: icommunitys list of icommunity
"""
icommunitys = self.sysinv_client.icommunity.list()
return icommunitys
def snmp_community_create(self, community_dict):
"""Add the community for this region
:param: community_payload dictionary
:return: icommunity
"""
# Example community_dict: {"community": "cgcs"}
icommunity = None
community_create_dict = {}
for k, v in community_dict.items():
if k in SNMP_COMMUNITY_CREATION_ATTRIBUTES:
community_create_dict[str(k)] = v
LOG.info("snmp_community_create driver region={}"
"community_create_dict={}".format(
self.region_name, community_create_dict))
try:
icommunity = self.sysinv_client.icommunity.create(
**community_create_dict)
except HTTPConflict:
LOG.info("snmp_community_create exists region={}"
"community_dict={}".format(
self.region_name, community_dict))
# Retrieve the existing icommunity
communitys = self.snmp_community_list()
for community in communitys:
if community.community == community_dict.get('community'):
LOG.info("snmp_community_create found existing {}"
"for region: {}".format(
community, self.region_name))
icommunity = community
break
except Exception as e:
LOG.error("snmp_community_create exception={}".format(e))
raise e
return icommunity
def snmp_community_delete(self, community):
"""Delete the community for this region
:param: community
"""
try:
LOG.info("snmp_community_delete region {} community: {}".format(
self.region_name, community))
self.sysinv_client.icommunity.delete(community)
except HTTPNotFound:
LOG.info("snmp_community_delete NotFound {} for region: {}".format(
community, self.region_name))
raise exceptions.CommunityNotFound(region_name=self.region_name,
community=community)
except Exception as e:
LOG.error("snmp_community_delete exception={}".format(e))
raise e
def get_certificates(self):
"""Get the certificates for this region

View File

@ -89,20 +89,6 @@ class OAMAddressesNotFound(NotFound):
message = _("OAM Addresses Not Found")
class TrapDestNotFound(NotFound):
message = _("Trapdest in region=%(region_name)s with ip_address "
"%(ip_address)s not found")
class CommunityAlreadyExists(Conflict):
message = _("Community %(community)s in region=%(region_name)s "
"already exists")
class CommunityNotFound(NotFound):
message = _("Community %(community)s not found in region=%(region_name)s")
class CertificateNotFound(NotFound):
message = _("Certificate in region=%(region_name)s with signature "
"%(signature)s not found")

View File

@ -378,10 +378,6 @@ class ComputeAPIController(APIController):
class SysinvAPIController(APIController):
ENDPOINT_TYPE = consts.ENDPOINT_TYPE_PLATFORM
RESOURCE_ID_MAP = {
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST: 'ip_address',
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM: 'community'
}
OK_STATUS_CODE = [
webob.exc.HTTPOk.code,
webob.exc.HTTPAccepted.code,
@ -624,14 +620,7 @@ class SysinvAPIController(APIController):
resource_id = json.loads(response.body)['software_version']
resource_ids = [resource_id]
else:
if (operation_type == consts.OPERATION_TYPE_POST and
resource_type in self.RESOURCE_ID_MAP):
# need to get the id from the request data since it is
# not available in the header
rid = self.RESOURCE_ID_MAP.get(resource_type)
resource_id = json.loads(request_body)[rid]
else:
resource_id = self.get_resource_id_from_link(request_header)
resource_id = self.get_resource_id_from_link(request_header)
resource_ids = [resource_id]
if operation_type != consts.OPERATION_TYPE_DELETE:
resource_info['payload'] = json.loads(request_body)

View File

@ -80,16 +80,6 @@ DNS_PATHS = [
'/v1/idns/{uuid}'
]
TRAP_DEST_PATHS = [
'/v1/itrapdest',
'/v1/itrapdest/{ip}'
]
COMMUNITY_STRING_PATHS = [
'/v1/icommunity',
'/v1/icommunity/{community}'
]
CERTIFICATE_PATHS = [
'/v1/certificate/certificate_install',
'/v1/certificate/{uuid}'
@ -111,8 +101,6 @@ DEVICE_IMAGE_PATHS = [
SYSINV_PATH_MAP = {
consts.RESOURCE_TYPE_SYSINV_DNS: DNS_PATHS,
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST: TRAP_DEST_PATHS,
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM: COMMUNITY_STRING_PATHS,
consts.RESOURCE_TYPE_SYSINV_CERTIFICATE: CERTIFICATE_PATHS,
consts.RESOURCE_TYPE_SYSINV_USER: USER_PATHS,
consts.RESOURCE_TYPE_SYSINV_LOAD: LOAD_PATHS,
@ -331,8 +319,6 @@ ROUTE_METHOD_MAP = {
},
consts.ENDPOINT_TYPE_PLATFORM: {
consts.RESOURCE_TYPE_SYSINV_DNS: ['PATCH', 'PUT'],
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST: ['POST', 'DELETE'],
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM: ['POST', 'DELETE'],
consts.RESOURCE_TYPE_SYSINV_CERTIFICATE: ['POST', 'DELETE'],
consts.RESOURCE_TYPE_SYSINV_USER: ['PATCH', 'PUT'],
consts.RESOURCE_TYPE_SYSINV_LOAD: ['POST', 'DELETE'],

View File

@ -75,8 +75,6 @@ ORCH_REQUEST_ABORTED = "aborted"
# SysInv Resources
RESOURCE_TYPE_SYSINV_CERTIFICATE = "certificates"
RESOURCE_TYPE_SYSINV_DNS = "idns"
RESOURCE_TYPE_SYSINV_SNMP_COMM = "icommunity"
RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST = "itrapdest"
RESOURCE_TYPE_SYSINV_USER = "iuser"
RESOURCE_TYPE_SYSINV_FERNET_REPO = "fernet_repo"
RESOURCE_TYPE_SYSINV_LOAD = "loads"

View File

@ -44,9 +44,6 @@ class SysinvSyncThread(SyncThread):
consts.RESOURCE_TYPE_SYSINV_FERNET_REPO
]
SYSINV_ADD_DELETE_RESOURCES = [consts.RESOURCE_TYPE_SYSINV_SNMP_COMM,
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST]
SYSINV_CREATE_RESOURCES = [consts.RESOURCE_TYPE_SYSINV_CERTIFICATE,
consts.RESOURCE_TYPE_SYSINV_FERNET_REPO]
@ -64,10 +61,6 @@ class SysinvSyncThread(SyncThread):
self.sync_handler_map = {
consts.RESOURCE_TYPE_SYSINV_DNS:
self.sync_platform_resource,
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM:
self.sync_platform_resource,
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST:
self.sync_platform_resource,
consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
self.sync_platform_resource,
consts.RESOURCE_TYPE_SYSINV_USER:
@ -82,8 +75,6 @@ class SysinvSyncThread(SyncThread):
self.audit_resources = [
consts.RESOURCE_TYPE_SYSINV_CERTIFICATE,
consts.RESOURCE_TYPE_SYSINV_DNS,
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM,
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST,
consts.RESOURCE_TYPE_SYSINV_USER,
consts.RESOURCE_TYPE_SYSINV_FERNET_REPO,
]
@ -170,141 +161,6 @@ class SysinvSyncThread(SyncThread):
.format(rsrc.id, subcloud_rsrc_id, nameservers),
extra=self.log_extra)
def sync_itrapdest(self, s_os_client, request, rsrc):
switcher = {
consts.OPERATION_TYPE_POST: self.snmp_trapdest_create,
consts.OPERATION_TYPE_CREATE: self.snmp_trapdest_create,
consts.OPERATION_TYPE_DELETE: self.snmp_trapdest_delete,
}
func = switcher[request.orch_job.operation_type]
try:
func(s_os_client, request, rsrc)
except Exception as e:
LOG.exception(e)
raise e
def snmp_trapdest_create(self, s_os_client, request, rsrc):
LOG.info("snmp_trapdest_create region {} resource_info={}".format(
self.region_name,
request.orch_job.resource_info),
extra=self.log_extra)
resource_info_dict = jsonutils.loads(request.orch_job.resource_info)
payload = resource_info_dict.get('payload')
if not payload:
payload = resource_info_dict
try:
itrapdest = s_os_client.sysinv_client.snmp_trapdest_create(
payload)
itrapdest_id = itrapdest.uuid
ip_address = itrapdest.ip_address
except (AttributeError, TypeError) as e:
LOG.info("snmp_trapdest_create error {}".format(e),
extra=self.log_extra)
raise exceptions.SyncRequestFailedRetry
# Now persist the subcloud resource to the DB for later
subcloud_rsrc_id = self.persist_db_subcloud_resource(
rsrc.id, ip_address)
LOG.info("SNMP trapdest {}:{} [{}/{}] created".format(rsrc.id,
subcloud_rsrc_id, ip_address, itrapdest_id),
extra=self.log_extra)
return itrapdest
def snmp_trapdest_delete(self, s_os_client, request, rsrc):
subcloud_rsrc = self.get_db_subcloud_resource(rsrc.id)
if not subcloud_rsrc:
return
try:
s_os_client.sysinv_client.snmp_trapdest_delete(
subcloud_rsrc.subcloud_resource_id)
except dccommon_exceptions.TrapDestNotFound:
# SNMP trapdest already deleted in subcloud, carry on.
LOG.info("SNMP trapdest not in subcloud, may be already deleted",
extra=self.log_extra)
except (AttributeError, TypeError) as e:
LOG.info("snmp_trapdest_delete error {}".format(e),
extra=self.log_extra)
raise exceptions.SyncRequestFailedRetry
subcloud_rsrc.delete()
# Master Resource can be deleted only when all subcloud resources
# are deleted along with corresponding orch_job and orch_requests.
LOG.info("SNMP trapdest {}:{} [{}] deleted".format(
rsrc.id, subcloud_rsrc.id,
subcloud_rsrc.subcloud_resource_id),
extra=self.log_extra)
def sync_icommunity(self, s_os_client, request, rsrc):
switcher = {
consts.OPERATION_TYPE_POST: self.snmp_community_create,
consts.OPERATION_TYPE_CREATE: self.snmp_community_create,
consts.OPERATION_TYPE_DELETE: self.snmp_community_delete,
}
func = switcher[request.orch_job.operation_type]
try:
func(s_os_client, request, rsrc)
except Exception as e:
LOG.exception(e)
raise exceptions.SyncRequestFailedRetry
def snmp_community_create(self, s_os_client, request, rsrc):
LOG.info("snmp_community_create region {} resource_info={}".format(
self.region_name,
request.orch_job.resource_info),
extra=self.log_extra)
resource_info_dict = jsonutils.loads(request.orch_job.resource_info)
payload = resource_info_dict.get('payload')
if not payload:
payload = resource_info_dict
try:
icommunity = s_os_client.sysinv_client.snmp_community_create(
payload)
icommunity_id = icommunity.uuid
community = icommunity.community
except (AttributeError, TypeError) as e:
LOG.info("snmp_community_create error {}".format(e),
extra=self.log_extra)
raise exceptions.SyncRequestFailedRetry
# Now persist the subcloud resource to the DB for later
subcloud_rsrc_id = self.persist_db_subcloud_resource(
rsrc.id, community)
LOG.info("SNMP community {}:{} [{}/{}] created".format(rsrc.id,
subcloud_rsrc_id, community, icommunity_id),
extra=self.log_extra)
return icommunity
def snmp_community_delete(self, s_os_client, request, rsrc):
subcloud_rsrc = self.get_db_subcloud_resource(rsrc.id)
if not subcloud_rsrc:
return
try:
s_os_client.sysinv_client.snmp_community_delete(
subcloud_rsrc.subcloud_resource_id)
except dccommon_exceptions.CommunityNotFound:
# Community already deleted in subcloud, carry on.
LOG.info("SNMP community not in subcloud, may be already deleted",
extra=self.log_extra)
except (AttributeError, TypeError) as e:
LOG.info("snmp_community_delete error {}".format(e),
extra=self.log_extra)
raise exceptions.SyncRequestFailedRetry
subcloud_rsrc.delete()
# Master Resource can be deleted only when all subcloud resources
# are deleted along with corresponding orch_job and orch_requests.
LOG.info("SNMP community {}:{} [{}] deleted".format(
rsrc.id, subcloud_rsrc.id,
subcloud_rsrc.subcloud_resource_id),
extra=self.log_extra)
def update_certificate(self, s_os_client, signature,
certificate=None, data=None):
@ -586,10 +442,6 @@ class SysinvSyncThread(SyncThread):
thread_name='audit')
if resource_type == consts.RESOURCE_TYPE_SYSINV_DNS:
return [self.get_dns_resource(os_client)]
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_COMM:
return self.get_snmp_community_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST:
return self.get_snmp_trapdest_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
return self.get_certificates_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_USER:
@ -612,10 +464,6 @@ class SysinvSyncThread(SyncThread):
thread_name='audit')
if resource_type == consts.RESOURCE_TYPE_SYSINV_DNS:
return [self.get_dns_resource(os_client)]
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_COMM:
return self.get_snmp_community_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST:
return self.get_snmp_trapdest_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
return self.get_certificates_resources(os_client)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_USER:
@ -660,12 +508,6 @@ class SysinvSyncThread(SyncThread):
def get_dns_resource(self, os_client):
return os_client.sysinv_client.get_dns()
def get_snmp_trapdest_resources(self, os_client):
return os_client.sysinv_client.snmp_trapdest_list()
def get_snmp_community_resources(self, os_client):
return os_client.sysinv_client.snmp_community_list()
def get_certificates_resources(self, os_client):
certificate_list = os_client.sysinv_client.get_certificates()
# Only sync the specified certificates to subclouds
@ -683,22 +525,7 @@ class SysinvSyncThread(SyncThread):
return FernetKeyManager.to_resource_info(keys)
def get_resource_id(self, resource_type, resource):
if resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_COMM:
if hasattr(resource, 'community'):
LOG.debug("get_resource_id for community {}".format(resource))
return resource.community
elif hasattr(resource, 'master_id'):
return resource.master_id
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST:
if hasattr(resource, 'ip_address') and \
hasattr(resource, 'community'):
LOG.debug("get_resource_id resource={} has ip_address and "
"community".format(resource),
extra=self.log_extra)
return resource.ip_address
elif hasattr(resource, 'master_id'):
return resource.master_id
elif resource_type == consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
if resource_type == consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
if hasattr(resource, 'signature'):
LOG.debug("get_resource_id signature={}".format(
resource.signature))
@ -741,22 +568,6 @@ class SysinvSyncThread(SyncThread):
same_nameservers = False
return same_nameservers
def same_snmp_trapdest(self, i1, i2):
LOG.debug("same_snmp_trapdest i1={}, i2={}".format(i1, i2),
extra=self.log_extra)
return (i1.ip_address == i2.ip_address and
i1.community == i2.community)
def same_snmp_community(self, i1, i2):
LOG.debug("same_snmp_community i1={}, i2={}".format(i1, i2),
extra=self.log_extra)
if i1.community and (i1.community != i2.community):
if i1.signature == self.RESOURCE_UUID_NULL:
LOG.info("Master Resource SNMP Community NULL UUID")
return True
return False
return True
def same_certificate(self, i1, i2):
LOG.debug("same_certificate i1={}, i2={}".format(i1, i2),
extra=self.log_extra)
@ -796,10 +607,6 @@ class SysinvSyncThread(SyncThread):
def same_resource(self, resource_type, m_resource, sc_resource):
if resource_type == consts.RESOURCE_TYPE_SYSINV_DNS:
return self.same_dns(m_resource, sc_resource)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_COMM:
return self.same_snmp_community(m_resource, sc_resource)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST:
return self.same_snmp_trapdest(m_resource, sc_resource)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_CERTIFICATE:
return self.same_certificate(m_resource, sc_resource)
elif resource_type == consts.RESOURCE_TYPE_SYSINV_USER:
@ -813,17 +620,8 @@ class SysinvSyncThread(SyncThread):
def audit_discrepancy(self, resource_type, m_resource, sc_resources):
# Return true to try the audit_action
if resource_type in self.SYSINV_ADD_DELETE_RESOURCES:
# It could be that the details are different
# between master cloud and subcloud now.
# Thus, delete the resource before creating it again.
master_id = self.get_resource_id(resource_type, m_resource)
self.schedule_work(self.endpoint_type, resource_type,
master_id,
consts.OPERATION_TYPE_DELETE)
return True
elif (resource_type in self.SYSINV_MODIFY_RESOURCES or
resource_type in self.SYSINV_CREATE_RESOURCES):
if (resource_type in self.SYSINV_MODIFY_RESOURCES or
resource_type in self.SYSINV_CREATE_RESOURCES):
# The resource differs, signal to perform the audit_action
return True
@ -896,8 +694,6 @@ class SysinvSyncThread(SyncThread):
def get_resource_info(self, resource_type,
resource, operation_type=None):
payload_resources = [consts.RESOURCE_TYPE_SYSINV_DNS,
consts.RESOURCE_TYPE_SYSINV_SNMP_COMM,
consts.RESOURCE_TYPE_SYSINV_SNMP_TRAPDEST,
consts.RESOURCE_TYPE_SYSINV_CERTIFICATE,
consts.RESOURCE_TYPE_SYSINV_USER,
]