Ejemplo n.º 1
0
def create_s3key(module, client):
    user_id = module.params.get('user_id')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    user_management_server = ionoscloud.UserManagementApi(client)

    try:
        response = user_management_server.um_users_s3keys_post_with_http_info(
            user_id=user_id)
        (s3key_response, _, headers) = response

        if wait:
            request_id = _get_request_id(headers['Location'])
            client.wait_for_completion(request_id=request_id,
                                       timeout=wait_timeout)

        return {
            'changed': True,
            'failed': False,
            'action': 'create',
            's3key': s3key_response.to_dict()
        }

    except Exception as e:
        module.fail_json(msg="failed to create the s3key: %s" % to_native(e))
        return {
            'changed': False,
            'failed': True,
            'action': 'create',
        }
Ejemplo n.º 2
0
def delete_s3key(module, client):
    user_id = module.params.get('user_id')
    key_id = module.params.get('key_id')

    user_management_server = ionoscloud.UserManagementApi(client)

    s3key_list = user_management_server.um_users_s3keys_get(user_id=user_id,
                                                            depth=5)
    s3key = _get_resource(s3key_list, key_id)

    if not s3key:
        module.exit_json(changed=False)

    try:
        user_management_server.um_users_s3keys_delete(user_id, key_id)
        return {'action': 'delete', 'changed': True, 'id': key_id}

    except Exception as e:
        module.fail_json(msg="failed to delete the s3key: %s" % to_native(e))
        return {'action': 'delete', 'changed': False, 'id': key_id}
Ejemplo n.º 3
0
def delete_shares(module, client):
    """
    Remove shares

    module : AnsibleModule object
    client: authenticated ionoscloud object.

    Returns:
        True if the share was removed, false otherwise
    """
    group = module.params.get('group')
    user_management_server = ionoscloud.UserManagementApi(api_client=client)

    # Locate UUID for the group
    group_list = user_management_server.um_groups_get(depth=2)
    group_id = _get_resource_id(group_list, group, module, "Group")

    if module.check_mode:
        module.exit_json(changed=True)

    try:

        for uuid in module.params.get('resource_ids'):
            user_management_server.um_groups_shares_delete(group_id=group_id, resource_id=uuid)

        return {
            'action': 'delete',
            'changed': True,
            'id': group_id
        }
    except Exception as e:
        module.fail_json(msg="failed to remove the shares: %s" % to_native(e))
        return {
            'action': 'delete',
            'changed': False,
            'id': group_id
        }
Ejemplo n.º 4
0
def update_s3key(module, client):
    user_id = module.params.get('user_id')
    key_id = module.params.get('key_id')
    active = module.params.get('active')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    user_management_server = ionoscloud.UserManagementApi(client)

    properties = S3KeyProperties(active=active)

    if module.check_mode:
        module.exit_json(changed=True)
    try:
        response = user_management_server.um_users_s3keys_put_with_http_info(
            user_id, key_id, S3Key(properties=properties))
        (s3key_response, _, headers) = response

        if wait:
            request_id = _get_request_id(headers['Location'])
            client.wait_for_completion(request_id=request_id,
                                       timeout=wait_timeout)

        return {
            'changed': True,
            'failed': False,
            'action': 'update',
            's3key': s3key_response.to_dict()
        }

    except Exception as e:
        module.fail_json(msg="failed to update the s3key: %s" % to_native(e))
        return {
            'changed': False,
            'failed': True,
            'action': 'update',
        }
Ejemplo n.º 5
0
def update_shares(module, client):
    """
    Update shares.

    module : AnsibleModule object
    client: authenticated ionoscloud object.

    Returns:
        The share instances
    """
    group = module.params.get('group')
    user_management_server = ionoscloud.UserManagementApi(api_client=client)

    # Locate UUID for the group
    group_list = user_management_server.um_groups_get(depth=2)
    group_id = _get_resource_id(group_list, group, module, "Group")

    edit_privilege = module.params.get('edit_privilege')
    share_privilege = module.params.get('share_privilege')
    resource_ids = module.params.get('resource_ids')

    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    if module.check_mode:
        module.exit_json(changed=True)

    try:
        share_list = user_management_server.um_groups_shares_get(group_id=group_id, depth=2)
        existing = dict()
        for share in share_list.items:
            existing[share.id] = share

        responses = []

        for uuid in resource_ids:
            if uuid in existing.keys():
                share = existing[uuid]
                if edit_privilege is None:
                    edit_privilege = share.properties.edit_privilege
                if share_privilege is None:
                    share_privilege = share.properties.share_privilege

                share_properties = GroupShareProperties(edit_privilege=edit_privilege or False,
                                                        share_privilege=share_privilege or False)
                share = GroupShare(properties=share_properties)

                response = user_management_server.um_groups_shares_put_with_http_info(group_id=group_id,
                                                                                      resource_id=uuid, resource=share)
                (share_response, _, headers) = response
                if wait:
                    request_id = _get_request_id(headers['Location'])
                    client.wait_for_completion(request_id=request_id, timeout=wait_timeout)

            return {
                'changed': True,
                'failed': False,
                'action': 'update',
                'share': [s.to_dict() for s in responses]
            }

    except Exception as e:
        module.fail_json(msg="failed to update the shares: %s" % to_native(e))
