Skip to content

Commit 9ddd2d9

Browse files
authored
feat: add colocation endpoints (#45)
Add the colocation endpoints to list all endpoints and retrieve a single endpoint by name. Fixes #24 Signed-off-by: Roald Nefs <[email protected]>
1 parent 1fd3ad6 commit 9ddd2d9

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes in **python-transip** are documented below.
44
## [Unreleased]
55
### Added
66
- The option to replace all existing nameservers of a single domain at once from the `transip.v6.objects.Domain.nameservers` service.
7+
- The option to list all colocations from the `transip.TransIP.colocations` service ([#24](https://github.com/roaldnefs/python-transip/issues/24)).
8+
- The option to retrieve a single colocation by name from the `transip.TransIP.colocations` service ([#24](https://github.com/roaldnefs/python-transip/issues/24)).
79

810
## [0.4.0] (2021-01-24)
911
### Added

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
- [VPS](#vps)
5858
- [HA-IP](#ha-ip)
5959
- [Colocation](#colocation)
60+
- [Colocations](#colocations)
61+
- [The **Colocation** class](#the-colocation-class)
62+
- [List all colocations](#list-all-colocations)
63+
- [Get colocation](#get-colocation)
6064

6165
## Introduction
6266
Welcome to the Python TransIP documentation.
@@ -708,3 +712,49 @@ The documentation for managing **HA-IPs** and related resources has not yet been
708712

709713
## Colocation
710714
The documentation for managing **colocations** and related resources has not yet been documented. Feel free to file an [issue](https://github.com/roaldnefs/python-transip/issues/new/choose) for adding the missing section(s) in the documentation.
715+
716+
### Colocations
717+
Manage colocations.
718+
719+
#### The **Colocation** class
720+
When listing all colocations in your TransIP account, a list of **transip.v6.objects.Colocation** objects is returned.
721+
722+
**_class_ Colocation**
723+
724+
The **Colocation** class makes the following attributes available:
725+
726+
- **name**: The colocation name.
727+
- **ipRanges**: The list of IP ranges.
728+
729+
#### List all colocations
730+
Retrieve the colocations registered in your TransIP account by calling **transip.TransIP.colocations.list()**. This will return a list of **transip.v6.objects.Colocation** objects.
731+
732+
For example:
733+
```python
734+
import transip
735+
# Initialize a client using the TransIP demo token.
736+
client = transip.TransIP(access_token=transip.v6.DEMO_TOKEN)
737+
738+
# Retrieve the colocations registered in your TransIP account.
739+
colocations = client.colocations.list()
740+
# Show the colocations information on the screen.
741+
for colocation in colocations:
742+
ip_ranges = ' '.join(colocation.ipRanges)
743+
print(f"Colocation: {colocation.name} has IP ranges: {ip_ranges}")
744+
```
745+
746+
#### Get colocation
747+
Retrieve a single colocation registered in your TransIP account by its ID by calling **transip.TransIP.colocations.get(_name_)**. This will return a **transip.v6.objects.Colocation** object.
748+
749+
For example:
750+
```python
751+
import transip
752+
# Initialize a client using the TransIP demo token.
753+
client = transip.TransIP(access_token=transip.v6.DEMO_TOKEN)
754+
755+
# Retrieve a colocation by its name.
756+
colocation = client.colocations.get('example2')
757+
# Show colocation information on the screen.
758+
ip_ranges = ' '.join(colocation.ipRanges)
759+
print(f"Colocation: {colocation.name} has IP ranges: {ip_ranges}")
760+
```

tests/services/test_colocations.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2021 Roald Nefs <[email protected]>
4+
#
5+
# This file is part of python-transip.
6+
#
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from typing import List, Tuple, Any, Dict, Optional
21+
import responses # type: ignore
22+
import unittest
23+
import tempfile
24+
import os
25+
26+
from transip import TransIP
27+
from transip.v6.objects import Colocation
28+
from tests.utils import load_responses_fixtures
29+
30+
31+
class ColocationsTest(unittest.TestCase):
32+
"""Test the ColocationService."""
33+
34+
client: TransIP
35+
36+
@classmethod
37+
def setUpClass(cls) -> None:
38+
"""Set up a minimal TransIP client for using the colocation services."""
39+
cls.client = TransIP(access_token='ACCESS_TOKEN')
40+
41+
def setUp(self) -> None:
42+
"""Setup mocked responses for the '/colocations' endpoint."""
43+
load_responses_fixtures("colocations.json")
44+
45+
@responses.activate
46+
def test_list(self) -> None:
47+
colocations: List[Colocation] = self.client.colocations.list() # type: ignore
48+
self.assertEqual(len(colocations), 1)
49+
self.assertEqual(colocations[0].get_id(), "example2") # type: ignore
50+
51+
@responses.activate
52+
def test_get(self) -> None:
53+
colocation_name: str = "example2"
54+
colocation: Colocation = self.client.colocations.get(colocation_name) # type: ignore
55+
self.assertEqual(colocation.get_id(), colocation_name) # type: ignore

transip/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def __init__(
108108
self.vpss: Type['ApiService'] = (
109109
objects.VpsService(self) # type: ignore
110110
)
111+
self.colocations: Type['ApiService'] = (
112+
objects.ColocationService(self) # type: ignore
113+
)
111114

112115
@property
113116
def url(self) -> str:

transip/v6/objects.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,17 @@ class VpsService(GetMixin, DeleteMixin, ListMixin, ApiService):
460460

461461
_resp_list_attr: str = "vpss"
462462
_resp_get_attr: str = "vps"
463+
464+
465+
class Colocation(ApiObject):
466+
467+
_id_attr: str = "name"
468+
469+
470+
class ColocationService(GetMixin, ListMixin, ApiService):
471+
472+
_path: str = "/colocations"
473+
_obj_cls: Optional[Type[ApiObject]] = Colocation
474+
475+
_resp_list_attr: str = "colocations"
476+
_resp_get_attr: str = "colocation"

0 commit comments

Comments
 (0)