Renaming folder structure
[osm/MON.git] / osm_mon / plugins / CloudWatch / connection.py
1 ##
2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
20 ##
21
22 '''
23 Connecting with AWS services --CloudWatch/EC2 using Required keys
24 '''
25
26 __author__ = "Wajeeha Hamid"
27 __date__ = "18-September-2017"
28
29 import sys
30 import os
31
32 try:
33 import boto
34 import boto.ec2
35 import boto.vpc
36 import boto.ec2.cloudwatch
37 import boto.ec2.connection
38 import logging as log
39 from boto.ec2.cloudwatch.alarm import MetricAlarm
40 from boto.ec2.cloudwatch.dimension import Dimension
41 from boto.sns import connect_to_region
42 from boto.utils import get_instance_metadata
43
44 except:
45 exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`")
46
47
48 class Connection():
49 """Connection Establishement with AWS -- VPC/EC2/CloudWatch"""
50 #-----------------------------------------------------------------------------------------------------------------------------
51 def setEnvironment(self):
52
53 """Credentials for connecting to AWS-CloudWatch"""
54 self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
55 self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
56 self.AWS_REGION = os.environ.get("AWS_EC2_REGION","us-west-2")
57 #TOPIC = 'YOUR_TOPIC'
58 #-----------------------------------------------------------------------------------------------------------------------------
59 def connection_instance(self):
60 try:
61 #VPC Connection
62 self.vpc_conn = boto.vpc.connect_to_region(self.AWS_REGION,
63 aws_access_key_id=self.AWS_KEY,
64 aws_secret_access_key=self.AWS_SECRET)
65
66
67 #EC2 Connection
68 self.ec2_conn = boto.ec2.connect_to_region(self.AWS_REGION,
69 aws_access_key_id=self.AWS_KEY,
70 aws_secret_access_key=self.AWS_SECRET)
71
72
73 """ TODO : Required to add actions against alarms when needed """
74 #self.sns = connect_to_region(self.AWS_REGION)
75 #self.topics = self.sns.get_all_topics()
76 #self.topic = self.topics[u'ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn']
77
78 #Cloudwatch Connection
79 self.cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
80 self.AWS_REGION,
81 aws_access_key_id=self.AWS_KEY,
82 aws_secret_access_key=self.AWS_SECRET)
83 connection_dict = dict()
84 connection_dict['ec2_connection'] = self.ec2_conn
85 connection_dict['cloudwatch_connection'] = self.cloudwatch_conn
86 return connection_dict
87
88 except Exception as e:
89 log.error("Failed to Connect with AWS %s: ",str(e))
90