import { getAll } from '@waldur/core/api';
import { translate } from '@waldur/i18n';
import { validateRuntimeState } from '@waldur/resource/actions/base';
import { ResourceAction, ActionContext } from '@waldur/resource/actions/types';
import { Volume, VirtualMachine } from '@waldur/resource/types';

export default function createAction(ctx: ActionContext<Volume>): ResourceAction {
  return {
    name: 'attach',
    type: 'form',
    method: 'POST',
    title: translate('Attach'),
    validators: [
      validateRuntimeState('available'),
    ],
    init: async (resource, _, action) => {
      const params = {
        service_uuid: ctx.resource.service_uuid,
        availability_zone_name: resource.availability_zone_name,
      };
      const instances = await getAll<VirtualMachine>('/openstacktenant-instances/', {params});
      action.fields.instance.choices = instances.map(choice => ({
        value: choice.url,
        display_name: choice.name,
      }));
    },
    fields: [
      {
        name: 'instance',
        label: translate('Instance'),
        type: 'select',
        required: true,
      },
      {
        name: 'device',
        label: translate('Device'),
        type: 'string',
        help_text: translate('Name of volume as instance device e.g. /dev/vdb.'),
        required: false,
      },
    ],
  };
}

import { getAll } from '@waldur/core/api';
import { translate } from '@waldur/i18n';
import { validateRuntimeState } from '@waldur/resource/actions/base';
import { ResourceAction, ActionContext } from '@waldur/resource/actions/types';
import { Volume, VirtualMachine } from '@waldur/resource/types';

export default function createAction(ctx: ActionContext<Volume>): ResourceAction {
  return {
    name: 'attach',
    type: 'form',
    method: 'POST',
    title: translate('Attach'),
    validators: [
      validateRuntimeState('available'),
    ],
    init: async (resource, _, action) => {
      const params = {
        attach_volume_uuid: resource.uuid,
        field: ['url', 'name'],
      };
      const instances = await getAll<VirtualMachine>('/openstacktenant-instances/', {params});
      action.fields.instance.choices = instances.map(choice => ({
        value: choice.url,
        display_name: choice.name,
      }));
    },
    fields: [
      {
        name: 'instance',
        label: translate('Instance'),
        type: 'select',
        required: true,
      },
      {
        name: 'device',
        label: translate('Device'),
        type: 'string',
        help_text: translate('Name of volume as instance device e.g. /dev/vdb.'),
        required: false,
      },
    ],
  };
}

Use filter for instances that could be attached to the volume
Use filter for instances that could be attached to the volume [WAL-2414]
