Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit cb3cebf

Browse files
authored
Merge pull request #1 from PokaInc/feat-initial-impl
Initial impl
2 parents 8b30cc0 + 9e18d8c commit cb3cebf

File tree

13 files changed

+525
-0
lines changed

13 files changed

+525
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
MANIFEST
2+
.idea
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
env/
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
30+
# tests artifacts
31+
.cache
32+
.pytest_cache

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: python
2+
python:
3+
- "2.6"
4+
- "2.7"
5+
- "3.3"
6+
- "3.4"
7+
- "3.5"
8+
- "3.5-dev" # 3.5 development branch
9+
- "3.6"
10+
- "3.6-dev" # 3.6 development branch
11+
- "3.7-dev" # 3.7 development branch
12+
- "nightly" # currently points to 3.7-dev
13+
before_install:
14+
- pip install --upgrade pytest
15+
# command to install dependencies
16+
install:
17+
- "pip install e ."
18+
- "pip install -r requirements_test.txt"
19+
# command to run tests
20+
script: py.test

LICENSE.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Poka Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.PHONY: build publish-test
2+
3+
clean:
4+
-@rm -dr build dist
5+
6+
check-tools-versions:
7+
python check_tools_version.py
8+
9+
build: check-tools-versions
10+
@python setup.py sdist
11+
@python setup.py bdist_wheel
12+
13+
publish-test: clean build
14+
@twine upload --repository testpypi dist/*
15+
16+
publish: clean build
17+
@twine upload --repository pypi dist/*

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
arnparse
2+
========
3+
4+
Parse ARNs using Python
5+
6+
[![Build Status](https://travis-ci.org/PokaInc/arnparse.svg?branch=master)](https://travis-ci.org/PokaInc/arnparse)
7+
8+
Motivation
9+
----------
10+
11+
Sometimes, you want to parse an Amazon Resource Name (ARN) into its
12+
components to get some useful information from the ARN (e.g. AWS region,
13+
account ID, etc).
14+
15+
You can find documentation on ARNs and their components here:
16+
https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
17+
18+
Installation
19+
------------
20+
21+
`pip install arnparse`
22+
23+
Examples
24+
--------
25+
26+
### S3 Object
27+
28+
```python
29+
from arnparse import arnparse
30+
31+
arn = arnparse('arn:aws:s3:::my_corporate_bucket/exampleobject.png')
32+
33+
assert arn.partition == 'aws'
34+
assert arn.service == 's3'
35+
assert arn.region is None
36+
assert arn.account_id is None
37+
assert arn.resource_type is None
38+
assert arn.resource == 'my_corporate_bucket/exampleobject.png'
39+
```
40+
41+
### VPC
42+
43+
```python
44+
from arnparse import arnparse
45+
46+
arn = arnparse('arn:aws:ec2:us-east-1:123456789012:vpc/vpc-fd580e98')
47+
48+
assert arn.partition == 'aws'
49+
assert arn.service == 'ec2'
50+
assert arn.region == 'us-east-1'
51+
assert arn.account_id == '123456789012'
52+
assert arn.resource_type == 'vpc'
53+
assert arn.resource == 'vpc-fd580e98'
54+
```
55+
56+
### CloudWatch Alarm
57+
58+
```python
59+
from arnparse import arnparse
60+
61+
arn = arnparse('arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarmName')
62+
63+
assert arn.partition == 'aws'
64+
assert arn.service == 'cloudwatch'
65+
assert arn.region == 'us-east-1'
66+
assert arn.account_id == '123456789012'
67+
assert arn.resource_type == 'alarm'
68+
assert arn.resource == 'MyAlarmName'
69+
```
70+
71+
### SNS Topic
72+
73+
```python
74+
from arnparse import arnparse
75+
76+
arn = arnparse('arn:aws:sns:*:123456789012:my_corporate_topic')
77+
78+
assert arn.partition == 'aws'
79+
assert arn.service == 'sns'
80+
assert arn.region == '*'
81+
assert arn.account_id == '123456789012'
82+
assert arn.resource_type is None
83+
assert arn.resource == 'my_corporate_topic'
84+
```
85+
86+
### API Gateway
87+
88+
```python
89+
from arnparse import arnparse
90+
91+
arn = arnparse('arn:aws:apigateway:us-east-1::a123456789012bc3de45678901f23a45:/test/mydemoresource/*')
92+
93+
assert arn.partition == 'aws'
94+
assert arn.service == 'apigateway'
95+
assert arn.region == 'us-east-1'
96+
assert arn.account_id is None
97+
assert arn.resource_type is None
98+
assert arn.resource == 'a123456789012bc3de45678901f23a45:/test/mydemoresource/*'
99+
```

arnparse/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .arnparse import arnparse

arnparse/arnparse.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import absolute_import
2+
3+
from arnparse.str_utils import empty_str_to_none
4+
5+
6+
class MalformedArnError(Exception):
7+
def __init__(self, arn_str):
8+
self.arn_str = arn_str
9+
10+
def __str__(self):
11+
return 'arn_str: {arn_str}'.format(arn_str=self.arn_str)
12+
13+
14+
class Arn(object):
15+
def __init__(self, partition, service, region, account_id, resource_type, resource):
16+
self.partition = partition
17+
self.service = service
18+
self.region = region
19+
self.account_id = account_id
20+
self.resource_type = resource_type
21+
self.resource = resource
22+
23+
24+
def arnparse(arn_str):
25+
if not arn_str.startswith('arn:'):
26+
raise MalformedArnError(arn_str)
27+
28+
elements = arn_str.split(':', 5)
29+
30+
resource = elements[5]
31+
resource_type = None
32+
33+
service = elements[2]
34+
if service not in ['s3', 'sns', 'apigateway', 'execute-api']:
35+
if '/' in resource:
36+
resource_type, resource = resource.split('/', 1)
37+
elif ':' in resource:
38+
resource_type, resource = resource.split(':', 1)
39+
40+
return Arn(
41+
partition=elements[1],
42+
service=service,
43+
region=empty_str_to_none(elements[3]),
44+
account_id=empty_str_to_none(elements[4]),
45+
resource_type=resource_type,
46+
resource=resource,
47+
)

arnparse/str_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def empty_str_to_none(str_):
2+
if str_ == '':
3+
return None
4+
5+
return str_

check_tools_version.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from distutils.version import StrictVersion
2+
3+
import setuptools
4+
import twine
5+
import wheel
6+
7+
if __name__ == '__main__':
8+
"""
9+
Ensure that all tools are correctly installed. See https://stackoverflow.com/a/26737258
10+
"""
11+
assert StrictVersion(setuptools.__version__) >= StrictVersion('38.6.0'), 'Please upgrade setuptools. ' \
12+
'See https://stackoverflow.com/a/26737258'
13+
assert StrictVersion(twine.__version__) >= StrictVersion('1.11.0'), 'Please upgrade twine. ' \
14+
'See https://stackoverflow.com/a/26737258'
15+
assert StrictVersion(wheel.__version__) >= StrictVersion('0.31.0'), 'Please upgrade wheel. ' \
16+
'See https://stackoverflow.com/a/26737258'

requirements_test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest
2+
pytest-mock

0 commit comments

Comments
 (0)