Пример #1
0
def main():
    """
    Main function

    :returns: Image Information
    """
    module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'),
                                              region=dict(default='na',
                                                          type='str'),
                                              datacenter=dict(required=True,
                                                              type='str'),
                                              name=dict(required=True,
                                                        type='str'),
                                              ovf_name=dict(required=True,
                                                            type='str'),
                                              wait=dict(required=False,
                                                        default=True,
                                                        type='bool'),
                                              wait_time=dict(required=False,
                                                             default=3600,
                                                             type='int'),
                                              wait_poll_interval=dict(
                                                  required=False,
                                                  default=15,
                                                  type='int')),
                           supports_check_mode=True)

    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    image_name = module.params.get('name')
    datacenter = module.params.get('datacenter')
    image = None

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    # Create the API client
    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Check if the image already exists
    try:
        images = client.list_customer_image(
            datacenter_id=datacenter,
            image_name=image_name).get('customerImage')
        if images:
            image = [x for x in images if x.get('name') == image_name][0]
            if not image:
                module.fail_json(
                    msg='Could not find the image {0}'.format(image_name))
    except (KeyError, AttributeError, IndexError, NTTMCPAPIException) as e:
        module.fail_json(
            msg='The was an error finding the image: {0}'.format(e))

    if module.check_mode:
        module.exit_json(
            msg='The image with ID {0} can be exported to the OVF named {1}'.
            format(image.get('id'), module.params.get('ovf_name')))

    # Attempt to export
    try:
        result = client.export_image(image_id=image.get('id'),
                                     ovf_name=module.params.get('ovf_name'))
        if module.params.get('wait'):
            wait_for_image_export(module, client, datacenter, image_name)
        module.exit_json(
            changed=True,
            msg='The image was successfully exported with the export ID {0}'.
            format(result))
    except NTTMCPAPIException as e:
        module.fail_json(
            msg='Error exporting the image: {0}'.format(e).replace('"', '\''))
Пример #2
0
def main():
    """
    Main function

    :returns: Image Information
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        datacenter=dict(required=True, type='str'),
        name=dict(required=True, type='str'),
        description=dict(required=False, type='str'),
        ovf_package=dict(required=False, type='str'),
        guest_customization=dict(required=False, default=True, type='bool'),
        state=dict(default='present', choices=['present', 'absent']),
        wait=dict(required=False, default=True, type='bool'),
        wait_time=dict(required=False, default=3600, type='int'),
        wait_poll_interval=dict(required=False, default=15, type='int')),
                           supports_check_mode=True)

    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    image_name = module.params['name']
    datacenter = module.params['datacenter']
    image_exists = None

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    # Create the API client
    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Check if the image already exists
    try:
        images = client.list_customer_image(
            datacenter_id=datacenter,
            image_name=image_name).get('customerImage')
        if images:
            image_exists = [
                image for image in images if image.get('name') == image_name
            ][0]
    except (KeyError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(
            msg='There was an issue connecting to the API: {0}'.format(e))
    except IndexError:
        pass
    if module.params['state'] == 'present':
        if image_exists:
            # Implement check_mode
            if module.check_mode:
                module.exit_json(
                    msg=
                    'An image with this name already exists. Existing image is included',
                    data=image_exists)
            module.fail_json(msg='Image name {0} already exists'.format(
                module.params['name']))
        # Implement check_mode
        if module.check_mode:
            module.exit_json(
                msg='The image will be imported into Cloud Control')
        import_image(module=module, client=client)
    elif module.params['state'] == 'absent':
        if not image_exists:
            module.exit_json(msg='Image name {0} does not exist'.format(
                module.params.get('name')))
        # Implement check_mode
        if module.check_mode:
            module.exit_json(
                msg='This image will be removed from Cloud Control',
                data=image_exists)
        delete_image(module=module, client=client, image=image_exists)