Skip to content

Commit

Permalink
Initial support for the v3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jul 25, 2019
1 parent e5e593c commit 1a4a748
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
@@ -1,4 +1,4 @@
# [Racher API](http://rancher-api.hive.pt)
# [Rancher API](http://rancher-api.hive.pt)

The Python Rancher API client.

Expand All @@ -11,8 +11,8 @@ The Rancher API projects takes most of its reference implementation from the [Of
| Name | Type | Description |
| ----- | ----- | ----- |
| **RANCHER_BASE_URL** | `str` | The base URL to the rancher server of target (eg: `http://rancher.domain.com:8080/v2-beta/`) (defaults to `None`). |
| **RANCHER_USERNAME** | `str` | The username to be used for API authentication (defaults to `None`). |
| **RANCHER_PASSWORD** | `str` | The password to be used for API authentication (defaults to `None`). |
| **RANCHER_USERNAME** | `str` | The username (access key) to be used for API authentication (defaults to `None`). |
| **RANCHER_PASSWORD** | `str` | The password (secret key) to be used for API authentication (defaults to `None`). |

## License

Expand Down
42 changes: 42 additions & 0 deletions src/examples/app.py
Expand Up @@ -51,6 +51,48 @@ def __init__(self, *args, **kwargs):
def index(self):
return self.stacks()

@appier.route("/clusters", "GET")
def clusters(self):
api = self.get_api()
clusters = api.list_clusters()
return clusters

@appier.route("/clusters/<str:id>", "GET")
def cluster_safe(self, id):
api = self.get_api()
cluster = api.get_cluster_safe(id)
return cluster

@appier.route("/clusters/<str:cluster>/projects", "GET")
def projects(self, cluster):
api = self.get_api()
cluster = api.get_cluster_safe(cluster)["id"]
projects = api.list_projects(cluster)
return projects

@appier.route("/clusters/<str:cluster>/projects/<str:id>", "GET")
def project_safe(self, cluster, id):
api = self.get_api()
cluster = api.get_cluster_safe(cluster)["id"]
project = api.get_project_safe(cluster, id)
return project

@appier.route("/clusters/<str:cluster>/projects/<str:project>/workloads", "GET")
def workloads(self, cluster, project):
api = self.get_api()
cluster = api.get_cluster_safe(cluster)["id"]
project = api.get_project_safe(cluster, project)["id"]
workloads = api.list_workloads(project)
return workloads

@appier.route("/clusters/<str:cluster>/projects/<str:project>/workloads/<str:id>", "GET")
def workload_safe(self, cluster, project, id):
api = self.get_api()
cluster = api.get_cluster_safe(cluster)["id"]
project = api.get_project_safe(cluster, project)["id"]
workload = api.get_workload_safe(project, id)
return workload

@appier.route("/stacks", "GET")
def stacks(self):
api = self.get_api()
Expand Down
2 changes: 2 additions & 0 deletions src/rancher/__init__.py
Expand Up @@ -35,11 +35,13 @@
""" The license for the module """

from . import base
from . import cluster
from . import service
from . import stack
from . import workload

from .base import API
from .cluster import ClusterAPI
from .project import ProjectAPI
from .service import ServiceAPI
from .stack import StackAPI
Expand Down
2 changes: 2 additions & 0 deletions src/rancher/base.py
Expand Up @@ -42,6 +42,7 @@
import appier

from . import stack
from . import cluster
from . import project
from . import service
from . import workload
Expand All @@ -51,6 +52,7 @@
class API(
appier.API,
stack.StackAPI,
cluster.ClusterAPI,
project.ProjectAPI,
service.ServiceAPI,
workload.WorkloadAPI
Expand Down
66 changes: 66 additions & 0 deletions src/rancher/cluster.py
@@ -0,0 +1,66 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Rancher API
# Copyright (c) 2008-2019 Hive Solutions Lda.
#
# This file is part of Hive Rancher API.
#
# Hive Rancher API is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Rancher API 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
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Rancher API. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2019 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

class ClusterAPI(object):
"""
The cluster API endpoints used by the Rancher 2.x
infra-structure.
"""

def list_clusters(self, *args, **kwargs):
url = self.base_url + "clusters"
contents = self.get(url, **kwargs)
data = contents["data"]
return data

def list_clusters_name(self, name):
url = self.base_url + "clusters?name=%s" % name
contents = self.get(url)
data = contents["data"]
return data

def get_cluster(self, id):
url = self.base_url + "clusters/%s" % id
contents = self.get(url)
return contents

def get_cluster_safe(self, id):
contents = self.list_clusters_name(id)
if contents: return contents[0]
return self.get_cluster(id)
20 changes: 18 additions & 2 deletions src/rancher/project.py
Expand Up @@ -43,8 +43,24 @@ class ProjectAPI(object):
infra-structure.
"""

def list_projects(self, *args, **kwargs):
url = self.base_url + "project"
def list_projects(self, cluster, *args, **kwargs):
url = self.base_url + "clusters/%s/projects" % cluster
contents = self.get(url, **kwargs)
data = contents["data"]
return data

def list_projects_name(self, cluster, name):
url = self.base_url + "clusters/%s/projects?name=%s" % (cluster, name)
contents = self.get(url)
data = contents["data"]
return data

def get_project(self, cluster, id):
url = self.base_url + "clusters/%s/projects/%s" % (cluster, id)
contents = self.get(url)
return contents

def get_project_safe(self, cluster, id):
contents = self.list_projects_name(cluster, id)
if contents: return contents[0]
return self.get_project(cluster, id)
13 changes: 9 additions & 4 deletions src/rancher/workload.py
Expand Up @@ -44,18 +44,23 @@ class WorkloadAPI(object):
"""

def list_workloads(self, project, *args, **kwargs):
url = self.base_url + "project/%s/workload" % project
url = self.base_url + "projects/%s/workloads" % project
contents = self.get(url, **kwargs)
data = contents["data"]
return data

def list_workloads_name(self, project, name):
url = self.base_url + "project/%s/workload?name=%s" % (project, name)
url = self.base_url + "projects/%s/workloads?name=%s" % (project, name)
contents = self.get(url)
data = contents["data"]
return data

def get_workload(self, project, id):
url = self.base_url + "project/%s/workload/%s" % (project, id)
def get_workload(self, cluster, project, id):
url = self.base_url + "projects/%s/workloads/%s" % (project, id)
contents = self.get(url)
return contents

def get_workload_safe(self, project, id):
contents = self.list_workloads_name(project, id)
if contents: return contents[0]
return self.get_workload(project, id)

0 comments on commit 1a4a748

Please sign in to comment.