def create_vm(entity, context):
    """
    Create a VM for an given OCCI entity.

    entity -- the OCCI resource entity.
    context -- the os context.
    """
    # TODO: needs major overhaul!

    if 'occi.compute.hostname' in entity.attributes:
        name = entity.attributes['occi.compute.hostname']
    else:
        name = None
    key_name = key_data = None
    password = utils.generate_password()
    access_ip_v4 = None
    access_ip_v6 = None
    user_data = None
    metadata = {}
    injected_files = []
    min_count = max_count = 1
    requested_networks = None
    sg_names = []
    availability_zone = None
    config_drive = None
    block_device_mapping = None
    kernel_id = ramdisk_id = None
    auto_disk_config = None
    scheduler_hints = None

    resource_template = None
    os_template = None
    for mixin in entity.mixins:
        if isinstance(mixin, os_mixins.ResourceTemplate):
            resource_template = mixin
        elif isinstance(mixin, os_mixins.OsTemplate):
            os_template = mixin
        elif mixin == os_addon.OS_KEY_PAIR_EXT:
            attr = 'org.openstack.credentials.publickey.name'
            key_name = entity.attributes[attr]
            attr = 'org.openstack.credentials.publickey.data'
            key_data = entity.attributes[attr]
        elif mixin == os_addon.OS_USER_DATA_EXT:
            attr = 'org.openstack.compute.user_data'
            user_data = entity.attributes[attr]
        # Look for security group. If the group is non-existant, the
        # call to create will fail.
        if os_addon.SEC_GROUP in mixin.related:
            secgroup = COMPUTE_API.security_group_api.get(context,
                                                          name=mixin.term)
            sg_names.append(secgroup["name"])

    if not os_template:
        raise AttributeError('Please provide a valid OS Template.')

    if resource_template:
        inst_type = flavors.get_flavor_by_flavor_id(resource_template.res_id)
    else:
        inst_type = None
    # make the call
    try:
        (instances, _reservation_id) = COMPUTE_API.create(
            context=context,
            instance_type=inst_type,
            image_href=os_template.os_id,
            kernel_id=kernel_id,
            ramdisk_id=ramdisk_id,
            min_count=min_count,
            max_count=max_count,
            display_name=name,
            display_description=name,
            key_name=key_name,
            key_data=key_data,
            security_group=sg_names,
            availability_zone=availability_zone,
            user_data=user_data,
            metadata=metadata,
            injected_files=injected_files,
            admin_password=password,
            block_device_mapping=block_device_mapping,
            access_ip_v4=access_ip_v4,
            access_ip_v6=access_ip_v6,
            requested_networks=requested_networks,
            config_drive=config_drive,
            auto_disk_config=auto_disk_config,
            scheduler_hints=scheduler_hints)
    except Exception as e:
        raise AttributeError(e.message)

    # return first instance
    return instances[0]
dataset/ETHPy150Open tmetsch/occi-os/occi_os_api/nova_glue/vm.py/create_vm/original