Package: atlasapi
Python Bindings for the Atlas Public APIThis project intends to create a fairly opinionated set of bindings for the Atlas Public API which makes interacting
with Atlas using Python easier. The API makes extensive use of enums and other helper type objects to take some
of the guess work of administering Atlas clusters with Python.In most cases objects will be returned based upon the structure of the json returned but the API Endpoints. These objects
are defined either in thespecs.pymodule or in a module named after the objects themselves (alerts.pyfor example).All calls to the Atlas API require API credentials, you can configure them in your Atlas project.Atlas APIConfigure Atlas API AccessCurrent state of the python-atlasapi supportDocumentationFound athttps://python-atlasapi.readthedocs.io/Autobuilt on each commit.InstallationThis package is available for Python 3.6+.You can install the latest released version from pypi.pip3installatlasapiUsageGet All Database Usersfromatlasapi.atlasimportAtlasa=Atlas("<user>","<password>","<groupid>")# Low level Apidetails=a.DatabaseUsers.get_all_database_users(pageNum=1,itemsPerPage=100)# Iterableforuserina.DatabaseUsers.get_all_database_users(iterable=True):print(user["username"])Create a Database Userfromatlasapi.atlasimportAtlasfromatlasapi.specsimportDatabaseUsersPermissionsSpecs,RoleSpecsa=Atlas("<user>","<password>","<groupid>")p=DatabaseUsersPermissionsSpecs("test","password for test user")p.add_roles("test-db",[RoleSpecs.dbAdmin,RoleSpecs.readWrite])p.add_role("other-test-db",RoleSpecs.readWrite,"a_collection")details=a.DatabaseUsers.create_a_database_user(p)Update a Database Userfromatlasapi.atlasimportAtlasfromatlasapi.specsimportDatabaseUsersUpdatePermissionsSpecs,RoleSpecsa=Atlas("<user>","<password>","<groupid>")# Update roles and passwordp=DatabaseUsersUpdatePermissionsSpecs("password for test user")p.add_role("test-db",RoleSpecs.read,"a_collection")details=a.DatabaseUsers.update_a_database_user("test",p)Delete a Database Userfromatlasapi.atlasimportAtlasa=Atlas("<user>","<password>","<groupid>")details=a.DatabaseUsers.delete_a_database_user("test")Get a Single Database Userfromatlasapi.atlasimportAtlasa=Atlas("<user>","<password>","<groupid>")details=a.DatabaseUsers.get_a_single_database_user("test")Clustersfromatlasapi.atlasimportAtlasfromatlasapi.clustersimportAdvancedOptionsa=Atlas("<user>","<password>","<groupid>")# Is existing cluster ?a.Clusters.is_existing_cluster("cluster-dev")# Get All Clustersforclusterina.Clusters.get_all_clusters(iterable=True):print(cluster["name"])# Get a Single Clusterdetails=a.Clusters.get_single_cluster("cluster-dev")# Delete a Cluster (dry run, raise ErrConfirmationRequested)details=a.Clusters.delete_cluster("cluster-dev")# Delete a Cluster (approved)details=a.Clusters.delete_cluster("cluster-dev",areYouSure=True)# Create a Simple Replica Set Clusterdetails=a.Clusters.create_basic_rs(name="cluster-dev")# Create a clusterprovider_settings:ProviderSettings=ProviderSettings()regions_config=RegionConfig()replication_specs=ReplicationSpecs(regions_config={provider_settings.region_name:regions_config.__dict__})cluster_config=ClusterConfig(name='test2',providerSettings=provider_settings,replication_specs=replication_specs)output=a.Clusters.create_cluster(cluster_config)# Modify a clusterexisting_config=a.Clusters.get_single_cluster_as_obj(cluster=TEST_CLUSTER_NAME)out.providerSettings.instance_size_name=InstanceSizeName.M10out.disk_size_gb=13new_config=a.Clusters.modify_cluster('pyAtlasAPIClustersTest',out)pprint(new_config)# Modify cluster instance sizea.Clusters.modify_cluster_instanct_size(cluster='pyAtlasAPIClustersTest',new_cluster_size=InstanceSizeName.M20)# Pause(unpause) a clustera.Clusters.pause_cluster(cluster='pyAtlasAPIClustersTest',toggle_if_paused=True)# Get Advanced Optionsa.Clusters.get_single_cluster_advanced_options(cluster='pyAtlasAPIClustersTest')# Set Advanced Optionsoptions=AdvancedOptions(failIndexKeyTooLong=True)self.a.Clusters.modify_cluster_advanced_options(cluster='pyAtlasAPIClustersTest',advanced_options=options)Alertsfromatlasapi.atlasimportAtlasfromatlasapi.specsimportAlertStatusSpeca=Atlas("<user>","<password>","<groupid>")# Get All Alerts in OPEN statusforalertina.Alerts.get_all_alerts(AlertStatusSpec.OPEN,iterable=True):print(alert["id"])# Get an Alertdetails=a.Alerts.get_an_alert("597f221fdf9db113ce1755cd")# Acknowledge an Alert (BROKEN)#  until (now + 6 hours)fromdatetimeimportdatetime,timezone,timedeltanow=datetime.now(timezone.utc)until=now+timedelta(hours=6)details=a.Alerts.acknowledge_an_alert("597f221fdf9db113ce1755cd",until,"Acknowledge reason")#  forever (BROKEN)details=a.Alerts.acknowledge_an_alert_forever("597f221fdf9db113ce1755cd","Acknowledge reason")# Unacknowledge an Alert (BROKENdetails=a.Alerts.unacknowledge_an_alert("597f221fdf9db113ce1755cd")Metrics (Measurements)Examples coming soon.Logsfromatlasapi.atlasimportAtlasfromatlasapi.specsimportAlertStatusSpecatlas=Atlas("<user>","<password>","<groupid>")atlas.Hosts.fill_host_list()test_host=atlas.Hosts.host_list[0]print(f'Will get a mongod log for{test_host.hostname}')out=atlas.Hosts.get_loglines_for_host(host_obj=test_host,log_name=AtlasLogNames.MONGODB)foreach_lineinout:print(each_line.__dict__)WhitelistsExamples coming soon.Maintenance WindowsExamples coming soon.Error TypesAbout ErrAtlasGenericAll ErrAtlas* Exception class inherit from ErrAtlasGeneric.try:...exceptErrAtlasGenericase:c,details=e.getAtlasResponse()‘c’HTTP return code (4xx or 5xx for an error, 2xx otherwise)‘details’Response payloadExceptionsErrRoleA role is not compatible with AtlasErrPaginationAn issue occurs during a “Get All” function with ‘iterable=True’ErrPaginationLimitsOut of limit on ‘pageNum’ or ‘itemsPerPage’ parametersErrAtlasBadRequestSomething was wrong with the client request.ErrAtlasUnauthorizedAuthentication is requiredErrAtlasForbiddenAccess to the specified resource is not permitted.ErrAtlasNotFoundThe requested resource does not exist.ErrAtlasMethodNotAllowedThe HTTP method is not supported for the specified resource.ErrAtlasConflictThis is typically the response to a request to create or modify a property of an entity that is unique when an existing entity already exists with the same value for that property.ErrAtlasServerErrorsSomething unexpected went wrong.ErrConfirmationRequestedConfirmation requested to execute the call.Bugs or IssuesPlease report bugs, issues or feature requests toGithub
IssuesTestingCircle Cidevelopmaster