Readds plugins code and respective tests
[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 os
30
31 try:
32 import boto
33 import boto.ec2
34 import boto.vpc
35 import boto.ec2.cloudwatch
36 import boto.ec2.connection
37 import logging
38 from boto.ec2.cloudwatch.alarm import MetricAlarm
39 from boto.ec2.cloudwatch.dimension import Dimension
40 from boto.sns import connect_to_region
41 from boto.utils import get_instance_metadata
42
43 except:
44 exit("Boto not available. Try activating your virtualenv OR `pip install boto`")
45
46 log = logging.getLogger(__name__)
47
48
49 class Connection:
50 """Connection Establishment with AWS -- VPC/EC2/CloudWatch"""
51
52 def setEnvironment(self):
53 try:
54 """Credentials for connecting to AWS-CloudWatch"""
55 # Reads from the environment variables
56 self.AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
57 self.AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
58 self.AWS_REGION = os.environ.get("AWS_EC2_REGION", "us-west-2")
59
60 # TODO Read from the cloudwatch_credentials.txt file
61
62 return self.connection_instance()
63 except Exception as e:
64 log.error("AWS Credentials not configured, Try setting the access credentials first %s: ", str(e))
65
66 def connection_instance(self):
67 try:
68 # VPC Connection
69 self.vpc_conn = boto.vpc.connect_to_region(self.AWS_REGION,
70 aws_access_key_id=self.AWS_KEY,
71 aws_secret_access_key=self.AWS_SECRET)
72
73 # EC2 Connection
74 self.ec2_conn = boto.ec2.connect_to_region(self.AWS_REGION,
75 aws_access_key_id=self.AWS_KEY,
76 aws_secret_access_key=self.AWS_SECRET)
77
78 """ TODO : Required to add actions against alarms when needed """
79 # self.sns = connect_to_region(self.AWS_REGION)
80 # self.topics = self.sns.get_all_topics()
81 # self.topic = self.topics[u'ListTopicsResponse']['ListTopicsResult']['Topics'][0]['TopicArn']
82
83 # Cloudwatch Connection
84 self.cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
85 self.AWS_REGION,
86 aws_access_key_id=self.AWS_KEY,
87 aws_secret_access_key=self.AWS_SECRET)
88 connection_dict = dict()
89 connection_dict['ec2_connection'] = self.ec2_conn
90 connection_dict['cloudwatch_connection'] = self.cloudwatch_conn
91 return connection_dict
92
93 except Exception as e:
94 log.error("Failed to Connect with AWS %s: ", str(e))