Python Partner ExampleΒΆ

This is an example of how to use the Memset Firewalling API with Python and the standard module xmlrpclib for XML-RPC client access.

Once the partner API key has been entered, and the discount line filled in or removed, the code will run with Python 2 or Python 3.

#!/usr/bin/env python
Use a partner API key to create an associated account, and then a
free cloud storage service on the new account.
from pprint import pprint
from time import sleep
    from xmlrpclib import ServerProxy
except ImportError:
    # Python3
    from xmlrpc.client import ServerProxy

def get_connection(api_key):
    """Return an XMLRPC proxy when given an API key"""
    return ServerProxy('' % api_key)

# key which has access to partner only methods
partner_key = '<PUT PARTNER API KEY HERE>'

# Create a new account
partner_api = get_connection(partner_key)
result = partner_api.partner.account.create({
    'user_title': "Ms",
    'user_forename': "Jane",
    'user_surname': "Doe",
    'user_company': "ACME",
    'user_email': "",
    'user_phone': "01111111111",
    'user_address': "10 Downing Street",
    'user_town': "Westminster",
    'user_statecounty': "London",
    'user_postcode': "SW1A 2AA",
    'user_country': "GB"})
account_name = result['account']
account_key = result['api_key']['key']

# Call api methods on behalf of the newly generated account
# using the returned key.

# The partner account will have defined a list of methods
# it is able to call on behalf of the newly created account
# e.g. create.memstore, memstore.user.create, memstore.usage,
# job.status

account_api = get_connection(account_key)
result = account_api.create.memstore({
    'sku': 'CLOUDF5',
    'discount_code': '<PUT DISCOUNT CODE HERE>',  # this line is optional
    'dry_run': False})
job = result['job']

# wait for the job to finish
print("Waiting for setup")
while job['status'] != 'DONE':
    job = account_api.job.status({'id': job['id']})
service_name = result['service']

# create a new API key for the product, and create a method proxy for it
result = partner_api.partner.apikey.create({
    'account_name': account_name,
    'service': service_name,
    'methods': ['memstore.usage'],
    'comment': "Usage only"})

service_api = get_connection(result['key'])

# make a new user for the memstore
pprint(service_api.memstore.user.create({'name': service_name, 'username': 'a_username', 'password': 'a_password', 'enabled': True}))

# display the usage information about the new store
pprint(service_api.memstore.usage({'name': service_name}))

# create a new Memstore container
service_api.memstore.container.create({'name': service_name, 'container': 'a_container_name'})

# set an ACL on the newly created container granting read/write access to the previously created user
service_api.memstore.container.set_acl({'name': service_name, 'container': 'a_container_name', 'read_acl': ['a_username'], 'write_acl': ['a_username']})

# view the ACL of a container
pprint(service_api.memstore.container.acl({'name': service_name, 'container': 'a_container_name'}))
{'read_acl': ['a_username'], 'write_acl': ['a_username']}

Previous topic

Python Firewalling Example

Next topic

Java Example

This Page