跳转到主要内容

用于与ECS 2.x管理API交互的库

项目描述

ECS Minion是一个用于与ECS 2.x管理API交互的Python库

https://travis-ci.org/chadlung/ecsminion.svg?branch=master

注意: ECS是EMC的产品,受商标和版权保护。

此库遵循ECS API文档,文档位于此处。

使用此库非常简单。可以从PyPi安装ECSMinion

$ pip install ecsminion

此库应与Python 2.7.x和Python 3.4.x兼容。

创建ECSMinion类的实例允许以下参数

名称

必需

默认值

描述

username

None

用于获取ECS令牌的用户名

password

None

用于获取ECS令牌的密码

token

None

传递一个令牌给ECSMinion(忽略username/password)

ecs_endpoint

None

ECS API端点,例如:https://192.168.0.149:4443

token_endpoint

None

ECS API端点,例如:https://192.168.0.149:4443/login

verify_ssl

False

是否检查主机的SSL证书

token_filename

ecstoken.tkn

临时令牌的文件名

token_location

/tmp

存储临时令牌文件的位置

request_timeout

15.0

在给定秒数后停止等待响应,这是一个十进制值。例如:10.0是十秒

cache_token

True

是否缓存令牌,默认为true,您只有在想要直接为用户获取令牌时才应将其切换为false

以下是一个遍历大多数API调用的示例。请注意,有些调用比其他调用完成所需的时间更长。有时您可能需要将您的request_timeout设置为60.0

from ecsminion import ECSMinion, ECSMinionException