Ejemplo n.º 6
0
def create_shares(module, client):
    """
    Create shares.

    module : AnsibleModule object
    client: authenticated ionoscloud object.

    Returns:
        The share instance
    """
    user_management_server = ionoscloud.UserManagementApi(api_client=client)
    group = module.params.get('group')

    # Locate UUID for the group
    group_list = user_management_server.um_groups_get(depth=2)
    group_id = _get_resource_id(group_list, group, module, "Group")

    edit_privilege = module.params.get('edit_privilege')
    share_privilege = module.params.get('share_privilege')
    resource_ids = module.params.get('resource_ids')

    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    share_list = user_management_server.um_groups_shares_get(group_id=group_id, depth=2).items
    for share in share_list:
        if share.id in resource_ids:
            resource_ids.remove(share.id)

    should_change = True

    if not resource_ids:
        should_change = False

    if module.check_mode:
        module.exit_json(changed=should_change)

    if not should_change:
        return {
            'changed': False,
            'failed': False,
            'action': 'create',
            'shares': [s.to_dict() for s in share_list],
        }

    try:
        for uuid in resource_ids:
            share_properties = GroupShareProperties(edit_privilege=edit_privilege or False,
                                                    share_privilege=share_privilege or False)
            share = GroupShare(properties=share_properties)
            response = user_management_server.um_groups_shares_post_with_http_info(group_id=group_id, resource_id=uuid,
                                                                                   resource=share)
            (share_response, _, headers) = response

            if wait:
                request_id = _get_request_id(headers['Location'])
                client.wait_for_completion(request_id=request_id, timeout=wait_timeout)

        share_list = user_management_server.um_groups_shares_get(group_id=group_id, depth=2).items

        return {
            'changed': True,
            'failed': False,
            'action': 'create',
            'shares': [s.to_dict() for s in share_list]
        }

    except Exception as e:
        module.fail_json(msg="failed to create the shares: %s" % to_native(e))
Ejemplo n.º 7
0
def main():
    module = AnsibleModule(argument_spec=dict(
        firstname=dict(type='str'),
        lastname=dict(type='str'),
        email=dict(type='str', required=True),
        user_password=dict(type='str', default=None, no_log=True),
        administrator=dict(type='bool', default=None),
        force_sec_auth=dict(type='bool', default=None),
        groups=dict(type='list', default=None),
        api_url=dict(type='str',
                     default=None,
                     fallback=(env_fallback, ['IONOS_API_URL'])),
        sec_auth_active=dict(type='bool', default=False),
        s3_canonical_user_id=dict(type='str'),
        username=dict(type='str',
                      required=True,
                      aliases=['subscription_user'],
                      fallback=(env_fallback, ['IONOS_USERNAME'])),
        password=dict(type='str',
                      required=True,
                      aliases=['subscription_password'],
                      fallback=(env_fallback, ['IONOS_PASSWORD']),
                      no_log=True),
        wait=dict(type='bool', default=True),
        wait_timeout=dict(type='int', default=600),
        state=dict(type='str', default='present'),
    ),
                           supports_check_mode=True)

    if not HAS_SDK:
        module.fail_json(
            msg=
            'ionoscloud is required for this module, run `pip install ionoscloud`'
        )

    username = module.params.get('username')
    password = module.params.get('password')
    api_url = module.params.get('api_url')

    user_agent = 'ionoscloud-python/%s Ansible/%s' % (sdk_version, __version__)

    state = module.params.get('state')

    conf = {
        'username': username,
        'password': password,
    }

    if api_url is not None:
        conf['host'] = api_url
        conf['server_index'] = None

    configuration = ionoscloud.Configuration(**conf)

    with ApiClient(configuration) as api_client:
        api_client.user_agent = user_agent
        api_instance = ionoscloud.UserManagementApi(api_client)

        if state == 'absent':
            try:
                (result) = delete_user(module, api_instance)
                module.exit_json(**result)
            except Exception as e:
                module.fail_json(msg='failed to set user state: %s' %
                                 to_native(e))

        elif state == 'present':
            try:
                (user_dict) = create_user(module, api_instance, api_client)
                module.exit_json(**user_dict)
            except Exception as e:
                module.fail_json(msg='failed to set user state: %s' %
                                 to_native(e))

        elif state == 'update':
            try:
                (user_dict) = update_user(module, api_instance, api_client)
                module.exit_json(**user_dict)
            except Exception as e:
                module.fail_json(msg='failed to update user: %s' %
                                 to_native(e))
