/usr/lib/python2.7/dist-packages/ripe/atlas/cousteau/api_meta_data.py is in python-ripe-atlas-cousteau 1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | # Copyright (c) 2015 RIPE NCC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
from dateutil.tz import tzutc
from .request import AtlasRequest
from .exceptions import CousteauGenericError, APIResponseError
class EntityRepresentation(object):
"""
A crude representation of entity's meta data as we get it from the API.
"""
API_META_URL = ""
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.server = kwargs.get("server")
self.verify = kwargs.get("verify", True)
self.api_key = kwargs.get("key", "")
self.meta_data = kwargs.get("meta_data")
self._user_agent = kwargs.get("user_agent")
self._fields = kwargs.get("fields")
self.get_params = {}
if self.meta_data is None and self.id is None:
raise CousteauGenericError(
"Id or meta_data should be passed in order to create object."
)
if self._fields:
self.update_get_params()
if self.meta_data is None:
if not self._fetch_meta_data():
raise APIResponseError(self.meta_data)
self._populate_data()
def update_get_params(self):
"""Update HTTP GET params with the given fields that user wants to fetch."""
if isinstance(self._fields, (tuple, list)): # tuples & lists > x,y,z
self.get_params["fields"] = ",".join([str(_) for _ in self._fields])
elif isinstance(self._fields, str):
self.get_params["fields"] = self._fields
def _fetch_meta_data(self):
"""Makes an API call to fetch meta data for the given probe and stores the raw data."""
is_success, meta_data = AtlasRequest(
url_path=self.API_META_URL.format(self.id),
key=self.api_key,
server=self.server,
verify=self.verify,
user_agent=self._user_agent
).get(**self.get_params)
self.meta_data = meta_data
if not is_success:
return False
return True
def _populate_data(self):
"""
Passing some raw meta data from API response to instance properties
"""
raise NotImplementedError()
class Probe(EntityRepresentation):
"""
A crude representation of probe's meta data as we get it from the API.
"""
API_META_URL = "/api/v2/probes/{0}/"
def _populate_data(self):
"""Assing some probe's raw meta data from API response to instance properties"""
if self.id is None:
self.id = self.meta_data.get("id")
self.is_anchor = self.meta_data.get("is_anchor")
self.country_code = self.meta_data.get("country_code")
self.description = self.meta_data.get("description")
self.is_public = self.meta_data.get("is_public")
self.asn_v4 = self.meta_data.get("asn_v4")
self.asn_v6 = self.meta_data.get("asn_v6")
self.address_v4 = self.meta_data.get("address_v4")
self.address_v6 = self.meta_data.get("address_v6")
self.prefix_v4 = self.meta_data.get("prefix_v4")
self.prefix_v6 = self.meta_data.get("prefix_v6")
self.geometry = self.meta_data.get("geometry")
self.tags = self.meta_data.get("tags")
self.status = self.meta_data.get("status", {}).get("name")
def __str__(self):
return "Probe #{0}".format(self.id)
def __repr__(self):
return str(self)
class Measurement(EntityRepresentation):
"""
A crude representation of measurement's meta data as we get it from the API.
"""
API_META_URL = "/api/v2/measurements/{0}/"
def _populate_data(self):
"""Assinging some measurement's raw meta data from API response to instance properties"""
if self.id is None:
self.id = self.meta_data.get("id")
self.stop_time = None
self.creation_time = None
self.start_time = None
self.populate_times()
self.protocol = self.meta_data.get("af")
self.target_ip = self.meta_data.get("target_ip")
self.target_asn = self.meta_data.get("target_asn")
self.target = self.meta_data.get("target")
self.description = self.meta_data.get("description")
self.is_oneoff = self.meta_data.get("is_oneoff")
self.is_public = self.meta_data.get("is_public")
self.interval = self.meta_data.get("interval")
self.resolve_on_probe = self.meta_data.get("resolve_on_probe")
self.status_id = self.meta_data.get("status", {}).get("id")
self.status = self.meta_data.get("status", {}).get("name")
self.type = self.get_type()
self.result_url = self.meta_data.get("result")
def get_type(self):
"""
Getting type of measurement keeping backwards compatibility for
v2 API output changes.
"""
mtype = None
if "type" not in self.meta_data:
return mtype
mtype = self.meta_data["type"]
if isinstance(mtype, dict):
mtype = self.meta_data.get("type", {}).get("name", "").upper()
elif isinstance(mtype, str):
mtype = mtype
return mtype
def populate_times(self):
"""
Populates all different meta data times that comes with measurement if
they are present.
"""
stop_time = self.meta_data.get("stop_time")
if stop_time:
stop_naive = datetime.utcfromtimestamp(stop_time)
self.stop_time = stop_naive.replace(tzinfo=tzutc())
creation_time = self.meta_data.get("creation_time")
if creation_time:
creation_naive = datetime.utcfromtimestamp(creation_time)
self.creation_time = creation_naive.replace(tzinfo=tzutc())
start_time = self.meta_data.get("start_time")
if start_time:
start_naive = datetime.utcfromtimestamp(start_time)
self.start_time = start_naive.replace(tzinfo=tzutc())
def __str__(self):
return "Measurement #{0}".format(self.id)
def __repr__(self):
return str(self)
|