def create(self, req, body):
        """Create a new VSA."""
        context = req.environ['engine.context']

        if not body or 'vsa' not in body:
            LOG.debug(_("No body provided"), context=context)
            raise exc.HTTPUnprocessableEntity()

        vsa = body['vsa']

        display_name = vsa.get('displayName')
        vc_type = vsa.get('vcType', FLAGS.default_vsa_instance_type)
        try:
            instance_type = instance_types.get_instance_type_by_name(vc_type)
        except exception.NotFound:
            raise exc.HTTPNotFound()

        LOG.audit(_("Create VSA %(display_name)s of type %(vc_type)s"),
                  locals(),
                  context=context)

        args = dict(display_name=display_name,
                display_description=vsa.get('displayDescription'),
                instance_type=instance_type,
                storage=vsa.get('storage'),
                shared=vsa.get('shared'),
                availability_zone=vsa.get('placement', {}).\
                                          get('AvailabilityZone'))

        vsa = self.vsa_api.create(context, **args)

        instances = self._get_instances_by_vsa_id(context, vsa.get('id'))
        return {'vsa': _vsa_view(context, vsa, True, instances)}
    def show(self, req, id):
        """Return data about the given VSA."""
        context = req.environ['engine.context']

        try:
            vsa = self.vsa_api.get(context, vsa_id=id)
        except exception.NotFound:
            raise exc.HTTPNotFound()

        instances = self._get_instances_by_vsa_id(context, vsa.get('id'))
        return {'vsa': _vsa_view(context, vsa, True, instances)}
    def _items(self, req, details):
        """Return summary or detailed list of VSAs."""
        context = req.environ['engine.context']
        vsas = self.vsa_api.get_all(context)
        limited_list = common.limited(vsas, req)

        vsa_list = []
        for vsa in limited_list:
            instances = self._get_instances_by_vsa_id(context, vsa.get('id'))
            vsa_list.append(_vsa_view(context, vsa, details, instances))
        return {'vsaSet': vsa_list}
def _vsa_view(context, vsa, details=False, instances=None):
    """Map keys for vsa summary/detailed view."""
    d = {}

    d['id'] = vsa.get('id')
    d['name'] = vsa.get('name')
    d['displayName'] = vsa.get('display_name')
    d['displayDescription'] = vsa.get('display_description')

    d['createTime'] = vsa.get('created_at')
    d['status'] = vsa.get('status')

    if 'vsa_instance_type' in vsa:
        d['vcType'] = vsa['vsa_instance_type'].get('name', None)
    else:
        d['vcType'] = vsa['instance_type_id']

    d['vcCount'] = vsa.get('vc_count')
    d['driveCount'] = vsa.get('vol_count')

    d['ipAddress'] = None
    for instance in instances:
        fixed_addr = None
        floating_addr = None
        if instance['fixed_ips']:
            fixed = instance['fixed_ips'][0]
            fixed_addr = fixed['address']
            if fixed['floating_ips']:
                floating_addr = fixed['floating_ips'][0]['address']

        if floating_addr:
            d['ipAddress'] = floating_addr
            break
        else:
            d['ipAddress'] = d['ipAddress'] or fixed_addr

    return d