Ejemplo n.º 8
0
def main():
    module = AnsibleModule(argument_spec=dict(
        name=dict(type='str', required=True),
        create_datacenter=dict(type='bool', default=None),
        create_snapshot=dict(type='bool', default=None),
        reserve_ip=dict(type='bool', default=None),
        access_activity_log=dict(type='bool', default=None),
        create_pcc=dict(type='bool', default=None),
        s3_privilege=dict(type='bool', default=None),
        create_backup_unit=dict(type='bool', default=None),
        create_internet_access=dict(type='bool', default=None),
        create_k8s_cluster=dict(type='bool', default=None),
        users=dict(type='list', default=None),
        api_url=dict(type='str',
                     default=None,
                     fallback=(env_fallback, ['IONOS_API_URL'])),
        username=dict(type='str',
                      required=True,
                      aliases=['subscription_user'],
                      fallback=(env_fallback, ['IONOS_USERNAME'])),
        password=dict(type='str',
                      required=True,
                      aliases=['subscription_password'],
                      fallback=(env_fallback, ['IONOS_PASSWORD']),
                      no_log=True),
        wait=dict(type='bool', default=True),
        wait_timeout=dict(type='int', default=600),
        state=dict(type='str', default='present'),
    ),
                           supports_check_mode=True)

    if not HAS_SDK:
        module.fail_json(
            msg=
            'ionoscloud is required for this module, run `pip install ionoscloud`'
        )

    username = module.params.get('username')
    password = module.params.get('password')
    api_url = module.params.get('api_url')

    user_agent = 'ionoscloud-python/%s Ansible/%s' % (sdk_version, __version__)

    state = module.params.get('state')

    conf = {
        'username': username,
        'password': password,
    }

    if api_url is not None:
        conf['host'] = api_url
        conf['server_index'] = None

    configuration = ionoscloud.Configuration(**conf)

    with ApiClient(configuration) as api_client:
        api_client.user_agent = user_agent
        api_instance = ionoscloud.UserManagementApi(api_client)

        if state == 'absent':
            try:
                (result) = delete_group(module, api_instance)
                module.exit_json(**result)
            except Exception as e:
                module.fail_json(msg='failed to set group state: %s' %
                                 to_native(e))

        elif state == 'present':
            try:
                (group_dict) = create_group(module, api_client)
                module.exit_json(**group_dict)
            except Exception as e:
                module.fail_json(msg='failed to set group state: %s' %
                                 to_native(e))

        elif state == 'update':
            try:
                (group_dict) = update_group(module, api_client)
                module.exit_json(**group_dict)
            except Exception as e:
                module.fail_json(msg='failed to update group: %s' %
                                 to_native(e))
