2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
23 AWS-Plugin implements all the methods of MON to interact with AWS using the BOTO client
25 from io
import UnsupportedOperation
27 from osm_mon
.core
.settings
import Config
28 from osm_mon
.plugins
.CloudWatch
.metrics
import Metrics
30 __author__
= "Wajeeha Hamid"
31 __date__
= "18-September-2017"
35 log
= logging
.getLogger(__name__
)
39 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
42 self
._cfg
= Config
.instance()
43 self
.metric
= Metrics()
45 def create_metric_request(self
, metric_info
):
46 """Compatible API using normalized parameters"""
47 metric_resp
= self
.metric
.createMetrics(self
.cloudwatch_conn
, metric_info
)
50 def update_metric_request(self
, updated_info
):
51 """Compatible API using normalized parameters"""
52 update_resp
= self
.metric
.updateMetrics(self
.cloudwatch_conn
, updated_info
)
55 def delete_metric_request(self
, delete_info
):
56 """Compatible API using normalized parameters"""
57 del_resp
= self
.metric
.deleteMetrics(self
.cloudwatch_conn
, delete_info
)
60 def list_metrics_request(self
, list_info
):
61 """Compatible API using normalized parameters"""
62 list_resp
= self
.metric
.listMetrics(self
.cloudwatch_conn
, list_info
)
65 def read_metrics_data(self
, list_info
):
66 """Compatible API using normalized parameters
67 Read all metric data related to a specified metric"""
68 data_resp
= self
.metric
.metricsData(self
.cloudwatch_conn
, list_info
)
71 def metric_calls(self
, key
: str, metric_info
: dict, aws_conn
: dict):
72 """Gets the message from the common consumer"""
75 self
.cloudwatch_conn
= aws_conn
['cloudwatch_connection']
76 self
.ec2_conn
= aws_conn
['ec2_connection']
78 metric_response
= dict()
80 log
.debug("VIM support : AWS")
82 if key
== "create_metric_request":
83 if self
.check_resource(metric_info
['metric_create_request']['resource_uuid']):
84 metric_resp
= self
.create_metric_request(
85 metric_info
['metric_create_request']) # alarm_info = message.value
86 metric_response
['schema_version'] = metric_info
['schema_version']
87 metric_response
['schema_type'] = "create_metric_response"
88 metric_response
['metric_create_response'] = metric_resp
89 log
.info("Metric configured: %s", metric_resp
)
90 return metric_response
92 elif key
== "update_metric_request":
93 if self
.check_resource(metric_info
['metric_create_request']['resource_uuid']):
94 update_resp
= self
.update_metric_request(metric_info
['metric_create_request'])
95 metric_response
['schema_version'] = metric_info
['schema_version']
96 metric_response
['schema_type'] = "update_metric_response"
97 metric_response
['metric_update_response'] = update_resp
98 log
.info("Metric Updates: %s", metric_response
)
99 return metric_response
101 elif key
== "delete_metric_request":
102 if self
.check_resource(metric_info
['resource_uuid']):
103 del_resp
= self
.delete_metric_request(metric_info
)
104 log
.info("Metric Deletion Not supported in AWS : %s", del_resp
)
107 elif key
== "list_metric_request":
108 if self
.check_resource(metric_info
['metrics_list_request']['resource_uuid']):
109 list_resp
= self
.list_metrics_request(metric_info
['metrics_list_request'])
110 metric_response
['schema_version'] = metric_info
['schema_version']
111 metric_response
['schema_type'] = "list_metric_response"
112 metric_response
['correlation_id'] = metric_info
['metrics_list_request']['correlation_id']
113 metric_response
['vim_type'] = metric_info
['vim_type']
114 metric_response
['metrics_list'] = list_resp
115 log
.info("Metric List: %s", metric_response
)
116 return metric_response
118 elif key
== "read_metric_data_request":
119 if self
.check_resource(metric_info
['resource_uuid']):
120 data_resp
= self
.read_metrics_data(metric_info
)
121 metric_response
['schema_version'] = metric_info
['schema_version']
122 metric_response
['schema_type'] = "read_metric_data_response"
123 metric_response
['metric_name'] = metric_info
['metric_name']
124 metric_response
['metric_uuid'] = metric_info
['metric_uuid']
125 metric_response
['correlation_id'] = metric_info
['correlation_uuid']
126 metric_response
['resource_uuid'] = metric_info
['resource_uuid']
127 metric_response
['tenant_uuid'] = metric_info
['tenant_uuid']
128 metric_response
['metrics_data'] = data_resp
129 log
.info("Metric Data Response: %s", metric_response
)
130 return metric_response
133 raise UnsupportedOperation("Unknown key, no action will be performed")
135 except Exception as e
:
136 log
.error("Consumer exception: %s", str(e
))
138 def check_resource(self
, resource_uuid
):
140 """Checking the resource_uuid is present in EC2 instances"""
143 instances
= self
.ec2_conn
.get_all_instance_status()
144 status_resource
= False
147 for instance_id
in instances
:
148 instance_id
= str(instance_id
).split(':')[1]
149 if instance_id
== resource_uuid
:
150 check_resp
['resource_uuid'] = resource_uuid
151 status_resource
= True
153 status_resource
= False
156 return status_resource
158 except Exception as e
:
159 log
.error("Error in Plugin Inputs %s", str(e
))