Skip to content

Commit 1a67c5c

Browse files
committed
0.0.4
0 parents  commit 1a67c5c

36 files changed

+3262
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
build/
11+
dist/
12+
eggs/
13+
.eggs/
14+
*.egg-info/
15+
*.egg
16+
17+
# Shell Environment
18+
.bash_history
19+
.cache/
20+
.cf/
21+
22+
# Project-specific
23+
environment.sh
24+
25+
# Testing
26+
.tox/
27+
.coverage
28+
coverage/
29+
.coverage.*
30+
nosetests.xml
31+
coverage.xml
32+
*,cover

LICENSE

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Copyright (c) 2016 General Electric Company operating through GE Digital LLC
2+
Permission is hereby granted, free of charge, to any person obtaining a copy of
3+
this software and associated documentation files (the "Software"), to deal in
4+
the Software without restriction, including without limitation the rights to
5+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
6+
of the Software, and to permit persons to whom the Software is furnished to do
7+
so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all
10+
copies or substantial portions of the Software.
11+
12+
Other than the license expressly provided herein no license to or grant of any
13+
other intellectual property owned, controlled or licensed by General Electric
14+
Company or its affiliates (“GE”) is expressly or impliedly granted. GE
15+
reserves all rights with respect to all such intellectual property and nothing
16+
in this license shall be deemed a waiver of such rights.
17+
18+
It is expressly understood, acknowledged and agreed that you may provide
19+
General Electric reasonable suggestions, comments and feedback regarding the
20+
Software, including but not limited to usability, bug reports and test results,
21+
with respect to Software testing (collectively, "Feedback"). If you provide
22+
such Feedback to GE, you shall grant GE worldwide, non-exclusive, perpetual,
23+
irrevocable, royalty free, fully paid up rights to utilize such Feedback in any
24+
GE product or service in GE’s sole discretion.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE. NO GUARANTEE OF UNINTERRUPTED, TIMELY, SECURE, OR ERROR-FREE
33+
OPERATION IS MADE.
34+
35+
THESE LICENSED PROGRAMS MAY BE USED AS PART OF A DEVELOPMENT ENVIRONMENT, AND
36+
MAY BE COMBINED WITH OTHER CODE BY END-USERS. LICENSOR IS NOT ABLE TO GUARANTEE
37+
THAT THE LICENSED PROGRAMS WILL OPERATE WITHOUT DEFECTS WHEN USED IN
38+
COMBINATION WITH END-USER SOFTWARE. LICENSEE IS ADVISED TO SAFEGUARD IMPORTANT
39+
DATA, TO USE CAUTION, AND NOT TO RELY IN ANY WAY ON THE CORRECT FUNCTIONING OR
40+
PERFORMANCE OF ANY COMBINATION OF END-USER SOFTWARE AND THE LICENSED PROGRAMS
41+
AND/OR ACCOMPANYING MATERIALS. LICENSEE IS ADVISED NOT TO USE ANY COMBINATION
42+
OF LICENSED PROGRAMS AND END-USER PROVIDED SOFTWARE IN A PRODUCTION ENVIRONMENT
43+
WITHOUT PRIOR SUITABILITY AND DEFECT TESTING.

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
# PredixPy
3+
4+
The Predix Python SDK aims to help accelerate application development with
5+
client libraries for commonly used Predix Platform Services. This framework
6+
provides helper methods and classes.
7+
8+
You can find out more about services in the [Predix Service Catalog][catalog].
9+
10+
## Installation
11+
12+
To start using the SDK downloaded from PyPI:
13+
14+
```
15+
$ pip install predix
16+
```
17+
18+
If you want to install from source, see developer section below.
19+
20+
# Basic Usage
21+
22+
## Administration
23+
24+
When you have done a `cf login` to a Cloud Foundry endpoint you can begin
25+
creating services in Python.
26+
27+
```
28+
# Create a UAA service instance as most services require it
29+
# and create a client for your application
30+
import predix.admin.uaa
31+
uaa = predix.admin.uaa.UserAccountAuthentication()
32+
uaa.create('admin-secret')
33+
34+
# Cloud Foundry applications require client authorization and a
35+
# manifest helps for when you `cf push`.
36+
uaa.create_client('my-client', 'my-client-secret')
37+
uaa.add_to_manifest('./manifest.yml')
38+
uaa.add_client_to_manifest('my-client', 'my-client-secret', './manifest.yml')
39+
40+
# Create a Time Series service instance
41+
import predix.admin.timeseries
42+
ts = predix.admin.timeseries.TimeSeries()
43+
ts.create()
44+
45+
# Let client use this service
46+
ts.grant_client('my-client')
47+
ts.add_to_manifest('./manifest.yml')
48+
49+
```
50+
51+
## Applications
52+
53+
Once your space has been configured, you can use PredixPy to work with
54+
the services in your applications.
55+
56+
```
57+
# If not in a Cloud Foundry deployed environment and testing locally,
58+
# you can use this manifest utility to load environment variables
59+
# into your process.
60+
61+
import predix.app
62+
manifest = predix.app.Manifest('./manifest.yml')
63+
64+
# Start using the services
65+
66+
import predix.data.timeseries
67+
68+
ts = predix.data.timeseries.TimeSeries()
69+
ts.send('test', 12)
70+
print(ts.get_values('test'))
71+
```
72+
73+
# Developing PredixPy
74+
75+
## Setup Environment
76+
77+
To install into your environment for use in editable mode:
78+
79+
```
80+
$ pip install -e .
81+
```
82+
83+
Recommend using `virtualenv` or `venv`. If you prefer to use a docker
84+
environment there is one with cloud foundry pre-installed:
85+
86+
```
87+
$ docker run -it --rm --volume=$(pwd):/home/app j12y/cf-ade-py
88+
```
89+
90+
## Quality
91+
92+
To check coding style:
93+
```
94+
$ python setup.py flake8
95+
```
96+
97+
To check test coverage:
98+
```
99+
$ python setup.py nosetests --with-coverage --cover-xml
100+
```
101+
102+
## Distribution
103+
104+
Running setuptools sdist will produce a file `dist/predix-x.y.z.tar.gz` which
105+
can be useful. It is not only the file needed to publish to PyPI, it can be
106+
used in the **vendor/** folder used by the python_buildpack when deploying an
107+
app to Cloud Foundry.
108+
```
109+
$ python setup.py sdist
110+
```
111+
112+
113+
114+
---
115+
[catalog]: https://www.predix.io/catalog/services
116+

__init__.py

Whitespace-only changes.

predix/__init__.py

Whitespace-only changes.

predix/admin/__init__.py

Whitespace-only changes.

predix/admin/acs.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
import os
3+
4+
import predix.app
5+
import predix.security.uaa
6+
import predix.admin.service
7+
8+
9+
class AccessControl(object):
10+
"""
11+
Access Control provides attribute based access control.
12+
"""
13+
def __init__(self, name=None, uaa=None, *args, **kwargs):
14+
super(AccessControl, self).__init__(*args, **kwargs)
15+
self.service_name = 'predix-acs'
16+
self.plan_name = 'Free'
17+
self.service = predix.admin.service.PredixService(self.service_name,
18+
self.plan_name, name=name, uaa=uaa)
19+
20+
def exists(self):
21+
"""
22+
Returns whether or not this service already exists.
23+
"""
24+
return self.service.exists()
25+
26+
def create(self):
27+
"""
28+
Create an instance of the Access Control Service with the typical
29+
starting settings.
30+
"""
31+
self.service.create()
32+
os.environ['PREDIX_ACS_URI'] = self.service.settings.data['uri']
33+
os.environ['PREDIX_ACS_ZONE_ID'] = self.service.serttings.data['zone']['http-header-value']
34+
35+
def grant_client(self, client_id):
36+
"""
37+
Grant the given client id all the scopes and authorities
38+
needed to work with the access control service.
39+
"""
40+
zone = self.service.settings.data['zone']['oauth-scope']
41+
42+
scopes = ['openid', zone,
43+
'acs.policies.read', 'acs.attributes.read',
44+
'acs.policies.write', 'acs.attributes.write']
45+
46+
authorities = ['uaa.resource', zone,
47+
'acs.policies.read', 'acs.policies.write',
48+
'acs.attributes.read', 'acs.attributes.write']
49+
50+
self.service.uaa.uaac.update_client_grants(client_id, scope=scopes,
51+
authorities=authorities)
52+
53+
return self.service.uaa.uaac.get_client(client_id)
54+
55+
def add_to_manifest(self, manifest_path):
56+
"""
57+
Add details to the manifest that applications using
58+
this service may need to consume.
59+
"""
60+
manifest = predix.app.Manifest(manifest_path)
61+
62+
# Add this service to list of services
63+
manifest.add_service(self.service.name)
64+
65+
# Add environment variables
66+
manifest.add_env_var('PREDIX_ACS_URI',
67+
self.service.settings.data['uri'])
68+
manifest.add_env_var('PREDIX_ACS_ZONE_ID',
69+
self.service.settings.data['zone']['http-header-value'])
70+
71+
manifest.write_manifest()

predix/admin/analytics.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
import predix.app
3+
import predix.security.uaa
4+
import predix.admin.service
5+
6+
7+
class AnalyticsFramework(predix.admin.spaces.SpaceManager):
8+
"""
9+
Analytics framework.
10+
"""
11+
def __init__(self, plan_name=None, name=None, uaa=None, *args, **kwargs):
12+
super(AnalyticsFramework, self).__init__(*args, **kwargs)
13+
self.service_name = 'predix-analytics-framework'
14+
self.plan_name = plan_name or 'Free'
15+
self.service = predix.admin.service.PredixService(self.service_name,
16+
self.plan_name, name=name, uaa=uaa)
17+
18+
def exists(self):
19+
"""
20+
Returns whether or not this service already exists.
21+
"""
22+
return self.service.exists()
23+
24+
def create(self, asset, timeseries, client_id, client_secret,
25+
ui_client_id=None, ui_client_secret=None):
26+
"""
27+
Create an instance of the Analytics Framework Service with the
28+
typical starting settings.
29+
30+
If not provided, will reuse the runtime client for the ui
31+
as well.
32+
"""
33+
assert isinstance(asset, predix.admin.asset.Asset), \
34+
"Require an existing predix.admin.asset.Asset instance"
35+
assert isinstance(timeseries, predix.admin.timeseries.TimeSeries), \
36+
"Require an existing predix.admin.timeseries.TimeSeries instance"
37+
38+
parameters = {
39+
'predixAssetZoneId': asset.get_zone_id(),
40+
'predixTimeseriesZoneId': timeseries.get_query_zone_id(),
41+
'runtimeClientId': client_id,
42+
'runtimeClientSecret': client_secret,
43+
'uiClientId': ui_client_id or client_id,
44+
'uiClientSecret': ui_client_secret or client_secret,
45+
'uiDomainPrefix': self.service.name,
46+
}
47+
48+
self.service.create(parameters=parameters)

predix/admin/asset.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
import os
3+
4+
import predix.app
5+
import predix.security.uaa
6+
import predix.admin.service
7+
8+
9+
class Asset(object):
10+
"""
11+
Asset Service provides asset management functionality.
12+
"""
13+
def __init__(self, plan_name=None, name=None, uaa=None, *args, **kwargs):
14+
super(Asset, self).__init__(*args, **kwargs)
15+
self.service_name = 'predix-asset'
16+
self.plan_name = plan_name or 'Free'
17+
self.service = predix.admin.service.PredixService(self.service_name,
18+
self.plan_name, name=name, uaa=uaa)
19+
20+
def exists(self):
21+
"""
22+
Returns whether or not this service already exists.
23+
"""
24+
return self.service.exists()
25+
26+
def create(self):
27+
"""
28+
Create an instance of the Asset Service with the typical
29+
starting settings.
30+
"""
31+
self.service.create()
32+
os.environ['PREDIX_ASSET_URI'] = self.service.settings.data['uri']
33+
os.environ['PREDIX_ASSET_ZONE_ID'] = self.get_zone_id()
34+
35+
def grant_client(self, client_id):
36+
"""
37+
Grant the given client id all the scopes and authorities
38+
needed to work with the asset service.
39+
"""
40+
zone = self.service.settings.data['zone']['oauth-scope']
41+
42+
scopes = ['openid', zone]
43+
44+
authorities = ['uaa.resource', zone]
45+
46+
self.service.uaa.uaac.update_client_grants(client_id, scope=scopes,
47+
authorities=authorities)
48+
49+
return self.service.uaa.uaac.get_client(client_id)
50+
51+
def get_zone_id(self):
52+
"""
53+
Returns the Predix-Zone-Id used for this service.
54+
"""
55+
return self.service.settings.data['zone']['http-header-value']
56+
57+
def add_to_manifest(self, manifest_path):
58+
"""
59+
Add details to the manifest that applications using
60+
this service may need to consume.
61+
"""
62+
manifest = predix.app.Manifest(manifest_path)
63+
64+
# Add this service to list of services
65+
manifest.add_service(self.service.name)
66+
67+
# Add environment variables
68+
manifest.add_env_var('PREDIX_ASSET_URI',
69+
self.service.settings.data['uri'])
70+
manifest.add_env_var('PREDIX_ASSET_ZONE_ID', self.get_zone_id())
71+
72+
manifest.write_manifest()

predix/admin/cf/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)