Ejemplo n.º 9
0
def update_group(module, client):
    """
    Updates a group.

    module : AnsibleModule object
    client: authenticated ionoscloud object.

    Returns:
        The group instance
    """
    name = module.params.get('name')
    create_datacenter = module.params.get('create_datacenter')
    create_snapshot = module.params.get('create_snapshot')
    reserve_ip = module.params.get('reserve_ip')
    access_activity_log = module.params.get('access_activity_log')
    create_pcc = module.params.get('create_pcc')
    s3_privilege = module.params.get('s3_privilege')
    create_backup_unit = module.params.get('create_backup_unit')
    create_internet_access = module.params.get('create_internet_access')
    create_k8s_cluster = module.params.get('create_k8s_cluster')
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    user_management_server = ionoscloud.UserManagementApi(client)

    try:
        group = None
        group_id = None
        groups = user_management_server.um_groups_get(depth=2)
        for g in groups.items:
            if name == g.properties.name:
                group = g
                group_id = g.id
                break

        if group:
            if module.check_mode:
                module.exit_json(changed=True)

            if create_datacenter is None:
                create_datacenter = group.properties.create_data_center
            if create_snapshot is None:
                create_snapshot = group.properties.create_snapshot
            if reserve_ip is None:
                reserve_ip = group.properties.reserve_ip
            if access_activity_log is None:
                access_activity_log = group.properties.access_activity_log
            if create_pcc is None:
                create_pcc = group.properties.create_pcc
            if s3_privilege is None:
                s3_privilege = group.properties.s3_privilege
            if create_backup_unit is None:
                create_backup_unit = group.properties.create_backup_unit
            if create_internet_access is None:
                create_internet_access = group.properties.create_internet_access
            if create_k8s_cluster is None:
                create_k8s_cluster = group.properties.create_k8s_cluster

            group_properties = GroupProperties(
                name=name,
                create_data_center=create_datacenter,
                create_snapshot=create_snapshot,
                reserve_ip=reserve_ip,
                access_activity_log=access_activity_log,
                create_pcc=create_pcc,
                s3_privilege=s3_privilege,
                create_backup_unit=create_backup_unit,
                create_internet_access=create_internet_access,
                create_k8s_cluster=create_k8s_cluster)

            group = Group(properties=group_properties)

            response = user_management_server.um_groups_put_with_http_info(
                group_id=group_id, group=group)
            (group_response, _, headers) = response

            if wait:
                request_id = _get_request_id(headers['Location'])
                client.wait_for_completion(request_id=request_id,
                                           timeout=wait_timeout)

            if module.params.get('users') is not None:

                group = user_management_server.um_groups_find_by_id(
                    group_id=group_response.id, depth=2)
                old_gu = []
                for u in group.entities.users.items:
                    old_gu.append(u.id)

                all_users = user_management_server.um_users_get(depth=2)
                new_gu = []

                for u in module.params.get('users'):
                    user_id = _get_user_id(all_users, u)
                    new_gu.append(user_id)

                for user_id in old_gu:
                    if user_id not in new_gu:
                        user_management_server.um_groups_users_delete(
                            group_id=group.id, user_id=user_id)

                for user_id in new_gu:
                    if user_id not in old_gu:
                        response = user_management_server.um_groups_users_post_with_http_info(
                            group_id=group.id, user=User(id=user_id))
                        (user_response, _, headers) = response
                        request_id = _get_request_id(headers['Location'])
                        client.wait_for_completion(request_id=request_id,
                                                   timeout=wait_timeout)
            return {
                'changed': True,
                'failed': False,
                'action': 'update',
                'group': group_response.to_dict()
            }
        else:
            module.fail_json(msg='Group \'%s\' not found.' % str(name))

    except Exception as e:
        module.fail_json(msg="failed to update the group: %s" % to_native(e))
Ejemplo n.º 10
0
def create_group(module, client):
    """
    Creates a group.

    module : AnsibleModule object
    client: authenticated ionoscloud object.

    Returns:
        The group instance
    """
    name = module.params.get('name')
    create_datacenter = module.params.get('create_datacenter')
    create_snapshot = module.params.get('create_snapshot')
    reserve_ip = module.params.get('reserve_ip')
    access_activity_log = module.params.get('access_activity_log')
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')
    create_pcc = module.params.get('create_pcc')
    s3_privilege = module.params.get('s3_privilege')
    create_backup_unit = module.params.get('create_backup_unit')
    create_internet_access = module.params.get('create_internet_access')
    create_k8s_cluster = module.params.get('create_k8s_cluster')

    user_management_server = ionoscloud.UserManagementApi(client)

    group = None
    groups = user_management_server.um_groups_get(depth=2)
    for g in groups.items:
        if name == g.properties.name:
            group = g
            break

    should_change = group is None

    if module.check_mode:
        module.exit_json(changed=should_change)

    if not should_change:
        return {
            'changed': False,
            'failed': False,
            'action': 'create',
            'group': group.to_dict()
        }

    try:
        group_properties = GroupProperties(
            name=name,
            create_data_center=create_datacenter or False,
            create_snapshot=create_snapshot or False,
            reserve_ip=reserve_ip or False,
            access_activity_log=access_activity_log or False,
            create_pcc=create_pcc or False,
            s3_privilege=s3_privilege or False,
            create_backup_unit=create_backup_unit or False,
            create_internet_access=create_internet_access or False,
            create_k8s_cluster=create_k8s_cluster or False)

        group = Group(properties=group_properties)
        response = user_management_server.um_groups_post_with_http_info(group)
        (group_response, _, headers) = response

        if wait:
            request_id = _get_request_id(headers['Location'])
            client.wait_for_completion(request_id=request_id,
                                       timeout=wait_timeout)

        return {
            'changed': True,
            'failed': False,
            'action': 'create',
            'group': group_response.to_dict()
        }

    except Exception as e:
        module.fail_json(msg="failed to create the group: %s" % to_native(e))