Skip to content

Commit

Permalink
Create new Samples
Browse files Browse the repository at this point in the history
General
-------
Put static helper functions in a base class

Gateway
-------
Initial configuration sample

Portal
------
Removing devices from portal
Simple global admin activities
  • Loading branch information
ygalblum committed Mar 6, 2020
1 parent 7a88ed8 commit c0df056
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 0 deletions.
123 changes: 123 additions & 0 deletions samples/gateway_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import logging

from cterasdk import Gateway, gateway_enum, config, CTERAException

from sample_base import CTERASDKSampleBase


class GatewaySample(CTERASDKSampleBase):
_gateway_address_request_string = "the Gateway Address"
_gateway_username_request_string = "the user name"
_gateway_new_hostname_request_string = "a new hostname for the Gateway"
_gateway_new_location_request_string = "a new location for the Gateway"
_gateway_portal_address_request_string = "the Portal Address"
_gateway_volume_name_request_string = "a name for the Gateway volume"
_share_directory_name_request_string = "a new for the new shared directory"

def __init__(self):
self._device = None
self._volume_name = None

def run(self):
print("CTERA SDK - Gateway Demo")
self._connect_to_gateway()
self._device.test()
self._create_first_user()
self._create_user()
self._set_host_name()
self._set_location()
self._set_dns_server()
self._configure_volume()
self._connect_to_portal()
self._start_cloud_caching()
self._disable_first_time_wizard()
self._create_share()
print("Demo Completed")

def _connect_to_gateway(self):
gateway_address = GatewaySample._get_input(GatewaySample._gateway_address_request_string)
self._device = Gateway(gateway_address)

def _create_first_user(self):
print("Creating the Gateway's first user")
username = GatewaySample._get_input(GatewaySample._gateway_username_request_string)
password = GatewaySample._get_password(username)
self._device.users.add_first_user(username, password)
print("Logged in as {username}".format(username=username))

def _create_user(self):
print("Adding a user to the Gateway")
username = GatewaySample._get_input(GatewaySample._gateway_username_request_string)
password = GatewaySample._get_password(username)
self._device.users.add(username, password, fullName=username, email="{username}@example.com".format(username=username))
print("User {username} was added successfully".format(username=username))

def _set_host_name(self):
print("Setting the Gateway's Host Name")
new_host_name = GatewaySample._get_input(GatewaySample._gateway_new_hostname_request_string)
self._device.config.set_hostname(new_host_name)
print("Gateway Host name was set to {new_host_name}".format(new_host_name=new_host_name))

def _set_location(self):
print("Setting the Gateway's Location")
new_location = GatewaySample._get_input(GatewaySample._gateway_new_location_request_string)
self._device.config.set_location(new_location)
print("The Gateway's location was set to {new_location}".format(new_location=new_location))

def _configure_volume(self):
print("Configuring the Gateway's volume")
self._volume_name = GatewaySample._get_input(GatewaySample._gateway_volume_name_request_string)
self._device.volumes.delete_all()
self._device.drive.format_all()
self._device.volumes.add(self._volume_name)
self._device.afp.disable()
self._device.ftp.disable()
self._device.rsync.disable()
print("The volume {volume_name} was added successfully".format(volume_name=self._volume_name))

def _set_dns_server(self):
print("Setting the Gateway's DNS Server to 8.8.8.8")
ipaddr = self._device.get('/status/network/ports/0/ip')
self._device.network.set_static_ipaddr(ipaddr.address, ipaddr.netmask, ipaddr.gateway, '8.8.8.8')
print("DNS Server was set to 8.8.8.8")

def _connect_to_portal(self):
print("Connecting the Gateway to the Portal")
address = GatewaySample._get_input(GatewaySample._gateway_portal_address_request_string)
username = GatewaySample._get_input(GatewaySample._gateway_username_request_string)
password = GatewaySample._get_password(username)
self._device.services.connect(address, username, password)
print("Successfully connected to Portal at {address}".format(address=address))

def _start_cloud_caching(self):
print("Starting Cloud Sync")
self._device.cache.enable()
self._device.sync.unsuspend()
self._device.sync.refresh()
print("Cloud Sync was configured successfully")

def _disable_first_time_wizard(self):
print("Disabling the First Time Wizard")
self._device.put('/config/gui/openFirstTimeWizard', False)

def _create_share(self):
print("Creating a new share")
share_dir_base_path = 'public'
directory_name = self._get_input(GatewaySample._share_directory_name_request_string)
directory_path = '/'.join([share_dir_base_path, directory_name])
self._device.files.mkdir(directory_path, recurse=True)
self._device.shares.add(
directory_name,
'/'.join([self._volume_name, directory_path]),
acl=[('LG', 'Everyone', 'RW'), ('LG', 'Administrators', 'RO')],
access=gateway_enum.Acl.OnlyAuthenticatedUsers
)
print("New share was created successfully")


