File: Metadata.py

package info (click to toggle)
python-digitalocean 1.16.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 912 kB
  • sloc: python: 4,961; makefile: 46
file content (46 lines) | stat: -rw-r--r-- 1,328 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
import requests
try:
    from urlparse import urljoin
except ImportError:
    from urllib.parse import urljoin

from .baseapi import BaseAPI


class Metadata(BaseAPI):
    """
        Metadata API: Provide useful information about the current Droplet.
        See: https://developers.digitalocean.com/metadata/#introduction
    """
    droplet_id = None
    end_point = "http://169.254.169.254/metadata/v1"

    def __init__(self, *args, **kwargs):
        super(Metadata, self).__init__(*args, **kwargs)
        self.end_point = "http://169.254.169.254/metadata/v1"

    def get_data(self, url, headers=dict(), params=dict(), render_json=True):
        """
            Customized version of get_data to directly get the data without
            using the authentication method.
        """
        url = urljoin(self.end_point, url)

        response = requests.get(url, headers=headers, params=params,
                                timeout=self.get_timeout())

        if render_json:
            return response.json()
        return response.content

    def load(self):
        metadata = self.get_data("v1.json")

        for attr in metadata.keys():
            setattr(self, attr, metadata[attr])

        return self

    def __str__(self):
        return "<Metadata: %s>" % (self.droplet_id)