try:
    client = ECSMinion(username='ecsadmin@internal',
                       password='PASSWORD',
                       token_endpoint='https://192.168.0.149:4443/login',
                       ecs_endpoint='https://192.168.0.149:4443',
                       request_timeout=15.0)

    print(client.user_info.whoami())

    # Configuration
    print(client.certificate.get_certificate_chain())
    print(client.configuration_properties.get_properties(category='ALL'))
    print(client.configuration_properties.get_properties_metadata())
    print(client.licensing.add_license(license=data_dict))
    print(client.licensing.get_license())

    # User Management
    print(client.secret_key.create_new_secret_key('user1'))
    print(client.secret_key.get_user_secret_keys(uid='user1'))
    print(client.secret_key.get_user_secret_keys(
        uid='user1', namespace='namespace1'))
    print(client.user_object.get_object_users('namespace1'))
    print(client.user_object.get_object_user_info('user1'))
    print(client.user_object.get_object_user_info('user1', 'namespace1'))
    print(client.user_object.add_object_user(
        uid='user2', namespace='namespace1', tags=['mytag1', 'mytag1']))
    print(client.user_object.deactivate_object_user(
        uid='user2', namespace='namespace1'))
    print(client.user_object.get_object_user_info('user1'))
    print(client.user_object.lock_object_user('user1', True, 'namespace1'))
    print(client.user_object.get_object_user_lock('user1', 'namespace1'))
    print(client.user_object.lock_object_user('user1', False, 'namespace1'))
    print(client.user_object.get_object_user_lock('user1', 'namespace1'))
    print(client.authentication_provider.get_authentication_providers())
    print(client.authentication_provider.get_authentication_provider(
        'urn:AuthProvider:317843ad-71eb-4a86-b1bd-806f4275008a'))

    # Support
    print(client.call_home.get_connectemc_config())

    # Multi-tenancy
    print(client.namespace.get_namespaces())
    print(client.namespace.get_namespace('namespace1'))
    print(client.namespace.get_retention_classes('namespace1'))
    print(client.namespace.create_retention_class('the-name', 2, 'namespace2'))
    print(client.namespace.update_retention_class('the-name', 199, 'namespace2'))
    print(client.namespace.get_namespace_quota('namespace1'))
    print(client.namespace.update_namespace_quota(2, 2, 'namespace1'))

    # Monitoring
    print(client.capacity.get_cluster_capacity())
    print(client.events.get_audit_events(
        '2015-06-08T01:00', '2015-06-09T00:00', 'namespace1'))
    # With a marker and a limit param (marker has been shortened)
    print(client.events.get_audit_events(
        '2015-06-08T01:00', '2015-06-09T00:00', 'namespace1', limit=3,
        marker='CIbS7YbdKRI4dXJuOnN0bTRkYzYtOWUxNy03MGFkYzAzMWUxNDQ='))
    # Only a few of the dashboard APIs are shown, there are more
    print(client.dashboard.get_local_zone())
    print(client.dashboard.get_local_zone_replication_groups())
    print(client.dashboard.get_local_zone_rglinks_failed())
    print(client.dashboard.get_local_zone_storage_pools())
    print(client.dashboard.get_local_zone_nodes())
    print(client.dashboard.get_node_processes('172.29.3.148'))
    print(client.dashboard.get_local_zone_replication_group_bootstrap_links())

    # Provisioning
    print(client.node.get_nodes())
    print(client.bucket.create_bucket(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.deactivate_bucket(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.get_buckets(namespace='namespace1'))
    print(client.bucket.set_bucket_retention(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.get_bucket_retention(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.get_bucket_info(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.update_bucket_owner(bucket_name='bucket-test1', new_owner='user2', namespace='namespace1'))
    print(client.bucket.update_bucket_is_stale_allowed(bucket_name='bucket-test1', is_stale_allowed=False, namespace='namespace1'))
    print(client.bucket.get_bucket_lock(bucket_name='bucket-test1', namespace='namespace1'))
    # For the following is_locked must be passed as 'true' or 'false', not True/False
    print(client.bucket.set_lock_bucket(bucket_name='bucket-test1', is_locked='false', namespace='namespace1'))
    print(client.bucket.update_bucket_quota(bucket_name='bucket-test1', block_size=1, notification_size=2, namespace='namespace1'))
    print(client.bucket.get_bucket_quota(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.delete_bucket_quota(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.get_bucket_acl(bucket_name='bucket-test1', namespace='namespace1'))
    print(client.bucket.get_acl_permissions())
    print(client.bucket.get_acl_groups())
    print(client.base_url.get_all_configured_base_urls())
    print(client.base_url.get_base_url('urn:ObjectBaseUrl:6c74e6fb-a2a1-4386-bc25-b4399a6e74ce'))
    print(client.base_url.create_base_url('TestBaseURL', 'test.com', False))
    print(client.base_url.modify_base_url('urn:ObjectBaseUrl:19c391eb-37f4-4c65-a7a9-474668f71607',
                                          'SomeBaseURL', 'test.org', False))
    print(client.base_url.delete_base_url('urn:ObjectBaseUrl:19c391eb-37f4-4c65-a7a9-474668f71607'))
    print(client.data_store.get_data_stores())
    print(client.data_store.get_data_store('192.29.3.51'))
    print(client.data_store.get_data_stores_by_storage_pool_id('urn:storageos:VirtualArray:3c4e8cca-2e3d-4f8d-b183-1c69ce2d5b37'))
    print(client.storage_pool.get_virtual_arrays()  # Local VDC
    print(client.storage_pool.get_virtual_arrays(vdc_id='urn:storageos:VirtualDataCenterData:a9faea85-d377-4a42-b5f1-fa15829f0c33'))
    print(client.storage_pool.get_virtual_array('urn:storageos:VirtualArray:3c4e8cca-2e3d-4f8d-b183-1c69ce2d5b37'))
    print(client.storage_pool.add_virtual_array(name='Varray1', description='Test storage pool 1'))
    print(client.virtual_data_center.get_all_vdcs())
    print(client.virtual_data_center.get_vdc_by_id('urn:storageos:VirtualDataCenterData:a9faea85-d377-4a42-b5f1-fa15829f0c33'))
    print(client.virtual_data_center.get_local_vdc())
    print(client.virtual_data_center.get_local_vdc_secret_key())
    print(client.virtual_data_center.get_vdc_by_name('tiva01'))
    print(client.virtual_data_center.insert_vdc_attributes('vdc1', '10.247.179.200', '1234secret'))
    print(client.virtual_data_center.deactivate_vdc('urn:storageos:VirtualDataCenterData:a9faea85-d377-4a42-b5f1-fa15829f0c33'))
    print(client.management_object.create_local_user_info('newadminuser', 'password', True, True))
    print(client.management_object.modify_local_user_info('newadminuser', 'password2', False, True))
    print(client.management_object.delete_local_user_info('newadminuser'))
    print(client.management_object.get_local_management_users())
    print(client.management_object.get_local_user_info('admin'))

    # Geo Replication
    print(client.replication_group.get_replication_groups())
    print(client.replication_group.get_replication_group(
        'urn:storageos:ReplicationGroupInfo:c2b0d3c4-c778-4a24-8da5-6a89784c4eeb:global'))
    print(client.replication_group.update_replication_group(
        'urn:storageos:ReplicationGroupInfo:c2b0d3c4-c778-4a24-8da5-6a89784c4eeb:global',
        'a-name' 'the-description', True)
    print(client.temp_failed_zone.get_all_temp_failed_zones())
    print(client.temp_failed_zone.get_temp_failed_zone(
        'urn:storageos:ReplicationGroupInfo:c2b0d3c4-c778-4a24-8da5-6a89784c4eeb:global'))

    # Metering/Billing
    print(client.billing.get_bucket_billing_info('bucket-test', 'namespace1'))
    print(client.billing.get_namespace_billing_info('namespace1', include_bucket_detail=True))
    print(client.billing.get_namespace_billing_sample(
        'namespace1', start_time='2015-06-15T00:00',
        end_time='2015-06-15T1:00', include_bucket_detail=True))
    print(client.billing.get_bucket_billing_sample(
        'bucket-test', 'namespace1',
        start_time='2015-06-15T00:00', end_time='2015-06-15T1:00'))

except ECSMinionException as ecsminion_ex:
    print('Message: {0}'.format(ecsminion_ex.message))
    print('Status Code Returned: {0}\n'.format(ecsminion_ex.http_status_code))
    print('ECS API Message: {0}'.format(ecsminion_ex.ecs_message))
except Exception as ex:
    print(ex.message)

示例:上传ECS许可证

根据ECS API文档,对POST /license的调用应包含以下类似的负载

{
    "license_feature": [
        {
            "serial": "",
            "version": "",
            "issued_date": "",
            "expiration_date": "",
            "model": "",
            "product": "",
            "site_id": "",
            "issuer": "",
            "notice": "",
            "licensed_ind": "",
            "expired_ind": "",
            "license_id_indicator": "",
            "error_message": "",
            "storage_capacity_unit": "",
            "storage_capacity": "",
            "trial_license_ind": ""
        }
    ],
    "license_text": ""
}

因此,如果您消费包含此类许可证数据的JSON文件,您可以使用licensing.add_license()方法上传它

import json
import pprint

pp = pprint.PrettyPrinter()

try:
    with open("ECS2.1_License.json") as data:
        license = json.load(data)

    pp.pprint(client.licensing.add_license(license))

except ValueError as val_ex:  # includes simplejson.decoder.JSONDecodeError
    print("Couldn't parse JSON data: {0}".format(val_ex.message))
except ECSMinionException as ecsminion_ex:
    print('Message: {0}'.format(ecsminion_ex.message))
    print('Status Code Returned: {0}\n'.format(ecsminion_ex.http_status_code))
    print('ECS API Message: {0}'.format(ecsminion_ex.ecs_message))
except Exception as ex:
    print(ex.message)

示例:启用日志输出

ECSMinion利用Python的logging模块。按照以下方式从您的应用程序中启用它

import logging

# Default to INFO level logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

现在ECSMinion会告诉您它在做什么(以及requests库也会这样做)。如果您想了解有关HTTP请求和标头的更多信息,请使用以下方法

import logging
import httplib

# Default to DEBUG level logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)

# Show HTTP headers and payloads
httplib.HTTPConnection.debuglevel = 1

如果您不想看到任何requests日志,可以使用logging过滤器过滤它,或者仅更改该库的日志级别

import logging

# Default to INFO level logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

# Only show errors from requests lib
logging.getLogger('requests.packages.urllib3').setLevel(logging.ERROR)

示例:使用有效的令牌而不是提供用户名和密码

您将认证令牌直接传递给ECSMinion,这意味着您不需要提供用户名/密码。以下是一个示例(令牌已被缩短)

client = ECSMinion(token='ALAcbGZtbjh6eVB3eUF1TzFEZWNmc0M2VVl2QjBVPQM',
                   token_endpoint='https://192.168.1.146:4443/login',
                   ecs_endpoint='https://192.168.1.146:4443',
                   request_timeout=15.0)

示例:获取令牌

可以通过将cache_token参数设置为false,然后调用get_token来获取用户令牌

from ecsminion import ECSMinion, ECSMinionException


if __name__ == "__main__":
    try:
        client = ECSMinion(username='someone',
                           password='password',
                           token=None,
                           token_endpoint='https://192.168.1.146:4443/login',
                           ecs_endpoint='https://192.168.1.146:4443',
                           request_timeout=15.0,
                           cache_token=False)

        print(client.get_token())

    except ECSMinionException as ecsminion_ex:
        print('Message: {0}'.format(ecsminion_ex.message))
        print('Status Code Returned: {0}\n'.format(ecsminion_ex.http_status_code))
        print('ECS API Message: {0}'.format(ecsminion_ex.ecs_message))
    except Exception as ex:
        print(ex.message)

示例:删除缓存的令牌

from ecsminion import ECSMinion, ECSMinionException


if __name__ == "__main__":
    try:
        client = ECSMinion(username='someone',
                           password='password',
                           token=None,
                           token_endpoint='https://192.168.1.146:4443/login',
                           ecs_endpoint='https://192.168.1.146:4443',
                           request_timeout=15.0,
                           cache_token=False)

        print(client.remove_cached_token())

    except ECSMinionException as ecsminion_ex:
        print('Message: {0}'.format(ecsminion_ex.message))
        print('Status Code Returned: {0}\n'.format(ecsminion_ex.http_status_code))
        print('ECS API Message: {0}'.format(ecsminion_ex.ecs_message))
    except Exception as ex:
        print(ex.message)

许可证

此软件库根据Apache License 2.0向您发布。有关更多信息,请参阅LICENSE

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

ecsminion-0.7.6.tar.gz (29.4 kB 查看哈希值)

上传时间

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面