This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new app for services infrastructure tracking
- Loading branch information
1 parent
14792ba
commit c1dc02d
Showing
21 changed files
with
1,177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
arrow | ||
attrs | ||
boto3 | ||
celery | ||
celery-redbeat | ||
colorama | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from faker import Faker | ||
import pytest | ||
|
||
from zoo.datacenters import amazon as uut, models | ||
|
||
fake = Faker() | ||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_amazon_map_to_nodes(mocker): | ||
mocker.patch( | ||
"zoo.datacenters.amazon.iter_hosted_zones", | ||
return_value=[ | ||
{"Id": "hostedzone/1", "Name": "example.com"}, | ||
{ | ||
"Id": "hostedzone/2", | ||
"Name": "zoo.example.com", | ||
"AliasTarget": {"DNSName": "elb1.example.com"}, | ||
}, | ||
], | ||
) | ||
mocker.patch( | ||
"zoo.datacenters.amazon.iter_resource_record_sets", | ||
return_value=[ | ||
{ | ||
"Type": "A", | ||
"Name": "test.example.com", | ||
"AliasTarget": {"DNSName": "elb1.example.com"}, | ||
}, | ||
{ | ||
"Type": "CNAME", | ||
"Name": "test2.example.com", | ||
"ResourceRecords": [{"Value": "elb2.example.com"}], | ||
}, | ||
], | ||
) | ||
|
||
def get_load_balancers(version=1, **kwargs): | ||
result = None | ||
if version == 1: | ||
result = [ | ||
{ | ||
"DNSName": "elb1.example.com", | ||
"Instances": [{"InstanceId": "i-a"}, {"InstanceId": "i-b"}], | ||
} | ||
] | ||
elif version == 2: | ||
result = [ | ||
{"DNSName": "elb2.example.com", "Instances": [{"InstanceId": "i-c"}]} | ||
] | ||
return result | ||
|
||
mocker.patch("zoo.datacenters.amazon.iter_load_balancers", new=get_load_balancers) | ||
mocker.patch( | ||
"zoo.datacenters.amazon.iter_ec2_instances", | ||
return_value=[ | ||
{"InstanceId": "i-a", "PrivateDnsName": "ip-1"}, | ||
{"InstanceId": "i-b", "PrivateDnsName": "ip-2"}, | ||
{"InstanceId": "i-c", "PrivateDnsName": "ip-3"}, | ||
], | ||
) | ||
|
||
uut.map_to_nodes(profiles="default") | ||
|
||
root = models.InfraNode.objects.get(kind=models.NodeKind.AWS_ROOT_DNS) | ||
|
||
zones = {zone.value: zone for zone in root.targets.all()} | ||
assert set(zones) == {"zoo.example.com", "example.com"} | ||
|
||
record_sets = {rs.value: rs for rs in zones["example.com"].targets.all()} | ||
assert set(record_sets) == {"test.example.com", "test2.example.com"} | ||
|
||
[elb1_dns] = record_sets["test.example.com"].targets.all() | ||
[elb2_dns] = record_sets["test2.example.com"].targets.all() | ||
|
||
[elb1] = elb1_dns.targets.all() | ||
[elb2] = elb2_dns.targets.all() | ||
|
||
assert elb1.value == "elb1.example.com" | ||
assert elb2.value == "elb2.example.com" | ||
|
||
assert {instance.value for instance in elb1.targets.all()} == {"ip-1", "ip-2"} | ||
assert {instance.value for instance in elb2.targets.all()} == {"ip-3"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from zoo.datacenters import mapping as uut | ||
|
||
|
||
def test_url_matches_dns(): | ||
assert uut.url_matches_dns("zoo.example.com", "*.example.com") | ||
assert uut.url_matches_dns("zoo.example.com", "zoo.*.com") | ||
assert uut.url_matches_dns("zoo.example.com", "zoo.example.com") | ||
|
||
assert not uut.url_matches_dns("zoo.example.com", "abc.example.com") | ||
assert not uut.url_matches_dns("zoo.example.com", "abc.*.com") | ||
assert not uut.url_matches_dns("zoo.example.com", "*.example.cz") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
|
||
from zoo.datacenters import models as uut | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_infra_node__get_or_create_node(): | ||
root = uut.InfraNode.get_or_create_node(kind="kind1", value="value1") | ||
|
||
assert root.value == "value1" | ||
assert list(root.sources.all()) == [] | ||
assert list(root.targets.all()) == [] | ||
|
||
node = uut.InfraNode.get_or_create_node(kind="kind2", value="value2", source=root) | ||
|
||
assert list(node.sources.all()) == [root] | ||
assert list(root.targets.all()) == [node] | ||
|
||
assert root.id == uut.InfraNode.get_or_create_node(kind="kind1", value="value1").id | ||
|
||
|
||
def test_infra_node__find_sources_by_kind(): | ||
root = uut.InfraNode.objects.create(kind="root", value="123") | ||
|
||
node_dns = uut.InfraNode.objects.create(kind="dns", value="a1b") | ||
node_abc = uut.InfraNode.objects.create(kind="abc", value="a2b") | ||
|
||
root.targets.add(node_dns) | ||
root.targets.add(node_abc) | ||
|
||
node_dns2 = uut.InfraNode.objects.create(kind="dns", value="b11") | ||
node_dns3 = uut.InfraNode.objects.create(kind="dns", value="b22") | ||
node_abc.targets.add(node_dns2) | ||
node_dns2.targets.add(node_dns3) | ||
|
||
leaf = uut.InfraNode.objects.create(kind="leaf", value="ccc") | ||
node_dns.targets.add(leaf) | ||
node_dns3.targets.add(leaf) | ||
|
||
dns_sources = leaf.find_sources_by_kind("dns") | ||
dns_nodes_ids = {node_dns.id, node_dns2.id, node_dns3.id} | ||
assert {source.id for source in dns_sources} == dns_nodes_ids | ||
|
||
root_sources = leaf.find_sources_by_kind("root") | ||
assert list(root_sources) == [root] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from faker import Faker | ||
import pytest | ||
|
||
from zoo.datacenters import rancher as uut, models | ||
|
||
fake = Faker() | ||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_rancher_parse_members_from_project(): | ||
names = [fake.name(), fake.name()] | ||
members = uut.parse_members_from_project( | ||
{ | ||
"id": "p2", | ||
"members": [ | ||
{ | ||
"type": "projectMember", | ||
"externalId": f"cn={names[0]},ou=People,dc=example,dc=com", | ||
"role": "owner", | ||
}, | ||
{ | ||
"type": "projectMember", | ||
"externalId": f"cn={names[1]},ou=People,dc=example,dc=com", | ||
"role": "owner", | ||
}, | ||
], | ||
} | ||
) | ||
assert members == names | ||
|
||
|
||
def test_rancher_map_to_nodes(mocker): | ||
mocker.patch("zoo.datacenters.rancher.iter_projects", return_value=[{"id": "p1"}]) | ||
mocker.patch( | ||
"zoo.datacenters.rancher.iter_services", | ||
return_value=[ | ||
{"id": "s1", "launchConfig": {"imageUuid": "docker:lb"}}, | ||
{"id": "s2", "launchConfig": {"imageUuid": "docker:zoo"}}, | ||
], | ||
) | ||
mocker.patch( | ||
"zoo.datacenters.rancher.iter_load_balancers", | ||
return_value=[ | ||
{ | ||
"id": "lb1", | ||
"lbConfig": { | ||
"portRules": [ | ||
{ | ||
"hostname": "zoo.example.com", | ||
"sourcePort": 80, | ||
"protocol": "http", | ||
"serviceId": "s2", | ||
} | ||
] | ||
}, | ||
"publicEndpoints": [{"hostId": "h1"}], | ||
} | ||
], | ||
) | ||
mocker.patch( | ||
"zoo.datacenters.rancher.iter_hosts", | ||
return_value=[{"id": "h1", "hostname": "ip-127-0-0-1"}], | ||
) | ||
|
||
uut.map_to_nodes() | ||
|
||
project = models.InfraNode.objects.get(kind=models.NodeKind.RANCHER_PROJ_ID) | ||
assert project.value == "p1" | ||
|
||
hosts = project.targets.all() | ||
assert {host.value for host in hosts} == {"ip-127-0-0-1"} | ||
|
||
lbs = hosts[0].targets.all() | ||
assert {lb.value for lb in lbs} == {"lb1"} | ||
|
||
portrules = lbs[0].targets.all() | ||
assert {portrule.value for portrule in portrules} == {"zoo.example.com"} | ||
|
||
services = portrules[0].targets.all() | ||
assert {service.value for service in services} == {"s2"} | ||
|
||
images = services[0].targets.all() | ||
assert {image.value for image in images} == {"docker:zoo"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.contrib import admin | ||
|
||
from . import models | ||
|
||
|
||
class DatacenterAdmin(admin.ModelAdmin): | ||
search_fields = ("provider", "region") | ||
|
||
|
||
admin.site.register(models.Datacenter, DatacenterAdmin) |
Oops, something went wrong.