Minor bug fixes for common consumer and updated AWS plugin code with access credentials 99/5799/1
authorjavaid <usman.javaid@xflowresearch.com>
Wed, 20 Dec 2017 10:28:03 +0000 (15:28 +0500)
committerjavaid <usman.javaid@xflowresearch.com>
Wed, 20 Dec 2017 10:28:03 +0000 (15:28 +0500)
Change-Id: I62da309f2f3899df8155a786a4708f626f53086b
Signed-off-by: javaid <usman.javaid@xflowresearch.com>
osm_mon/core/message_bus/common_consumer
osm_mon/plugins/CloudWatch/access_credentials.py [new file with mode: 0644]
osm_mon/plugins/CloudWatch/connection.py
osm_mon/plugins/CloudWatch/plugin_alarm.py
osm_mon/plugins/CloudWatch/plugin_metric.py

index 0255965..709c07e 100755 (executable)
@@ -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.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
 
 
 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()
 # 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()
 
 #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":
                     message, openstack_auth, auth_token)
 
             elif vim_type == "aws":
-                cloudwatch_metrics.metric_calls(message)
                 log.info("This message is for the CloudWatch plugin.")
                 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.")
 
             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":
                 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.")
                 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.")
 
             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":
                 auth_token = openstack_auth._authenticate(message=message)
 
             elif vim_type == "aws":
-                #TODO Access credentials later
                 log.info("This message is for the CloudWatch plugin.")
                 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.")
 
             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 (file)
index 0000000..75083dd
--- /dev/null
@@ -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))
index 547c454..609f6aa 100644 (file)
@@ -49,12 +49,18 @@ class Connection():
     """Connection Establishement with AWS -- VPC/EC2/CloudWatch"""
 #-----------------------------------------------------------------------------------------------------------------------------
     def setEnvironment(self):  
     """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:
 #-----------------------------------------------------------------------------------------------------------------------------
     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,
                 #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)
                 
                 """ TODO : Required to add actions against alarms when needed """
                 #self.sns = connect_to_region(self.AWS_REGION)
index adc4d29..5888771 100644 (file)
@@ -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): 
 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.metricAlarm = MetricAlarm()
         self.metric = Metrics()
-        self.connection()
         self.producer = KafkaProducer('')     
         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)
 #---------------------------------------------------------------------------------------------------------------------------   
     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)
 #--------------------------------------------------------------------------------------------------------------------------- 
 
         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"""
         """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)
 
             log.info("Action required against: %s" % (message.topic))
             alarm_info = json.loads(message.value)
 
index 6b9598f..8c897d8 100644 (file)
@@ -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): 
 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.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'''
 #---------------------------------------------------------------------------------------------------------------------------   
     def create_metric_request(self,metric_info):
         '''Comaptible API using normalized parameters'''
@@ -81,7 +68,7 @@ class plugin_metrics():
         return data_resp
 #--------------------------------------------------------------------------------------------------------------------------- 
 
         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.
         '''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:
         '''
         
         try:
+            self.cloudwatch_conn = aws_conn['cloudwatch_connection']
+            self.ec2_conn = aws_conn['ec2_connection']
+
             metric_info = json.loads(message.value)
             metric_response = dict()
 
             metric_info = json.loads(message.value)
             metric_response = dict()