From: javaid Date: Wed, 20 Dec 2017 10:28:03 +0000 (+0500) Subject: Minor bug fixes for common consumer and updated AWS plugin code with access credentials X-Git-Tag: v4.0.0~39 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F99%2F5799%2F1;p=osm%2FMON.git Minor bug fixes for common consumer and updated AWS plugin code with access credentials Change-Id: I62da309f2f3899df8155a786a4708f626f53086b Signed-off-by: javaid --- diff --git a/osm_mon/core/message_bus/common_consumer b/osm_mon/core/message_bus/common_consumer index 0255965..709c07e 100755 --- a/osm_mon/core/message_bus/common_consumer +++ b/osm_mon/core/message_bus/common_consumer @@ -41,6 +41,8 @@ from osm_mon.plugins.OpenStack.Gnocchi import metrics from osm_mon.plugins.CloudWatch.plugin_alarm import plugin_alarms from osm_mon.plugins.CloudWatch.plugin_metric import plugin_metrics +from osm_mon.plugins.CloudWatch.connection import Connection +from osm_mon.plugins.CloudWatch.access_credentials import AccessCredentials from osm_mon.plugins.vRealiseOps import plugin_receiver @@ -59,6 +61,8 @@ openstack_alarms = alarming.Alarming() # Create CloudWatch alarm and metric instances cloudwatch_alarms = plugin_alarms() cloudwatch_metrics = plugin_metrics() +aws_connection = Connection() +aws_access_credentials = AccessCredentials() #Create vROps plugin_receiver class instance vrops_rcvr = plugin_receiver.PluginReceiver() @@ -89,8 +93,9 @@ try: message, openstack_auth, auth_token) elif vim_type == "aws": - cloudwatch_metrics.metric_calls(message) log.info("This message is for the CloudWatch plugin.") + aws_conn = aws_connection.setEnvironment() + cloudwatch_metrics.metric_calls(message,aws_conn) elif vim_type == "vmware": log.info("This metric_request message is for the vROPs plugin.") @@ -108,8 +113,9 @@ try: openstack_alarms.alarming(message, openstack_auth, auth_token) elif vim_type == "aws": - cloudwatch_alarms.alarm_calls(message) log.info("This message is for the CloudWatch plugin.") + aws_conn = aws_connection.setEnvironment() + cloudwatch_alarms.alarm_calls(message, aws_conn) elif vim_type == "vmware": log.info("This alarm_request message is for the vROPs plugin.") @@ -127,8 +133,8 @@ try: auth_token = openstack_auth._authenticate(message=message) elif vim_type == "aws": - #TODO Access credentials later log.info("This message is for the CloudWatch plugin.") + aws_access_credentials.access_credential_calls(message) elif vim_type == "vmware": log.info("This access_credentials message is for the vROPs plugin.") diff --git a/osm_mon/plugins/CloudWatch/access_credentials.py b/osm_mon/plugins/CloudWatch/access_credentials.py new file mode 100644 index 0000000..75083dd --- /dev/null +++ b/osm_mon/plugins/CloudWatch/access_credentials.py @@ -0,0 +1,66 @@ +## +# Copyright 2017 xFlow Research Pvt. Ltd +# This file is part of MON module +# All Rights Reserved. +# +# 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact with: usman.javaid@xflowresearch.com +## + +''' +Access credentials class implements all the methods to store the access credentials for AWS +''' + +__author__ = "Usman Javaid" +__date__ = "20-December-2017" + +import os +import sys +import json +import logging + +logging.basicConfig(filename='MON_plugins.log', + format='%(asctime)s %(message)s', + datefmt='%m/%d/%Y %I:%M:%S %p', filemode='a', + level=logging.INFO) +log = logging.getLogger(__name__) + +class AccessCredentials(): + + def access_credential_calls(self,message): + try: + message = json.loads(message.value)['access_config'] + + AWS_KEY = message['user'] + AWS_SECRET = message['password'] + AWS_REGION = message['vim_tenant_name'] + + os.environ['AWS_ACCESS_KEY_ID'] = AWS_KEY + os.environ['AWS_SECRET_ACCESS_KEY'] = AWS_SECRET + os.environ['AWS_EC2_REGION'] = AWS_REGION + + + #aws_credentials.txt file to save the access credentials + cloudwatch_credentials = open("../../plugins/CloudWatch/cloudwatch_credentials.txt","w+") + cloudwatch_credentials.write("AWS_ACCESS_KEY_ID="+AWS_KEY+ + "\nAWS_SECRET_ACCESS_KEY="+AWS_SECRET+ + "\nAWS_EC2_REGION="+AWS_REGION) + + #Closing the file + cloudwatch_credentials.close() + log.info("Access credentials sourced for CloudWatch MON plugin") + + except Exception as e: + log.error("Access credentials not provided correctly: %s", str(e)) diff --git a/osm_mon/plugins/CloudWatch/connection.py b/osm_mon/plugins/CloudWatch/connection.py index 547c454..609f6aa 100644 --- a/osm_mon/plugins/CloudWatch/connection.py +++ b/osm_mon/plugins/CloudWatch/connection.py @@ -49,12 +49,18 @@ class Connection(): """Connection Establishement with AWS -- VPC/EC2/CloudWatch""" #----------------------------------------------------------------------------------------------------------------------------- def setEnvironment(self): + try: + """Credentials for connecting to AWS-CloudWatch""" + #Reads from the environment variables + self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID") + self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY") + self.AWS_REGION = os.environ.get("AWS_EC2_REGION","us-west-2") - """Credentials for connecting to AWS-CloudWatch""" - self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID") - self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY") - self.AWS_REGION = os.environ.get("AWS_EC2_REGION","us-west-2") - #TOPIC = 'YOUR_TOPIC' + #TODO Read from the cloudwatch_credentials.txt file + + return self.connection_instance() + except Exception as e: + log.error("AWS Credentials not configured, Try setting the access credentials first %s: ",str(e)) #----------------------------------------------------------------------------------------------------------------------------- def connection_instance(self): try: @@ -67,8 +73,7 @@ class Connection(): #EC2 Connection self.ec2_conn = boto.ec2.connect_to_region(self.AWS_REGION, aws_access_key_id=self.AWS_KEY, - aws_secret_access_key=self.AWS_SECRET) - + aws_secret_access_key=self.AWS_SECRET) """ TODO : Required to add actions against alarms when needed """ #self.sns = connect_to_region(self.AWS_REGION) diff --git a/osm_mon/plugins/CloudWatch/plugin_alarm.py b/osm_mon/plugins/CloudWatch/plugin_alarm.py index adc4d29..5888771 100644 --- a/osm_mon/plugins/CloudWatch/plugin_alarm.py +++ b/osm_mon/plugins/CloudWatch/plugin_alarm.py @@ -39,18 +39,9 @@ from producer import KafkaProducer class plugin_alarms(): """Receives Alarm info from MetricAlarm and connects with the consumer/producer""" def __init__ (self): - self.conn = Connection() self.metricAlarm = MetricAlarm() self.metric = Metrics() - self.connection() self.producer = KafkaProducer('') -#--------------------------------------------------------------------------------------------------------------------------- - def connection(self): - """Connecting instances with CloudWatch""" - self.conn.setEnvironment() - self.conn = self.conn.connection_instance() - self.cloudwatch_conn = self.conn['cloudwatch_connection'] - self.ec2_conn = self.conn['ec2_connection'] #--------------------------------------------------------------------------------------------------------------------------- def configure_alarm(self,alarm_info): alarm_id = self.metricAlarm.config_alarm(self.cloudwatch_conn,alarm_info) @@ -73,9 +64,12 @@ class plugin_alarms(): return self.metric.metricsData(self.cloudwatch_conn,metric_name,period,instance_id) #--------------------------------------------------------------------------------------------------------------------------- - def alarm_calls(self,message): + def alarm_calls(self,message,aws_conn): """Gets the message from the common consumer""" - try: + try: + self.cloudwatch_conn = aws_conn['cloudwatch_connection'] + self.ec2_conn = aws_conn['ec2_connection'] + log.info("Action required against: %s" % (message.topic)) alarm_info = json.loads(message.value) diff --git a/osm_mon/plugins/CloudWatch/plugin_metric.py b/osm_mon/plugins/CloudWatch/plugin_metric.py index 6b9598f..8c897d8 100644 --- a/osm_mon/plugins/CloudWatch/plugin_metric.py +++ b/osm_mon/plugins/CloudWatch/plugin_metric.py @@ -38,21 +38,8 @@ import logging as log class plugin_metrics(): """Receives Alarm info from MetricAlarm and connects with the consumer/producer """ def __init__ (self): - self.conn = Connection() self.metric = Metrics() self.producer = KafkaProducer('') - self.connection() -#--------------------------------------------------------------------------------------------------------------------------- - def connection(self): - try: - """Connecting instances with CloudWatch""" - self.conn.setEnvironment() - self.conn = self.conn.connection_instance() - self.cloudwatch_conn = self.conn['cloudwatch_connection'] - self.ec2_conn = self.conn['ec2_connection'] - - except Exception as e: - log.error("Failed to Connect with AWS %s: " + str(e)) #--------------------------------------------------------------------------------------------------------------------------- def create_metric_request(self,metric_info): '''Comaptible API using normalized parameters''' @@ -81,7 +68,7 @@ class plugin_metrics(): return data_resp #--------------------------------------------------------------------------------------------------------------------------- - def metric_calls(self,message): + def metric_calls(self,message,aws_conn): '''Consumer will consume the message from SO, 1) parse the message and trigger the methods ac cording to keys and topics provided in request. @@ -93,6 +80,9 @@ class plugin_metrics(): ''' try: + self.cloudwatch_conn = aws_conn['cloudwatch_connection'] + self.ec2_conn = aws_conn['ec2_connection'] + metric_info = json.loads(message.value) metric_response = dict()