File: Project.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 (144 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download
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
from .baseapi import BaseAPI, GET, POST, DELETE, PUT


class Project(BaseAPI):
    def __init__(self,*args, **kwargs):
        self.name = None
        self.description = None
        self.purpose = None
        self.environment = None
        self.id = None
        self.is_default = None
        self.owner_uuid = None
        self.owner_id = None
        self.created_at = None
        self.updated_at = None
        self.resources = None
        super(Project,self).__init__(*args, **kwargs)

    @classmethod
    def get_object(cls, api_token, project_id):
        """Class method that will return a Project object by ID.
        Args:
            api_token (str): token
            kwargs (str): project id or project name
        """

        project = cls(token=api_token, id=project_id)
        project.load()
        return project

    def load(self):
        # URL https://api.digitalocean.com/v2/projects
        project = self.get_data("projects/%s" % self.id)
        project = project['project']
        for attr in project.keys():
            setattr(self, attr, project[attr])

    def set_as_default_project(self):

        data = {
            "name": self.name,
            "description": self.description,
            "purpose": self.purpose,
            "environment": self.environment,
            "is_default": True
        }

        project = self.get_data("projects/%s" % self.id, type=PUT, params=data)
        return project

    def create_project(self):

        """Creating Project with the following arguments
        Args:
            api_token (str): token
            "name": Name of the Project - Required
            "description": Description of the Project - Optional
            "purpose": Purpose of the project - Required
            "environment": Related Environment of  Project - Optional
                         - Development
                         - Stating
                         - Production
        """

        data = {
            "name": self.name,
            "purpose": self.purpose
        }
        if self.description:
            data['description'] = self.description
        if self.environment:
            data['environment'] = self.environment

        data = self.get_data("projects", type=POST, params=data)

        if data:
            self.id = data['project']['id']
            self.owner_uuid = data['project']['owner_uuid']
            self.owner_id = data['project']['owner_id']
            self.name = data['project']['name']
            self.description = data['project']['description']
            self.purpose = data['project']['purpose']
            self.environment = data['project']['environment']
            self.is_default = data['project']['is_default']
            self.created_at = data['project']['created_at']
            self.updated_at = data['project']['updated_at']

    def delete_project(self):
        data = dict()
        return self.get_data("projects/%s" % self.id, type=DELETE, params=data)

    def update_project(self, **kwargs):
        data = dict()
        data['name'] = kwargs.get("name", self.name)
        data['description'] = kwargs.get("description", self.description)
        data['purpose'] = kwargs.get("purpose", self.purpose)
        """
        Options for Purpose by Digital Ocean
         - Just Trying out DigitalOcean
         - Class Project / Educational Purposes
         - Website or blog
         - Web Application
         - Service or API
         - Mobile Application
         - Machine Learning / AI / Data Processing
         - IoT
         - Operational / Developer tooling
         - Other
        """
        data['environment'] = kwargs.get("environment", self.environment)
        """
        Options for Environment by Digital Ocean
         - Development
         - Stating
         - Production
        """
        data['is_default'] = kwargs.get("is_default", self.is_default)
        update_response = self.get_data("projects/%s" % self.id, type=PUT, params=data)
        for attr in update_response['project'].keys():
            setattr(self, attr, update_response['project'][attr])

    def get_all_resources(self):
        project_resources_response = self.get_data("projects/%s/resources" % self.id)
        project_resources = project_resources_response['resources']
        self.resources = []
        for i in project_resources:
            self.resources.append(i['urn'])
        return self.resources

    def load_resources(self):
        project_resources_response = self.get_data("projects/%s/resources" % self.id)
        project_resources = project_resources_response['resources']
        self.resources = []
        for i in project_resources:
            self.resources.append(i['urn'])

    def assign_resource(self, resources):
        data = {
            'resources': resources
        }
        return self.get_data("projects/%s/resources" % self.id, type=POST, params=data)

    def __str__(self):
        return "<Project: " + self.name + "> " + self.id