if __name__ == "__main__":
config.Logging.get().setLevel(logging.DEBUG)
try:
GatewaySample().run()
except CTERAException as error:
print(error)
80 changes: 80 additions & 0 deletions samples/portal_simple_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from cterasdk import config, CTERAException, GlobalAdmin, portal_enum, gateway_enum

from sample_base import CTERASDKSampleBase


class PortalSimpleSample(CTERASDKSampleBase):
_portal_address_request_string = "the Portal Address"
_portal_username_request_string = "the user name"
_portal_device_name_request_string = "the device name"
_share_directory_name_request_string = "a new for the new shared directory"

def __init__(self):
self._global_admin = None

def run(self):
config.http['ssl'] = 'Trust'
self._connect_to_portal()
self._handle_users()

print("Printing all devices")
self._print_devices()

print("Printing only vGateways")
self._print_devices(deviceTypes=['vGateway'], include=['deviceReportedStatus'])

self._remote_device_operations()
self._print_system_logs()

self._global_admin.logout()

def _connect_to_portal(self):
gateway_address = CTERASDKSampleBase._get_input(PortalSimpleSample._portal_address_request_string)
self._global_admin = GlobalAdmin(gateway_address)
username = CTERASDKSampleBase._get_input(PortalSimpleSample._portal_username_request_string)
password = CTERASDKSampleBase._get_password(username)
self._global_admin.login(username, password)

def _handle_users(self):
print("Creating a new user")
self._global_admin.users.add('alice', 'walice@acme.com', 'Alice', 'Wonderland', 'password1!', portal_enum.Role.ReadWriteAdmin)
print("User was created succussfully")

print("Deleting previously created user")
self._global_admin.users.delete('alice')
print("User was deleted successfully")

def _print_devices(self, deviceTypes=None, include=None):
for device in self._global_admin.devices.filers(include=include, deviceTypes=deviceTypes):
print(device)

def _remote_device_operations(self):
print("Creating a new share")
device_name = CTERASDKSampleBase._get_input(PortalSimpleSample._portal_device_name_request_string)
device = self._global_admin.devices.device(device_name)
share_dir_base_path = 'public'
directory_name = self._get_input(PortalSimpleSample._share_directory_name_request_string)
directory_path = '/'.join([share_dir_base_path, directory_name])
device.shares.add(
directory_name,
'/'.join(['main', directory_path]),
acl=[('LG', 'Everyone', 'RW'), ('LG', 'Administrators', 'RO')],
access=gateway_enum.Acl.OnlyAuthenticatedUsers
)
print("New Share was created successfully")
print("Deleting previosly created share")
device.shares.delete(directory_name)
print("Share was deleted successfully")

def _print_system_logs(self):
print("Printing Cloud Sync logs")
for log in self._global_admin.logs.logs(topic=portal_enum.LogTopic.CloudSync):
print(log.msg)


if __name__ == "__main__":
try:
PortalSimpleSample().run()
except CTERAException as error:

print(error)
44 changes: 44 additions & 0 deletions samples/remove_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import csv

from cterasdk import config, CTERAException, GlobalAdmin

from sample_base import CTERASDKSampleBase


class PortalRemoveDevicesSample(CTERASDKSampleBase):
_portal_address_request_string = "the Portal Address"
_portal_username_request_string = "the user name"

def __init__(self):
self._global_admin = None

def run(self):
config.http['ssl'] = 'Trust'
self._connect_to_portal()

self._global_admin.portals.browse('acme')

with open('devices.csv', newline='\n') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in spamreader:
device_name = row[0]
logging.getLogger().info('Deleting device. %s', {'name' : device_name})
try:
self._global_admin.execute('/devices/' + device_name, 'delete', 'deviceAndFolders')
logging.getLogger().info('Device deleted. %s', {'name' : device_name})
except CTERAException:
logging.getLogger().error('Failed deleting device. %s', {'name' : device_name})

self._global_admin.logout()

def _connect_to_portal(self):
gateway_address = PortalRemoveDevicesSample._get_input(PortalRemoveDevicesSample._portal_address_request_string)
self._global_admin = GlobalAdmin(gateway_address)
username = PortalRemoveDevicesSample._get_input(PortalRemoveDevicesSample._portal_username_request_string)
password = PortalRemoveDevicesSample._get_password(username)
self._global_admin.login(username, password)


if __name__ == "__main__":
PortalRemoveDevicesSample().run()
19 changes: 19 additions & 0 deletions samples/sample_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import getpass

class CTERASDKSampleBase:
_password_request_string = "the password for {username}"

@staticmethod
def _get_password(username):
return CTERASDKSampleBase._get_input(CTERASDKSampleBase._password_request_string.format(username=username), echo=False)

@staticmethod
def _get_input(prompt, echo=True):
prompt = "Please enter {prompt}: ".format(prompt=prompt)
response = None
while not response:
if echo:
response = input(prompt)
else:
response = getpass.getpass(prompt)
return response

0 comments on commit c0df056

Please sign in to comment.