From 84b64b7d801913ddf047e9fe637e3b92c67ad1f7 Mon Sep 17 00:00:00 2001 From: stSolo Date: Fri, 24 Mar 2023 17:09:30 +0300 Subject: [PATCH] v1.3.0 --- CHANGELOG.md | 39 +++- README.md | 184 +++++++++++++++--- README_EN.md | 6 +- config/config.go | 70 ++++++- config/legacy-config.go | 68 ++++++- go.mod | 1 + go.sum | 1 + internal/client/transport.go | 2 + internal/validators/custom.go | 119 +++++++++-- internal/validators/helper.go | 5 + internal/validators/messages.go | 99 ++++++++-- internal/validators/validator.go | 71 ++++++- internal/validators/values.go | 39 +++- pkg/cloudapi/account/add_user.go | 2 +- pkg/cloudapi/account/update_user.go | 2 +- pkg/cloudapi/bservice/group_add.go | 2 +- pkg/cloudapi/compute/filter.go | 92 ++++++++- pkg/cloudapi/compute/pfw_add.go | 2 +- pkg/cloudapi/compute/user_grant.go | 2 +- pkg/cloudapi/compute/user_update.go | 2 +- pkg/cloudapi/disks/create.go | 33 +--- pkg/cloudapi/disks/delete.go | 19 +- pkg/cloudapi/disks/delete_disks.go | 24 +-- pkg/cloudapi/disks/filter.go | 72 +++++++ pkg/cloudapi/disks/get.go | 19 +- pkg/cloudapi/disks/limitio.go | 19 +- pkg/cloudapi/disks/list_types.go | 2 +- pkg/cloudapi/disks/rename.go | 24 +-- pkg/cloudapi/disks/resize.go | 30 ++- pkg/cloudapi/disks/restore.go | 24 +-- pkg/cloudapi/disks/share.go | 19 +- pkg/cloudapi/disks/snapshot_delete.go | 27 +-- pkg/cloudapi/disks/snapshot_rollback.go | 30 ++- pkg/cloudapi/disks/unshare.go | 19 +- pkg/cloudapi/extnet/get.go | 19 +- pkg/cloudapi/extnet/list_computes.go | 19 +- pkg/cloudapi/flipgroup/compute_add.go | 24 +-- pkg/cloudapi/flipgroup/compute_remove.go | 24 +-- pkg/cloudapi/flipgroup/create.go | 39 +--- pkg/cloudapi/flipgroup/delete.go | 19 +- pkg/cloudapi/flipgroup/edit.go | 23 +-- pkg/cloudapi/flipgroup/get.go | 19 +- pkg/cloudapi/image/create.go | 2 +- pkg/cloudapi/k8ci/get.go | 20 +- pkg/cloudapi/k8s/create.go | 34 +--- pkg/cloudapi/k8s/delete.go | 21 +- pkg/cloudapi/k8s/delete_master_from_group.go | 29 +-- pkg/cloudapi/k8s/delete_worker_from_group.go | 29 +-- pkg/cloudapi/k8s/disable_enable.go | 25 ++- pkg/cloudapi/k8s/find_group_by_label.go | 28 +-- pkg/cloudapi/k8s/get.go | 19 +- pkg/cloudapi/k8s/get_config.go | 19 +- pkg/cloudapi/k8s/get_node_annotations.go | 24 +-- pkg/cloudapi/k8s/get_node_labels.go | 26 +-- pkg/cloudapi/k8s/get_node_taints.go | 26 +-- pkg/cloudapi/k8s/restore.go | 19 +- pkg/cloudapi/k8s/start.go | 19 +- pkg/cloudapi/k8s/stop.go | 19 +- pkg/cloudapi/k8s/update.go | 19 +- pkg/cloudapi/k8s/worker_add.go | 29 +-- pkg/cloudapi/k8s/worker_reset.go | 29 +-- pkg/cloudapi/k8s/worker_restart.go | 29 +-- pkg/cloudapi/k8s/workers_group_add.go | 23 +-- pkg/cloudapi/k8s/workers_group_delete.go | 24 +-- pkg/cloudapi/k8s/workers_group_get_by_name.go | 24 +-- pkg/cloudapi/kvmppc/create.go | 41 ++-- pkg/cloudapi/kvmppc/create_blank.go | 51 ++--- pkg/cloudapi/kvmx86/create.go | 41 ++-- pkg/cloudapi/kvmx86/create_blank.go | 51 ++--- pkg/cloudapi/lb/backend_create.go | 26 +-- pkg/cloudapi/lb/backend_delete.go | 24 +-- pkg/cloudapi/lb/backend_server_add.go | 39 ++-- pkg/cloudapi/lb/backend_server_delete.go | 29 +-- pkg/cloudapi/lb/backend_server_update.go | 39 ++-- pkg/cloudapi/lb/backend_update.go | 26 +-- pkg/cloudapi/lb/config_reset.go | 19 +- pkg/cloudapi/lb/create.go | 38 ++-- pkg/cloudapi/lb/delete.go | 19 +- pkg/cloudapi/lb/disable_enable.go | 25 ++- pkg/cloudapi/lb/filter.go | 27 +++ pkg/cloudapi/lb/frontend_bind.go | 27 +-- pkg/cloudapi/lb/frontend_bind_delete.go | 29 +-- pkg/cloudapi/lb/frontend_bind_update.go | 29 +-- pkg/cloudapi/lb/frontend_create.go | 29 +-- pkg/cloudapi/lb/frontend_delete.go | 24 +-- pkg/cloudapi/lb/get.go | 19 +- pkg/cloudapi/lb/restart.go | 19 +- pkg/cloudapi/lb/restore.go | 19 +- pkg/cloudapi/lb/start.go | 19 +- pkg/cloudapi/lb/stop.go | 19 +- pkg/cloudapi/lb/update.go | 24 +-- pkg/cloudapi/rg/access_grant.go | 28 +-- pkg/cloudapi/rg/access_revoke.go | 24 +-- pkg/cloudapi/rg/affinity_group_computes.go | 24 +-- pkg/cloudapi/rg/affinity_groups_get.go | 24 +-- pkg/cloudapi/rg/affinity_groups_list.go | 19 +- pkg/cloudapi/rg/audits.go | 19 +- pkg/cloudapi/rg/create.go | 31 +-- pkg/cloudapi/rg/delete.go | 19 +- pkg/cloudapi/rg/disable.go | 19 +- pkg/cloudapi/rg/enable.go | 19 +- pkg/cloudapi/rg/get.go | 19 +- pkg/cloudapi/rg/list_computes.go | 19 +- pkg/cloudapi/rg/list_lb.go | 19 +- pkg/cloudapi/rg/list_pfw.go | 19 +- pkg/cloudapi/rg/list_vins.go | 19 +- pkg/cloudapi/rg/restore.go | 19 +- pkg/cloudapi/rg/set_def_net.go | 24 +-- pkg/cloudapi/rg/update.go | 19 +- pkg/cloudapi/rg/usage.go | 19 +- pkg/cloudapi/tasks/get.go | 19 +- pkg/cloudapi/tasks/models.go | 6 + pkg/cloudapi/vins/audits.go | 19 +- pkg/cloudapi/vins/create_in_account.go | 24 +-- pkg/cloudapi/vins/create_in_rg.go | 26 +-- pkg/cloudapi/vins/delete.go | 19 +- pkg/cloudapi/vins/disable_enable.go | 25 ++- pkg/cloudapi/vins/extnet_connect.go | 19 +- pkg/cloudapi/vins/extnet_disconnect.go | 19 +- pkg/cloudapi/vins/extnet_list.go | 19 +- pkg/cloudapi/vins/get.go | 19 +- pkg/cloudapi/vins/ip_list.go | 19 +- pkg/cloudapi/vins/ip_release.go | 19 +- pkg/cloudapi/vins/ip_reserve.go | 26 +-- pkg/cloudapi/vins/nat_rule_add.go | 36 ++-- pkg/cloudapi/vins/nat_rule_del.go | 24 +-- pkg/cloudapi/vins/nat_rule_list.go | 19 +- pkg/cloudapi/vins/restore.go | 19 +- pkg/cloudapi/vins/vnfdev_redeploy.go | 19 +- pkg/cloudapi/vins/vnfdev_restart.go | 19 +- pkg/cloudbroker/account/add_user.go | 31 +-- pkg/cloudbroker/account/audits.go | 18 +- pkg/cloudbroker/account/create.go | 26 +-- pkg/cloudbroker/account/delete.go | 24 +-- pkg/cloudbroker/account/delete_accounts.go | 24 +-- pkg/cloudbroker/account/delete_user.go | 24 +-- pkg/cloudbroker/account/disable.go | 24 +-- pkg/cloudbroker/account/disable_accounts.go | 19 +- pkg/cloudbroker/account/enable.go | 24 +-- pkg/cloudbroker/account/enable_accounts.go | 19 +- pkg/cloudbroker/account/get.go | 19 +- pkg/cloudbroker/account/list_computes.go | 19 +- pkg/cloudbroker/account/list_disks.go | 19 +- pkg/cloudbroker/account/list_flip_groups.go | 19 +- pkg/cloudbroker/account/list_rg.go | 19 +- pkg/cloudbroker/account/list_vins.go | 19 +- pkg/cloudbroker/account/restore.go | 24 +-- pkg/cloudbroker/account/update.go | 28 +-- .../account/update_resource_types.go | 27 +-- pkg/cloudbroker/account/update_user.go | 29 +-- .../compute/affinity_group_check_start.go | 24 +-- .../compute/affinity_label_remove.go | 19 +- pkg/cloudbroker/compute/affinity_label_set.go | 24 +-- pkg/cloudbroker/compute/affinity_relations.go | 19 +- pkg/cloudbroker/compute/affinity_rule_add.go | 54 +---- .../compute/affinity_rule_remove.go | 54 +---- .../compute/affinity_rules_clear.go | 19 +- .../compute/anti_affinity_rule_add.go | 54 +---- .../compute/anti_affinity_rule_clear.go | 19 +- .../compute/anti_affinity_rule_remove.go | 54 +---- pkg/cloudbroker/compute/attach_gpu.go | 24 +-- pkg/cloudbroker/compute/attach_pci_device.go | 24 +-- pkg/cloudbroker/compute/audits.go | 19 +- pkg/cloudbroker/compute/boot_order_get.go | 19 +- pkg/cloudbroker/compute/boot_order_set.go | 27 +-- pkg/cloudbroker/compute/cd_eject.go | 19 +- pkg/cloudbroker/compute/cd_insert.go | 24 +-- pkg/cloudbroker/compute/clone.go | 24 +-- pkg/cloudbroker/compute/computeci_set.go | 24 +-- pkg/cloudbroker/compute/computeci_unset.go | 19 +- pkg/cloudbroker/compute/create_template.go | 30 ++- pkg/cloudbroker/compute/delete.go | 19 +- pkg/cloudbroker/compute/detach_gpu.go | 19 +- pkg/cloudbroker/compute/detach_pci_device.go | 24 +-- pkg/cloudbroker/compute/disable.go | 19 +- pkg/cloudbroker/compute/disk_add.go | 31 +-- pkg/cloudbroker/compute/disk_attach.go | 24 +-- pkg/cloudbroker/compute/disk_del.go | 26 +-- pkg/cloudbroker/compute/disk_detach.go | 24 +-- pkg/cloudbroker/compute/disk_qos.go | 29 +-- pkg/cloudbroker/compute/disk_resize.go | 29 +-- pkg/cloudbroker/compute/enable.go | 19 +- pkg/cloudbroker/compute/filter.go | 90 +++++++++ pkg/cloudbroker/compute/get.go | 19 +- pkg/cloudbroker/compute/get_audits.go | 19 +- pkg/cloudbroker/compute/get_console_url.go | 19 +- pkg/cloudbroker/compute/get_log.go | 24 +-- pkg/cloudbroker/compute/list_gpu.go | 19 +- pkg/cloudbroker/compute/list_pci_device.go | 19 +- pkg/cloudbroker/compute/mass_delete.go | 19 +- pkg/cloudbroker/compute/mass_reboot.go | 19 +- .../compute/mass_repair_boot_fs.go | 19 +- pkg/cloudbroker/compute/mass_start.go | 19 +- pkg/cloudbroker/compute/mass_stop.go | 19 +- pkg/cloudbroker/compute/migrate.go | 19 +- pkg/cloudbroker/compute/migrate_storage.go | 36 ++-- .../compute/migrate_storage_abort.go | 19 +- .../compute/migrate_storage_clean_up.go | 19 +- .../compute/migrate_storage_info.go | 19 +- pkg/cloudbroker/compute/move_to_rg.go | 24 +-- pkg/cloudbroker/compute/net_attach.go | 31 +-- pkg/cloudbroker/compute/net_detach.go | 19 +- pkg/cloudbroker/compute/net_qos.go | 31 +-- pkg/cloudbroker/compute/pause.go | 19 +- pkg/cloudbroker/compute/pfw_add.go | 36 +--- pkg/cloudbroker/compute/pfw_del.go | 21 +- pkg/cloudbroker/compute/pfw_list.go | 19 +- pkg/cloudbroker/compute/pin_to_stack.go | 24 +-- pkg/cloudbroker/compute/power_cycle.go | 19 +- pkg/cloudbroker/compute/reboot.go | 19 +- pkg/cloudbroker/compute/redeploy.go | 21 +- pkg/cloudbroker/compute/registration.go | 24 +-- pkg/cloudbroker/compute/repair_boot_fs.go | 19 +- pkg/cloudbroker/compute/reset.go | 19 +- pkg/cloudbroker/compute/resize.go | 19 +- pkg/cloudbroker/compute/restore.go | 19 +- pkg/cloudbroker/compute/resume.go | 19 +- pkg/cloudbroker/compute/snapshot_create.go | 24 +-- pkg/cloudbroker/compute/snapshot_delete.go | 24 +-- .../compute/snapshot_evict_disk.go | 24 +-- pkg/cloudbroker/compute/snapshot_list.go | 19 +- pkg/cloudbroker/compute/snapshot_rollback.go | 24 +-- pkg/cloudbroker/compute/snapshot_usage.go | 19 +- pkg/cloudbroker/compute/start.go | 19 +- pkg/cloudbroker/compute/stop.go | 19 +- pkg/cloudbroker/compute/tag_add.go | 29 +-- pkg/cloudbroker/compute/tag_remove.go | 24 +-- pkg/cloudbroker/compute/unpin_from_stack.go | 19 +- pkg/cloudbroker/compute/update.go | 19 +- pkg/cloudbroker/compute/user_grant.go | 31 +-- pkg/cloudbroker/compute/user_list.go | 19 +- pkg/cloudbroker/compute/user_revoke.go | 24 +-- pkg/cloudbroker/compute/user_update.go | 31 +-- pkg/cloudbroker/disks/create.go | 33 +--- pkg/cloudbroker/disks/delete.go | 19 +- pkg/cloudbroker/disks/delete_disks.go | 24 +-- pkg/cloudbroker/disks/filter.go | 72 +++++++ pkg/cloudbroker/disks/get.go | 19 +- pkg/cloudbroker/disks/limit_io.go | 19 +- pkg/cloudbroker/disks/list_types.go | 13 +- pkg/cloudbroker/disks/rename.go | 24 +-- pkg/cloudbroker/disks/resize.go | 29 ++- pkg/cloudbroker/disks/restore.go | 24 +-- pkg/cloudbroker/disks/share.go | 19 +- pkg/cloudbroker/disks/snapshot_delete.go | 26 +-- pkg/cloudbroker/disks/snapshot_rollback.go | 33 ++-- pkg/cloudbroker/disks/unshare.go | 19 +- pkg/cloudbroker/extnet/access_add.go | 24 +-- pkg/cloudbroker/extnet/access_remove.go | 24 +-- pkg/cloudbroker/extnet/create.go | 39 ++-- pkg/cloudbroker/extnet/default_qos_update.go | 19 +- pkg/cloudbroker/extnet/destroy.go | 19 +- pkg/cloudbroker/extnet/device_deploy.go | 19 +- pkg/cloudbroker/extnet/device_migrate.go | 24 +-- pkg/cloudbroker/extnet/device_remove.go | 19 +- pkg/cloudbroker/extnet/device_restart.go | 19 +- pkg/cloudbroker/extnet/disable.go | 19 +- pkg/cloudbroker/extnet/dns_apply.go | 19 +- pkg/cloudbroker/extnet/enable.go | 19 +- pkg/cloudbroker/extnet/get.go | 19 +- pkg/cloudbroker/extnet/ips_exclude.go | 24 +-- pkg/cloudbroker/extnet/ips_exclude_range.go | 29 +-- pkg/cloudbroker/extnet/ips_include.go | 24 +-- pkg/cloudbroker/extnet/ips_include_range.go | 29 +-- pkg/cloudbroker/extnet/ntp_apply.go | 19 +- pkg/cloudbroker/extnet/set_default.go | 19 +- pkg/cloudbroker/extnet/update.go | 19 +- pkg/cloudbroker/grid/add.go | 29 +-- pkg/cloudbroker/grid/change_settings.go | 24 +-- pkg/cloudbroker/grid/check_vms.go | 19 +- pkg/cloudbroker/grid/create_system_space.go | 39 ++-- .../grid/execute_maintenance_script.go | 29 +-- pkg/cloudbroker/grid/get.go | 19 +- pkg/cloudbroker/grid/get_backup.go | 25 ++- pkg/cloudbroker/grid/get_diagnosis.go | 25 ++- pkg/cloudbroker/grid/models.go | 2 +- pkg/cloudbroker/grid/purge_logs.go | 24 +-- pkg/cloudbroker/grid/rename.go | 24 +-- pkg/cloudbroker/grid/services_restart.go | 24 +-- .../grid/set_cpu_allocation_ratio.go | 24 +-- .../grid/set_mem_allocation_ratio.go | 24 +-- pkg/cloudbroker/image/computeci_set.go | 24 +-- pkg/cloudbroker/image/computeci_unset.go | 19 +- pkg/cloudbroker/image/create_cdrom_image.go | 38 +--- pkg/cloudbroker/image/create_image.go | 47 +---- pkg/cloudbroker/image/create_virtual.go | 24 +-- pkg/cloudbroker/image/delete.go | 24 +-- pkg/cloudbroker/image/delete_cdrom_image.go | 23 +-- pkg/cloudbroker/image/delete_images.go | 23 +-- pkg/cloudbroker/image/disable.go | 19 +- pkg/cloudbroker/image/edit.go | 19 +- pkg/cloudbroker/image/enable.go | 19 +- pkg/cloudbroker/image/get.go | 19 +- pkg/cloudbroker/image/link.go | 24 +-- pkg/cloudbroker/image/list_stacks.go | 19 +- pkg/cloudbroker/image/rename.go | 24 +-- pkg/cloudbroker/image/share.go | 24 +-- pkg/cloudbroker/image/sync_create_image.go | 56 +----- pkg/cloudbroker/image/update_nodes.go | 19 +- pkg/cloudbroker/k8ci/create.go | 61 ++---- pkg/cloudbroker/k8ci/delete.go | 19 +- pkg/cloudbroker/k8ci/disable.go | 19 +- pkg/cloudbroker/k8ci/enable.go | 19 +- pkg/cloudbroker/k8ci/get.go | 19 +- pkg/cloudbroker/k8ci/restore.go | 19 +- pkg/cloudbroker/k8s/create.go | 34 +--- pkg/cloudbroker/k8s/delete.go | 23 +-- .../k8s/delete_master_from_group.go | 29 +-- .../k8s/delete_worker_from_group.go | 29 +-- pkg/cloudbroker/k8s/disable.go | 19 +- pkg/cloudbroker/k8s/enable.go | 19 +- pkg/cloudbroker/k8s/find_group_by_label.go | 28 +-- pkg/cloudbroker/k8s/get.go | 19 +- pkg/cloudbroker/k8s/get_config.go | 19 +- pkg/cloudbroker/k8s/get_node_annotations.go | 24 +-- pkg/cloudbroker/k8s/get_node_labels.go | 24 +-- pkg/cloudbroker/k8s/get_node_taints.go | 26 +-- pkg/cloudbroker/k8s/restore.go | 19 +- pkg/cloudbroker/k8s/start.go | 19 +- pkg/cloudbroker/k8s/stop.go | 19 +- pkg/cloudbroker/k8s/update.go | 19 +- pkg/cloudbroker/k8s/worker_add.go | 28 +-- pkg/cloudbroker/k8s/worker_reset.go | 29 +-- pkg/cloudbroker/k8s/worker_restart.go | 29 +-- pkg/cloudbroker/k8s/workers_group_add.go | 24 +-- pkg/cloudbroker/k8s/workers_group_delete.go | 24 +-- .../k8s/workers_group_get_by_name.go | 24 +-- pkg/cloudbroker/kvmppc/create.go | 41 ++-- pkg/cloudbroker/kvmppc/create_blank.go | 51 ++--- pkg/cloudbroker/kvmppc/mass_create.go | 58 ++---- pkg/cloudbroker/kvmx86/create.go | 41 ++-- pkg/cloudbroker/kvmx86/create_blank.go | 51 ++--- pkg/cloudbroker/kvmx86/mass_create.go | 58 ++---- pkg/cloudbroker/lb/backend_create.go | 28 +-- pkg/cloudbroker/lb/backend_delete.go | 24 +-- pkg/cloudbroker/lb/backend_server_add.go | 39 ++-- pkg/cloudbroker/lb/backend_server_delete.go | 31 +-- pkg/cloudbroker/lb/backend_server_update.go | 39 ++-- pkg/cloudbroker/lb/backend_update.go | 26 +-- pkg/cloudbroker/lb/config_reset.go | 19 +- pkg/cloudbroker/lb/create.go | 36 ++-- pkg/cloudbroker/lb/delete.go | 19 +- pkg/cloudbroker/lb/disable.go | 19 +- pkg/cloudbroker/lb/enable.go | 19 +- pkg/cloudbroker/lb/filter.go | 27 +++ pkg/cloudbroker/lb/frontend_bind.go | 43 ++-- pkg/cloudbroker/lb/frontend_bind_delete.go | 29 +-- pkg/cloudbroker/lb/frontend_binding_update.go | 43 ++-- pkg/cloudbroker/lb/frontend_create.go | 29 +-- pkg/cloudbroker/lb/frontend_delete.go | 24 +-- pkg/cloudbroker/lb/get.go | 19 +- pkg/cloudbroker/lb/restart.go | 19 +- pkg/cloudbroker/lb/restore.go | 19 +- pkg/cloudbroker/lb/start.go | 19 +- pkg/cloudbroker/lb/stop.go | 19 +- pkg/cloudbroker/lb/update.go | 24 +-- pkg/cloudbroker/rg/access_grant.go | 28 +-- pkg/cloudbroker/rg/access_revoke.go | 24 +-- pkg/cloudbroker/rg/affinity_group_computes.go | 24 +-- pkg/cloudbroker/rg/affinity_groups_get.go | 24 +-- pkg/cloudbroker/rg/affinity_groups_list.go | 19 +- pkg/cloudbroker/rg/audits.go | 19 +- pkg/cloudbroker/rg/create.go | 31 +-- pkg/cloudbroker/rg/delete.go | 19 +- pkg/cloudbroker/rg/disable.go | 19 +- pkg/cloudbroker/rg/enable.go | 19 +- pkg/cloudbroker/rg/get.go | 19 +- pkg/cloudbroker/rg/list_computes.go | 19 +- pkg/cloudbroker/rg/list_lb.go | 19 +- pkg/cloudbroker/rg/list_pfw.go | 19 +- pkg/cloudbroker/rg/list_vins.go | 19 +- pkg/cloudbroker/rg/mass_delete.go | 19 +- pkg/cloudbroker/rg/mass_disable.go | 19 +- pkg/cloudbroker/rg/mass_enable.go | 19 +- pkg/cloudbroker/rg/restore.go | 19 +- pkg/cloudbroker/rg/set_def_net.go | 25 +-- pkg/cloudbroker/rg/update.go | 19 +- pkg/cloudbroker/rg/update_resource_types.go | 27 +-- pkg/cloudbroker/rg/usage.go | 19 +- pkg/cloudbroker/sep/access_grant.go | 24 +-- pkg/cloudbroker/sep/access_grant_to_pool.go | 24 +-- pkg/cloudbroker/sep/access_revoke.go | 24 +-- pkg/cloudbroker/sep/access_revoke_to_pool.go | 24 +-- pkg/cloudbroker/sep/add_consumer_nodes.go | 24 +-- pkg/cloudbroker/sep/add_provider_nodes.go | 24 +-- pkg/cloudbroker/sep/config_field_edit.go | 36 +--- pkg/cloudbroker/sep/config_insert.go | 24 +-- pkg/cloudbroker/sep/config_validate.go | 24 +-- pkg/cloudbroker/sep/consumption.go | 19 +- pkg/cloudbroker/sep/create.go | 29 +-- pkg/cloudbroker/sep/decommission.go | 19 +- pkg/cloudbroker/sep/del_consumer_nodes.go | 24 +-- pkg/cloudbroker/sep/delete.go | 19 +- pkg/cloudbroker/sep/disable.go | 19 +- pkg/cloudbroker/sep/disk_list.go | 19 +- pkg/cloudbroker/sep/enable.go | 19 +- pkg/cloudbroker/sep/get.go | 19 +- pkg/cloudbroker/sep/get_config.go | 19 +- pkg/cloudbroker/sep/get_pool.go | 24 +-- pkg/cloudbroker/sep/update_capacity_limit.go | 19 +- pkg/cloudbroker/tasks/get.go | 19 +- pkg/cloudbroker/tasks/models.go | 6 + pkg/cloudbroker/vins/audits.go | 19 +- pkg/cloudbroker/vins/create_in_account.go | 24 +-- pkg/cloudbroker/vins/create_in_rg.go | 24 +-- pkg/cloudbroker/vins/default_qos_update.go | 19 +- pkg/cloudbroker/vins/delete.go | 19 +- pkg/cloudbroker/vins/disable.go | 19 +- pkg/cloudbroker/vins/enable.go | 19 +- pkg/cloudbroker/vins/extnet_connect.go | 24 +-- pkg/cloudbroker/vins/extnet_disconnect.go | 19 +- pkg/cloudbroker/vins/extnet_list.go | 19 +- pkg/cloudbroker/vins/get.go | 19 +- pkg/cloudbroker/vins/ip_list.go | 19 +- pkg/cloudbroker/vins/ip_release.go | 19 +- pkg/cloudbroker/vins/ip_reserve.go | 26 +-- pkg/cloudbroker/vins/mass_delete.go | 19 +- pkg/cloudbroker/vins/mass_disable.go | 19 +- pkg/cloudbroker/vins/mass_enable.go | 19 +- pkg/cloudbroker/vins/nat_rule_add.go | 38 ++-- pkg/cloudbroker/vins/nat_rule_del.go | 24 +-- pkg/cloudbroker/vins/nat_rule_list.go | 19 +- pkg/cloudbroker/vins/net_qos.go | 19 +- pkg/cloudbroker/vins/restore.go | 19 +- pkg/cloudbroker/vins/vnfdev_redeploy.go | 19 +- pkg/cloudbroker/vins/vnfdev_reset.go | 19 +- pkg/cloudbroker/vins/vnfdev_restart.go | 21 +- pkg/cloudbroker/vins/vnfdev_start.go | 19 +- pkg/cloudbroker/vins/vnfdev_stop.go | 21 +- samples/config/config.json | 8 + samples/config/config.yml | 6 + samples/config/legacy-config.json | 7 + samples/config/legacy-config.yml | 5 + 433 files changed, 4236 insertions(+), 6506 deletions(-) create mode 100644 samples/config/config.json create mode 100644 samples/config/config.yml create mode 100644 samples/config/legacy-config.json create mode 100644 samples/config/legacy-config.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 86fa395..58e4215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,39 @@ -## Version 1.2.1 +## Version 1.3.0 -### Bug fixes +### Features -#### Legacy Client +- Created CloudAPI/CloudBroker filtering, sorting and serialization functions for List requests. + - Every handler with present List request has available FilterBy functions. Filtering by ID, Name is common for each handler. + - In case user needs to filter response by uncommon field FilterFunc with user-specified predicate is also available. + - CloudAPI/CloudBroker computes, disks and lb also have specific Filter methods predefined, to name a few: + - computes: + - FilterByK8SID, used to filter computes used by specified k8s cluster; + - FilterByK8SMasters, FilterByK8SWorkers, used to filter master/workers nodes. Best used after FilterByK8SID call; + - FilterByLBID, used to filter computes used by specified load balancer; -- Fixed password and username encoding -- Fixed request params absence in HTTP Transport + - disks: + - FilterByK8SID, used to filter disks attached to computes inside specified k8s cluster; + - FilterByLBID, used to filter disks attached to computes inside specified load balancer; -#### All + - lb: + - FilterByK8SID, used to filter load balancers used by specified k8s cluster; + +- Reinvented request validation using go-validator. Made easier to manipulate and add on to. + - Request/Config validation now uses tags instead of hard-coded validation functions; + +- Added ability to parse client configuration from JSON or YAML formatted files. + +### Bug Fixes + +- Fixed SSO_URL trailing slash possibly breaking authentication process. +- Fixed cloudbroker/vins/nat_rule_add request model types. +- Fixed cloudbroker/grid DiskSize field type +- Fixed TasksResult, InfoResult in cloudbroker/cloudapi/tasks/models JSON unmarshalling. + +### Tests + +- Covered CloudAPI/CloudBroker filters with unit tests. + +### Other - Updated module to new repository diff --git a/README.md b/README.md index 42a0979..1c5ea30 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Decort SDK - это библиотека, написанная на языке G - Версия 1.0.x Decort-SDK соответствует 3.8.4 версии платформы - Версия 1.1.x Decort-SDK соответствует 3.8.5 версии платформы - Версия 1.2.x Decort-SDK соответствует 3.8.5 версии платформы + - Версия 1.3.x Decort-SDK соответствует 3.8.5 версии платформы ## Оглавление @@ -16,6 +17,10 @@ Decort SDK - это библиотека, написанная на языке G - [Cloudbroker](#cloudbroker) - [Работа с библиотекой](#работа-с-библиотекой) - [Настройка конфигурации клиента](#настройка-конфигурации-клиента) + - [Пример конфигурации клиента](#пример-конфигурации-клиента) + - [Парсинг конфигурации из файла](#парсинг-конфигурации-из-файла) + - [Пример JSON конфигурации](#пример-json-конфигурации) + - [Пример YAML конфигурации](#пример-yaml-конфигурации) - [Создание клиента](#создание-клиента) - [Создание структуры запроса](#cоздание-структуры-запроса) - [Выполнение запроса](#выполнение-запроса) @@ -24,6 +29,10 @@ Decort SDK - это библиотека, написанная на языке G - [Сериализация](#сериализация) - [Работа с legacy клиентом](#работа-с-legacy-клиентом) - [Настройка конфигурации legacy клиента](#настройка-конфигурации-legacy-клиента) + - [Пример конфигурации legacy клиента](#пример-конфигурации-legacy-клиента) + - [Парсинг legacy конфигурации из файла](#парсинг-legacy-конфигурации-из-файла) + - [Пример legacy JSON конфигурации](#пример-legacy-json-конфигурации) + - [Пример legacy YAML конфигурации](#пример-legacy-yaml-конфигурации) - [Создание legacy клиента](#создание-legacy-клиента) - [Создание структуры запроса](#cоздание-структуры-запроса) - [Выполнение запроса](#выполнение-запроса) @@ -93,9 +102,10 @@ go get -u repository.basistech.ru/BASIS/decort-golang-sdk Алгоритм работы с библиотекой выглядит следующим образом: 1. Настройка конфигурации клиента. -2. Создание клиента. -3. Создание структуры запроса. -4. Выполнение запроса. +2. Парсинг конфигурации из файла. +3. Создание клиента. +4. Создание структуры запроса. +5. Выполнение запроса. ### Настройка конфигурации клиента @@ -110,11 +120,11 @@ go get -u repository.basistech.ru/BASIS/decort-golang-sdk | SSLSkipVerify | bool | Нет | Пропуск проверки подлинности сертификата, по умолчанию - true | | Token | string | Нет | JWT токен | -Пример кода: +#### Пример конфигурации клиента ```go import ( - "github.com/rudecs/decort-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) func main(){ // Настройка конфигурации @@ -128,6 +138,47 @@ func main(){ } ``` +#### Парсинг конфигурации из файла + +Также возможно создать переменную конфигурации из JSON или YAML файла, используя функцию `ParseConfigJSON` (или `ParseConfigYAML`) из пакета config. +
+*См. пример файлов конфигурации ниже и в директории `samples/`.* + +```go +import ( + "repository.basistech.ru/BASIS/decort-golang-sdk/config" +) + +func main() { + // Парсинг конфигурации из JSON-файла + cfg := config.ParseConfigJSON("") +} +``` + +#### Пример JSON конфигурации + +```json +{ + "appId": "", + "appSecret": "", + "ssoUrl": "https://sso.digitalenergy.online", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "sslSkipVerify": false +} +``` + +#### Пример YAML конфигурации + +```yaml +appId: +appSecret: +ssoUrl: https://sso.digitalenergy.online +decortUrl: https://mr4.digitalenergy.online +retries: 5 +sslSkipVerify: false +``` + ### Создание клиента Создание клиента происходит с помощью функции-строителя `New` из основного пакета `decort-sdk`, для избежания проблем с именами, пакету можно присвоить алиас `decort`. Функция принимает конфигурацию, возвращает структуру `DecortClient`, с помощью которой можно взаимодействовать с платформой. @@ -138,8 +189,8 @@ func main(){ package main import ( - "github.com/rudecs/decort-sdk/config" - decort "github.com/rudecs/decort-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" ) func main() { @@ -302,9 +353,9 @@ type CreateRequest struct { package main import ( - "github.com/rudecs/decort-sdk/config" - decort "github.com/rudecs/decort-sdk" - "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/kvmx86" ) func main() { @@ -402,9 +453,9 @@ import ( "log" "fmt" - "github.com/rudecs/decort-sdk/config" - decort "github.com/rudecs/decort-sdk" - "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/kvmx86" ) func main() { @@ -484,6 +535,52 @@ filtered := resp. // .... ``` +Также у `compute`, `disks`, и `lb` присутствуют специфические функции фильтрации, отправляющие дополнительные запросы. В качестве параметров принимают: +- context.Context - контекст для доп. запроса +- id (или другое поле, по которому производится фильтрация) +- interfaces.Caller - DECORT-клиент для запроса + +Так как эти функции возвращают не только результирующий слайс, но и возможную ошибку - конвейер придется прервать для обработки ошибки. + + +#### Использование на примере `compute.FilterByK8SID`: + +```go +func main() { + // Чтение конфигурации из файла + cfg := config.ParseConfigJSON("") + + // Создание клиента + client := decort.New(cfg) + + // Создание структуры запроса compute/list + req := compute.ListRequest{} + + // Запрос + resp, err := client.CloudAPI().Compute().List(context.Background(), req) + if err != nil { + log.Fatal(err) + } + + // Фильтрация по id кластера. + // Первый аргумент - контекст + // Второй - ID кластера + // Третий - DECORT-клиент + filtered, err := resp.FilterByK8SID(context.Background(), , client) + if err != nil { + log.Fatal(err) // Возможная ошибка запроса + } + + // Доп. фильтрация и сортировка результата - worker ноды кластера + workers := filtered.FilterByK8SWorkers().SortByCreatedTime(false) + + // Доп. фильтрация и сортировка результата - master ноды кластера + masters := filtered.FilterByK8SMasters().SortByCreatedTime(true) + + // .... +} +``` + ### Сортировка Функции сортировки так же могут быть объединены в конвейер: @@ -536,9 +633,9 @@ import ( "context" "log" - decort "github.com/rudecs/decort-sdk" - "github.com/rudecs/decort-sdk/config" - "github.com/rudecs/decort-sdk/pkg/cloudbroker/compute" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute" ) func main() { @@ -598,23 +695,61 @@ func main() { | SSLSkipVerify | bool | Нет | Пропуск проверки подлинности сертификата, по умолчанию - true | | Token | string | Нет | JWT токен | -Пример кода: +#### Пример конфигурации legacy клиента ```go import ( - "github.com/rudecs/decort-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) + func main(){ // Настройка конфигурации legacyCfg := config.LegacyConfig{ - Username: "", - Password: "", + Username: "", + Password: "", DecortURL: "https://mr4.digitalenergy.online", Retries: 5, } } ``` +#### Парсинг legacy конфигурации из файла + +Также возможно создать переменную конфигурации из JSON или YAML файла, используя функцию `ParseLegacyConfigJSON` (или `ParseLegacyConfigYAML`) из пакета config. +
+*См. пример файлов конфигурации ниже и в директории `samples/`.* + +```go +import ( + "repository.basistech.ru/BASIS/decort-golang-sdk/config" +) + +func main() { + // Парсинг конфигурации из YAML-файла + legacyCfg := config.ParseLegacyConfigYAML("") +} +``` + +#### Пример legacy JSON конфигурации + +```json +{ + "username": "", + "password": "", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "sslSkipVerify": true +} +``` + +#### Пример legacy YAML конфигурации +```yaml +username: +password: +decortUrl: https://mr4.digitalenergy.online +retries: 5 +sslSkipVerify: true +``` ### Создание legacy клиента Создание клиента происходит с помощью функции-строителя `NewLegacy` из основного пакета `decort-sdk`, для избежания проблем с именами, пакету можно присвоить алиас `decort`. Функция принимает конфигурацию, возвращает структуру `DecortClient`, с помощью которой можно взаимодействовать с платформой. @@ -625,8 +760,8 @@ func main(){ package main import ( - "github.com/rudecs/decort-sdk/config" - decort "github.com/rudecs/decort-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" ) func main() { @@ -651,8 +786,8 @@ package main import ( "fmt" - "github.com/rudecs/decort-sdk/config" - decort "github.com/rudecs/decort-sdk" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + decort "repository.basistech.ru/BASIS/decort-golang-sdk" ) func main() { @@ -685,4 +820,3 @@ func main() { fmt.Println(res) } -``` diff --git a/README_EN.md b/README_EN.md index 49852a5..5189b0c 100644 --- a/README_EN.md +++ b/README_EN.md @@ -14,7 +14,7 @@ Also the library have structures for responses. ## Install ```bash -go get -u github.com/rudecs/decort-sdk +go get -u repository.basistech.ru/BASIS/decort-golang-sdk ``` ## API List @@ -29,8 +29,8 @@ import ( "fmt" "log" - "github.com/rudecs/decort-sdk/config" - "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86" + "repository.basistech.ru/BASIS/decort-golang-sdk/config" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/kvmx86" ) func main() { diff --git a/config/config.go b/config/config.go index 796e867..224da90 100644 --- a/config/config.go +++ b/config/config.go @@ -1,39 +1,95 @@ package config +import ( + "encoding/json" + "os" + + "gopkg.in/yaml.v3" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" +) + // Configuration for creating request to platform type Config struct { // JWT platform token // Required: false // Example: "qwqwdfwv68979we0q9bfv7e9sbvd89798qrwv97ff" - Token string + Token string `json:"token" yaml:"token"` // Application (client) identifier for authorization // in the cloud platform controller in oauth2 mode. // Required: true // Example: "ewqfrvea7s890avw804389qwguf234h0otfi3w4eiu" - AppID string + AppID string `json:"appId" yaml:"appId" validate:"required"` // Application (client) secret code for authorization // in the cloud platform controller in oauth2 mode. // Example: "frvet09rvesfis0c9erv9fsov0vsdfi09ovds0f" - AppSecret string + AppSecret string `json:"appSecret" yaml:"appSecret" validate:"required"` // Platform authentication service address // Required: true // Example: "https://sso.digitalenergy.online" - SSOURL string + SSOURL string `json:"ssoUrl" yaml:"ssoUrl" validate:"url"` // The address of the platform on which the actions are planned // Required: true // Example: "https://mr4.digitalenergy.online" - DecortURL string + DecortURL string `json:"decortUrl" yaml:"decortUrl" validate:"url"` // Amount platform request attempts // Default value: 5 // Required: false - Retries uint64 + Retries uint64 `json:"retries" yaml:"retries"` // Skip verify, true by default // Required: false - SSLSkipVerify bool + SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"` +} + +// ParseConfigJSON parses Config from specified JSON-formatted file. +func ParseConfigJSON(path string) (Config, error) { + file, err := os.ReadFile(path) + if err != nil { + return Config{}, err + } + + var config Config + + err = json.Unmarshal(file, &config) + if err != nil { + return Config{}, err + } + + err = validators.ValidateConfig(config) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return Config{}, validators.ValidationError(validationError) + } + } + + return config, nil +} + +// ParseConfigYAML parses Config from specified YAML-formatted file. +func ParseConfigYAML(path string) (Config, error) { + file, err := os.ReadFile(path) + if err != nil { + return Config{}, err + } + + var config Config + + err = yaml.Unmarshal(file, &config) + if err != nil { + return Config{}, err + } + + err = validators.ValidateConfig(config) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return Config{}, validators.ValidationError(validationError) + } + } + + return config, nil } diff --git a/config/legacy-config.go b/config/legacy-config.go index 7052cab..81175c1 100644 --- a/config/legacy-config.go +++ b/config/legacy-config.go @@ -1,33 +1,89 @@ package config +import ( + "encoding/json" + "os" + + "gopkg.in/yaml.v3" + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" +) + // Legacy client configuration type LegacyConfig struct { // ServiceAccount username // Required: true // Example : "osh_mikoev" - Username string + Username string `json:"username" yaml:"username" validate:"required"` // ServiceAccount password // Required: true // Example: "[1o>hYkjnJr)HI78q7t&#%8Lm" - Password string + Password string `json:"password" yaml:"password" validate:"required"` // Platform token // Required: false // Example: "158e76424b0d4810b6086hgbhj928fc4a6bc06e" - Token string + Token string `json:"token" yaml:"token"` // Address of the platform on which the actions are planned // Required: true // Example: "https://mr4.digitalenergy.online" - DecortURL string + DecortURL string `json:"decortUrl" yaml:"decortUrl" validate:"url"` // Amount platform request attempts // Default value: 5 // Required: false - Retries uint64 + Retries uint64 `json:"retries" yaml:"retries"` // Skip verify, true by default // Required: false - SSLSkipVerify bool + SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"` +} + +// ParseLegacyConfigJSON parses LegacyConfig from specified JSON-formatted file. +func ParseLegacyConfigJSON(path string) (LegacyConfig, error) { + file, err := os.ReadFile(path) + if err != nil { + return LegacyConfig{}, err + } + + var config LegacyConfig + + err = json.Unmarshal(file, &config) + if err != nil { + return LegacyConfig{}, err + } + + err = validators.ValidateConfig(config) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return LegacyConfig{}, validators.ValidationError(validationError) + } + } + + return config, nil +} + +// ParseLegacyConfigYAML parses LegacyConfig from specified YAML-formatted file. +func ParseLegacyConfigYAML(path string) (LegacyConfig, error) { + file, err := os.ReadFile(path) + if err != nil { + return LegacyConfig{}, err + } + + var config LegacyConfig + + err = yaml.Unmarshal(file, &config) + if err != nil { + return LegacyConfig{}, err + } + + err = validators.ValidateConfig(config) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return LegacyConfig{}, validators.ValidationError(validationError) + } + } + + return config, nil } diff --git a/go.mod b/go.mod index 3dc5575..ee03956 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,5 @@ require ( golang.org/x/crypto v0.5.0 // indirect golang.org/x/sys v0.4.0 // indirect golang.org/x/text v0.6.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a653d6f..d2c9a3a 100644 --- a/go.sum +++ b/go.sum @@ -28,3 +28,4 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/client/transport.go b/internal/client/transport.go index e820a90..52a1aa4 100644 --- a/internal/client/transport.go +++ b/internal/client/transport.go @@ -23,6 +23,8 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { body := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s&response_type=id_token", t.clientID, t.clientSecret) bodyReader := strings.NewReader(body) + t.ssoURL = strings.TrimSuffix(t.ssoURL, "/") + req, _ := http.NewRequestWithContext(req.Context(), "POST", t.ssoURL+"/v1/oauth/access_token", bodyReader) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") diff --git a/internal/validators/custom.go b/internal/validators/custom.go index 7d9a924..57cd547 100644 --- a/internal/validators/custom.go +++ b/internal/validators/custom.go @@ -2,25 +2,48 @@ package validators import "github.com/go-playground/validator/v10" -// accountCUTypeValidator is used to validate CUType field. -func accountCUTypeValidator(fe validator.FieldLevel) bool { +// protoValidator is used to validate Proto fields. +func protoValidator(fe validator.FieldLevel) bool { fieldValue := fe.Field().String() - return StringInSlice(fieldValue, accountCUTypeValues) + return StringInSlice(fieldValue, protoValues) } -// accountAccessTypeValidator is used to validate AccessType field. -func accountAccessTypeValidator(fe validator.FieldLevel) bool { +// accessTypeValidator is used to validate AccessType fields. +func accessTypeValidator(fe validator.FieldLevel) bool { fieldValue := fe.Field().String() - return StringInSlice(fieldValue, accountAccessTypeValues) + return StringInSlice(fieldValue, accessTypeValues) } -// bserviceDriverValidator is used to validate Driver field. -func bserviceDriverValidator(fe validator.FieldLevel) bool { +// resTypesValidator is used to validate ResTypes fields. +func resTypesValidator(fe validator.FieldLevel) bool { + fieldSlice, ok := fe.Field().Interface().([]string) + if !ok { + return false + } + + for _, value := range fieldSlice { + if !StringInSlice(value, resTypesValues) { + return false + } + } + + return true +} + +// driverValidator is used to validate Driver fields. +func driverValidator(fe validator.FieldLevel) bool { fieldValue := fe.Field().String() - return StringInSlice(fieldValue, bserviceDriverValues) + return StringInSlice(fieldValue, driverValues) +} + +// accountCUTypeValidator is used to validate CUType field. +func accountCUTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, accountCUTypeValues) } // bserviceModeValidator is used to validate Mode field. @@ -65,11 +88,76 @@ func computeNetTypeValidator(fe validator.FieldLevel) bool { return StringInSlice(fieldValue, computeNetTypeValues) } -// computeProtoValidator is used to validate Proto field. -func computeProtoValidator(fe validator.FieldLevel) bool { +// computeOrderValidator is used to validate Order field. +func computeOrderValidator(fe validator.FieldLevel) bool { + fieldSlice, ok := fe.Field().Interface().([]string) + if !ok { + return false + } + + for _, value := range fieldSlice { + if !StringInSlice(value, computeOrderValues) { + return false + } + } + + return true +} + +// computeDataDisksValidator is used to validate DataDisks field. +func computeDataDisksValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, computeDataDisksValues) +} + +// diskTypeValidator is used to validate Type field. +func diskTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, diskTypeValues) +} + +// flipgroupClientTypeValidator is used to validate ClientType field. +func flipgroupClientTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, flipgroupClientTypeValues) +} + +// kvmNetTypeValidator is used to validate NetType field. +func kvmNetTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, kvmNetTypeValues) +} + +// lbAlgorithmValidator is used to validate Algorithm field. +func lbAlgorithmValidator(fe validator.FieldLevel) bool { fieldValue := fe.Field().String() - return StringInSlice(fieldValue, computeProtoValues) + return StringInSlice(fieldValue, lbAlgorithmValues) +} + +// rgDefNetValidator is used to validate DefNet field. +func rgDefNetValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, rgDefNetValues) +} + +// rgNetTypeValidator is used to validate NetType field. +func rgNetTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, rgNetTypeValues) +} + +// vinsTypeValidator is used to validate Type field. +func vinsTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, vinsTypeValues) } // imageBootTypeValidator is used to validate BootType field. @@ -108,3 +196,10 @@ func imageArchitectureValidator(fe validator.FieldLevel) bool { return StringInSlice(fieldValue, imageArchitectureValues) } + +// sepFieldTypeValidator is used to validate FieldType field. +func sepFieldTypeValidator(fe validator.FieldLevel) bool { + fieldValue := fe.Field().String() + + return StringInSlice(fieldValue, sepFieldTypeValues) +} diff --git a/internal/validators/helper.go b/internal/validators/helper.go index 213bd55..6a2f7ed 100644 --- a/internal/validators/helper.go +++ b/internal/validators/helper.go @@ -11,6 +11,11 @@ func ValidateRequest(req interface{}) error { return validate.Struct(req) } +func ValidateConfig(cfg interface{}) error { + validate := getDecortValidator() + return validate.Struct(cfg) +} + func ValidationError(fe validator.FieldError) error { return errors.New(errorMessage(fe)) } diff --git a/internal/validators/messages.go b/internal/validators/messages.go index 521ad46..1716ac4 100644 --- a/internal/validators/messages.go +++ b/internal/validators/messages.go @@ -12,7 +12,7 @@ func errorMessage(fe validator.FieldError) string { switch fe.Tag() { - // Default Validators + // Common Validators case "required": return fmt.Sprintf("%s %s is required", prefix, fe.Field()) case "gt": @@ -26,26 +26,38 @@ func errorMessage(fe validator.FieldError) string { case "email": return fmt.Sprintf("%s %s: unexpected E-Mail format", prefix, fe.Field()) - // Account Validators - case "accountAccessType": - return fmt.Sprintf("%s %s must be one of the followng: %s", + case "driver": + return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), - joinValues(accountAccessTypeValues)) + joinValues(driverValues)) - case "accountCUType": + case "accessType": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), - joinValues(accountCUTypeValues)) + joinValues(accessTypeValues)) - // BService Validators - case "bserviceDriver": + case "resTypes": + return fmt.Sprintf("%s %s can contain only the following values: %s", + prefix, + fe.Field(), + joinValues(resTypesValues)) + + case "proto": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(protoValues)) + + // Account Validators + case "accountCUType": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), - joinValues(bserviceDriverValues)) + joinValues(accountCUTypeValues)) + // BService Validators case "bserviceMode": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, @@ -83,14 +95,68 @@ func errorMessage(fe validator.FieldError) string { fe.Field(), joinValues(computeNetTypeValues)) - case "computeProto": + case "computeOrder": + return fmt.Sprintf("%s %s can contain only the following values: %s", + prefix, + fe.Field(), + joinValues(computeOrderValues)) + + case "computeDataDisks": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), - joinValues(computeProtoValues)) + joinValues(computeDataDisksValues)) + + // Disk Validators + case "diskType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(diskTypeValues)) + + // Flipgroup Validators + case "flipgroupClientType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(flipgroupClientTypeValues)) + + // KVM_X86/KVM_PPC Validators + case "kvmNetType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(kvmNetTypeValues)) + + // LB Validators + case "lbAlgorithm": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(lbAlgorithmValues)) + + // RG Validators + case "rgDefNet": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(rgDefNetValues)) + + case "rgNetType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(rgNetTypeValues)) + + // ViNS Validators + case "vinsType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(vinsTypeValues)) // Image Validators - case "bootType": + case "imageBootType": return fmt.Sprintf("%s %s must be one of the following: %s", prefix, fe.Field(), @@ -114,6 +180,13 @@ func errorMessage(fe validator.FieldError) string { fe.Field(), joinValues(imageArchitectureValues)) + // SEP Validators + case "sepFieldType": + return fmt.Sprintf("%s %s must be one of the following: %s", + prefix, + fe.Field(), + joinValues(sepFieldTypeValues)) + } return fe.Error() diff --git a/internal/validators/validator.go b/internal/validators/validator.go index 8ff71dc..2cffe16 100644 --- a/internal/validators/validator.go +++ b/internal/validators/validator.go @@ -34,37 +34,47 @@ func getDecortValidator() *validator.Validate { // registerAllValidators registers all custom validators in DecortValidator. func registerAllValidators(validate *validator.Validate) error { - err := validate.RegisterValidation("bootType", imageBootTypeValidator) + err := validate.RegisterValidation("proto", protoValidator) if err != nil { return err } - err = validate.RegisterValidation("imageType", imageTypeValidator) + err = validate.RegisterValidation("accessType", accessTypeValidator) if err != nil { return err } - err = validate.RegisterValidation("imageDrivers", imageDriversValidator) + err = validate.RegisterValidation("resTypes", resTypesValidator) if err != nil { return err } - err = validate.RegisterValidation("imageArchitecture", imageArchitectureValidator) + err = validate.RegisterValidation("driver", driverValidator) if err != nil { return err } - err = validate.RegisterValidation("accountAccessType", accountAccessTypeValidator) + err = validate.RegisterValidation("imageBootType", imageBootTypeValidator) if err != nil { return err } - err = validate.RegisterValidation("accountCUType", accountCUTypeValidator) + err = validate.RegisterValidation("imageType", imageTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("imageDrivers", imageDriversValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("imageArchitecture", imageArchitectureValidator) if err != nil { return err } - err = validate.RegisterValidation("bserviceDriver", bserviceDriverValidator) + err = validate.RegisterValidation("accountCUType", accountCUTypeValidator) if err != nil { return err } @@ -99,7 +109,52 @@ func registerAllValidators(validate *validator.Validate) error { return err } - err = validate.RegisterValidation("computeProto", computeProtoValidator) + err = validate.RegisterValidation("computeOrder", computeOrderValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("computeDataDisks", computeDataDisksValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("diskType", diskTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("flipgroupClientType", flipgroupClientTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("kvmNetType", kvmNetTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("lbAlgorithm", lbAlgorithmValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("rgDefNet", rgDefNetValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("rgNetType", rgNetTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("vinsType", vinsTypeValidator) + if err != nil { + return err + } + + err = validate.RegisterValidation("sepFieldType", sepFieldTypeValidator) if err != nil { return err } diff --git a/internal/validators/values.go b/internal/validators/values.go index b28922b..b56e58d 100644 --- a/internal/validators/values.go +++ b/internal/validators/values.go @@ -1,21 +1,40 @@ package validators var ( - accountAccessTypeValues = []string{"R", "RCX", "ARCXDU"} - accountCUTypeValues = []string{"CU_M", "CU_C", "CU_D", "CU_S", "CU_A", "CU_NO", "CU_I", "CU_NP"} + driverValues = []string{"KVM_X86", "KVM_PPC"} + accessTypeValues = []string{"R", "RCX", "ARCXDU"} + resTypesValues = []string{"compute", "vins", "k8s", "openshift", "lb", "flipgroup"} + protoValues = []string{"tcp", "udp"} - bserviceDriverValues = []string{"KVM_X86, KVM_PPC"} - bserviceModeValues = []string{"ABSOLUTE", "RELATIVE"} + accountCUTypeValues = []string{"CU_M", "CU_C", "CU_D", "CU_S", "CU_A", "CU_NO", "CU_I", "CU_NP"} - computeTopologyValues = []string{"compute", "node"} - computePolicyValues = []string{"RECOMMENDED", "REQUIRED"} - computeModeValues = []string{"EQ", "EN", "ANY"} - computeDiskTypeValues = []string{"D", "B"} - computeNetTypeValues = []string{"EXTNET", "VINS"} - computeProtoValues = []string{"tcp", "udp"} + bserviceModeValues = []string{"ABSOLUTE", "RELATIVE"} + + computeTopologyValues = []string{"compute", "node"} + computePolicyValues = []string{"RECOMMENDED", "REQUIRED"} + computeModeValues = []string{"EQ", "EN", "ANY"} + computeDiskTypeValues = []string{"D", "B"} + computeNetTypeValues = []string{"EXTNET", "VINS"} + computeOrderValues = []string{"cdrom", "network", "hd"} + computeDataDisksValues = []string{"KEEP", "DETACH", "DESTROY"} + + diskTypeValues = []string{"B", "T", "D"} + + flipgroupClientTypeValues = []string{"compute", "vins"} + + kvmNetTypeValues = []string{"EXTNET", "VINS", "NONE"} + + lbAlgorithmValues = []string{"roundrobin", "static-rr", "leastconn"} + + rgDefNetValues = []string{"PRIVATE", "PUBLIC", "NONE"} + rgNetTypeValues = []string{"PUBLIC", "PRIVATE"} + + vinsTypeValues = []string{"DHCP", "VIP", "EXCLUDE"} imageBootTypeValues = []string{"uefi", "bios"} imageTypeValues = []string{"windows", "linux", "other"} imageDriversValues = []string{"KVM_X86"} imageArchitectureValues = []string{"X86_64", "PPC64_LE"} + + sepFieldTypeValues = []string{"int", "str", "bool", "list", "dict"} ) diff --git a/pkg/cloudapi/account/add_user.go b/pkg/cloudapi/account/add_user.go index 0f87627..00b10fb 100644 --- a/pkg/cloudapi/account/add_user.go +++ b/pkg/cloudapi/account/add_user.go @@ -23,7 +23,7 @@ type AddUserRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype" validate:"required,accountAccessType"` + AccessType string `url:"accesstype" json:"accesstype" validate:"required,accessType"` } // AddUser gives a user access rights. diff --git a/pkg/cloudapi/account/update_user.go b/pkg/cloudapi/account/update_user.go index 5da4cde..ba523d1 100644 --- a/pkg/cloudapi/account/update_user.go +++ b/pkg/cloudapi/account/update_user.go @@ -23,7 +23,7 @@ type UpdateUserRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype" validate:"required,accountAccessType"` + AccessType string `url:"accesstype" json:"accesstype" validate:"required,accessType"` } // UpdateUser updates user access rights diff --git a/pkg/cloudapi/bservice/group_add.go b/pkg/cloudapi/bservice/group_add.go index d830bc8..0df96b4 100644 --- a/pkg/cloudapi/bservice/group_add.go +++ b/pkg/cloudapi/bservice/group_add.go @@ -43,7 +43,7 @@ type GroupAddRequest struct { // - KVM_X86 // - KVM_PPC // Required: true - Driver string `url:"driver" json:"driver" validate:"bserviceDriver"` + Driver string `url:"driver" json:"driver" validate:"driver"` // Storage endpoint provider ID // Required: false diff --git a/pkg/cloudapi/compute/filter.go b/pkg/cloudapi/compute/filter.go index 1402123..881f0c8 100644 --- a/pkg/cloudapi/compute/filter.go +++ b/pkg/cloudapi/compute/filter.go @@ -1,5 +1,13 @@ package compute +import ( + "context" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb" +) + // FilterByID returns ListComputes with specified ID. func (lc ListComputes) FilterByID(id uint64) ListComputes { predicate := func(ic ItemCompute) bool { @@ -36,7 +44,7 @@ func (lc ListComputes) FilterByTechStatus(techStatus string) ListComputes { return lc.FilterFunc(predicate) } -// FilterByDiskID return ListComputes with specified DiskID. +// FilterByDiskID returns ListComputes with specified DiskID. func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes { predicate := func(ic ItemCompute) bool { for _, disk := range ic.Disks { @@ -50,6 +58,88 @@ func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes { return lc.FilterFunc(predicate) } +// FilterByK8SID returns master and worker nodes (ListComputes) inside specified K8S cluster. +func (lc ListComputes) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListComputes, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(ic ItemCompute) bool { + for _, info := range cluster.K8SGroups.Masters.DetailedInfo { + if info.ID == ic.ID { + return true + } + } + + for _, worker := range cluster.K8SGroups.Workers { + for _, info := range worker.DetailedInfo { + if info.ID == ic.ID { + return true + } + } + } + + return false + } + + return lc.FilterFunc(predicate), nil +} + +// K8SMasters is used to filter master nodes. Best used after FilterByK8SID function. +func (lc ListComputes) FilterByK8SMasters() ListComputes { + predicate := func(ic ItemCompute) bool { + for _, rule := range ic.AntiAffinityRules { + if rule.Value == "master" { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// K8SMasters is used to filter worker nodes. Best used after FilterByK8SID function. +func (lc ListComputes) FilterByK8SWorkers() ListComputes { + predicate := func(ic ItemCompute) bool { + for _, rule := range ic.AntiAffinityRules { + if rule.Value == "worker" { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// FilterByLBID returns ListComputes used by specified Load Balancer. +func (lc ListComputes) FilterByLBID(ctx context.Context, lbID uint64, decortClient interfaces.Caller) (ListComputes, error) { + caller := lb.New(decortClient) + + req := lb.GetRequest{ + LBID: lbID, + } + + foundLB, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(ic ItemCompute) bool { + return ic.ID == foundLB.PrimaryNode.ComputeID || ic.ID == foundLB.SecondaryNode.ComputeID + } + + return lc.FilterFunc(predicate), nil +} + // FilterFunc allows filtering ListComputes based on a user-specified predicate. func (lc ListComputes) FilterFunc(predicate func(ItemCompute) bool) ListComputes { var result ListComputes diff --git a/pkg/cloudapi/compute/pfw_add.go b/pkg/cloudapi/compute/pfw_add.go index bc42fdf..d6ce4fc 100644 --- a/pkg/cloudapi/compute/pfw_add.go +++ b/pkg/cloudapi/compute/pfw_add.go @@ -29,7 +29,7 @@ type PFWAddRequest struct { // Network protocol // either "tcp" or "udp" // Required: true - Proto string `url:"proto" json:"proto" validate:"computeProto"` + Proto string `url:"proto" json:"proto" validate:"proto"` } // PFWAdd add port forward rule diff --git a/pkg/cloudapi/compute/user_grant.go b/pkg/cloudapi/compute/user_grant.go index 37152f9..c6894c7 100644 --- a/pkg/cloudapi/compute/user_grant.go +++ b/pkg/cloudapi/compute/user_grant.go @@ -24,7 +24,7 @@ type UserGrantRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype" validate:"accountAccessType"` + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // UserGrant grant user access to the compute diff --git a/pkg/cloudapi/compute/user_update.go b/pkg/cloudapi/compute/user_update.go index 251e995..fee2917 100644 --- a/pkg/cloudapi/compute/user_update.go +++ b/pkg/cloudapi/compute/user_update.go @@ -24,7 +24,7 @@ type UserUpdateRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype" validate:"accountAccessType"` + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // UserUpdate updates user access to the compute diff --git a/pkg/cloudapi/disks/create.go b/pkg/cloudapi/disks/create.go index 2831273..28e1d40 100644 --- a/pkg/cloudapi/disks/create.go +++ b/pkg/cloudapi/disks/create.go @@ -2,7 +2,6 @@ package disks import ( "context" - "errors" "net/http" "strconv" @@ -13,15 +12,15 @@ import ( type CreateRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // ID of the grid (platform) // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Name of disk // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Description of disk // Required: false @@ -36,7 +35,7 @@ type CreateRequest struct { // - D=Data // - T=Temp // Required: true - Type string `url:"type" json:"type"` + Type string `url:"type" json:"type" validate:"diskType"` // Size in GB default is 0 // Required: false @@ -55,29 +54,13 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` } -func (drq CreateRequest) validate() error { - if drq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if drq.GID == 0 { - return errors.New("validation-error: field GID can not be empty or equal to 0") - } - if drq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - validType := validators.StringInSlice(drq.Type, []string{"B", "D", "T"}) - if !validType { - return errors.New("validation-error: field Type must be set as B, D or T") - } - - return nil -} - // Create creates a disk func (d Disks) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/create" diff --git a/pkg/cloudapi/disks/delete.go b/pkg/cloudapi/disks/delete.go index 446c1f5..ae2fbbd 100644 --- a/pkg/cloudapi/disks/delete.go +++ b/pkg/cloudapi/disks/delete.go @@ -2,16 +2,17 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request for delete disk type DeleteRequest struct { // ID of disk to delete // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Detach disk from machine first // Required: false @@ -26,19 +27,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (d DeleteRequest) validate() error { - if d.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // Delete deletes disk by ID func (d Disks) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/delete" diff --git a/pkg/cloudapi/disks/delete_disks.go b/pkg/cloudapi/disks/delete_disks.go index 72e2c3f..cca44b7 100644 --- a/pkg/cloudapi/disks/delete_disks.go +++ b/pkg/cloudapi/disks/delete_disks.go @@ -2,42 +2,34 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for multiple disks type DisksDeleteRequest struct { // List of disk ids to delete // Required: true - DisksIDs []uint64 `url:"diskIds" json:"diskIds"` + DisksIDs []uint64 `url:"diskIds" json:"diskIds" validate:"required"` // Reason for deleting the disks // Required: true - Reason string `url:"reason" json:"reason"` + Reason string `url:"reason" json:"reason" validate:"required"` // Whether to completely delete the disks, works only with non attached disks // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (drq DisksDeleteRequest) validate() error { - if len(drq.DisksIDs) == 0 { - return errors.New("validation-error: field DisksIDs must include one or more disks ids") - } - if drq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil -} - // DeleteDisks deletes multiple disks permanently func (d Disks) DeleteDisks(ctx context.Context, req DisksDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/deleteDisks" diff --git a/pkg/cloudapi/disks/filter.go b/pkg/cloudapi/disks/filter.go index 73188ef..3a69c4b 100644 --- a/pkg/cloudapi/disks/filter.go +++ b/pkg/cloudapi/disks/filter.go @@ -1,5 +1,14 @@ package disks +import ( + "context" + "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb" +) + // FilterByID returns ListDisks with specified ID. func (ld ListDisks) FilterByID(id uint64) ListDisks { predicate := func(idisk ItemDisk) bool { @@ -36,6 +45,69 @@ func (ld ListDisks) FilterByTechStatus(techStatus string) ListDisks { return ld.FilterFunc(predicate) } +// FilterByComputeID is used to filter ListDisks attached to specified compute. +func (ld ListDisks) FilterByComputeID(computeID uint64) ListDisks { + predicate := func(idisk ItemDisk) bool { + for k := range idisk.Computes { + if k == strconv.FormatUint(computeID, 10) { + return true + } + } + + return false + } + + return ld.FilterFunc(predicate) +} + +// FilterByK8SID is used to filter ListDisks by specified K8S cluster. +func (ld ListDisks) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListDisks, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + var result ListDisks + + for _, masterCompute := range cluster.K8SGroups.Masters.DetailedInfo { + result = append(result, ld.FilterByComputeID(masterCompute.ID)...) + } + + for _, workerGroup := range cluster.K8SGroups.Workers { + for _, workerCompute := range workerGroup.DetailedInfo { + result = append(result, ld.FilterByComputeID(workerCompute.ID)...) + } + } + + return result, nil +} + +// FilterByLBID is used to filter ListDisks used by computes inside specified Load Balancer. +func (ld ListDisks) FilterByLBID(ctx context.Context, lbID uint64, decortClient interfaces.Caller) (ListDisks, error) { + caller := lb.New(decortClient) + + req := lb.GetRequest{ + LBID: lbID, + } + + foundLB, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + var result ListDisks + result = append(result, ld.FilterByComputeID(foundLB.PrimaryNode.ComputeID)...) + result = append(result, ld.FilterByComputeID(foundLB.SecondaryNode.ComputeID)...) + + return result, nil +} + // FilterFunc allows filtering ListDisks based on a user-specified predicate. func (ld ListDisks) FilterFunc(predicate func(ItemDisk) bool) ListDisks { var result ListDisks diff --git a/pkg/cloudapi/disks/get.go b/pkg/cloudapi/disks/get.go index b4a1ae5..622175e 100644 --- a/pkg/cloudapi/disks/get.go +++ b/pkg/cloudapi/disks/get.go @@ -3,31 +3,26 @@ package disks import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about disk type GetRequest struct { // ID of the disk // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq GetRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Get gets disk details // Notice: the devicename field is the name as it is passed to the kernel (kname in linux) for unattached disks this field has no relevant value func (d Disks) Get(ctx context.Context, req GetRequest) (*RecordDisk, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/get" diff --git a/pkg/cloudapi/disks/limitio.go b/pkg/cloudapi/disks/limitio.go index eaeb4f3..1130032 100644 --- a/pkg/cloudapi/disks/limitio.go +++ b/pkg/cloudapi/disks/limitio.go @@ -2,16 +2,17 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for limit IO type LimitIORequest struct { // ID of the disk to limit // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Alias for total_iops_sec for backwards compatibility // Required: false @@ -70,21 +71,15 @@ type LimitIORequest struct { SizeIOPSSec uint64 `url:"size_iops_sec,omitempty" json:"size_iops_sec,omitempty"` } -func (drq LimitIORequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil -} - // LimitIO limit IO for a certain disk // total and read/write options are not allowed to be combined // see http://libvirt.org/formatdomain.html#elementsDisks iotune section for more details func (d Disks) LimitIO(ctx context.Context, req LimitIORequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/limitIO" diff --git a/pkg/cloudapi/disks/list_types.go b/pkg/cloudapi/disks/list_types.go index 7c03a50..71c4819 100644 --- a/pkg/cloudapi/disks/list_types.go +++ b/pkg/cloudapi/disks/list_types.go @@ -10,7 +10,7 @@ import ( type ListTypesRequest struct { // Show detailed disk types by seps // Required: false - Detailed bool `url:"detailed" json:"detailed"` + Detailed bool `url:"detailed,omitempty" json:"detailed,omitempty"` } // ListTypes gets list defined disk types diff --git a/pkg/cloudapi/disks/rename.go b/pkg/cloudapi/disks/rename.go index 9dd0881..45c309c 100644 --- a/pkg/cloudapi/disks/rename.go +++ b/pkg/cloudapi/disks/rename.go @@ -2,38 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rename disk type RenameRequest struct { // ID of the disk to rename // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New name of disk // Required: true - Name string `url:"name" json:"name"` -} - -func (drq RenameRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if drq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - - return nil + Name string `url:"name" json:"name" validate:"required"` } // Rename rename disk func (d Disks) Rename(ctx context.Context, req RenameRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/rename" diff --git a/pkg/cloudapi/disks/resize.go b/pkg/cloudapi/disks/resize.go index c800c1b..34a9fbe 100644 --- a/pkg/cloudapi/disks/resize.go +++ b/pkg/cloudapi/disks/resize.go @@ -2,31 +2,21 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resize disk type ResizeRequest struct { // ID of the disk to resize // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New size of the disk in GB // Required: true - Size uint64 `url:"size" json:"size"` -} - -func (drq ResizeRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if drq.Size == 0 { - return errors.New("validation-error: field Size can not be empty or equal to 0") - } - - return nil + Size uint64 `url:"size" json:"size" validate:"required"` } // Resize resize disk @@ -34,9 +24,11 @@ func (drq ResizeRequest) validate() error { // in that case please stop and start your machine after changing the disk size, for your changes to be reflected. // This method will not be used for disks, assigned to computes. Only unassigned disks and disks, assigned with "old" virtual machines. func (d Disks) Resize(ctx context.Context, req ResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/resize" @@ -59,9 +51,11 @@ func (d Disks) Resize(ctx context.Context, req ResizeRequest) (bool, error) { // in that case please stop and start your machine after changing the disk size, for your changes to be reflected. // This method will not be used for disks, assigned to "old" virtual machines. Only unassigned disks and disks, assigned with computes. func (d Disks) Resize2(ctx context.Context, req ResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/resize2" diff --git a/pkg/cloudapi/disks/restore.go b/pkg/cloudapi/disks/restore.go index 3f0cb3e..d96abb9 100644 --- a/pkg/cloudapi/disks/restore.go +++ b/pkg/cloudapi/disks/restore.go @@ -2,38 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore a deleted unattached disk type RestoreRequest struct { // ID of the disk to restore // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Reason for restoring the disk // Required: true - Reason string `url:"reason" json:"reason"` -} - -func (drq RestoreRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if drq.Reason == "" { - return errors.New("validation-error: field Reason can not be empty") - } - - return nil + Reason string `url:"reason" json:"reason" validate:"required"` } // Restore restore a deleted unattached disk from recycle bin func (d Disks) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/restore" diff --git a/pkg/cloudapi/disks/share.go b/pkg/cloudapi/disks/share.go index 9f8e7e5..ab74db7 100644 --- a/pkg/cloudapi/disks/share.go +++ b/pkg/cloudapi/disks/share.go @@ -2,31 +2,26 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for share data disk type ShareRequest struct { // ID of the disk to share // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq ShareRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Share shares data disk func (d Disks) Share(ctx context.Context, req ShareRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/share" diff --git a/pkg/cloudapi/disks/snapshot_delete.go b/pkg/cloudapi/disks/snapshot_delete.go index 7b49e48..b6d867d 100644 --- a/pkg/cloudapi/disks/snapshot_delete.go +++ b/pkg/cloudapi/disks/snapshot_delete.go @@ -2,39 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete snapshot type SnapshotDeleteRequest struct { // ID of disk to delete // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Label of the snapshot to delete - // Required: false - Label string `url:"label" json:"label"` -} - -func (drq SnapshotDeleteRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - if drq.Label == "" { - return errors.New("validation-error: field Label can not be empty") - } - - return nil + // Required: true + Label string `url:"label" json:"label" validate:"required"` } // SnapshotDelete deletes a snapshot func (d Disks) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/snapshotDelete" diff --git a/pkg/cloudapi/disks/snapshot_rollback.go b/pkg/cloudapi/disks/snapshot_rollback.go index 98765f9..8838bd8 100644 --- a/pkg/cloudapi/disks/snapshot_rollback.go +++ b/pkg/cloudapi/disks/snapshot_rollback.go @@ -2,42 +2,34 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rollback snapshot type SnapshotRollbackRequest struct { // ID of the disk // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Label of the snapshot to rollback - // Required: true - Label string `url:"label" json:"label"` + // Required: false + Label string `url:"label,omitempty" json:"label,omitempty"` // Timestamp of the snapshot to rollback - // Required: true - TimeStamp uint64 `url:"timestamp" json:"timestamp"` -} - -func (drq SnapshotRollbackRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - if drq.Label == "" && drq.TimeStamp == 0 { - return errors.New("validation-error: field Label or field TimeStamp can not be empty") - } - - return nil + // Required: false + TimeStamp uint64 `url:"timestamp,omitempty" json:"timestamp,omitempty"` } // SnapshotRollback rollback an individual disk snapshot func (d Disks) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/snapshotRollback" diff --git a/pkg/cloudapi/disks/unshare.go b/pkg/cloudapi/disks/unshare.go index c306a2b..18f48d6 100644 --- a/pkg/cloudapi/disks/unshare.go +++ b/pkg/cloudapi/disks/unshare.go @@ -2,31 +2,26 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unshare data disk type UnshareRequest struct { // ID of the disk to unshare // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq UnshareRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Unshare unshares data disk func (d Disks) Unshare(ctx context.Context, req UnshareRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/disks/unshare" diff --git a/pkg/cloudapi/extnet/get.go b/pkg/cloudapi/extnet/get.go index d49cd02..4da9d71 100644 --- a/pkg/cloudapi/extnet/get.go +++ b/pkg/cloudapi/extnet/get.go @@ -3,30 +3,25 @@ package extnet import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about external network type GetRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq GetRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID can not be empty or equal to 0") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // Get gets detailed information about external network func (e ExtNet) Get(ctx context.Context, req GetRequest) (*RecordExtNet, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/extnet/get" diff --git a/pkg/cloudapi/extnet/list_computes.go b/pkg/cloudapi/extnet/list_computes.go index 4ce2e6f..ea25e58 100644 --- a/pkg/cloudapi/extnet/list_computes.go +++ b/pkg/cloudapi/extnet/list_computes.go @@ -3,30 +3,25 @@ package extnet import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list computes type ListComputesRequest struct { // Filter by account ID // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (erq ListComputesRequest) validate() error { - if erq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListComputes gets computes from account with extnets func (e ExtNet) ListComputes(ctx context.Context, req ListComputesRequest) (ListExtNetComputes, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/extnet/listComputes" diff --git a/pkg/cloudapi/flipgroup/compute_add.go b/pkg/cloudapi/flipgroup/compute_add.go index 000f207..59cdf64 100644 --- a/pkg/cloudapi/flipgroup/compute_add.go +++ b/pkg/cloudapi/flipgroup/compute_add.go @@ -2,38 +2,30 @@ package flipgroup import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add compute instance type ComputeAddRequest struct { // ID of the Floating IP group to add compute instance to // Required: true - FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId"` + FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId" validate:"required"` // ID of the compute instance to add to this group // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (frq ComputeAddRequest) validate() error { - if frq.FLIPGroupID == 0 { - return errors.New("field FLIPGroupID can not be empty or equal to 0") - } - if frq.ComputeID == 0 { - return errors.New("field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ComputeAdd add compute instance to the Floating IP group func (f FLIPGroup) ComputeAdd(ctx context.Context, req ComputeAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/computeAdd" diff --git a/pkg/cloudapi/flipgroup/compute_remove.go b/pkg/cloudapi/flipgroup/compute_remove.go index 233ddd2..94c8b79 100644 --- a/pkg/cloudapi/flipgroup/compute_remove.go +++ b/pkg/cloudapi/flipgroup/compute_remove.go @@ -2,38 +2,30 @@ package flipgroup import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove compute instance type ComputeRemoveRequest struct { // ID of the Floating IP group to remove compute instance from // Required: true - FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId"` + FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId" validate:"required"` // ID of the compute instance to remove // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (frq ComputeRemoveRequest) validate() error { - if frq.FLIPGroupID == 0 { - return errors.New("field FLIPGroupID can not be empty or equal to 0") - } - if frq.ComputeID == 0 { - return errors.New("field ComputeID can not be empty or equal to 0") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ComputeRemove remove compute instance from the Floating IP group func (f FLIPGroup) ComputeRemove(ctx context.Context, req ComputeRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/computeRemove" diff --git a/pkg/cloudapi/flipgroup/create.go b/pkg/cloudapi/flipgroup/create.go index 47f72e0..3ca683f 100644 --- a/pkg/cloudapi/flipgroup/create.go +++ b/pkg/cloudapi/flipgroup/create.go @@ -3,7 +3,6 @@ package flipgroup import ( "context" "encoding/json" - "errors" "net/http" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -13,28 +12,28 @@ import ( type CreateRequest struct { // Account ID // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // FLIPGroup name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Network type // Should be one of: // - EXTNET // - VINS // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"computeNetType"` // ID of external network or VINS // Required: true - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId" json:"netId" validate:"required"` // Type of client // - 'compute' // - 'vins' (will be later) // Required: true - ClientType string `url:"clientType" json:"clientType"` + ClientType string `url:"clientType" json:"clientType" validate:"flipgroupClientType"` // IP address to associate with this group. If empty, the platform will autoselect IP address // Required: false @@ -45,33 +44,13 @@ type CreateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (frq CreateRequest) validate() error { - if frq.AccountID == 0 { - return errors.New("field AccountID can not be empty or equal to 0") - } - if frq.NetID == 0 { - return errors.New("field NetID can not be empty or equal to 0") - } - if frq.Name == "" { - return errors.New("field Name can not be empty") - } - validator := validators.StringInSlice(frq.NetType, []string{"EXTNET", "VINS"}) - if !validator { - return errors.New("field Name can be only EXTNET or VINS") - } - validator = validators.StringInSlice(frq.ClientType, []string{"compute", "node"}) - if !validator { - return errors.New("field Name can be only compute or node") - } - - return nil -} - // Create method will create a new FLIPGorup in the specified Account func (f FLIPGroup) Create(ctx context.Context, req CreateRequest) (*RecordFLIPGroup, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/create" diff --git a/pkg/cloudapi/flipgroup/delete.go b/pkg/cloudapi/flipgroup/delete.go index d96fe82..a1e2cc0 100644 --- a/pkg/cloudapi/flipgroup/delete.go +++ b/pkg/cloudapi/flipgroup/delete.go @@ -2,31 +2,26 @@ package flipgroup import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete FLIPGroup type DeleteRequest struct { // FLIPGroup ID // Required: true - FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId"` -} - -func (frq DeleteRequest) validate() error { - if frq.FLIPGroupID == 0 { - return errors.New("field FLIPGroupID can not be empty or equal to 0") - } - - return nil + FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId" validate:"required"` } // Delete method wil delete Floating IP group func (f FLIPGroup) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/delete" diff --git a/pkg/cloudapi/flipgroup/edit.go b/pkg/cloudapi/flipgroup/edit.go index c4bd911..f24c91a 100644 --- a/pkg/cloudapi/flipgroup/edit.go +++ b/pkg/cloudapi/flipgroup/edit.go @@ -2,39 +2,34 @@ package flipgroup import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for edit FLIPGroup type EditRequest struct { // FLIPGroup ID // Required: true - FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId"` + FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId" validate:"required"` // FLIPGroup name - // Required: true + // Required: false Name string `url:"name,omitempty" json:"name,omitempty"` // FLIPGroup description - // Required: true + // Required: false Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (frq EditRequest) validate() error { - if frq.FLIPGroupID == 0 { - return errors.New("field FLIPGroupID can not be empty or equal to 0") - } - - return nil -} - // Edit edits FLIPGroup fields func (f FLIPGroup) Edit(ctx context.Context, req EditRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/edit" diff --git a/pkg/cloudapi/flipgroup/get.go b/pkg/cloudapi/flipgroup/get.go index 9b5b051..cba029d 100644 --- a/pkg/cloudapi/flipgroup/get.go +++ b/pkg/cloudapi/flipgroup/get.go @@ -3,30 +3,25 @@ package flipgroup import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about FLIPGroup type GetRequest struct { // FLIPGroup ID // Required: true - FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId"` -} - -func (frq GetRequest) validate() error { - if frq.FLIPGroupID == 0 { - return errors.New("field FLIPGroupID can not be empty or equal to 0") - } - - return nil + FLIPGroupID uint64 `url:"flipgroupId" json:"flipgroupId" validate:"required"` } // Get gets details of the specified Floating IP group func (f FLIPGroup) Get(ctx context.Context, req GetRequest) (*ItemFLIPGroup, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/flipgroup/get" diff --git a/pkg/cloudapi/image/create.go b/pkg/cloudapi/image/create.go index 9b4e02b..1ebe8bc 100644 --- a/pkg/cloudapi/image/create.go +++ b/pkg/cloudapi/image/create.go @@ -24,7 +24,7 @@ type CreateRequest struct { // Boot type of image bios or UEFI // Required: true - BootType string `url:"boottype" json:"boottype" validate:"required,bootType"` + BootType string `url:"boottype" json:"boottype" validate:"required,imageBootType"` // Image type // Should be: diff --git a/pkg/cloudapi/k8ci/get.go b/pkg/cloudapi/k8ci/get.go index 8f53090..a578529 100644 --- a/pkg/cloudapi/k8ci/get.go +++ b/pkg/cloudapi/k8ci/get.go @@ -3,29 +3,25 @@ package k8ci import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about K8CI type GetRequest struct { // ID of the K8 catalog item to get // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` -} - -func (krq GetRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("field K8CIID can not be empty or equal to 0") - } - - return nil + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` } // Get gets details of the specified K8 catalog item func (k K8CI) Get(ctx context.Context, req GetRequest) (*RecordK8CI, error) { - if err := req.validate(); err != nil { - return nil, err + err := validators.ValidateRequest(req) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/k8ci/get" diff --git a/pkg/cloudapi/k8s/create.go b/pkg/cloudapi/k8s/create.go index 56fddda..5d41f3f 100644 --- a/pkg/cloudapi/k8s/create.go +++ b/pkg/cloudapi/k8s/create.go @@ -2,28 +2,29 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create kubernetes cluster type CreateRequest struct { // Name of Kubernetes cluster // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Resource Group ID for cluster placement // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // ID of Kubernetes catalog item (k8sci) for cluster // Required: true - K8SCIID uint64 `url:"k8ciId" json:"k8ciId"` + K8SCIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` // Name for first worker group created with cluster // Required: true - WorkerGroupName string `url:"workerGroupName" json:"workerGroupName"` + WorkerGroupName string `url:"workerGroupName" json:"workerGroupName" validate:"required"` // ID of SEP to create boot disks for master nodes. Uses images SEP ID if not set // Required: false @@ -102,28 +103,13 @@ type CreateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if krq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if krq.K8SCIID == 0 { - return errors.New("validation-error: field K8SCIID can not be empty or equal to 0") - } - if krq.WorkerGroupName == "" { - return errors.New("validation-error: field WorkerGroupName can not be empty") - } - - return nil -} - // Create creates a new Kubernetes cluster in the specified Resource Group func (k8s K8S) Create(ctx context.Context, req CreateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/create" diff --git a/pkg/cloudapi/k8s/delete.go b/pkg/cloudapi/k8s/delete.go index f0e8c8f..5a5a71c 100644 --- a/pkg/cloudapi/k8s/delete.go +++ b/pkg/cloudapi/k8s/delete.go @@ -2,36 +2,31 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete kubernetes cluster type DeleteRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // True if cluster is destroyed permanently. // Otherwise it can be restored from Recycle Bin // Required: true - Permanently bool `url:"permanently" json:"permanently"` -} - -func (krq DeleteRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + Permanently bool `url:"permanently" json:"permanently" validate:"required"` } // Delete deletes kubernetes cluster func (k8s K8S) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/delete" diff --git a/pkg/cloudapi/k8s/delete_master_from_group.go b/pkg/cloudapi/k8s/delete_master_from_group.go index 111ccd9..b23b231 100644 --- a/pkg/cloudapi/k8s/delete_master_from_group.go +++ b/pkg/cloudapi/k8s/delete_master_from_group.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete master from group type DeleteMasterFromGroupRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the masters compute group // Required: true - MasterGroupID uint64 `url:"masterGroupId" json:"masterGroupId"` + MasterGroupID uint64 `url:"masterGroupId" json:"masterGroupId" validate:"required"` // List of Compute IDs of master nodes to delete // Required: true - MasterIDs []string `url:"masterIds" json:"masterIds"` -} - -func (krq DeleteMasterFromGroupRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.MasterGroupID == 0 { - return errors.New("validation-error: field MasterGroupID can not be empty or equal to 0") - } - if len(krq.MasterIDs) == 0 { - return errors.New("validation-error: field MasterIDs can not be empty") - } - - return nil + MasterIDs []string `url:"masterIds" json:"masterIds" validate:"min=1"` } // DeleteMasterFromGroup deletes compute from masters group in selected Kubernetes cluster func (k8s K8S) DeleteMasterFromGroup(ctx context.Context, req DeleteMasterFromGroupRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/deleteMasterFromGroup" diff --git a/pkg/cloudapi/k8s/delete_worker_from_group.go b/pkg/cloudapi/k8s/delete_worker_from_group.go index e49b46b..748d26a 100644 --- a/pkg/cloudapi/k8s/delete_worker_from_group.go +++ b/pkg/cloudapi/k8s/delete_worker_from_group.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete worker from group type DeleteWorkerFromGroupRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to delete // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq DeleteWorkerFromGroupRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID can not be empty or equal to 0") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerID can not be empty or equal to 0") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // DeleteWorkerFromGroup deletes worker compute from workers group in selected Kubernetes cluster func (k8s K8S) DeleteWorkerFromGroup(ctx context.Context, req DeleteWorkerFromGroupRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/deleteWorkerFromGroup" diff --git a/pkg/cloudapi/k8s/disable_enable.go b/pkg/cloudapi/k8s/disable_enable.go index 9bf7c96..9d5dbf0 100644 --- a/pkg/cloudapi/k8s/disable_enable.go +++ b/pkg/cloudapi/k8s/disable_enable.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable/enable kubernetes cluster type DisabelEnableRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq DisabelEnableRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Disable disables kubernetes cluster by ID func (k8s K8S) Disable(ctx context.Context, req DisabelEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/disable" @@ -46,9 +41,11 @@ func (k8s K8S) Disable(ctx context.Context, req DisabelEnableRequest) (bool, err // Enable enables kubernetes cluster by ID func (k8s K8S) Enable(ctx context.Context, req DisabelEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/enable" diff --git a/pkg/cloudapi/k8s/find_group_by_label.go b/pkg/cloudapi/k8s/find_group_by_label.go index c4efe9b..2eaa838 100644 --- a/pkg/cloudapi/k8s/find_group_by_label.go +++ b/pkg/cloudapi/k8s/find_group_by_label.go @@ -3,42 +3,34 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about group of kubernetes cluster type FindGroupByLabelRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // List of labels to search // Required: true - Labels []string `url:"labels" json:"labels"` + Labels []string `url:"labels" json:"labels" validate:"min=1"` // If true and more than one label provided, select only groups that have all provided labels. // If false - groups that have at least one label - // Required: true - Strict bool `url:"strict" json:"strict"` -} - -func (krq FindGroupByLabelRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if len(krq.Labels) == 0 { - return errors.New("validation-error: field Labels can not be empty") - } - - return nil + // Required: false + Strict bool `url:"strict,omitempty" json:"strict,omitempty"` } // FindGroupByLabel find worker group information by one on more labels func (k8s K8S) FindGroupByLabel(ctx context.Context, req FindGroupByLabelRequest) (ListK8SGroups, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/findGroupByLabel" diff --git a/pkg/cloudapi/k8s/get.go b/pkg/cloudapi/k8s/get.go index 239ca9f..6545b3c 100644 --- a/pkg/cloudapi/k8s/get.go +++ b/pkg/cloudapi/k8s/get.go @@ -3,30 +3,25 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about kubernetes cluster type GetRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq GetRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Get gets information about Kubernetes cluster func (k8s K8S) Get(ctx context.Context, req GetRequest) (*RecordK8S, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/get" diff --git a/pkg/cloudapi/k8s/get_config.go b/pkg/cloudapi/k8s/get_config.go index 9c0d987..79a7c56 100644 --- a/pkg/cloudapi/k8s/get_config.go +++ b/pkg/cloudapi/k8s/get_config.go @@ -2,30 +2,25 @@ package k8s import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get configuration of kubernetes cluster type GetConfigRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq GetConfigRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // GetConfig gets configuration data to access Kubernetes cluster func (k8s K8S) GetConfig(ctx context.Context, req GetConfigRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/getConfig" diff --git a/pkg/cloudapi/k8s/get_node_annotations.go b/pkg/cloudapi/k8s/get_node_annotations.go index 51659c2..be1eb49 100644 --- a/pkg/cloudapi/k8s/get_node_annotations.go +++ b/pkg/cloudapi/k8s/get_node_annotations.go @@ -2,37 +2,29 @@ package k8s import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get node annotations type GetNodeAnnotationsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Node ID // Required: true - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeAnnotationsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeAnnotations gets kubernetes cluster worker node annotations func (k8s K8S) GetNodeAnnotations(ctx context.Context, req GetNodeAnnotationsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/getNodeAnnotations" diff --git a/pkg/cloudapi/k8s/get_node_labels.go b/pkg/cloudapi/k8s/get_node_labels.go index 423c962..3d5773a 100644 --- a/pkg/cloudapi/k8s/get_node_labels.go +++ b/pkg/cloudapi/k8s/get_node_labels.go @@ -2,37 +2,29 @@ package k8s import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get node labels type GetNodeLabelsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Node ID - // Required: false - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeLabelsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + // Required: true + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeLabels gets kubernetes cluster worker node labels func (k8s K8S) GetNodeLabels(ctx context.Context, req GetNodeLabelsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/getNodeLabels" diff --git a/pkg/cloudapi/k8s/get_node_taints.go b/pkg/cloudapi/k8s/get_node_taints.go index 780c249..34dfdc2 100644 --- a/pkg/cloudapi/k8s/get_node_taints.go +++ b/pkg/cloudapi/k8s/get_node_taints.go @@ -2,37 +2,29 @@ package k8s import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get node taints type GetNodeTaintsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Node ID - // Required: false - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeTaintsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + // Required: true + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeTaints gets kubernetes cluster worker node taints func (k8s K8S) GetNodeTaints(ctx context.Context, req GetNodeTaintsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/getNodeTaints" diff --git a/pkg/cloudapi/k8s/restore.go b/pkg/cloudapi/k8s/restore.go index cf52a8e..75fc3d4 100644 --- a/pkg/cloudapi/k8s/restore.go +++ b/pkg/cloudapi/k8s/restore.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore kubernetes cluster type RestoreRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq RestoreRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Restore restore kubernetes cluster from Recycle Bin func (k8s K8S) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/restore" diff --git a/pkg/cloudapi/k8s/start.go b/pkg/cloudapi/k8s/start.go index 8c33dca..a35add7 100644 --- a/pkg/cloudapi/k8s/start.go +++ b/pkg/cloudapi/k8s/start.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start kubernetes cluster type StartRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq StartRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Start starts kubernetes cluster by ID func (k8s K8S) Start(ctx context.Context, req StartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/start" diff --git a/pkg/cloudapi/k8s/stop.go b/pkg/cloudapi/k8s/stop.go index 6ecdf97..07e50e7 100644 --- a/pkg/cloudapi/k8s/stop.go +++ b/pkg/cloudapi/k8s/stop.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop kubernetes cluster type StopRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq StopRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Stop stops kubernetes cluster by ID func (k8s K8S) Stop(ctx context.Context, req StopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/stop" diff --git a/pkg/cloudapi/k8s/update.go b/pkg/cloudapi/k8s/update.go index 49e15fa..588856b 100644 --- a/pkg/cloudapi/k8s/update.go +++ b/pkg/cloudapi/k8s/update.go @@ -2,16 +2,17 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update kubernetes cluster type UpdateRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // New name to set. // If empty string is passed, name is not updated @@ -24,19 +25,13 @@ type UpdateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq UpdateRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - - return nil -} - // Update updates name or description of Kubernetes cluster func (k8s K8S) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/update" diff --git a/pkg/cloudapi/k8s/worker_add.go b/pkg/cloudapi/k8s/worker_add.go index 1442c78..bc465f5 100644 --- a/pkg/cloudapi/k8s/worker_add.go +++ b/pkg/cloudapi/k8s/worker_add.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add worker to a kubernetes cluster type WorkerAddRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // How many worker nodes to add // Required: true - Num uint64 `url:"num" json:"num"` -} - -func (krq WorkerAddRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID can not be empty or equal to 0") - } - if krq.Num == 0 { - return errors.New("validation-error: field Num can not be empty or equal to 0") - } - - return nil + Num uint64 `url:"num" json:"num" validate:"required"` } // WorkerAdd add worker nodes to a Kubernetes cluster func (k8s K8S) WorkerAdd(ctx context.Context, req WorkerAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workerAdd" diff --git a/pkg/cloudapi/k8s/worker_reset.go b/pkg/cloudapi/k8s/worker_reset.go index cfcbe42..24d7bc5 100644 --- a/pkg/cloudapi/k8s/worker_reset.go +++ b/pkg/cloudapi/k8s/worker_reset.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for hard reset kubernetes cluster type WorkerResetRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to reset // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq WorkerResetRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID can not be empty or equal to 0") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerID can not be empty or equal to 0") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // WorkerReset hard reset (compute start + stop) worker node of the Kubernetes cluster func (k8s K8S) WorkerReset(ctx context.Context, req WorkerResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workerReset" diff --git a/pkg/cloudapi/k8s/worker_restart.go b/pkg/cloudapi/k8s/worker_restart.go index 65e423c..86aa205 100644 --- a/pkg/cloudapi/k8s/worker_restart.go +++ b/pkg/cloudapi/k8s/worker_restart.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart worker node type WorkerRestartRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to restart // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq WorkerRestartRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID can not be empty or equal to 0") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerID can not be empty or equal to 0") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // WorkerRestart soft restart (reboot OS) worker node of the Kubernetes cluster func (k8s K8S) WorkerRestart(ctx context.Context, req WorkerRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workerRestart" diff --git a/pkg/cloudapi/k8s/workers_group_add.go b/pkg/cloudapi/k8s/workers_group_add.go index 6ca7a59..9c561ac 100644 --- a/pkg/cloudapi/k8s/workers_group_add.go +++ b/pkg/cloudapi/k8s/workers_group_add.go @@ -2,20 +2,21 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add workers group type WorkersGroupAddRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of SEP to create boot disks for default worker nodes group. Uses images SEP ID if not set // Required: false @@ -57,21 +58,13 @@ type WorkersGroupAddRequest struct { WorkerDisk uint64 `url:"workerDisk,omitempty" json:"workerDisk,omitempty"` } -func (krq WorkersGroupAddRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - return nil -} - // WorkersGroupAdd adds workers group to Kubernetes cluster func (k8s K8S) WorkersGroupAdd(ctx context.Context, req WorkersGroupAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workersGroupAdd" diff --git a/pkg/cloudapi/k8s/workers_group_delete.go b/pkg/cloudapi/k8s/workers_group_delete.go index bd02bd9..2de143d 100644 --- a/pkg/cloudapi/k8s/workers_group_delete.go +++ b/pkg/cloudapi/k8s/workers_group_delete.go @@ -2,38 +2,30 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete workers group type WorkersGroupDeleteRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group ID // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` -} - -func (krq WorkersGroupDeleteRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID can not be empty or equal to 0") - } - - return nil + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` } // WorkersGroupDelete deletes workers group from Kubernetes cluster func (k8s K8S) WorkersGroupDelete(ctx context.Context, req WorkersGroupDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workersGroupDelete" diff --git a/pkg/cloudapi/k8s/workers_group_get_by_name.go b/pkg/cloudapi/k8s/workers_group_get_by_name.go index d220555..2320d67 100644 --- a/pkg/cloudapi/k8s/workers_group_get_by_name.go +++ b/pkg/cloudapi/k8s/workers_group_get_by_name.go @@ -3,37 +3,29 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about worker group type WorkersGroupGetByNameRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group name // Required: true - GroupName string `url:"groupName" json:"groupName"` -} - -func (krq WorkersGroupGetByNameRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.GroupName == "" { - return errors.New("validation-error: field WorkersGroupID can not be empty") - } - - return nil + GroupName string `url:"groupName" json:"groupName" validate:"required"` } // WorkersGroupGetByName gets worker group metadata by name func (k8s K8S) WorkersGroupGetByName(ctx context.Context, req WorkersGroupGetByNameRequest) (*RecordK8SGroups, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/k8s/workersGroupGetByName" diff --git a/pkg/cloudapi/kvmppc/create.go b/pkg/cloudapi/kvmppc/create.go index 9500bbe..8a5ae8d 100644 --- a/pkg/cloudapi/kvmppc/create.go +++ b/pkg/cloudapi/kvmppc/create.go @@ -2,34 +2,35 @@ package kvmppc import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM PowerPC VM type CreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // ID of the OS image to base this VM on; // Could be boot disk image or CD-ROM image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB // Required: false @@ -50,7 +51,7 @@ type CreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -84,31 +85,13 @@ type CreateRequest struct { IPAType string `url:"ipaType,omitempty" json:"ipaType,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if krq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU can not be empty or equal to 0") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM can not be empty or equal to 0") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID can not be empty or equal to 0") - } - - return nil -} - // Create creates KVM PowerPC VM based on specified OS image func (k KVMPPC) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/kvmppc/create" diff --git a/pkg/cloudapi/kvmppc/create_blank.go b/pkg/cloudapi/kvmppc/create_blank.go index 95e3d72..db17493 100644 --- a/pkg/cloudapi/kvmppc/create_blank.go +++ b/pkg/cloudapi/kvmppc/create_blank.go @@ -2,42 +2,43 @@ package kvmppc import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM PowerPC VM from scratch type CreateBlankRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Size of the boot disk in GB // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + BootDisk uint64 `url:"bootDisk" json:"bootDisk" validate:"required"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + SEPID uint64 `url:"sepId" json:"sepId" validate:"required"` // Pool to use if SEP ID is set, can be also empty if needed to be chosen by system // Required: true - Pool string `url:"pool" json:"pool"` + Pool string `url:"pool" json:"pool" validate:"required"` // Network type // Should be one of: @@ -45,7 +46,7 @@ type CreateBlankRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -63,37 +64,13 @@ type CreateBlankRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateBlankRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if krq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU can not be empty or equal to 0") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM can not be empty or equal to 0") - } - if krq.BootDisk == 0 { - return errors.New("validation-error: field BootDisk can not be empty or equal to 0") - } - if krq.SEPID == 0 { - return errors.New("validation-error: field SepID can not be empty or equal to 0") - } - if krq.Pool == "" { - return errors.New("validation-error: field Pool can not be empty") - } - - return nil -} - // CreateBlank creates KVM PowerPC VM from scratch func (k KVMPPC) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/kvmppc/createBlank" diff --git a/pkg/cloudapi/kvmx86/create.go b/pkg/cloudapi/kvmx86/create.go index 42d2c3f..be706a1 100644 --- a/pkg/cloudapi/kvmx86/create.go +++ b/pkg/cloudapi/kvmx86/create.go @@ -2,34 +2,35 @@ package kvmx86 import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM x86 VM type CreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // ID of the OS image to base this VM on; // Could be boot disk image or CD-ROM image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB // Required: false @@ -50,7 +51,7 @@ type CreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -84,31 +85,13 @@ type CreateRequest struct { IPAType string `url:"ipaType,omitempty" json:"ipaType,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if krq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU can not be empty or equal to 0") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM can not be empty or equal to 0") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID can not be empty or equal to 0") - } - - return nil -} - // Create creates KVM x86 VM based on specified OS image func (k KVMX86) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/kvmx86/create" diff --git a/pkg/cloudapi/kvmx86/create_blank.go b/pkg/cloudapi/kvmx86/create_blank.go index b8dff6b..3771fc3 100644 --- a/pkg/cloudapi/kvmx86/create_blank.go +++ b/pkg/cloudapi/kvmx86/create_blank.go @@ -2,42 +2,43 @@ package kvmx86 import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM x86 VM from scratch type CreateBlankRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Size of the boot disk in GB // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + BootDisk uint64 `url:"bootDisk" json:"bootDisk" validate:"required"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + SEPID uint64 `url:"sepId" json:"sepId" validate:"required"` // Pool to use if sepId is set, can be also empty if needed to be chosen by system // Required: true - Pool string `url:"pool" json:"pool"` + Pool string `url:"pool" json:"pool" validate:"required"` // Network type // Should be one of: @@ -45,7 +46,7 @@ type CreateBlankRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -63,37 +64,13 @@ type CreateBlankRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateBlankRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if krq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU can not be empty or equal to 0") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM can not be empty or equal to 0") - } - if krq.BootDisk == 0 { - return errors.New("validation-error: field BootDisk can not be empty or equal to 0") - } - if krq.SEPID == 0 { - return errors.New("validation-error: field SepID can not be empty or equal to 0") - } - if krq.Pool == "" { - return errors.New("validation-error: field Pool can not be empty") - } - - return nil -} - // CreateBlank creates KVM x86 VM from scratch func (k KVMX86) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/kvmx86/createBlank" diff --git a/pkg/cloudapi/lb/backend_create.go b/pkg/cloudapi/lb/backend_create.go index c39349e..a7aa9bb 100644 --- a/pkg/cloudapi/lb/backend_create.go +++ b/pkg/cloudapi/lb/backend_create.go @@ -2,20 +2,21 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create backend type BackendCreateRequest struct { // ID of the load balancer instance to backendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all backends of this load balancer - name of the new backend to create // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Algorithm // Should be one of: @@ -23,7 +24,7 @@ type BackendCreateRequest struct { // - static-rr // - leastconn // Required: false - Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty"` + Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty" validate:"omitempty,lbAlgorithm"` // Interval in milliseconds between two consecutive availability // checks of the server that is considered available @@ -66,22 +67,13 @@ type BackendCreateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendCreateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - - return nil -} - // BackendCreate creates new backend on the specified load balancer func (l LB) BackendCreate(ctx context.Context, req BackendCreateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendCreate" diff --git a/pkg/cloudapi/lb/backend_delete.go b/pkg/cloudapi/lb/backend_delete.go index 3a53a42..cc21ffb 100644 --- a/pkg/cloudapi/lb/backend_delete.go +++ b/pkg/cloudapi/lb/backend_delete.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete backend type BackendDeleteRequest struct { // ID of the load balancer instance to BackendDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Cannot be emtpy string - name of the backend to delete // Required: true - BackendName string `url:"backendName" json:"backendName"` -} - -func (lbrq BackendDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - - return nil + BackendName string `url:"backendName" json:"backendName" validate:"required"` } // BackendDelete deletes backend from the specified load balancer. // Warning: you cannot undo this action! func (l LB) BackendDelete(ctx context.Context, req BackendDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendDelete" diff --git a/pkg/cloudapi/lb/backend_server_add.go b/pkg/cloudapi/lb/backend_server_add.go index ea39da6..a2f3d51 100644 --- a/pkg/cloudapi/lb/backend_server_add.go +++ b/pkg/cloudapi/lb/backend_server_add.go @@ -2,32 +2,33 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add server definition to the backend type BackendServerAddRequest struct { // ID of the load balancer instance to BackendServerAdd // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` + ServerName string `url:"serverName" json:"serverName" validate:"required"` // IP address of the server // Required: true - Address string `url:"address" json:"address"` + Address string `url:"address" json:"address" validate:"required"` // Port number on the server // Required: true - Port uint64 `url:"port" json:"port"` + Port uint64 `url:"port" json:"port" validate:"required"` // Set to disabled if this server should be used regardless of its state // Required: false @@ -71,31 +72,13 @@ type BackendServerAddRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendServerAddRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName can not be empty") - } - if lbrq.Address == "" { - return errors.New("validation-error: field Address can not be empty") - } - if lbrq.Port == 0 { - return errors.New("validation-error: field Port can not be empty or equal to 0") - } - - return nil -} - // BackendServerAdd adds server definition to the backend on the specified load balancer func (l LB) BackendServerAdd(ctx context.Context, req BackendServerAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendServerAdd" diff --git a/pkg/cloudapi/lb/backend_server_delete.go b/pkg/cloudapi/lb/backend_server_delete.go index c96c2cc..1c99281 100644 --- a/pkg/cloudapi/lb/backend_server_delete.go +++ b/pkg/cloudapi/lb/backend_server_delete.go @@ -2,46 +2,35 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete server definition type BackendServerDeleteRequest struct { // ID of the load balancer instance to BackendServerDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` -} - -func (lbrq BackendServerDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName can not be empty") - } - - return nil + ServerName string `url:"serverName" json:"serverName" validate:"required"` } // BackendServerDelete deletes server definition from the backend on the specified load balancer. // Warning: you cannot undo this action! func (l LB) BackendServerDelete(ctx context.Context, req BackendServerDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendServerDelete" diff --git a/pkg/cloudapi/lb/backend_server_update.go b/pkg/cloudapi/lb/backend_server_update.go index c08a500..c2894ec 100644 --- a/pkg/cloudapi/lb/backend_server_update.go +++ b/pkg/cloudapi/lb/backend_server_update.go @@ -2,32 +2,33 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update server type BackendServerUpdateRequest struct { // ID of the load balancer instance to BackendServerAdd // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` + ServerName string `url:"serverName" json:"serverName" validate:"required"` // IP address of the server // Required: true - Address string `url:"address" json:"address"` + Address string `url:"address" json:"address" validate:"required"` // Port number on the server // Required: true - Port uint64 `url:"port" json:"port"` + Port uint64 `url:"port" json:"port" validate:"required"` // Set to disabled if this server should be used regardless of its state // Required: false @@ -71,31 +72,13 @@ type BackendServerUpdateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendServerUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName can not be empty") - } - if lbrq.Address == "" { - return errors.New("validation-error: field Address can not be empty") - } - if lbrq.Port == 0 { - return errors.New("validation-error: field Port can not be empty or equal to 0") - } - - return nil -} - // BackendServerUpdate updates server definition on the backend of load balancer func (l LB) BackendServerUpdate(ctx context.Context, req BackendServerUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendServerUpdate" diff --git a/pkg/cloudapi/lb/backend_update.go b/pkg/cloudapi/lb/backend_update.go index 6ad5592..3aff5e9 100644 --- a/pkg/cloudapi/lb/backend_update.go +++ b/pkg/cloudapi/lb/backend_update.go @@ -2,20 +2,21 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update backend type BackendUpdateRequest struct { // ID of the load balancer instance to backendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all backends of this load balancer - name of the new backend to create // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Algorithm // Should be one of: @@ -23,7 +24,7 @@ type BackendUpdateRequest struct { // - static-rr // - leastconn // Required: false - Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty"` + Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty" validate:"omitempty,lbAlgorithm"` // Interval in milliseconds between two consecutive availability // checks of the server that is considered available @@ -66,22 +67,13 @@ type BackendUpdateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - - return nil -} - // BackendUpdate updates existing backend on the specified load balancer. Note that backend name cannot be changed func (l LB) BackendUpdate(ctx context.Context, req BackendUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/backendUpdate" diff --git a/pkg/cloudapi/lb/config_reset.go b/pkg/cloudapi/lb/config_reset.go index 5b718dc..b9a356a 100644 --- a/pkg/cloudapi/lb/config_reset.go +++ b/pkg/cloudapi/lb/config_reset.go @@ -2,32 +2,27 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reset config type ConfigResetRequest struct { // ID of the load balancer instance to ConfigReset // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq ConfigResetRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // ConfigReset reset current software configuration of the specified load balancer. // Warning: this action cannot be undone! func (l LB) ConfigReset(ctx context.Context, req ConfigResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/configReset" diff --git a/pkg/cloudapi/lb/create.go b/pkg/cloudapi/lb/create.go index 4baad6e..e437882 100644 --- a/pkg/cloudapi/lb/create.go +++ b/pkg/cloudapi/lb/create.go @@ -2,61 +2,47 @@ package lb import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create load balancer type CreateRequest struct { // ID of the resource group where this load balancer instance will be located // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of the load balancer. // Must be unique among all load balancers in this Resource Group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // External network to connect this load balancer to // Required: true - ExtNetID uint64 `url:"extnetId" json:"extnetId"` + ExtNetID uint64 `url:"extnetId" json:"extnetId" validate:"required"` // Internal network (VINS) to connect this load balancer to // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Start now Load balancer - // Required: false - Start bool `url:"start" json:"start"` + // Required: true + Start bool `url:"start" json:"start" validate:"required"` // Text description of this load balancer // Required: false Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (lbrq CreateRequest) validate() error { - if lbrq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - if lbrq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if lbrq.ExtNetID == 0 { - return errors.New("validation-error: field ExtNetID can not be empty or equal to 0") - } - if lbrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil -} - // Create method will create a new load balancer instance func (l LB) Create(ctx context.Context, req CreateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/lb/create" diff --git a/pkg/cloudapi/lb/delete.go b/pkg/cloudapi/lb/delete.go index 46eafdb..48f945a 100644 --- a/pkg/cloudapi/lb/delete.go +++ b/pkg/cloudapi/lb/delete.go @@ -2,35 +2,30 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete load balancer type DeleteRequest struct { // ID of the load balancer instance to delete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Set to true to delete load balancer immediately bypassing recycle bin // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (lbrq DeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil -} - // Delete deletes specified load balancer func (l LB) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/delete" diff --git a/pkg/cloudapi/lb/disable_enable.go b/pkg/cloudapi/lb/disable_enable.go index 8742bab..b0b7771 100644 --- a/pkg/cloudapi/lb/disable_enable.go +++ b/pkg/cloudapi/lb/disable_enable.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable/enable load balancer type DisableEnableRequest struct { // ID of the load balancer instance to disable/enable // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq DisableEnableRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Disable disables specified load balancer instance func (l LB) Disable(ctx context.Context, req DisableEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/disable" @@ -46,9 +41,11 @@ func (l LB) Disable(ctx context.Context, req DisableEnableRequest) (bool, error) // Enable enables specified load balancer instance func (l LB) Enable(ctx context.Context, req DisableEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/enable" diff --git a/pkg/cloudapi/lb/filter.go b/pkg/cloudapi/lb/filter.go index 59185b3..6718f1f 100644 --- a/pkg/cloudapi/lb/filter.go +++ b/pkg/cloudapi/lb/filter.go @@ -1,5 +1,12 @@ package lb +import ( + "context" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s" +) + // FilterByID returns ListLB with specified ID. func (ll ListLB) FilterByID(id uint64) ListLB { predicate := func(ill ItemLoadBalancer) bool { @@ -36,6 +43,26 @@ func (ll ListLB) FilterByImageID(imageID uint64) ListLB { return ll.FilterFunc(predicate) } +// FilterByK8SID returns ListLB used by specified K8S cluster. +func (ll ListLB) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListLB, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(ill ItemLoadBalancer) bool { + return cluster.LBID == ill.ID + } + + return ll.FilterFunc(predicate), nil +} + // FilterFunc allows filtering ListLB based on a user-specified predicate. func (ll ListLB) FilterFunc(predicate func(ItemLoadBalancer) bool) ListLB { var result ListLB diff --git a/pkg/cloudapi/lb/frontend_bind.go b/pkg/cloudapi/lb/frontend_bind.go index 46fb125..86e6d14 100644 --- a/pkg/cloudapi/lb/frontend_bind.go +++ b/pkg/cloudapi/lb/frontend_bind.go @@ -2,20 +2,21 @@ package lb import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for frontend bind type FrontendBindRequest struct { // ID of the load balancer instance to FrontendBind // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to update // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to update // Required: true @@ -33,25 +34,13 @@ type FrontendBindRequest struct { BindingPort uint64 `url:"bindingPort,omitempty" json:"bindingPort,omitempty"` } -func (lbrq FrontendBindRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName can not be empty") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName can not be empty") - } - - return nil -} - // FrontendBind bind frontend from specified load balancer instance func (l LB) FrontendBind(ctx context.Context, req FrontendBindRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/lb/frontendBind" diff --git a/pkg/cloudapi/lb/frontend_bind_delete.go b/pkg/cloudapi/lb/frontend_bind_delete.go index 1011bee..5538b9b 100644 --- a/pkg/cloudapi/lb/frontend_bind_delete.go +++ b/pkg/cloudapi/lb/frontend_bind_delete.go @@ -2,45 +2,34 @@ package lb import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete bind type FrontendBindDeleteRequest struct { // ID of the load balancer instance to FrontendBindDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to delete // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to delete // Required: true - BindingName string `url:"bindingName" json:"bindingName"` -} - -func (lbrq FrontendBindDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName can not be empty") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName can not be empty") - } - - return nil + BindingName string `url:"bindingName" json:"bindingName" validate:"required"` } // FrontendBindDelete deletes binding from the specified load balancer frontend func (l LB) FrontendBindDelete(ctx context.Context, req FrontendBindDeleteRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/lb/frontendBindDelete" diff --git a/pkg/cloudapi/lb/frontend_bind_update.go b/pkg/cloudapi/lb/frontend_bind_update.go index 3bcc115..97e8d5e 100644 --- a/pkg/cloudapi/lb/frontend_bind_update.go +++ b/pkg/cloudapi/lb/frontend_bind_update.go @@ -2,24 +2,25 @@ package lb import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update binding type FrontendBindUpdateRequest struct { // ID of the load balancer instance to FrontendBindUpdate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to update // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to update // Required: true - BindingName string `url:"bindingName" json:"bindingName"` + BindingName string `url:"bindingName" json:"bindingName" validate:"required"` // If specified must be within the IP range of either Ext Net or ViNS, // where this load balancer is connected - new IP address to use for this binding. @@ -33,25 +34,13 @@ type FrontendBindUpdateRequest struct { BindingPort uint64 `url:"bindingPort,omitempty" json:"bindingPort,omitempty"` } -func (lbrq FrontendBindUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName can not be empty") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName can not be empty") - } - - return nil -} - // FrontendBindUpdate updates binding for the specified load balancer frontend func (l LB) FrontendBindUpdate(ctx context.Context, req FrontendBindUpdateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/lb/frontendBindingUpdate" diff --git a/pkg/cloudapi/lb/frontend_create.go b/pkg/cloudapi/lb/frontend_create.go index 9d92ba6..a5d5600 100644 --- a/pkg/cloudapi/lb/frontend_create.go +++ b/pkg/cloudapi/lb/frontend_create.go @@ -2,47 +2,36 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create frontend type FrontendCreateRequest struct { // ID of the load balancer instance to FrontendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all frontends of // this load balancer - name of the new frontend to create // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Should be one of the backends existing on // this load balancer - name of the backend to use // Required: true - BackendName string `url:"backendName" json:"backendName"` -} - -func (lbrq FrontendCreateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName can not be empty") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName can not be empty") - } - - return nil + BackendName string `url:"backendName" json:"backendName" validate:"required"` } // FrontendCreate creates new frontend on the specified load balancer func (l LB) FrontendCreate(ctx context.Context, req FrontendCreateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/frontendCreate" diff --git a/pkg/cloudapi/lb/frontend_delete.go b/pkg/cloudapi/lb/frontend_delete.go index 7412ee8..b4cc068 100644 --- a/pkg/cloudapi/lb/frontend_delete.go +++ b/pkg/cloudapi/lb/frontend_delete.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete frontend type FrontendDeleteRequest struct { // ID of the load balancer instance to FrontendDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to delete // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` -} - -func (lbrq FrontendDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName can not be empty") - } - - return nil + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` } // FrontendDelete deletes frontend from the specified load balancer. // Warning: you cannot undo this action! func (l LB) FrontendDelete(ctx context.Context, req FrontendDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/frontendDelete" diff --git a/pkg/cloudapi/lb/get.go b/pkg/cloudapi/lb/get.go index 63575ed..34366c4 100644 --- a/pkg/cloudapi/lb/get.go +++ b/pkg/cloudapi/lb/get.go @@ -3,30 +3,25 @@ package lb import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about load balancer type GetRequest struct { // ID of the load balancer to get details for // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq GetRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Get gets detailed information about load balancer func (l LB) Get(ctx context.Context, req GetRequest) (*RecordLB, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/get" diff --git a/pkg/cloudapi/lb/restart.go b/pkg/cloudapi/lb/restart.go index 84f48de..56574b5 100644 --- a/pkg/cloudapi/lb/restart.go +++ b/pkg/cloudapi/lb/restart.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart load balancer type RestartRequest struct { // ID of the load balancer instance to restart // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq RestartRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Restart restarts specified load balancer instance func (l LB) Restart(ctx context.Context, req RestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/restart" diff --git a/pkg/cloudapi/lb/restore.go b/pkg/cloudapi/lb/restore.go index 67c8ac6..2b73254 100644 --- a/pkg/cloudapi/lb/restore.go +++ b/pkg/cloudapi/lb/restore.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore load balancer type RestoreRequest struct { // ID of the load balancer instance to restore // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq RestoreRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Restore restore load balancer from recycle bin func (l LB) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/restore" diff --git a/pkg/cloudapi/lb/start.go b/pkg/cloudapi/lb/start.go index 850ba79..9541b0f 100644 --- a/pkg/cloudapi/lb/start.go +++ b/pkg/cloudapi/lb/start.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start load balancer type StartRequest struct { // ID of the load balancer instance to start // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq StartRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Start starts specified load balancer instance func (l LB) Start(ctx context.Context, req StartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/start" diff --git a/pkg/cloudapi/lb/stop.go b/pkg/cloudapi/lb/stop.go index ca068de..efda2cc 100644 --- a/pkg/cloudapi/lb/stop.go +++ b/pkg/cloudapi/lb/stop.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop load balancer type StopRequest struct { // ID of the load balancer instance to stop // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq StopRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Stop stops specified load balancer instance func (l LB) Stop(ctx context.Context, req StopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/start" diff --git a/pkg/cloudapi/lb/update.go b/pkg/cloudapi/lb/update.go index c1627c0..9178481 100644 --- a/pkg/cloudapi/lb/update.go +++ b/pkg/cloudapi/lb/update.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update load balancer type UpdateRequest struct { // ID of the load balancer to update // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // New description of this load balancer. // If omitted, current description is retained // Required: true - Description string `url:"desc" json:"desc"` -} - -func (lbrq UpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID can not be empty or equal to 0") - } - if lbrq.Description == "" { - return errors.New("validation-error: field Description can not be empty") - } - - return nil + Description string `url:"desc" json:"desc" validate:"required"` } // Update updates some of load balancer attributes func (l LB) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/lb/update" diff --git a/pkg/cloudapi/rg/access_grant.go b/pkg/cloudapi/rg/access_grant.go index d0c5791..65c25c8 100644 --- a/pkg/cloudapi/rg/access_grant.go +++ b/pkg/cloudapi/rg/access_grant.go @@ -2,7 +2,6 @@ package rg import ( "context" - "errors" "net/http" "strconv" @@ -13,44 +12,31 @@ import ( type AccessGrantRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // User or group name to grant access // Required: true - User string `url:"user" json:"user"` + User string `url:"user" json:"user" validate:"required"` // Access rights to set, one of: // - "R" // - "RCX" // - "ARCXDU" // Required: true - Right string `url:"right" json:"right"` + Right string `url:"right" json:"right" validate:"accessType"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq AccessGrantRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - if rgrq.User == "" { - return errors.New("field User can not be empty") - } - validate := validators.StringInSlice(rgrq.Right, []string{"R", "RCX", "ARCXDU"}) - if !validate { - return errors.New("field Right can only be one of 'R', 'RCX' or 'ARCXDU'") - } - - return nil -} - // AccessGrant grants user or group access to the resource group as specified func (r RG) AccessGrant(ctx context.Context, req AccessGrantRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/accessGrant" diff --git a/pkg/cloudapi/rg/access_revoke.go b/pkg/cloudapi/rg/access_revoke.go index fb72dd3..3182f98 100644 --- a/pkg/cloudapi/rg/access_revoke.go +++ b/pkg/cloudapi/rg/access_revoke.go @@ -2,42 +2,34 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access type AccessRevokeRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // User or group name to revoke access // Required: true - User string `url:"user" json:"user"` + User string `url:"user" json:"user" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq AccessRevokeRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - if rgrq.User == "" { - return errors.New("field User can not be empty") - } - - return nil -} - // AccessRevoke revokes specified user or group access from the resource group func (r RG) AccessRevoke(ctx context.Context, req AccessRevokeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/accessRevoke" diff --git a/pkg/cloudapi/rg/affinity_group_computes.go b/pkg/cloudapi/rg/affinity_group_computes.go index cf7cb70..dd45b44 100644 --- a/pkg/cloudapi/rg/affinity_group_computes.go +++ b/pkg/cloudapi/rg/affinity_group_computes.go @@ -3,37 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of all computes with their relationships type AffinityGroupComputesRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Affinity group label // Required: true - AffinityGroup string `url:"affinityGroup" json:"affinityGroup"` -} - -func (rgrq AffinityGroupComputesRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - if rgrq.AffinityGroup == "" { - return errors.New("field AffinityGroup cat not be empty") - } - - return nil + AffinityGroup string `url:"affinityGroup" json:"affinityGroup" validate:"required"` } // AffinityGroupComputes gets list of all computes with their relationships to another computes func (r RG) AffinityGroupComputes(ctx context.Context, req AffinityGroupComputesRequest) (ListAffinityGroups, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/affinityGroupComputes" diff --git a/pkg/cloudapi/rg/affinity_groups_get.go b/pkg/cloudapi/rg/affinity_groups_get.go index 00bf8c3..326f598 100644 --- a/pkg/cloudapi/rg/affinity_groups_get.go +++ b/pkg/cloudapi/rg/affinity_groups_get.go @@ -3,37 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list computes from affinity group type AffinityGroupsGetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Label affinity group // Required: true - AffinityGroup string `url:"affinityGroup" json:"affinityGroup"` -} - -func (rgrq AffinityGroupsGetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - if rgrq.AffinityGroup == "" { - return errors.New("field AffinityGroup cat not be empty") - } - - return nil + AffinityGroup string `url:"affinityGroup" json:"affinityGroup" validate:"required"` } // AffinityGroupsGet gets list computes in the specified affinity group func (r RG) AffinityGroupsGet(ctx context.Context, req AffinityGroupsGetRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/affinityGroupsGet" diff --git a/pkg/cloudapi/rg/affinity_groups_list.go b/pkg/cloudapi/rg/affinity_groups_list.go index f42bfbf..e785306 100644 --- a/pkg/cloudapi/rg/affinity_groups_list.go +++ b/pkg/cloudapi/rg/affinity_groups_list.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of affinity groups from resource group type AffinityGroupsListRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq AffinityGroupsListRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // AffinityGroupsList gets all currently defined affinity groups in this resource group with compute IDs func (r RG) AffinityGroupsList(ctx context.Context, req AffinityGroupsListRequest) (map[string][]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/affinityGroupsList" diff --git a/pkg/cloudapi/rg/audits.go b/pkg/cloudapi/rg/audits.go index feb1fa7..5915c1d 100644 --- a/pkg/cloudapi/rg/audits.go +++ b/pkg/cloudapi/rg/audits.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audit type AuditsRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq AuditsRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // Audits gets audit records for the specified resource group object func (r RG) Audits(ctx context.Context, req AuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/audits" diff --git a/pkg/cloudapi/rg/create.go b/pkg/cloudapi/rg/create.go index eab7a99..88a6379 100644 --- a/pkg/cloudapi/rg/create.go +++ b/pkg/cloudapi/rg/create.go @@ -2,24 +2,25 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create resource group type CreateRequest struct { // Account, which will own this resource group // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Grid ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Name of this resource group. Must be unique within the account // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required,min=2"` // Max size of memory in MB // Required: false @@ -53,7 +54,7 @@ type CreateRequest struct { // - PUBLIC // - NONE // Required: false - DefNet string `url:"def_net,omitempty" json:"def_net,omitempty"` + DefNet string `url:"def_net,omitempty" json:"def_net,omitempty" validate:"omitempty,rgDefNet"` // Private network IP CIDR if default network PRIVATE // Required: false @@ -80,25 +81,13 @@ type CreateRequest struct { RegisterComputes bool `url:"registerComputes,omitempty" json:"registerComputes,omitempty"` } -func (rgrq CreateRequest) validate() error { - if rgrq.AccountID == 0 { - return errors.New("field AccountID can not be empty or equal to 0") - } - if rgrq.GID == 0 { - return errors.New("field GID can not be empty or equal to 0") - } - if len(rgrq.Name) < 2 { - return errors.New("field Name can not be shorter than two bytes") - } - - return nil -} - // Create creates resource group func (r RG) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/create" diff --git a/pkg/cloudapi/rg/delete.go b/pkg/cloudapi/rg/delete.go index 35500ac..95457bc 100644 --- a/pkg/cloudapi/rg/delete.go +++ b/pkg/cloudapi/rg/delete.go @@ -2,16 +2,17 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete resource group type DeleteRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Set to True if you want force delete non-empty resource group // Required: false @@ -27,19 +28,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq DeleteRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Delete deletes resource group func (r RG) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/delete" diff --git a/pkg/cloudapi/rg/disable.go b/pkg/cloudapi/rg/disable.go index c26a3d3..d86a47d 100644 --- a/pkg/cloudapi/rg/disable.go +++ b/pkg/cloudapi/rg/disable.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable resource group type DisableRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq DisableRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Disable disables resource group func (r RG) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/disable" diff --git a/pkg/cloudapi/rg/enable.go b/pkg/cloudapi/rg/enable.go index d4b2ad1..f08c807 100644 --- a/pkg/cloudapi/rg/enable.go +++ b/pkg/cloudapi/rg/enable.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable resource group type EnableRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq EnableRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Enable enables resource group func (r RG) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/enable" diff --git a/pkg/cloudapi/rg/get.go b/pkg/cloudapi/rg/get.go index 5790b45..98d94db 100644 --- a/pkg/cloudapi/rg/get.go +++ b/pkg/cloudapi/rg/get.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about resource group type GetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq GetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Get gets current configuration of the resource group func (r RG) Get(ctx context.Context, req GetRequest) (*RecordResourceGroup, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/get" diff --git a/pkg/cloudapi/rg/list_computes.go b/pkg/cloudapi/rg/list_computes.go index 6de0edd..702a7a0 100644 --- a/pkg/cloudapi/rg/list_computes.go +++ b/pkg/cloudapi/rg/list_computes.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of computes type ListComputesRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq ListComputesRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // ListComputes gets list of all compute instances under specified resource group, accessible by the user func (r RG) ListComputes(ctx context.Context, req ListComputesRequest) (ListComputes, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/listComputes" diff --git a/pkg/cloudapi/rg/list_lb.go b/pkg/cloudapi/rg/list_lb.go index 9940ac1..dcc3e34 100644 --- a/pkg/cloudapi/rg/list_lb.go +++ b/pkg/cloudapi/rg/list_lb.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list load balancers type ListLBRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq ListLBRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // ListLB gets list all load balancers in the specified resource group, accessible by the user func (r RG) ListLB(ctx context.Context, req ListLBRequest) (ListLB, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/listLb" diff --git a/pkg/cloudapi/rg/list_pfw.go b/pkg/cloudapi/rg/list_pfw.go index f33b096..3b4afbb 100644 --- a/pkg/cloudapi/rg/list_pfw.go +++ b/pkg/cloudapi/rg/list_pfw.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list port forward rules type ListPFWRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq ListPFWRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // ListPFW gets list port forward rules for the specified resource group func (r RG) ListPFW(ctx context.Context, req ListPFWRequest) (ListPortForwards, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/listPFW" diff --git a/pkg/cloudapi/rg/list_vins.go b/pkg/cloudapi/rg/list_vins.go index dd86398..fc7fe6c 100644 --- a/pkg/cloudapi/rg/list_vins.go +++ b/pkg/cloudapi/rg/list_vins.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINSes type ListVINSRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq ListVINSRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // ListVINS gets list all ViNSes under specified resource group, accessible by the user func (r RG) ListVINS(ctx context.Context, req ListVINSRequest) (ListVINS, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/listVins" diff --git a/pkg/cloudapi/rg/restore.go b/pkg/cloudapi/rg/restore.go index 7f49024..7aaed7b 100644 --- a/pkg/cloudapi/rg/restore.go +++ b/pkg/cloudapi/rg/restore.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore resource group type RestoreRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq RestoreRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Restore restores resource group from recycle bin func (r RG) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/restore" diff --git a/pkg/cloudapi/rg/set_def_net.go b/pkg/cloudapi/rg/set_def_net.go index 6702f07..91258ab 100644 --- a/pkg/cloudapi/rg/set_def_net.go +++ b/pkg/cloudapi/rg/set_def_net.go @@ -2,7 +2,6 @@ package rg import ( "context" - "errors" "net/http" "strconv" @@ -13,40 +12,31 @@ import ( type SetDefNetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Network type // Should be one of: // - "PUBLIC" // - "PRIVATE" // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"rgNetType"` // Network ID // Required: false - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId,omitempty" json:"netId,omitempty"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq SetDefNetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - if !validators.StringInSlice(rgrq.NetType, []string{"PUBLIC", "PRIVATE"}) { - return errors.New("field NetType can only be one of 'PUBLIC' or 'PRIVATE'") - } - - return nil -} - // SetDefNet sets default network for attach associated virtual machines func (r RG) SetDefNet(ctx context.Context, req SetDefNetRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/setDefNet" diff --git a/pkg/cloudapi/rg/update.go b/pkg/cloudapi/rg/update.go index a738bdc..afe8933 100644 --- a/pkg/cloudapi/rg/update.go +++ b/pkg/cloudapi/rg/update.go @@ -2,16 +2,17 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update resource group type UpdateRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // New name // Required: false @@ -50,19 +51,13 @@ type UpdateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq UpdateRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Update updates resource group func (r RG) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/update" diff --git a/pkg/cloudapi/rg/usage.go b/pkg/cloudapi/rg/usage.go index c338ff1..80c326a 100644 --- a/pkg/cloudapi/rg/usage.go +++ b/pkg/cloudapi/rg/usage.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get report of resource usage type UsageRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq UsageRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("field RGID can not be empty or equal to 0") - } - - return nil -} - // Usage gets report resource usage on the resource group func (r RG) Usage(ctx context.Context, req UsageRequest) (*RecordResourceUsage, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/rg/usage" diff --git a/pkg/cloudapi/tasks/get.go b/pkg/cloudapi/tasks/get.go index 68f05b7..49cafb1 100644 --- a/pkg/cloudapi/tasks/get.go +++ b/pkg/cloudapi/tasks/get.go @@ -3,30 +3,25 @@ package tasks import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get infromation about task type GetRequest struct { // ID of audit // Required: true - AuditID string `url:"auditId" json:"auditId"` -} - -func (trq GetRequest) validate() error { - if trq.AuditID == "" { - return errors.New("validation-error: field AuditID can not be empty") - } - - return nil + AuditID string `url:"auditId" json:"auditId" validate:"required"` } // Get gets background API task status and result func (t Tasks) Get(ctx context.Context, req GetRequest) (*RecordAsyncTask, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/tasks/get" diff --git a/pkg/cloudapi/tasks/models.go b/pkg/cloudapi/tasks/models.go index 8ea542b..8a3edfe 100644 --- a/pkg/cloudapi/tasks/models.go +++ b/pkg/cloudapi/tasks/models.go @@ -32,6 +32,12 @@ func (r *TaskResult) UnmarshalJSON(b []byte) error { } else { return fmt.Errorf("could not unmarshal %v into int", res[0]) } + } else { + n, err := strconv.Atoi(string(b)) + if err != nil { + return err + } + *r = TaskResult(n) } return nil diff --git a/pkg/cloudapi/vins/audits.go b/pkg/cloudapi/vins/audits.go index 9fe3377..ad59ab5 100644 --- a/pkg/cloudapi/vins/audits.go +++ b/pkg/cloudapi/vins/audits.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audits type AuditsRequest struct { // ID of the VINS // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq AuditsRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // Audits gets audit records for the specified VINS object func (v VINS) Audits(ctx context.Context, req AuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/audits" diff --git a/pkg/cloudapi/vins/create_in_account.go b/pkg/cloudapi/vins/create_in_account.go index 3e71b33..2970dc7 100644 --- a/pkg/cloudapi/vins/create_in_account.go +++ b/pkg/cloudapi/vins/create_in_account.go @@ -2,20 +2,21 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create VINS in account type CreateInAccountRequest struct { // VINS name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Grid ID // Required: false @@ -34,22 +35,13 @@ type CreateInAccountRequest struct { PreReservationsNum uint64 `url:"preReservationsNum,omitempty" json:"preReservationsNum,omitempty"` } -func (vrq CreateInAccountRequest) validate() error { - if vrq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if vrq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - - return nil -} - // CreateInAccount creates VINS in account level func (v VINS) CreateInAccount(ctx context.Context, req CreateInAccountRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/createInAccount" diff --git a/pkg/cloudapi/vins/create_in_rg.go b/pkg/cloudapi/vins/create_in_rg.go index c9dfd05..af244bd 100644 --- a/pkg/cloudapi/vins/create_in_rg.go +++ b/pkg/cloudapi/vins/create_in_rg.go @@ -2,24 +2,25 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create VINS in resource group type CreateInRGRequest struct { // VINS name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Private network IP CIDR // Required: false - IPCIDR string `url:"ipcidr,omitempty" json:"ipcidr,omitempty"` + IPCIDR string `url:"ipcidr,omitempty" json:"ipcidr,omitempty" validate:"required"` // External network ID // Required: false @@ -38,22 +39,13 @@ type CreateInRGRequest struct { PreReservationsNum uint `url:"preReservationsNum,omitempty" json:"preReservationsNum,omitempty"` } -func (vrq CreateInRGRequest) validate() error { - if vrq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if vrq.RGID == 0 { - return errors.New("validation-error: field RGID can not be empty or equal to 0") - } - - return nil -} - // CreateInRG creates VINS in resource group level func (v VINS) CreateInRG(ctx context.Context, req CreateInRGRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/createInRG" diff --git a/pkg/cloudapi/vins/delete.go b/pkg/cloudapi/vins/delete.go index 3645ea5..c9745e8 100644 --- a/pkg/cloudapi/vins/delete.go +++ b/pkg/cloudapi/vins/delete.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete VINS type DeleteRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Set to True if you want force delete non-empty VINS. // Primarily, VINS is considered non-empty if it has virtual machines connected to it, @@ -26,19 +27,13 @@ type DeleteRequest struct { Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (vrq DeleteRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil -} - // Delete deletes VINS func (v VINS) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/delete" diff --git a/pkg/cloudapi/vins/disable_enable.go b/pkg/cloudapi/vins/disable_enable.go index 4957c70..a238a14 100644 --- a/pkg/cloudapi/vins/disable_enable.go +++ b/pkg/cloudapi/vins/disable_enable.go @@ -2,31 +2,26 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable/enable VINS type DisableEnableRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq DisableEnableRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // Disable disables VINS func (v VINS) Disable(ctx context.Context, req DisableEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/disable" @@ -47,9 +42,11 @@ func (v VINS) Disable(ctx context.Context, req DisableEnableRequest) (bool, erro // Enable enables VINS func (v VINS) Enable(ctx context.Context, req DisableEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/enable" diff --git a/pkg/cloudapi/vins/extnet_connect.go b/pkg/cloudapi/vins/extnet_connect.go index 316109b..e2ef441 100644 --- a/pkg/cloudapi/vins/extnet_connect.go +++ b/pkg/cloudapi/vins/extnet_connect.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for connect external network type ExtNetConnectRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // External network ID // Required: false @@ -22,19 +23,13 @@ type ExtNetConnectRequest struct { IP string `url:"ip,omitempty" json:"ip,omitempty"` } -func (vrq ExtNetConnectRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil -} - // ExtNetConnect connect VINS to external network func (v VINS) ExtNetConnect(ctx context.Context, req ExtNetConnectRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/extNetConnect" diff --git a/pkg/cloudapi/vins/extnet_disconnect.go b/pkg/cloudapi/vins/extnet_disconnect.go index 4fd58f0..7bbe87f 100644 --- a/pkg/cloudapi/vins/extnet_disconnect.go +++ b/pkg/cloudapi/vins/extnet_disconnect.go @@ -2,31 +2,26 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disconnect VINS from external network type ExtNetDisconnectRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq ExtNetDisconnectRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // ExtNetDisconnect disconnect VINS from external network func (v VINS) ExtNetDisconnect(ctx context.Context, req ExtNetDisconnectRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/extNetDisconnect" diff --git a/pkg/cloudapi/vins/extnet_list.go b/pkg/cloudapi/vins/extnet_list.go index 610c9b8..80faebe 100644 --- a/pkg/cloudapi/vins/extnet_list.go +++ b/pkg/cloudapi/vins/extnet_list.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINS external network connections type ExtNetListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq ExtNetListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // ExtNetList show list of VINS external network connections func (v VINS) ExtNetList(ctx context.Context, req ExtNetListRequest) (ListExtNets, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/extNetList" diff --git a/pkg/cloudapi/vins/get.go b/pkg/cloudapi/vins/get.go index 2c0dda4..4d3ed3a 100644 --- a/pkg/cloudapi/vins/get.go +++ b/pkg/cloudapi/vins/get.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about VINS type GetRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq GetRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // Get gets information about VINS by ID func (v VINS) Get(ctx context.Context, req GetRequest) (*RecordVINS, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/get" diff --git a/pkg/cloudapi/vins/ip_list.go b/pkg/cloudapi/vins/ip_list.go index abba4ba..d6596a6 100644 --- a/pkg/cloudapi/vins/ip_list.go +++ b/pkg/cloudapi/vins/ip_list.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for DHCP IP type IPListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq IPListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // IPList show DHCP IP reservations on VINS func (v VINS) IPList(ctx context.Context, req IPListRequest) (ListIPs, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/ipList" diff --git a/pkg/cloudapi/vins/ip_release.go b/pkg/cloudapi/vins/ip_release.go index 2df364b..4ac8151 100644 --- a/pkg/cloudapi/vins/ip_release.go +++ b/pkg/cloudapi/vins/ip_release.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for IP relese type IPReleaseRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // IP address // Required: false @@ -22,20 +23,14 @@ type IPReleaseRequest struct { MAC string `url:"mac,omitempty" json:"mac,omitempty"` } -func (vrq IPReleaseRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil -} - // IPRelese delete IP reservation matched by specified IP & MAC address combination. // If both IP and MAC address are empty strings, all IP reservations will be deleted. func (v VINS) IPRelese(ctx context.Context, req IPReleaseRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/ipRelease" diff --git a/pkg/cloudapi/vins/ip_reserve.go b/pkg/cloudapi/vins/ip_reserve.go index 220cf45..fd3b9c7 100644 --- a/pkg/cloudapi/vins/ip_reserve.go +++ b/pkg/cloudapi/vins/ip_reserve.go @@ -2,7 +2,6 @@ package vins import ( "context" - "errors" "net/http" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -12,7 +11,7 @@ import ( type IPReserveRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Type of the reservation // Should be one of: @@ -20,7 +19,7 @@ type IPReserveRequest struct { // - VIP // - EXCLUDE // Required: true - Type string `url:"type" json:"type"` + Type string `url:"type" json:"type" validate:"vinsType"` // IP address to use. Non-empty string is required for type "EXCLUDE". // Ignored for types "DHCP" and "VIP". @@ -39,26 +38,13 @@ type IPReserveRequest struct { ComputeID uint64 `url:"computeId,omitempty" json:"computeId,omitempty"` } -func (vrq IPReserveRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - if vrq.Type == "" { - return errors.New("validation-error: field Type can not be empty") - } - validate := validators.StringInSlice(vrq.Type, []string{"DHCP", "VIP", "EXCLUDED"}) - if !validate { - return errors.New("'type' should be 'DHCP', 'VIP' or 'EXCLUDED'") - } - - return nil -} - // IPReserve creates reservation on ViNS DHCP func (v VINS) IPReserve(ctx context.Context, req IPReserveRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudapi/vins/ipReserve" diff --git a/pkg/cloudapi/vins/nat_rule_add.go b/pkg/cloudapi/vins/nat_rule_add.go index f56f7b7..f0ce080 100644 --- a/pkg/cloudapi/vins/nat_rule_add.go +++ b/pkg/cloudapi/vins/nat_rule_add.go @@ -2,28 +2,29 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create NAT rules type NATRuleAddRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Internal IP address to apply this rule to // Required: true - IntIP string `url:"intIp" json:"intIp"` + IntIP string `url:"intIp" json:"intIp" validate:"required"` // Internal IP port number to use for this rule // Required: true - IntPort uint `url:"intPort" json:"intPort"` + IntPort uint `url:"intPort" json:"intPort" validate:"required"` // External IP start port to use for this rule // Required: true - ExtPortStart uint `url:"extPortStart" json:"extPortStart"` + ExtPortStart uint `url:"extPortStart" json:"extPortStart" validate:"required"` // External IP end port to use for this rule // Required: false @@ -34,31 +35,16 @@ type NATRuleAddRequest struct { // - "tcp" // - "udp" // Required: false - Proto string `url:"proto,omitempty" json:"proto,omitempty"` -} - -func (vrq NATRuleAddRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - if vrq.IntIP == "" { - return errors.New("validation-error: field IntIP can not be empty") - } - if vrq.IntPort == 0 { - return errors.New("validation-error: field IntPort can not be empty or equal to 0") - } - if vrq.ExtPortStart == 0 { - return errors.New("validation-error: field ExtPortStart can not be empty or equal to 0") - } - - return nil + Proto string `url:"proto,omitempty" json:"proto,omitempty" validate:"omitempty,proto"` } // NATRuleAdd create NAT (port forwarding) rule on VINS func (v VINS) NATRuleAdd(ctx context.Context, req NATRuleAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/natRuleAdd" diff --git a/pkg/cloudapi/vins/nat_rule_del.go b/pkg/cloudapi/vins/nat_rule_del.go index 8e57ced..a85535d 100644 --- a/pkg/cloudapi/vins/nat_rule_del.go +++ b/pkg/cloudapi/vins/nat_rule_del.go @@ -2,39 +2,31 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete NAT rule type NATRuleDelRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // ID of the rule to delete. // Pass -1 to clear all rules at once // Required: true - RuleID uint64 `url:"ruleId" json:"ruleId"` -} - -func (vrq NATRuleDelRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - if vrq.RuleID == 0 { - return errors.New("validation-error: field RuleID can not be empty or equal to 0") - } - - return nil + RuleID uint64 `url:"ruleId" json:"ruleId" validate:"required"` } // NATRuleDel delete NAT (port forwarding) rule on VINS func (v VINS) NATRuleDel(ctx context.Context, req NATRuleDelRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/natRuleDel" diff --git a/pkg/cloudapi/vins/nat_rule_list.go b/pkg/cloudapi/vins/nat_rule_list.go index 3d8ffe0..05f1b7d 100644 --- a/pkg/cloudapi/vins/nat_rule_list.go +++ b/pkg/cloudapi/vins/nat_rule_list.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of NAT rules type NATRuleListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq NATRuleListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // NATRuleList gets list of NAT (port forwarding) rules func (v VINS) NATRuleList(ctx context.Context, req NATRuleListRequest) (ListNATRules, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/natRuleList" diff --git a/pkg/cloudapi/vins/restore.go b/pkg/cloudapi/vins/restore.go index 59ac8e0..86ee4d0 100644 --- a/pkg/cloudapi/vins/restore.go +++ b/pkg/cloudapi/vins/restore.go @@ -2,31 +2,26 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore type RestoreRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq RestoreRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // Restore restores VINS from recycle bin func (v VINS) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/restore" diff --git a/pkg/cloudapi/vins/vnfdev_redeploy.go b/pkg/cloudapi/vins/vnfdev_redeploy.go index f08d322..753d6ac 100644 --- a/pkg/cloudapi/vins/vnfdev_redeploy.go +++ b/pkg/cloudapi/vins/vnfdev_redeploy.go @@ -2,31 +2,26 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for redeploy VNFDevs type VNFDevRedeployRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq VNFDevRedeployRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // VNFDevRedeploy redeploy VINS VNFDevs func (v VINS) VNFDevRedeploy(ctx context.Context, req VNFDevRedeployRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/vnfdevRedeploy" diff --git a/pkg/cloudapi/vins/vnfdev_restart.go b/pkg/cloudapi/vins/vnfdev_restart.go index 6a1a7a8..3eecae8 100644 --- a/pkg/cloudapi/vins/vnfdev_restart.go +++ b/pkg/cloudapi/vins/vnfdev_restart.go @@ -2,31 +2,26 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reboot VINSes primary VNF device type VNFDevRestartRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq VNFDevRestartRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID can not be empty or equal to 0") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // VNFDevRestart reboot VINSes primary VNF device func (v VINS) VNFDevRestart(ctx context.Context, req VNFDevRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudapi/vins/vnfdevRestart" diff --git a/pkg/cloudbroker/account/add_user.go b/pkg/cloudbroker/account/add_user.go index eb93eb1..f7f95ac 100644 --- a/pkg/cloudbroker/account/add_user.go +++ b/pkg/cloudbroker/account/add_user.go @@ -2,7 +2,6 @@ package account import ( "context" - "errors" "net/http" "strconv" @@ -13,43 +12,27 @@ import ( type AddUserRequest struct { // ID of account to add to // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Name of the user to be given rights // Required: true - UserName string `url:"username" json:"username"` + Username string `url:"username" json:"username" validate:"required"` // Account permission types: // - 'R' for read only access // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (arq AddUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - if arq.UserName == "" { - return errors.New("validation-error: field UserName can not be empty") - } - if arq.AccessType == "" { - return errors.New("validation-error: field AccessType can not be empty") - } - validate := validators.StringInSlice(arq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !validate { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // AddUser gives a user access rights. func (a Account) AddUser(ctx context.Context, req AddUserRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/addUser" diff --git a/pkg/cloudbroker/account/audits.go b/pkg/cloudbroker/account/audits.go index 2024e5b..3014f16 100644 --- a/pkg/cloudbroker/account/audits.go +++ b/pkg/cloudbroker/account/audits.go @@ -3,29 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for give list account audits type AuditsRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq AuditsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID can not be empty or equal to 0") - } - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Audits gets audit records for the specified account object func (a Account) Audits(ctx context.Context, req AuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/audits" diff --git a/pkg/cloudbroker/account/create.go b/pkg/cloudbroker/account/create.go index 49ea155..0a15595 100644 --- a/pkg/cloudbroker/account/create.go +++ b/pkg/cloudbroker/account/create.go @@ -2,24 +2,25 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for creating account type CreateRequest struct { // Display name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Name of the account // Required: true - Username string `url:"username" json:"username"` + Username string `url:"username" json:"username" validate:"required"` // Email // Required: false - EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty"` + EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty" validate:"omitempty,email"` // Max size of memory in MB // Required: false @@ -55,23 +56,14 @@ type CreateRequest struct { UniqPools []string `url:"uniqPools,omitempty" json:"uniqPools,omitempty"` } -func (arq CreateRequest) validate() error { - if arq.Name == "" { - return errors.New("validation-error: field Name can not be empty") - } - if arq.Username == "" { - return errors.New("validation-error: field Username can not be empty") - } - - return nil -} - // Create creates account // Setting a cloud unit maximum to -1 or empty will not put any restrictions on the resource func (a Account) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/create" diff --git a/pkg/cloudbroker/account/delete.go b/pkg/cloudbroker/account/delete.go index 4a00c5e..fbf98ab 100644 --- a/pkg/cloudbroker/account/delete.go +++ b/pkg/cloudbroker/account/delete.go @@ -2,41 +2,33 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete account type DeleteRequest struct { // ID of account to delete // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Reason to delete // Required: true - Reason string `url:"reason" json:"reason"` + Reason string `url:"reason" json:"reason" validate:"required"` // Whether to completely delete the account // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (arq DeleteRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil -} - // Delete completes delete an account from the system Returns true if account is deleted or was already deleted or never existed func (a Account) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/delete" diff --git a/pkg/cloudbroker/account/delete_accounts.go b/pkg/cloudbroker/account/delete_accounts.go index aece37a..a5c10a7 100644 --- a/pkg/cloudbroker/account/delete_accounts.go +++ b/pkg/cloudbroker/account/delete_accounts.go @@ -2,41 +2,33 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete group accounts type DeleteAccountsRequest struct { // IDs of accounts // Required: true - AccountsIDs []uint64 `url:"accountIds" json:"accountIds"` + AccountsIDs []uint64 `url:"accountIds" json:"accountIds" validate:"min=1"` // Reason for deletion // Required: true - Reason string `url:"reason" json:"reason"` + Reason string `url:"reason" json:"reason" validate:"required"` // Whether to completely destroy accounts or not // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (arq DeleteAccountsRequest) validate() error { - if len(arq.AccountsIDs) == 0 { - return errors.New("validation-error: field AccountIDs must be set") - } - if arq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil -} - // DeleteAccounts destroy a group of accounts func (a Account) DeleteAccounts(ctx context.Context, req DeleteAccountsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/deleteAccounts" diff --git a/pkg/cloudbroker/account/delete_user.go b/pkg/cloudbroker/account/delete_user.go index c6595ce..ed1542d 100644 --- a/pkg/cloudbroker/account/delete_user.go +++ b/pkg/cloudbroker/account/delete_user.go @@ -2,42 +2,34 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access to account type DeleteUserRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // ID or emailaddress of the user to remove // Required: true - UserName string `url:"username" json:"username"` + UserName string `url:"username" json:"username" validate:"required"` // Recursively revoke access rights from owned cloudspaces and vmachines // Required: false RecursiveDelete bool `url:"recursivedelete,omitempty" json:"recursivedelete,omitempty"` } -func (arq DeleteUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.UserName == "" { - return errors.New("validation-error: field UserName must be set") - } - - return nil -} - // DeleteUser revokes user access from the account func (a Account) DeleteUser(ctx context.Context, req DeleteUserRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/deleteUser" diff --git a/pkg/cloudbroker/account/disable.go b/pkg/cloudbroker/account/disable.go index 894c835..c86d204 100644 --- a/pkg/cloudbroker/account/disable.go +++ b/pkg/cloudbroker/account/disable.go @@ -2,38 +2,30 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable account type DisableRequest struct { // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Reason to disable // Required: true - Reason string `url:"reason" json:"reason"` -} - -func (arq DisableRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil + Reason string `url:"reason" json:"reason" validate:"required"` } // Disable disables an account func (a Account) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/disable" diff --git a/pkg/cloudbroker/account/disable_accounts.go b/pkg/cloudbroker/account/disable_accounts.go index b6300e9..25e3923 100644 --- a/pkg/cloudbroker/account/disable_accounts.go +++ b/pkg/cloudbroker/account/disable_accounts.go @@ -2,30 +2,25 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable group accounts type DisableAccountsRequest struct { // IDs of accounts // Required: true - AccountIDs []uint64 `url:"accountIds,omitempty" json:"accountIds,omitempty"` -} - -func (arq DisableAccountsRequest) validate() error { - if len(arq.AccountIDs) == 0 { - return errors.New("validation-error: field AccountIDs must be set") - } - - return nil + AccountIDs []uint64 `url:"accountIds" json:"accountIds" validate:"min=1"` } // DisableAccounts disables accounts func (a Account) DisableAccounts(ctx context.Context, req DisableAccountsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/disableAccounts" diff --git a/pkg/cloudbroker/account/enable.go b/pkg/cloudbroker/account/enable.go index 9bdee47..5766de8 100644 --- a/pkg/cloudbroker/account/enable.go +++ b/pkg/cloudbroker/account/enable.go @@ -2,38 +2,30 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable account type EnableRequest struct { // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Reason to enable // Required: true - Reason string `url:"reason" json:"reason"` -} - -func (arq EnableRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.Reason == "" { - return errors.New("field Reason must be set") - } - - return nil + Reason string `url:"reason" json:"reason" validate:"required"` } // Enable enables an account func (a Account) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/enable" diff --git a/pkg/cloudbroker/account/enable_accounts.go b/pkg/cloudbroker/account/enable_accounts.go index df977eb..365e7b2 100644 --- a/pkg/cloudbroker/account/enable_accounts.go +++ b/pkg/cloudbroker/account/enable_accounts.go @@ -2,30 +2,25 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request for enable group accounts type EnableAccountsRequest struct { // IDs od accounts // Required: true - AccountIDs []uint64 `url:"accountIds" json:"accountIds"` -} - -func (arq EnableAccountsRequest) validate() error { - if len(arq.AccountIDs) == 0 { - return errors.New("validation-error: field AccountIDs must be set") - } - - return nil + AccountIDs []uint64 `url:"accountIds" json:"accountIds" validate:"min=1"` } // EnableAccounts enables accounts func (a Account) EnableAccounts(ctx context.Context, req EnableAccountsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/enableAccounts" diff --git a/pkg/cloudbroker/account/get.go b/pkg/cloudbroker/account/get.go index 683f22b..ce36577 100644 --- a/pkg/cloudbroker/account/get.go +++ b/pkg/cloudbroker/account/get.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about account type GetRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq GetRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // Get gets information about account func (a Account) Get(ctx context.Context, req GetRequest) (*RecordAccount, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/get" diff --git a/pkg/cloudbroker/account/list_computes.go b/pkg/cloudbroker/account/list_computes.go index 977dc96..1d914d5 100644 --- a/pkg/cloudbroker/account/list_computes.go +++ b/pkg/cloudbroker/account/list_computes.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for a get list compute instances type ListComputesRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListComputesRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListComputes gets list all compute instances under specified account, accessible by the user func (a Account) ListComputes(ctx context.Context, req ListComputesRequest) (ListComputes, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/listComputes" diff --git a/pkg/cloudbroker/account/list_disks.go b/pkg/cloudbroker/account/list_disks.go index 0c183cb..edd6715 100644 --- a/pkg/cloudbroker/account/list_disks.go +++ b/pkg/cloudbroker/account/list_disks.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list deleted disks type ListDisksRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListDisksRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListDisks gets list all currently unattached disks under specified account func (a Account) ListDisks(ctx context.Context, req ListDisksRequest) (ListDisks, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/listDisks" diff --git a/pkg/cloudbroker/account/list_flip_groups.go b/pkg/cloudbroker/account/list_flip_groups.go index 9cd7903..e23ce19 100644 --- a/pkg/cloudbroker/account/list_flip_groups.go +++ b/pkg/cloudbroker/account/list_flip_groups.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list FLIPGroups type ListFLIPGroupsRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListFLIPGroupsRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListFLIPGroups gets list all FLIPGroups under specified account, accessible by the user func (a Account) ListFLIPGroups(ctx context.Context, req ListFLIPGroupsRequest) (ListFLIPGroups, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/listFlipGroups" diff --git a/pkg/cloudbroker/account/list_rg.go b/pkg/cloudbroker/account/list_rg.go index e9ad296..1da1377 100644 --- a/pkg/cloudbroker/account/list_rg.go +++ b/pkg/cloudbroker/account/list_rg.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list resource groups type ListRGRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListRGRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListRG gets list all resource groups under specified account, accessible by the user func (a Account) ListRG(ctx context.Context, req ListRGRequest) (ListRG, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/listRG" diff --git a/pkg/cloudbroker/account/list_vins.go b/pkg/cloudbroker/account/list_vins.go index 317dd9f..a35880a 100644 --- a/pkg/cloudbroker/account/list_vins.go +++ b/pkg/cloudbroker/account/list_vins.go @@ -3,30 +3,25 @@ package account import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINS type ListVINSRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (arq ListVINSRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // ListVINS gets list all ViNSes under specified account, accessible by the user func (a Account) ListVINS(ctx context.Context, req ListVINSRequest) (ListVINS, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/listVins" diff --git a/pkg/cloudbroker/account/restore.go b/pkg/cloudbroker/account/restore.go index 94897f0..7dd697a 100644 --- a/pkg/cloudbroker/account/restore.go +++ b/pkg/cloudbroker/account/restore.go @@ -2,37 +2,29 @@ package account import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore a deleted account type RestoreRequest struct { // ID an account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Reason to restore // Required: true - Reason string `url:"reason" json:"reason"` -} - -func (arq RestoreRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil + Reason string `url:"reason" json:"reason" validate:"required"` } // Restore restores a deleted account func (a Account) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/restore" diff --git a/pkg/cloudbroker/account/update.go b/pkg/cloudbroker/account/update.go index 93e1310..19df96a 100644 --- a/pkg/cloudbroker/account/update.go +++ b/pkg/cloudbroker/account/update.go @@ -2,28 +2,29 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update account type UpdateRequest struct { // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Display name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Name of the account // Required: true - Username string `url:"username" json:"username"` + Username string `url:"username,omitempty" json:"username,omitempty"` // Email // Required: false - EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty"` + EmailAddress string `url:"emailaddress,omitempty" json:"emailaddress,omitempty" validate:"omitempty,email"` // Max size of memory in MB // Required: false @@ -59,22 +60,13 @@ type UpdateRequest struct { UniqPools []string `url:"uniqPools,omitempty" json:"uniqPools,omitempty"` } -func (arq UpdateRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil -} - // Update updates an account name and resource types and limits func (a Account) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/update" diff --git a/pkg/cloudbroker/account/update_resource_types.go b/pkg/cloudbroker/account/update_resource_types.go index 57ca3ca..ad3ac7d 100644 --- a/pkg/cloudbroker/account/update_resource_types.go +++ b/pkg/cloudbroker/account/update_resource_types.go @@ -2,7 +2,6 @@ package account import ( "context" - "errors" "net/http" "strconv" @@ -13,7 +12,7 @@ import ( type UpdateResourceTypesRequest struct { // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Resource types available to create in this account // Each element in a resource type slice must be one of: @@ -24,29 +23,15 @@ type UpdateResourceTypesRequest struct { // - lb // - flipgroup // Required: true - ResTypes []string `url:"resourceTypes" json:"resourceTypes"` -} - -func (arq UpdateResourceTypesRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if len(arq.ResTypes) > 0 { - for _, value := range arq.ResTypes { - validate := validators.StringInSlice(value, []string{"compute", "vins", "k8s", "openshift", "lb", "flipgroup"}) - if !validate { - return errors.New("validation-error: Every resource type specified should be one of [compute, vins, k8s, openshift, lb, flipgroup]") - } - } - } - - return nil + ResTypes []string `url:"resourceTypes" json:"resourceTypes" validate:"min=1,resTypes"` } func (a Account) UpdateResourceTypes(ctx context.Context, req UpdateResourceTypesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/updateResourceTypes" diff --git a/pkg/cloudbroker/account/update_user.go b/pkg/cloudbroker/account/update_user.go index 36c330e..604e30b 100644 --- a/pkg/cloudbroker/account/update_user.go +++ b/pkg/cloudbroker/account/update_user.go @@ -2,48 +2,37 @@ package account import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update user access rights type UpdateUserRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Userid/Email for registered users or emailaddress for unregistered users // Required: true - UserID string `url:"userId" json:"userId"` + UserID string `url:"userId" json:"userId" validate:"required"` // Account permission types: // - 'R' for read only access // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (arq UpdateUserRequest) validate() error { - if arq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if arq.UserID == "" { - return errors.New("validation-error: field UserID must be set") - } - if arq.AccessType == "" { - return errors.New("validation-error: field AccessType must be set") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // UpdateUser updates user access rights func (a Account) UpdateUser(ctx context.Context, req UpdateUserRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/account/updateUser" diff --git a/pkg/cloudbroker/compute/affinity_group_check_start.go b/pkg/cloudbroker/compute/affinity_group_check_start.go index b5f2e20..4d39a8f 100644 --- a/pkg/cloudbroker/compute/affinity_group_check_start.go +++ b/pkg/cloudbroker/compute/affinity_group_check_start.go @@ -2,37 +2,29 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for check all computes with current affinity label can start type AffinityGroupCheckStartRequest struct { // ID of the resource group // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Affinity group label // Required: true - AffinityLabel string `url:"affinityLabel" json:"affinityLabel"` -} - -func (crq AffinityGroupCheckStartRequest) validate() error { - if crq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if crq.AffinityLabel == "" { - return errors.New("validation-error: field AffinityLabel must be set") - } - - return nil + AffinityLabel string `url:"affinityLabel" json:"affinityLabel" validate:"required"` } // AffinityGroupCheckStart check all computes with current affinity label can start func (c Compute) AffinityGroupCheckStart(ctx context.Context, req AffinityGroupCheckStartRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityGroupCheckStart" diff --git a/pkg/cloudbroker/compute/affinity_label_remove.go b/pkg/cloudbroker/compute/affinity_label_remove.go index 224bc01..a8b103f 100644 --- a/pkg/cloudbroker/compute/affinity_label_remove.go +++ b/pkg/cloudbroker/compute/affinity_label_remove.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear affinity label for compute type AffinityLabelRemoveRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` -} - -func (crq AffinityLabelRemoveRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` } // AffinityLabelRemove clear affinity label for compute func (c Compute) AffinityLabelRemove(ctx context.Context, req AffinityLabelRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityLabelRemove" diff --git a/pkg/cloudbroker/compute/affinity_label_set.go b/pkg/cloudbroker/compute/affinity_label_set.go index 587a452..2e7ef14 100644 --- a/pkg/cloudbroker/compute/affinity_label_set.go +++ b/pkg/cloudbroker/compute/affinity_label_set.go @@ -2,37 +2,29 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set affinity label for compute type AffinityLabelSetRequest struct { // IDs of the compute instances - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Affinity group label // Required: true - AffinityLabel string `url:"affinityLabel" json:"affinityLabel"` -} - -func (crq AffinityLabelSetRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.AffinityLabel == "" { - return errors.New("validation-error: field AffinityLabel must be set") - } - - return nil + AffinityLabel string `url:"affinityLabel" json:"affinityLabel" validate:"required"` } // AffinityLabelSet set affinity label for compute func (c Compute) AffinityLabelSet(ctx context.Context, req AffinityLabelSetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityLabelSet" diff --git a/pkg/cloudbroker/compute/affinity_relations.go b/pkg/cloudbroker/compute/affinity_relations.go index 7bbbf5c..65fad42 100644 --- a/pkg/cloudbroker/compute/affinity_relations.go +++ b/pkg/cloudbroker/compute/affinity_relations.go @@ -3,34 +3,29 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get dict of computes type AffinityRelationsRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Affinity group label // Required: false AffinityLabel string `url:"affinityLabel,omitempty" json:"affinityLabel,omitempty"` } -func (crq AffinityRelationsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID can not be empty or equal to 0") - } - - return nil -} - // AffinityRelations gets dict of computes divided by affinity and anti affinity rules func (c Compute) AffinityRelations(ctx context.Context, req AffinityRelationsRequest) (*RecordAffinityRelations, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityRelations" diff --git a/pkg/cloudbroker/compute/affinity_rule_add.go b/pkg/cloudbroker/compute/affinity_rule_add.go index d64efd1..5aba630 100644 --- a/pkg/cloudbroker/compute/affinity_rule_add.go +++ b/pkg/cloudbroker/compute/affinity_rule_add.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,20 +12,20 @@ import ( type AffinityRuleAddRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Should be one of: // - node // - compute // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -34,57 +33,24 @@ type AffinityRuleAddRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AffinityRuleAddRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology must be set") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy must be set") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode must be set") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - if crq.Value == "" { - return errors.New("validation-error: field Value must be set") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AffinityRuleAdd add affinity rule func (c Compute) AffinityRuleAdd(ctx context.Context, req AffinityRuleAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityRuleAdd" diff --git a/pkg/cloudbroker/compute/affinity_rule_remove.go b/pkg/cloudbroker/compute/affinity_rule_remove.go index 64edc11..3ed85f6 100644 --- a/pkg/cloudbroker/compute/affinity_rule_remove.go +++ b/pkg/cloudbroker/compute/affinity_rule_remove.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,18 +12,18 @@ import ( type AffinityRuleRemoveRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AffinityRuleRemoveRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AffinityRuleRemoveRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology must be set") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy must be set") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode must be set") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - if crq.Value == "" { - return errors.New("validation-error: field Value must be set") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AffinityRuleRemove remove affinity rule func (c Compute) AffinityRuleRemove(ctx context.Context, req AffinityRuleRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityRuleRemove" diff --git a/pkg/cloudbroker/compute/affinity_rules_clear.go b/pkg/cloudbroker/compute/affinity_rules_clear.go index 2babd55..31c1e5b 100644 --- a/pkg/cloudbroker/compute/affinity_rules_clear.go +++ b/pkg/cloudbroker/compute/affinity_rules_clear.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear affinity rules type AffinityRulesClearRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` -} - -func (crq AffinityRulesClearRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` } // AffinityRulesClear clear affinity rules func (c Compute) AffinityRulesClear(ctx context.Context, req AffinityRulesClearRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/affinityRulesClear" diff --git a/pkg/cloudbroker/compute/anti_affinity_rule_add.go b/pkg/cloudbroker/compute/anti_affinity_rule_add.go index 0f9d290..8dfa919 100644 --- a/pkg/cloudbroker/compute/anti_affinity_rule_add.go +++ b/pkg/cloudbroker/compute/anti_affinity_rule_add.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,18 +12,18 @@ import ( type AntiAffinityRuleAddRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AntiAffinityRuleAddRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AntiAffinityRuleAddRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology must be set") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy must be set") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode must be set") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - if crq.Value == "" { - return errors.New("validation-error: field Value must be set") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AntiAffinityRuleAdd add anti affinity rule func (c Compute) AntiAffinityRuleAdd(ctx context.Context, req AntiAffinityRuleAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/antiAffinityRuleAdd" diff --git a/pkg/cloudbroker/compute/anti_affinity_rule_clear.go b/pkg/cloudbroker/compute/anti_affinity_rule_clear.go index d362592..387289a 100644 --- a/pkg/cloudbroker/compute/anti_affinity_rule_clear.go +++ b/pkg/cloudbroker/compute/anti_affinity_rule_clear.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clear anti affinity rules type AntiAffinityRulesClearRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` -} - -func (crq AntiAffinityRulesClearRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` } // AntiAffinityRulesClear clear anti affinity rules func (c Compute) AntiAffinityRulesClear(ctx context.Context, req AntiAffinityRulesClearRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/antiAffinityRulesClear" diff --git a/pkg/cloudbroker/compute/anti_affinity_rule_remove.go b/pkg/cloudbroker/compute/anti_affinity_rule_remove.go index e7a52d0..037f148 100644 --- a/pkg/cloudbroker/compute/anti_affinity_rule_remove.go +++ b/pkg/cloudbroker/compute/anti_affinity_rule_remove.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,18 +12,18 @@ import ( type AntiAffinityRuleRemoveRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Compute or node, for whom rule applies // Required: true - Topology string `url:"topology" json:"topology"` + Topology string `url:"topology" json:"topology" validate:"computeTopology"` // The degree of 'strictness' of this rule // Should be one of: // - RECOMMENDED // - REQUIRED // Required: true - Policy string `url:"policy" json:"policy"` + Policy string `url:"policy" json:"policy" validate:"computePolicy"` // The comparison mode is 'value', recorded by the specified 'key' // Should be one of: @@ -32,57 +31,24 @@ type AntiAffinityRuleRemoveRequest struct { // - EN // - ANY // Required: true - Mode string `url:"mode" json:"mode"` + Mode string `url:"mode" json:"mode" validate:"computeMode"` // Key that are taken into account when analyzing this rule will be identified // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Value that must match the key to be taken into account when analyzing this rule // Required: true - Value string `url:"value" json:"value"` -} - -func (crq AntiAffinityRuleRemoveRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Topology == "" { - return errors.New("validation-error: field Topology must be set") - } - validator := validators.StringInSlice(crq.Topology, []string{"compute", "node"}) - if !validator { - return errors.New("validation-error: field Topology can be only compute or node") - } - if crq.Policy == "" { - return errors.New("validation-error: field Policy must be set") - } - validator = validators.StringInSlice(crq.Policy, []string{"RECOMMENDED", "REQUIRED"}) - if !validator { - return errors.New("validation-error: field Policy can be only RECOMMENDED or REQUIRED") - } - if crq.Mode == "" { - return errors.New("validation-error: field Mode must be set") - } - validator = validators.StringInSlice(crq.Mode, []string{"EQ", "NE", "ANY"}) - if !validator { - return errors.New("validation-error: field Mode can be only EQ, NE or ANY") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - if crq.Value == "" { - return errors.New("validation-error: field Value must be set") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // AntiAffinityRuleRemove remove anti affinity rule func (c Compute) AntiAffinityRuleRemove(ctx context.Context, req AntiAffinityRuleRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/antiAffinityRuleRemove" diff --git a/pkg/cloudbroker/compute/attach_gpu.go b/pkg/cloudbroker/compute/attach_gpu.go index 8dfbe4e..254f939 100644 --- a/pkg/cloudbroker/compute/attach_gpu.go +++ b/pkg/cloudbroker/compute/attach_gpu.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach GPU for compute type AttachGPURequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Identifier vGPU // Required: true - VGPUID uint64 `url:"vgpuId" json:"vgpuId"` -} - -func (crq AttachGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.VGPUID == 0 { - return errors.New("validation-error: field VGPUID must be set") - } - - return nil + VGPUID uint64 `url:"vgpuId" json:"vgpuId" validate:"required"` } // AttachGPU attach GPU for compute, returns vGPU ID on success func (c Compute) AttachGPU(ctx context.Context, req AttachGPURequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/attachGpu" diff --git a/pkg/cloudbroker/compute/attach_pci_device.go b/pkg/cloudbroker/compute/attach_pci_device.go index afb1a06..fb3be21 100644 --- a/pkg/cloudbroker/compute/attach_pci_device.go +++ b/pkg/cloudbroker/compute/attach_pci_device.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for atttach PCI device type AttachPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // PCI device ID // Required: true - DeviceID uint64 `url:"deviceId" json:"deviceId"` -} - -func (crq AttachPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DeviceID == 0 { - return errors.New("validation-error: field DeviceID must be set") - } - - return nil + DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"` } // AttachPCIDevice attach PCI device func (c Compute) AttachPCIDevice(ctx context.Context, req AttachPCIDeviceRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/attachPciDevice" diff --git a/pkg/cloudbroker/compute/audits.go b/pkg/cloudbroker/compute/audits.go index ceebb5e..7edd52d 100644 --- a/pkg/cloudbroker/compute/audits.go +++ b/pkg/cloudbroker/compute/audits.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audit records type AuditsRequest struct { // ID of the compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq AuditsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // Audits gets audit records for the specified compute object func (c Compute) Audits(ctx context.Context, req AuditsRequest) (ListDetailedAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/audits" diff --git a/pkg/cloudbroker/compute/boot_order_get.go b/pkg/cloudbroker/compute/boot_order_get.go index 67bef88..b76a77e 100644 --- a/pkg/cloudbroker/compute/boot_order_get.go +++ b/pkg/cloudbroker/compute/boot_order_get.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get boot order type BootOrderGetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq BootOrderGetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // BootOrderGet gets actual compute boot order information func (c Compute) BootOrderGet(ctx context.Context, req BootOrderGetRequest) ([]string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/bootOrderGet" diff --git a/pkg/cloudbroker/compute/boot_order_set.go b/pkg/cloudbroker/compute/boot_order_set.go index 2f1166f..71edfe1 100644 --- a/pkg/cloudbroker/compute/boot_order_set.go +++ b/pkg/cloudbroker/compute/boot_order_set.go @@ -3,7 +3,6 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -13,7 +12,7 @@ import ( type BootOrderSetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // List of boot devices // Should be one of: @@ -21,30 +20,16 @@ type BootOrderSetRequest struct { // - network // - hd // Required: true - Order []string `url:"order" json:"order"` -} - -func (crq BootOrderSetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if len(crq.Order) == 0 { - return errors.New("validation-error: field Order must be set") - } - for _, value := range crq.Order { - if validate := validators.StringInSlice(value, []string{"cdrom", "network", "hd"}); !validate { - return errors.New("validation-error: field ImageType can be cdrom, network, hd") - } - } - - return nil + Order []string `url:"order" json:"order" validate:"min=1,computeOrder"` } // BootOrderSet sets compute boot order func (c Compute) BootOrderSet(ctx context.Context, req BootOrderSetRequest) ([]string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/bootOrderSet" diff --git a/pkg/cloudbroker/compute/cd_eject.go b/pkg/cloudbroker/compute/cd_eject.go index 250e4a2..5dfdfa2 100644 --- a/pkg/cloudbroker/compute/cd_eject.go +++ b/pkg/cloudbroker/compute/cd_eject.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for eject CD image type CDEjectRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason to eject // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq CDEjectRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must ve set") - } - - return nil -} - // CDEject eject CD image to compute's CD-ROM func (c Compute) CDEject(ctx context.Context, req CDEjectRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/cdEject" diff --git a/pkg/cloudbroker/compute/cd_insert.go b/pkg/cloudbroker/compute/cd_insert.go index c2bf6da..415937d 100644 --- a/pkg/cloudbroker/compute/cd_insert.go +++ b/pkg/cloudbroker/compute/cd_insert.go @@ -2,41 +2,33 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for insert new CD image type CDInsertRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of CD-ROM image // Required: true - CDROMID uint64 `url:"cdromId" json:"cdromId"` + CDROMID uint64 `url:"cdromId" json:"cdromId" validate:"required"` // Reason to insert // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq CDInsertRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.CDROMID == 0 { - return errors.New("validation-error: field CDROMID must be set") - } - - return nil -} - // CDInsert insert new CD image to compute's CD-ROM func (c Compute) CDInsert(ctx context.Context, req CDInsertRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/cdInsert" diff --git a/pkg/cloudbroker/compute/clone.go b/pkg/cloudbroker/compute/clone.go index 25c4d94..b814ebe 100644 --- a/pkg/cloudbroker/compute/clone.go +++ b/pkg/cloudbroker/compute/clone.go @@ -2,20 +2,21 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for clone compute instance type CloneRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the clone // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Timestamp of the parent's snapshot to create clone from // Required: false @@ -30,22 +31,13 @@ type CloneRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq CloneRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil -} - // Clone clones compute instance func (c Compute) Clone(ctx context.Context, req CloneRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/clone" diff --git a/pkg/cloudbroker/compute/computeci_set.go b/pkg/cloudbroker/compute/computeci_set.go index 171bb8d..202de4d 100644 --- a/pkg/cloudbroker/compute/computeci_set.go +++ b/pkg/cloudbroker/compute/computeci_set.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set compute CI type ComputeCISetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the Compute CI // Required: true - ComputeCIID uint64 `url:"computeciId" json:"computeciId"` -} - -func (crq ComputeCISetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.ComputeCIID == 0 { - return errors.New("validation-error: field ComputeCIID must be set") - } - - return nil + ComputeCIID uint64 `url:"computeciId" json:"computeciId" validate:"required"` } // ComputeCISet sets compute CI ID for compute func (c Compute) ComputeCISet(ctx context.Context, req ComputeCISetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/computeciSet" diff --git a/pkg/cloudbroker/compute/computeci_unset.go b/pkg/cloudbroker/compute/computeci_unset.go index 8905c99..4691ad5 100644 --- a/pkg/cloudbroker/compute/computeci_unset.go +++ b/pkg/cloudbroker/compute/computeci_unset.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unset compite CI type ComputeCIUnsetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ComputeCIUnsetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ComputeCIUnset unsets compute CI ID from compute func (c Compute) ComputeCIUnset(ctx context.Context, req ComputeCIUnsetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/computeciUnset" diff --git a/pkg/cloudbroker/compute/create_template.go b/pkg/cloudbroker/compute/create_template.go index 7e995e1..b6e708e 100644 --- a/pkg/cloudbroker/compute/create_template.go +++ b/pkg/cloudbroker/compute/create_template.go @@ -2,21 +2,22 @@ package compute import ( "context" - "errors" "net/http" "strconv" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create template type CreateTemplateRequest struct { // ID of the compute to create template from // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name to assign to the template being created // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Reason for action // Required: false @@ -29,22 +30,13 @@ type CreateTemplateRequest struct { async bool `url:"async"` } -func (crq CreateTemplateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil -} - // CreateTemplateAsync create template from compute instance func (c Compute) CreateTemplateAsync(ctx context.Context, req CreateTemplateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } req.async = true @@ -63,9 +55,11 @@ func (c Compute) CreateTemplateAsync(ctx context.Context, req CreateTemplateRequ // CreateTemplate create template from compute instance func (c Compute) CreateTemplate(ctx context.Context, req CreateTemplateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } req.async = false diff --git a/pkg/cloudbroker/compute/delete.go b/pkg/cloudbroker/compute/delete.go index 1b92d29..35ea390 100644 --- a/pkg/cloudbroker/compute/delete.go +++ b/pkg/cloudbroker/compute/delete.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete compute type DeleteRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Delete permanently // Required: false @@ -26,19 +27,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DeleteRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Delete deletes compute func (c Compute) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/delete" diff --git a/pkg/cloudbroker/compute/detach_gpu.go b/pkg/cloudbroker/compute/detach_gpu.go index 3d12eb7..b285284 100644 --- a/pkg/cloudbroker/compute/detach_gpu.go +++ b/pkg/cloudbroker/compute/detach_gpu.go @@ -2,36 +2,31 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach VGPU for compute type DetachGPURequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Identifier virtual GPU // Required: false VGPUID int64 `url:"vgpuId,omitempty" json:"vgpuId,omitempty"` } -func (crq DetachGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // DetachGPU detach VGPU for compute. // If param VGPU ID is equivalent -1, then detach all VGPU for compute func (c Compute) DetachGPU(ctx context.Context, req DetachGPURequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/detachGpu" diff --git a/pkg/cloudbroker/compute/detach_pci_device.go b/pkg/cloudbroker/compute/detach_pci_device.go index ef8d1ff..30f053b 100644 --- a/pkg/cloudbroker/compute/detach_pci_device.go +++ b/pkg/cloudbroker/compute/detach_pci_device.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach PCI device type DetachPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // PCI device ID // Required: true - DeviceID uint64 `url:"deviceId" json:"deviceId"` -} - -func (crq DetachPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DeviceID == 0 { - return errors.New("validation-error: field DeviceID must be set") - } - - return nil + DeviceID uint64 `url:"deviceId" json:"deviceId" validate:"required"` } // DetachPCIDevice detach PCI device func (c Compute) DetachPciDevice(ctx context.Context, req DetachPCIDeviceRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/detachPciDevice" diff --git a/pkg/cloudbroker/compute/disable.go b/pkg/cloudbroker/compute/disable.go index 18c07ca..a2ae8ad 100644 --- a/pkg/cloudbroker/compute/disable.go +++ b/pkg/cloudbroker/compute/disable.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable compute type DisableRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DisableRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Disable disables compute func (c Compute) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/disable" diff --git a/pkg/cloudbroker/compute/disk_add.go b/pkg/cloudbroker/compute/disk_add.go index 3d4b6d2..8ba915c 100644 --- a/pkg/cloudbroker/compute/disk_add.go +++ b/pkg/cloudbroker/compute/disk_add.go @@ -2,31 +2,32 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create and attach disk to compute type DiskAddRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name for disk // Required: true - DiskName string `url:"diskName" json:"diskName"` + DiskName string `url:"diskName" json:"diskName" validate:"required"` // Disk size in GB // Required: true - Size uint64 `url:"size" json:"size"` + Size uint64 `url:"size" json:"size" validate:"required"` // Type of the disk // Should be one of: // - D // - B // Required: false - DiskType string `url:"diskType,omitempty" json:"diskType,omitempty"` + DiskType string `url:"diskType,omitempty" json:"diskType,omitempty" validate:"omitempty,computeDiskType"` // Storage endpoint provider ID // By default the same with boot disk @@ -47,25 +48,13 @@ type DiskAddRequest struct { ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"` } -func (crq DiskAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskName == "" { - return errors.New("validation-error: field DiskName must be set") - } - if crq.Size == 0 { - return errors.New("validation-error: field Size must be set") - } - - return nil -} - // DiskAdd creates new disk and attach to compute func (c Compute) DiskAdd(ctx context.Context, req DiskAddRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskAdd" diff --git a/pkg/cloudbroker/compute/disk_attach.go b/pkg/cloudbroker/compute/disk_attach.go index 1e8dd2b..de1339b 100644 --- a/pkg/cloudbroker/compute/disk_attach.go +++ b/pkg/cloudbroker/compute/disk_attach.go @@ -2,42 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for attach disk to compute type DiskAttachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to attach // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DiskAttachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // DiskAttach attach disk to compute func (c Compute) DiskAttach(ctx context.Context, req DiskAttachRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskAttach" diff --git a/pkg/cloudbroker/compute/disk_del.go b/pkg/cloudbroker/compute/disk_del.go index 2dff7f1..86594ae 100644 --- a/pkg/cloudbroker/compute/disk_del.go +++ b/pkg/cloudbroker/compute/disk_del.go @@ -2,46 +2,38 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach and delete disk from compute type DiskDelRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of disk instance // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // False if disk is to be deleted to recycle bin // Required: true - Permanently bool `url:"permanently" json:"permanently"` + Permanently bool `url:"permanently" json:"permanently" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DiskDelRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // DiskDel delete disk and detach from compute func (c Compute) DiskDel(ctx context.Context, req DiskDelRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskDel" diff --git a/pkg/cloudbroker/compute/disk_detach.go b/pkg/cloudbroker/compute/disk_detach.go index 507175e..d1e9aa0 100644 --- a/pkg/cloudbroker/compute/disk_detach.go +++ b/pkg/cloudbroker/compute/disk_detach.go @@ -2,42 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach disk from compute type DiskDetachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to detach // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DiskDetachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // DiskDetach detach disk from compute func (c Compute) DiskDetach(ctx context.Context, req DiskDetachRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskDetach" diff --git a/pkg/cloudbroker/compute/disk_qos.go b/pkg/cloudbroker/compute/disk_qos.go index 99b333b..cd083b6 100644 --- a/pkg/cloudbroker/compute/disk_qos.go +++ b/pkg/cloudbroker/compute/disk_qos.go @@ -2,45 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change QOS of the disk type DiskQOSRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to apply limits // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Limit IO for a certain disk total and read/write options are not allowed to be combined // Required: true - Limits string `url:"limits" json:"limits"` -} - -func (crq DiskQOSRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if crq.Limits == "" { - return errors.New("validation-error: field Limits must be set") - } - - return nil + Limits string `url:"limits" json:"limits" validate:"required"` } // DiskQOS change QOS of the disk func (c Compute) DiskQOS(ctx context.Context, req DiskQOSRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskQos" diff --git a/pkg/cloudbroker/compute/disk_resize.go b/pkg/cloudbroker/compute/disk_resize.go index d1052f2..861f0d5 100644 --- a/pkg/cloudbroker/compute/disk_resize.go +++ b/pkg/cloudbroker/compute/disk_resize.go @@ -2,49 +2,38 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change disk size type DiskResizeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the disk to resize // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New disk size // Required: true - Size uint64 `url:"size" json:"size"` + Size uint64 `url:"size" json:"size" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq DiskResizeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if crq.Size == 0 { - return errors.New("validation-error: field Size must be set") - } - - return nil -} - // DiskResize change disk size func (c Compute) DiskResize(ctx context.Context, req DiskResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/diskResize" diff --git a/pkg/cloudbroker/compute/enable.go b/pkg/cloudbroker/compute/enable.go index b4c544a..8dc5fe3 100644 --- a/pkg/cloudbroker/compute/enable.go +++ b/pkg/cloudbroker/compute/enable.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable compute type EnableRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq EnableRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Enable enables compute func (c Compute) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/enable" diff --git a/pkg/cloudbroker/compute/filter.go b/pkg/cloudbroker/compute/filter.go index 1402123..2e5fa35 100644 --- a/pkg/cloudbroker/compute/filter.go +++ b/pkg/cloudbroker/compute/filter.go @@ -1,5 +1,13 @@ package compute +import ( + "context" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb" +) + // FilterByID returns ListComputes with specified ID. func (lc ListComputes) FilterByID(id uint64) ListComputes { predicate := func(ic ItemCompute) bool { @@ -50,6 +58,88 @@ func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes { return lc.FilterFunc(predicate) } +// FilterByK8SID returns master and worker nodes (ListComputes) inside specified K8S cluster. +func (lc ListComputes) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListComputes, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(ic ItemCompute) bool { + for _, info := range cluster.K8SGroups.Masters.DetailedInfo { + if info.ID == ic.ID { + return true + } + } + + for _, worker := range cluster.K8SGroups.Workers { + for _, info := range worker.DetailedInfo { + if info.ID == ic.ID { + return true + } + } + } + + return false + } + + return lc.FilterFunc(predicate), nil +} + +// K8SMasters is used to filter master nodes. Best used after FilterByK8SID function. +func (lc ListComputes) FilterByK8SMasters() ListComputes { + predicate := func(ic ItemCompute) bool { + for _, rule := range ic.AntiAffinityRules { + if rule.Value == "master" { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// K8SMasters is used to filter worker nodes. Best used after FilterByK8SID function. +func (lc ListComputes) FilterByK8SWorkers() ListComputes { + predicate := func(ic ItemCompute) bool { + for _, rule := range ic.AntiAffinityRules { + if rule.Value == "worker" { + return true + } + } + return false + } + + return lc.FilterFunc(predicate) +} + +// FilterByLBID is used to filter ListComputes used by specified Load Balancer. +func (lc ListComputes) FilterByLBID(ctx context.Context, lbID uint64, decortClient interfaces.Caller) (ListComputes, error) { + caller := lb.New(decortClient) + + req := lb.GetRequest{ + LBID: lbID, + } + + foundLB, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(ic ItemCompute) bool { + return ic.ID == foundLB.PrimaryNode.ComputeID || ic.ID == foundLB.SecondaryNode.ComputeID + } + + return lc.FilterFunc(predicate), nil +} + // FilterFunc allows filtering ListComputes based on a user-specified predicate. func (lc ListComputes) FilterFunc(predicate func(ItemCompute) bool) ListComputes { var result ListComputes diff --git a/pkg/cloudbroker/compute/get.go b/pkg/cloudbroker/compute/get.go index 7e88901..6fbdca0 100644 --- a/pkg/cloudbroker/compute/get.go +++ b/pkg/cloudbroker/compute/get.go @@ -3,34 +3,29 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request for get information about compute type GetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason to action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq GetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Get gets information about compute func (c Compute) Get(ctx context.Context, req GetRequest) (*RecordCompute, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/get" diff --git a/pkg/cloudbroker/compute/get_audits.go b/pkg/cloudbroker/compute/get_audits.go index 04dfd39..e8c62e1 100644 --- a/pkg/cloudbroker/compute/get_audits.go +++ b/pkg/cloudbroker/compute/get_audits.go @@ -3,34 +3,29 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute audits type GetAuditsRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason to action // Required: true Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq GetAuditsRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // GetAudits gets compute audits func (c Compute) GetAudits(ctx context.Context, req GetAuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/getAudits" diff --git a/pkg/cloudbroker/compute/get_console_url.go b/pkg/cloudbroker/compute/get_console_url.go index b0ddaa2..9ae4888 100644 --- a/pkg/cloudbroker/compute/get_console_url.go +++ b/pkg/cloudbroker/compute/get_console_url.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get console URL type GetConsoleURLRequest struct { // ID of compute instance to get console for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq GetConsoleURLRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // GetConsoleURL gets computes console URL func (c Compute) GetConsoleURL(ctx context.Context, req GetConsoleURLRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/getConsoleUrl" diff --git a/pkg/cloudbroker/compute/get_log.go b/pkg/cloudbroker/compute/get_log.go index ee72b8b..910bb3d 100644 --- a/pkg/cloudbroker/compute/get_log.go +++ b/pkg/cloudbroker/compute/get_log.go @@ -2,37 +2,29 @@ package compute import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute logs type GetLogRequest struct { // ID of compute instance to get log for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Path to log file // Required: true - Path string `url:"path" json:"path"` -} - -func (crq GetLogRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Path == "" { - return errors.New("validation-error: field Path must be set") - } - - return nil + Path string `url:"path" json:"path" validate:"required"` } // GetLog gets compute's log file by path func (c Compute) GetLog(ctx context.Context, req GetLogRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/getLog" diff --git a/pkg/cloudbroker/compute/list_gpu.go b/pkg/cloudbroker/compute/list_gpu.go index 3e8cc08..f6ab49b 100644 --- a/pkg/cloudbroker/compute/list_gpu.go +++ b/pkg/cloudbroker/compute/list_gpu.go @@ -3,34 +3,29 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list GPU for compute type ListGPURequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Also list destroyed // Required: false ListDestroyed bool `url:"list_destroyed,omitempty" json:"list_destroyed,omitempty"` } -func (crq ListGPURequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // ListVGPU gets list GPU for compute func (c Compute) ListGPU(ctx context.Context, req ListGPURequest) ([]interface{}, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/listGpu" diff --git a/pkg/cloudbroker/compute/list_pci_device.go b/pkg/cloudbroker/compute/list_pci_device.go index 6e2231d..4cd5396 100644 --- a/pkg/cloudbroker/compute/list_pci_device.go +++ b/pkg/cloudbroker/compute/list_pci_device.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list PCI devices type ListPCIDeviceRequest struct { // Identifier compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq ListPCIDeviceRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // ListPCIDevice gets list PCI device func (c Compute) ListPCIDevice(ctx context.Context, req ListPCIDeviceRequest) (ListPCIDevices, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/listPciDevice" diff --git a/pkg/cloudbroker/compute/mass_delete.go b/pkg/cloudbroker/compute/mass_delete.go index 087c946..048da6b 100644 --- a/pkg/cloudbroker/compute/mass_delete.go +++ b/pkg/cloudbroker/compute/mass_delete.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete several computes type MassDeleteRequest struct { // IDs of compute instances to delete // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Delete computes permanently // Required: false @@ -22,19 +23,13 @@ type MassDeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MassDeleteRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil -} - // MassDelete starts jobs to delete several computes func (c Compute) MassDelete(ctx context.Context, req MassDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/massDelete" diff --git a/pkg/cloudbroker/compute/mass_reboot.go b/pkg/cloudbroker/compute/mass_reboot.go index 6141678..f33426b 100644 --- a/pkg/cloudbroker/compute/mass_reboot.go +++ b/pkg/cloudbroker/compute/mass_reboot.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reboot several computes type MassRebootRequest struct { // IDs of compute instances to reboot // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MassRebootRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil -} - // MassReboot starts jobs to reboot several computes func (c Compute) MassReboot(ctx context.Context, req MassRebootRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/massReboot" diff --git a/pkg/cloudbroker/compute/mass_repair_boot_fs.go b/pkg/cloudbroker/compute/mass_repair_boot_fs.go index a7b2058..d427189 100644 --- a/pkg/cloudbroker/compute/mass_repair_boot_fs.go +++ b/pkg/cloudbroker/compute/mass_repair_boot_fs.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for repair boot disk filesystem on several computes type MassRepairBootFSRequest struct { // IDs of compute instances which boot file systems will be repaired // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MassRepairBootFSRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil -} - // MassRepairBootFS repair boot disk filesystem on several computes func (c Compute) MassRepairBootFS(ctx context.Context, req MassRepairBootFSRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/massRepairBootFs" diff --git a/pkg/cloudbroker/compute/mass_start.go b/pkg/cloudbroker/compute/mass_start.go index 9247e3b..ab87808 100644 --- a/pkg/cloudbroker/compute/mass_start.go +++ b/pkg/cloudbroker/compute/mass_start.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start several computes type MassStartRequest struct { // IDs of compute instances to start // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MassStartRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil -} - // MassStart starts jobs to start several computes func (c Compute) MassStart(ctx context.Context, req MassStartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/massStart" diff --git a/pkg/cloudbroker/compute/mass_stop.go b/pkg/cloudbroker/compute/mass_stop.go index e1d5189..83cc054 100644 --- a/pkg/cloudbroker/compute/mass_stop.go +++ b/pkg/cloudbroker/compute/mass_stop.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for several stop computes type MassStopRequest struct { // IDs of compute instances to stop // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Force stop compute // Required: false @@ -22,19 +23,13 @@ type MassStopRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MassStopRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - - return nil -} - // MassStop starts jobs to stop several computes func (c Compute) MassStop(ctx context.Context, req MassStopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/massStop" diff --git a/pkg/cloudbroker/compute/migrate.go b/pkg/cloudbroker/compute/migrate.go index b00b518..9645366 100644 --- a/pkg/cloudbroker/compute/migrate.go +++ b/pkg/cloudbroker/compute/migrate.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for migrate compute type MigrateRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Particular Stack ID to migrate this compute to // Required: false @@ -27,19 +28,13 @@ type MigrateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq MigrateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Migrate migrates compute to another stack func (c Compute) Migrate(ctx context.Context, req MigrateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/migrate" diff --git a/pkg/cloudbroker/compute/migrate_storage.go b/pkg/cloudbroker/compute/migrate_storage.go index 648e773..1824e83 100644 --- a/pkg/cloudbroker/compute/migrate_storage.go +++ b/pkg/cloudbroker/compute/migrate_storage.go @@ -2,49 +2,33 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for migration type MigrateStorageRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // SEP ID to migrate disks // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + SEPID uint64 `url:"sepId" json:"sepId" validate:"required"` // SEP pool name to migrate disks // Required: true - PoolName string `url:"poolName" json:"poolName"` + PoolName string `url:"poolName" json:"poolName" validate:"required"` // Target stack ID // Required: true - StackID uint64 `url:"stackId" json:"stackId"` + StackID uint64 `url:"stackId" json:"stackId" validate:"required"` // Async API call // Required: true - Sync bool `url:"sync" json:"sync"` -} - -func (crq MigrateStorageRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if crq.PoolName == "" { - return errors.New("validation-error: field PoolName must be set") - } - if crq.StackID == 0 { - return errors.New("validation-error: field StackID must be set") - } - - return nil + Sync bool `url:"sync" json:"sync" validate:"required"` } // MigrateStorage gets complex compute migration @@ -52,9 +36,11 @@ func (crq MigrateStorageRequest) validate() error { // be migrated to specified SEP to specified pool. // This action can take up to 84 hours func (c Compute) MigrateStorage(ctx context.Context, req MigrateStorageRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/migrateStorage" diff --git a/pkg/cloudbroker/compute/migrate_storage_abort.go b/pkg/cloudbroker/compute/migrate_storage_abort.go index 02b6ccc..d52401e 100644 --- a/pkg/cloudbroker/compute/migrate_storage_abort.go +++ b/pkg/cloudbroker/compute/migrate_storage_abort.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for abort migration type MigrateStorageAbortRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq MigrateStorageAbortRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // MigrateStorageAbort abort complex compute migration job func (c Compute) MigrateStorageAbort(ctx context.Context, req MigrateStorageAbortRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/migrateStorageAbort" diff --git a/pkg/cloudbroker/compute/migrate_storage_clean_up.go b/pkg/cloudbroker/compute/migrate_storage_clean_up.go index 6d4c4d1..68d8275 100644 --- a/pkg/cloudbroker/compute/migrate_storage_clean_up.go +++ b/pkg/cloudbroker/compute/migrate_storage_clean_up.go @@ -2,33 +2,28 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for cleanup resources after finished migration type MigrateStorageCleanUpRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq MigrateStorageCleanUpRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // MigrateStorageCleanUp cleanup resources after finished (success of failed) complex compute migration. // If the migration was successful, then old disks will be removed, else new (target) disks will be removed. // Do it wisely! func (c Compute) MigrateStorageCleanUp(ctx context.Context, req MigrateStorageCleanUpRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/migrateStorageCleanup" diff --git a/pkg/cloudbroker/compute/migrate_storage_info.go b/pkg/cloudbroker/compute/migrate_storage_info.go index 4ef8af6..4fb8806 100644 --- a/pkg/cloudbroker/compute/migrate_storage_info.go +++ b/pkg/cloudbroker/compute/migrate_storage_info.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get info about migration type MigrateStorageInfoRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq MigrateStorageInfoRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // MigrateStorageInfo gets info about last (include ongoing) storage migration func (c Compute) MigrateStorageInfo(ctx context.Context, req MigrateStorageInfoRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/migrateStorageInfo" diff --git a/pkg/cloudbroker/compute/move_to_rg.go b/pkg/cloudbroker/compute/move_to_rg.go index 2bd7e42..f7e6e76 100644 --- a/pkg/cloudbroker/compute/move_to_rg.go +++ b/pkg/cloudbroker/compute/move_to_rg.go @@ -2,20 +2,21 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for move compute new resource group type MoveToRGRequest struct { // ID of the compute instance to move // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the target resource group // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // New name for the compute upon successful move, // if name change required. @@ -33,22 +34,13 @@ type MoveToRGRequest struct { ForceStop bool `url:"forceStop,omitempty" json:"forceStop,omitempty"` } -func (crq MoveToRGRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // MoveToRG moves compute instance to new resource group func (c Compute) Validate(ctx context.Context, req MoveToRGRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/moveToRg" diff --git a/pkg/cloudbroker/compute/net_attach.go b/pkg/cloudbroker/compute/net_attach.go index 75c611a..3b13667 100644 --- a/pkg/cloudbroker/compute/net_attach.go +++ b/pkg/cloudbroker/compute/net_attach.go @@ -3,7 +3,6 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -13,19 +12,19 @@ import ( type NetAttachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Network type // 'EXTNET' for connect to external network directly // and 'VINS' for connect to ViNS // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"computeNetType"` // Network ID for connect to // For EXTNET - external network ID // For VINS - VINS ID // Required: true - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId" json:"netId" validate:"required"` // Directly required IP address for new network interface // Required: true @@ -36,29 +35,13 @@ type NetAttachRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq NetAttachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.NetType == "" { - return errors.New("validation-error: field NetType must be set") - } - validator := validators.StringInSlice(crq.NetType, []string{"EXTNET", "VINS"}) - if !validator { - return errors.New("validation-error: field NetType can be only EXTNET or VINS") - } - if crq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // NetAttach attach network to compute and gets info about network func (c Compute) NetAttach(ctx context.Context, req NetAttachRequest) (*RecordNetAttach, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/netAttach" diff --git a/pkg/cloudbroker/compute/net_detach.go b/pkg/cloudbroker/compute/net_detach.go index 0815da4..6ce79df 100644 --- a/pkg/cloudbroker/compute/net_detach.go +++ b/pkg/cloudbroker/compute/net_detach.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for detach networ to compute type NetDetachRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // IP of the network interface // Required: false @@ -26,19 +27,13 @@ type NetDetachRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq NetDetachRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // NetDetach detach network to compute func (c Compute) NetDetach(ctx context.Context, req NetDetachRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/netDetach" diff --git a/pkg/cloudbroker/compute/net_qos.go b/pkg/cloudbroker/compute/net_qos.go index 265c058..a6e8cab 100644 --- a/pkg/cloudbroker/compute/net_qos.go +++ b/pkg/cloudbroker/compute/net_qos.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,18 +12,18 @@ import ( type NetQOSRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Network ID // Required: true - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId" json:"netId" validate:"required"` // Network type // Should be one of: // - VINS // - EXTNET // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"computeNetType"` // Internal traffic, kbit // Required: false @@ -39,29 +38,13 @@ type NetQOSRequest struct { EgressRate uint64 `url:"egress_rate,omitempty" json:"egress_rate,omitempty"` } -func (crq NetQOSRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.NetType == "" { - return errors.New("validation-error: field NetType must be set") - } - validator := validators.StringInSlice(crq.NetType, []string{"EXTNET", "VINS"}) - if !validator { - return errors.New("validation-error: field NetType can be only EXTNET or VINS") - } - if crq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // NetQOS update compute interfaces QOS func (c Compute) NetQOS(ctx context.Context, req NetQOSRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/netQos" diff --git a/pkg/cloudbroker/compute/pause.go b/pkg/cloudbroker/compute/pause.go index f31053d..b5d0460 100644 --- a/pkg/cloudbroker/compute/pause.go +++ b/pkg/cloudbroker/compute/pause.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for pause compute type PauseRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq PauseRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Pause pause compute func (c Compute) Pause(ctx context.Context, req PauseRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/pause" diff --git a/pkg/cloudbroker/compute/pfw_add.go b/pkg/cloudbroker/compute/pfw_add.go index ccbea22..ff220d0 100644 --- a/pkg/cloudbroker/compute/pfw_add.go +++ b/pkg/cloudbroker/compute/pfw_add.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type PFWAddRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // External start port number for the rule // Required: true - PublicPortStart uint64 `url:"publicPortStart" json:"publicPortStart"` + PublicPortStart uint64 `url:"publicPortStart" json:"publicPortStart" validate:"required"` // End port number (inclusive) for the ranged rule // Required: false @@ -25,46 +24,27 @@ type PFWAddRequest struct { // Internal base port number // Required: true - LocalBasePort uint64 `url:"localBasePort" json:"localBasePort"` + LocalBasePort uint64 `url:"localBasePort" json:"localBasePort" validate:"required"` // Network protocol // Should be one of: // - tcp // - udp // Required: true - Proto string `url:"proto" json:"proto"` + Proto string `url:"proto" json:"proto" validate:"proto"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq PFWAddRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.PublicPortStart == 0 { - return errors.New("validation-error: field PublicPortStart must be set") - } - if crq.LocalBasePort == 0 { - return errors.New("validation-error: field LocalBasePort must be set") - } - if crq.Proto == "" { - return errors.New("validation-error: field Proto must be set") - } - validate := validators.StringInSlice(crq.Proto, []string{"tcp", "udp"}) - if !validate { - return errors.New("validation-error: field Proto must be tcp or udp") - } - - return nil -} - // PFWAdd add port forward rule func (c Compute) PFWAdd(ctx context.Context, req PFWAddRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/pfwAdd" diff --git a/pkg/cloudbroker/compute/pfw_del.go b/pkg/cloudbroker/compute/pfw_del.go index a489d7c..0d3221a 100644 --- a/pkg/cloudbroker/compute/pfw_del.go +++ b/pkg/cloudbroker/compute/pfw_del.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete port forward rule type PFWDelRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the rule to delete. If specified, all other arguments will be ignored // Required: false @@ -34,26 +35,20 @@ type PFWDelRequest struct { // - tcp // - udp // Required: false - Proto string `url:"proto,omitempty" json:"proto,omitempty"` + Proto string `url:"proto,omitempty" json:"proto,omitempty" validate:"omitempty,proto"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq PFWDelRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // PFWDel delete port forward rule func (c Compute) PFWDel(ctx context.Context, req PFWDelRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/pfwDel" diff --git a/pkg/cloudbroker/compute/pfw_list.go b/pkg/cloudbroker/compute/pfw_list.go index 4e14e8c..4fadaa1 100644 --- a/pkg/cloudbroker/compute/pfw_list.go +++ b/pkg/cloudbroker/compute/pfw_list.go @@ -3,34 +3,29 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list port forwards type PFWListRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq PFWListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // PFWList gets compute port forwards list func (c Compute) PFWList(ctx context.Context, req PFWListRequest) (ListPFW, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/pfwList" diff --git a/pkg/cloudbroker/compute/pin_to_stack.go b/pkg/cloudbroker/compute/pin_to_stack.go index 365f0b5..9d4dfd7 100644 --- a/pkg/cloudbroker/compute/pin_to_stack.go +++ b/pkg/cloudbroker/compute/pin_to_stack.go @@ -2,42 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for pin comptute to stack type PinToStackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Stack ID to pin to // Required: true - TargetStackID uint64 `url:"targetStackId" json:"targetStackId"` + TargetStackID uint64 `url:"targetStackId" json:"targetStackId" validate:"required"` // Try to migrate or not if compute in running states // Required: false Force bool `url:"force" json:"force"` } -func (crq PinToStackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.TargetStackID == 0 { - return errors.New("validation-error: field TargetStackID must be set") - } - - return nil -} - // PinToStack pin compute to current stack func (c Compute) PinToStack(ctx context.Context, req PinToStackRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/pinToStack" diff --git a/pkg/cloudbroker/compute/power_cycle.go b/pkg/cloudbroker/compute/power_cycle.go index 6ad9c4d..60b53a7 100644 --- a/pkg/cloudbroker/compute/power_cycle.go +++ b/pkg/cloudbroker/compute/power_cycle.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for force stop and start compute type PowerCycleRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq PowerCycleRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // PowerCycle makes force stop and start compute func (c Compute) PowerCycle(ctx context.Context, req PowerCycleRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/powerCycle" diff --git a/pkg/cloudbroker/compute/reboot.go b/pkg/cloudbroker/compute/reboot.go index 6aaf147..1859170 100644 --- a/pkg/cloudbroker/compute/reboot.go +++ b/pkg/cloudbroker/compute/reboot.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reboot compute type RebootRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq RebootRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Reboot reboot compute func (c Compute) Reboot(ctx context.Context, req RebootRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/reboot" diff --git a/pkg/cloudbroker/compute/redeploy.go b/pkg/cloudbroker/compute/redeploy.go index 2eff6dd..7c61058 100644 --- a/pkg/cloudbroker/compute/redeploy.go +++ b/pkg/cloudbroker/compute/redeploy.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for redeploy type RedeployRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of the new OS image, if image change is required // Required: false @@ -27,7 +28,7 @@ type RedeployRequest struct { // - DETACH // - DESTROY // Required: false - DataDisks string `url:"dataDisks,omitempty" json:"dataDisks,omitempty"` + DataDisks string `url:"dataDisks,omitempty" json:"dataDisks,omitempty" validate:"omitempty,computeDataDisks"` // Should the compute be restarted upon successful redeploy // Required: false @@ -42,19 +43,13 @@ type RedeployRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq RedeployRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Redeploy redeploy compute func (c Compute) Redeploy(ctx context.Context, req RedeployRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/redeploy" diff --git a/pkg/cloudbroker/compute/registration.go b/pkg/cloudbroker/compute/registration.go index 0f000d6..e89ec3c 100644 --- a/pkg/cloudbroker/compute/registration.go +++ b/pkg/cloudbroker/compute/registration.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set compute registered in RT type RegistrationRequest struct { // ID of the Compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Unique compute registration key // Required: true - RegistrationKey string `url:"registrationKey" json:"registrationKey"` -} - -func (crq RegistrationRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.RegistrationKey == "" { - return errors.New("validation-error: field RegistrationKey must be set") - } - - return nil + RegistrationKey string `url:"registrationKey" json:"registrationKey" validate:"required"` } // Registration sets compute registered in RT func (c Compute) Registration(ctx context.Context, req RegistrationRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/registration" diff --git a/pkg/cloudbroker/compute/repair_boot_fs.go b/pkg/cloudbroker/compute/repair_boot_fs.go index b9db663..c52f5f4 100644 --- a/pkg/cloudbroker/compute/repair_boot_fs.go +++ b/pkg/cloudbroker/compute/repair_boot_fs.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for repair filesystem type RepairBootFSRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq RepairBootFSRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // RepairBootFS repair compute boot disk filesystem func (c Compute) RepairBootFS(ctx context.Context, req RepairBootFSRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/repairBootFs" diff --git a/pkg/cloudbroker/compute/reset.go b/pkg/cloudbroker/compute/reset.go index d703404..ed34749 100644 --- a/pkg/cloudbroker/compute/reset.go +++ b/pkg/cloudbroker/compute/reset.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reset compute type ResetRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq ResetRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Reset reset compute func (c Compute) Reset(ctx context.Context, req ResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/reset" diff --git a/pkg/cloudbroker/compute/resize.go b/pkg/cloudbroker/compute/resize.go index 7c1a334..4861d0b 100644 --- a/pkg/cloudbroker/compute/resize.go +++ b/pkg/cloudbroker/compute/resize.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resize compute type ResizeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // New CPU count. // Pass 0 if no change to CPU count is required @@ -32,19 +33,13 @@ type ResizeRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq ResizeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Resize resize compute instance func (c Compute) Resize(ctx context.Context, req ResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/resize" diff --git a/pkg/cloudbroker/compute/restore.go b/pkg/cloudbroker/compute/restore.go index df08be6..6dce42d 100644 --- a/pkg/cloudbroker/compute/restore.go +++ b/pkg/cloudbroker/compute/restore.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore compute type RestoreRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq RestoreRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Restore restore compute from recycle bin func (c Compute) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/restore" diff --git a/pkg/cloudbroker/compute/resume.go b/pkg/cloudbroker/compute/resume.go index 2e902c2..e1635c1 100644 --- a/pkg/cloudbroker/compute/resume.go +++ b/pkg/cloudbroker/compute/resume.go @@ -2,35 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resume compute type ResumeRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq ResumeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Resume resume Compute from paused state func (c Compute) Resume(ctx context.Context, req ResumeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/resume" diff --git a/pkg/cloudbroker/compute/snapshot_create.go b/pkg/cloudbroker/compute/snapshot_create.go index 5877868..10d134b 100644 --- a/pkg/cloudbroker/compute/snapshot_create.go +++ b/pkg/cloudbroker/compute/snapshot_create.go @@ -2,39 +2,31 @@ package compute import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create snapshot type SnapshotCreateRequest struct { // ID of the compute instance to create snapshot for // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label for snapshot. // Must be unique among this compute snapshots // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotCreateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Label == "" { - return errors.New("validation-error: field Label must be set") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotCreate create compute snapshot func (c Compute) SnapshotCreate(ctx context.Context, req SnapshotCreateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotCreate" diff --git a/pkg/cloudbroker/compute/snapshot_delete.go b/pkg/cloudbroker/compute/snapshot_delete.go index 5cc7e13..a97ed53 100644 --- a/pkg/cloudbroker/compute/snapshot_delete.go +++ b/pkg/cloudbroker/compute/snapshot_delete.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete snapshot type SnapshotDeleteRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label of snapshot to delete // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotDeleteRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Label == "" { - return errors.New("validation-error: field Label must be set") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotDelete delete specified compute snapshot func (c Compute) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotDelete" diff --git a/pkg/cloudbroker/compute/snapshot_evict_disk.go b/pkg/cloudbroker/compute/snapshot_evict_disk.go index 4b9696b..d2a9e8a 100644 --- a/pkg/cloudbroker/compute/snapshot_evict_disk.go +++ b/pkg/cloudbroker/compute/snapshot_evict_disk.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for evict specified disk type SnapshotEvictDiskRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" valdiate:"required"` // ID of the disk instance // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (crq SnapshotEvictDiskRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // SnapshotEvictDisk evict specified disk from all snapshots of a compute instance func (c Compute) SnapshotEvictDisk(ctx context.Context, req SnapshotEvictDiskRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotEvictDisk" diff --git a/pkg/cloudbroker/compute/snapshot_list.go b/pkg/cloudbroker/compute/snapshot_list.go index f3b2313..ec52356 100644 --- a/pkg/cloudbroker/compute/snapshot_list.go +++ b/pkg/cloudbroker/compute/snapshot_list.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list snapshots type SnapshotListRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq SnapshotListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // SnapshotList gets list compute snapshots func (c Compute) SnapshotList(ctx context.Context, req SnapshotListRequest) (ListSnapshots, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotList" diff --git a/pkg/cloudbroker/compute/snapshot_rollback.go b/pkg/cloudbroker/compute/snapshot_rollback.go index d66a424..1206290 100644 --- a/pkg/cloudbroker/compute/snapshot_rollback.go +++ b/pkg/cloudbroker/compute/snapshot_rollback.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rollback type SnapshotRollbackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Text label of snapshot to rollback // Required: true - Label string `url:"label" json:"label"` -} - -func (crq SnapshotRollbackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Label == "" { - return errors.New("validation-error: field Label must be set") - } - - return nil + Label string `url:"label" json:"label" validate:"required"` } // SnapshotRollback rollback specified compute snapshot func (c Compute) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotRollback" diff --git a/pkg/cloudbroker/compute/snapshot_usage.go b/pkg/cloudbroker/compute/snapshot_usage.go index 740a0a7..33c34fe 100644 --- a/pkg/cloudbroker/compute/snapshot_usage.go +++ b/pkg/cloudbroker/compute/snapshot_usage.go @@ -3,15 +3,16 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get compute snapshot real size on storage type SnapshotUsageRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Specify to show usage exact for this snapshot. // Leave empty for get usage for all compute snapshots @@ -19,21 +20,15 @@ type SnapshotUsageRequest struct { Label string `url:"label,omitempty" json:"label,omitempty"` } -func (crq SnapshotUsageRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // SnapshotUsage Get compute snapshot real size on storage. // Always returns list of json objects, and first json object contains summary about all related // snapshots. func (c Compute) SnapshotUsage(ctx context.Context, req SnapshotUsageRequest) (ListSnapshotUsage, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/snapshotUsage" diff --git a/pkg/cloudbroker/compute/start.go b/pkg/cloudbroker/compute/start.go index 7de68f6..187c7ad 100644 --- a/pkg/cloudbroker/compute/start.go +++ b/pkg/cloudbroker/compute/start.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start compute type StartRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // ID of CD-ROM live image to boot // Required: false @@ -26,19 +27,13 @@ type StartRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq StartRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Start starts compute func (c Compute) Start(ctx context.Context, req StartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/start" diff --git a/pkg/cloudbroker/compute/stop.go b/pkg/cloudbroker/compute/stop.go index 131dd55..1dc6a1d 100644 --- a/pkg/cloudbroker/compute/stop.go +++ b/pkg/cloudbroker/compute/stop.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop compute type StopRequest struct { // ID of compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Force stop compute // Required: false @@ -22,19 +23,13 @@ type StopRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq StopRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field computeID must be set") - } - - return nil -} - // Stop stops compute func (c Compute) Stop(ctx context.Context, req StopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/stop" diff --git a/pkg/cloudbroker/compute/tag_add.go b/pkg/cloudbroker/compute/tag_add.go index a21ed43..67e6669 100644 --- a/pkg/cloudbroker/compute/tag_add.go +++ b/pkg/cloudbroker/compute/tag_add.go @@ -2,45 +2,34 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add tag to compute type TagAddRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Tag key // Required: true - Key string `url:"key" json:"key"` + Key string `url:"key" json:"key" validate:"required"` // Tag value // Required: true - Value string `url:"value" json:"value"` -} - -func (crq TagAddRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - if crq.Value == "" { - return errors.New("validation-error: field Value must be set") - } - - return nil + Value string `url:"value" json:"value" validate:"required"` } // TagAdd add tag to compute tags dict func (c Compute) TagAdd(ctx context.Context, req TagAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/tagAdd" diff --git a/pkg/cloudbroker/compute/tag_remove.go b/pkg/cloudbroker/compute/tag_remove.go index 440f1bd..c70d579 100644 --- a/pkg/cloudbroker/compute/tag_remove.go +++ b/pkg/cloudbroker/compute/tag_remove.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove tag from compute type TagRemoveRequest struct { // IDs of the compute instances // Required: true - ComputeIDs []uint64 `url:"computeIds" json:"computeIds"` + ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"` // Tag key // Required: true - Key string `url:"key" json:"key"` -} - -func (crq TagRemoveRequest) validate() error { - if len(crq.ComputeIDs) == 0 { - return errors.New("validation-error: field ComputeIDs must be set") - } - if crq.Key == "" { - return errors.New("validation-error: field Key must be set") - } - - return nil + Key string `url:"key" json:"key" validate:"required"` } // TagRemove removes tag from compute tags dict func (c Compute) TagRemove(ctx context.Context, req TagRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/tagRemove" diff --git a/pkg/cloudbroker/compute/unpin_from_stack.go b/pkg/cloudbroker/compute/unpin_from_stack.go index ec5b511..36402cc 100644 --- a/pkg/cloudbroker/compute/unpin_from_stack.go +++ b/pkg/cloudbroker/compute/unpin_from_stack.go @@ -2,31 +2,26 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unpin from stack type UnpinFromStackRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq UnpinFromStackRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // UnpinFromStack unpin compute from current stack func (c Compute) UnpinFromStack(ctx context.Context, req UnpinFromStackRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/unpinFromStack" diff --git a/pkg/cloudbroker/compute/update.go b/pkg/cloudbroker/compute/update.go index 84f09ee..46be1dd 100644 --- a/pkg/cloudbroker/compute/update.go +++ b/pkg/cloudbroker/compute/update.go @@ -2,16 +2,17 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update compute type UpdateRequest struct { // ID of the compute // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // New name // Required: false @@ -26,19 +27,13 @@ type UpdateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (crq UpdateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil -} - // Update updates some properties of the compute func (c Compute) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/update" diff --git a/pkg/cloudbroker/compute/user_grant.go b/pkg/cloudbroker/compute/user_grant.go index f0eb976..28f71e2 100644 --- a/pkg/cloudbroker/compute/user_grant.go +++ b/pkg/cloudbroker/compute/user_grant.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type UserGrantRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to add // Required: true - Username string `url:"userName" json:"userName"` + Username string `url:"userName" json:"userName" validate:"required"` // Access type // Should be one of: @@ -25,32 +24,16 @@ type UserGrantRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (crq UserGrantRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Username == "" { - return errors.New("validation-error: field Username must be set") - } - if crq.AccessType == "" { - return errors.New("validation-error: field AccessType must be set") - } - validator := validators.StringInSlice(crq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !validator { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // UserGrant grant user access to the compute func (c Compute) UserGrant(ctx context.Context, req UserGrantRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/userGrant" diff --git a/pkg/cloudbroker/compute/user_list.go b/pkg/cloudbroker/compute/user_list.go index cc98cc2..0433ee2 100644 --- a/pkg/cloudbroker/compute/user_list.go +++ b/pkg/cloudbroker/compute/user_list.go @@ -3,30 +3,25 @@ package compute import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list users for compute type UserListRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` -} - -func (crq UserListRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - - return nil + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` } // UserList gets users list for compute func (c Compute) UserList(ctx context.Context, req UserListRequest) (*RecordACL, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/userList" diff --git a/pkg/cloudbroker/compute/user_revoke.go b/pkg/cloudbroker/compute/user_revoke.go index 1232faf..cdf03ba 100644 --- a/pkg/cloudbroker/compute/user_revoke.go +++ b/pkg/cloudbroker/compute/user_revoke.go @@ -2,38 +2,30 @@ package compute import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke user access type UserRevokeRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to remove // Required: true - Username string `url:"userName" json:"userName"` -} - -func (crq UserRevokeRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Username == "" { - return errors.New("validation-error: field Username must be set") - } - - return nil + Username string `url:"userName" json:"userName" validate:"required"` } // UserRevoke revokes user access to the compute func (c Compute) UserRevoke(ctx context.Context, req UserRevokeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/userRevoke" diff --git a/pkg/cloudbroker/compute/user_update.go b/pkg/cloudbroker/compute/user_update.go index 2f4c4d0..5e0867e 100644 --- a/pkg/cloudbroker/compute/user_update.go +++ b/pkg/cloudbroker/compute/user_update.go @@ -2,7 +2,6 @@ package compute import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type UserUpdateRequest struct { // ID of the compute instance // Required: true - ComputeID uint64 `url:"computeId" json:"computeId"` + ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"` // Name of the user to update // Required: true - Username string `url:"userName" json:"userName"` + Username string `url:"userName" json:"userName" validate:"required"` // Access type // Should be one of: @@ -25,32 +24,16 @@ type UserUpdateRequest struct { // - 'RCX' for Write // - 'ARCXDU' for Admin // Required: true - AccessType string `url:"accesstype" json:"accesstype"` -} - -func (crq UserUpdateRequest) validate() error { - if crq.ComputeID == 0 { - return errors.New("validation-error: field ComputeID must be set") - } - if crq.Username == "" { - return errors.New("validation-error: field UserName must be set") - } - if crq.AccessType == "" { - return errors.New("validation-error: field AccessType must be set") - } - validator := validators.StringInSlice(crq.AccessType, []string{"R", "RCX", "ARCXDU"}) - if !validator { - return errors.New("validation-error: field AccessType can be only R, RCX or ARCXDU") - } - - return nil + AccessType string `url:"accesstype" json:"accesstype" validate:"accessType"` } // UserUpdate updates user access to the compute func (c Compute) UserUpdate(ctx context.Context, req UserUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/compute/userUpdate" diff --git a/pkg/cloudbroker/disks/create.go b/pkg/cloudbroker/disks/create.go index 0f2df8f..4d697f7 100644 --- a/pkg/cloudbroker/disks/create.go +++ b/pkg/cloudbroker/disks/create.go @@ -2,7 +2,6 @@ package disks import ( "context" - "errors" "net/http" "strconv" @@ -13,15 +12,15 @@ import ( type CreateRequest struct { // ID of the account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // ID of the grid (platform) // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Name of disk // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Description of disk // Required: false @@ -36,7 +35,7 @@ type CreateRequest struct { // - D=Data // - T=Temp // Required: true - Type string `url:"type" json:"type"` + Type string `url:"type" json:"type" validate:"diskType"` // Size in GB default is 0 // Required: false @@ -55,29 +54,13 @@ type CreateRequest struct { Pool string `url:"pool,omitempty" json:"pool,omitempty"` } -func (drq CreateRequest) validate() error { - if drq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - if drq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if drq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - validate := validators.StringInSlice(drq.Type, []string{"B", "D", "T"}) - if !validate { - return errors.New("validation-error: field must be B, D or T") - } - - return nil -} - // Create creates a disk func (d Disks) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/create" diff --git a/pkg/cloudbroker/disks/delete.go b/pkg/cloudbroker/disks/delete.go index b462b93..0207237 100644 --- a/pkg/cloudbroker/disks/delete.go +++ b/pkg/cloudbroker/disks/delete.go @@ -2,16 +2,17 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request for delete disk type DeleteRequest struct { // ID of disk to delete // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Detach disk from machine first // Required: false @@ -26,19 +27,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (drq DeleteRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // Delete deletes disk by ID func (d Disks) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/delete" diff --git a/pkg/cloudbroker/disks/delete_disks.go b/pkg/cloudbroker/disks/delete_disks.go index 097929b..b362f2b 100644 --- a/pkg/cloudbroker/disks/delete_disks.go +++ b/pkg/cloudbroker/disks/delete_disks.go @@ -2,42 +2,34 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for multiple disks type DeleteDisksRequest struct { // List of disk ids to delete // Required: true - DisksIDs []uint64 `url:"diskIds" json:"diskIds"` + DisksIDs []uint64 `url:"diskIds" json:"diskIds" validate:"min=1"` // Reason for deleting the disks // Required: true - Reason string `url:"reason" json:"reason"` + Reason string `url:"reason" json:"reason" validate:"required"` // Whether to completely delete the disks, works only with non attached disks // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (drq DeleteDisksRequest) validate() error { - if len(drq.DisksIDs) == 0 { - return errors.New("validation-error: field DiskIDs must be set") - } - if drq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil -} - // DeleteDisks deletes multiple disks permanently func (d Disks) DeleteDisks(ctx context.Context, req DeleteDisksRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/deleteDisks" diff --git a/pkg/cloudbroker/disks/filter.go b/pkg/cloudbroker/disks/filter.go index 74c7478..0067e52 100644 --- a/pkg/cloudbroker/disks/filter.go +++ b/pkg/cloudbroker/disks/filter.go @@ -1,5 +1,14 @@ package disks +import ( + "context" + "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb" +) + // FilterByID returns ListDisks with specified ID. func (ld ListDisks) FilterByID(id uint64) ListDisks { predicate := func(idisk ItemDisk) bool { @@ -45,6 +54,69 @@ func (ld ListDisks) FilterByImageID(imageID uint64) ListDisks { return ld.FilterFunc(predicate) } +// FilterByComputeID is used to filter ListDisks attached to specified compute. +func (ld ListDisks) FilterByComputeID(computeID uint64) ListDisks { + predicate := func(idisk ItemDisk) bool { + for k := range idisk.Computes { + if k == strconv.FormatUint(computeID, 10) { + return true + } + } + + return false + } + + return ld.FilterFunc(predicate) +} + +// FilterByK8SID is used to filter ListDisks by specified K8S cluster. +func (ld ListDisks) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListDisks, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + var result ListDisks + + for _, masterCompute := range cluster.K8SGroups.Masters.DetailedInfo { + result = append(result, ld.FilterByComputeID(masterCompute.ID)...) + } + + for _, workerGroup := range cluster.K8SGroups.Workers { + for _, workerCompute := range workerGroup.DetailedInfo { + result = append(result, ld.FilterByComputeID(workerCompute.ID)...) + } + } + + return result, nil +} + +// FilterByLBID is used to filter ListDisks used by computes inside specified Load Balancer. +func (ld ListDisks) FilterByLBID(ctx context.Context, lbID uint64, decortClient interfaces.Caller) (ListDisks, error) { + caller := lb.New(decortClient) + + req := lb.GetRequest{ + LBID: lbID, + } + + foundLB, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + var result ListDisks + result = append(result, ld.FilterByComputeID(foundLB.PrimaryNode.ComputeID)...) + result = append(result, ld.FilterByComputeID(foundLB.SecondaryNode.ComputeID)...) + + return result, nil +} + // FilterFunc allows filtering ListDisks based on a user-specified predicate. func (ld ListDisks) FilterFunc(predicate func(ItemDisk) bool) ListDisks { var result ListDisks diff --git a/pkg/cloudbroker/disks/get.go b/pkg/cloudbroker/disks/get.go index fe70f35..572e9fc 100644 --- a/pkg/cloudbroker/disks/get.go +++ b/pkg/cloudbroker/disks/get.go @@ -3,31 +3,26 @@ package disks import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about disk type GetRequest struct { // ID of the disk // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq GetRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Get gets disk details // Notice: the devicename field is the name as it is passed to the kernel (kname in linux) for unattached disks this field has no relevant value func (d Disks) Get(ctx context.Context, req GetRequest) (*RecordDisk, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/get" diff --git a/pkg/cloudbroker/disks/limit_io.go b/pkg/cloudbroker/disks/limit_io.go index fbc707d..5e069d4 100644 --- a/pkg/cloudbroker/disks/limit_io.go +++ b/pkg/cloudbroker/disks/limit_io.go @@ -2,16 +2,17 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for limit IO type LimitIORequest struct { // ID of the disk to limit // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Alias for total_iops_sec for backwards compatibility // Required: false @@ -70,21 +71,15 @@ type LimitIORequest struct { SizeIOPSSec uint64 `url:"size_iops_sec,omitempty" json:"size_iops_sec,omitempty"` } -func (drq LimitIORequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - - return nil -} - // LimitIO limit IO for a certain disk // total and read/write options are not allowed to be combined // see http://libvirt.org/formatdomain.html#elementsDisks iotune section for more details func (d Disks) LimitIO(ctx context.Context, req LimitIORequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/limitIO" diff --git a/pkg/cloudbroker/disks/list_types.go b/pkg/cloudbroker/disks/list_types.go index da609b0..61c9bce 100644 --- a/pkg/cloudbroker/disks/list_types.go +++ b/pkg/cloudbroker/disks/list_types.go @@ -4,17 +4,26 @@ import ( "context" "encoding/json" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list types of disks type ListTypesRequest struct { // Show detailed disk types by seps - // Required: false - Detailed bool `url:"detailed" json:"detailed"` + // Required: true + Detailed bool `url:"detailed" json:"detailed" validate:"required"` } // ListTypes gets list defined disk types func (d Disks) ListTypes(ctx context.Context, req ListTypesRequest) ([]interface{}, error) { + err := validators.ValidateRequest(req) + if err != nil { + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } + } + url := "/cloudbroker/disks/listTypes" res, err := d.client.DecortApiCall(ctx, http.MethodPost, url, req) diff --git a/pkg/cloudbroker/disks/rename.go b/pkg/cloudbroker/disks/rename.go index cd4f38f..8419ae0 100644 --- a/pkg/cloudbroker/disks/rename.go +++ b/pkg/cloudbroker/disks/rename.go @@ -2,38 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rename disk type RenameRequest struct { // ID of the disk to rename // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New name of disk // Required: true - Name string `url:"name" json:"name"` -} - -func (drq RenameRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if drq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil + Name string `url:"name" json:"name" validate:"required"` } // Rename rename disk func (d Disks) Rename(ctx context.Context, req RenameRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/rename" diff --git a/pkg/cloudbroker/disks/resize.go b/pkg/cloudbroker/disks/resize.go index 79086b9..85a9287 100644 --- a/pkg/cloudbroker/disks/resize.go +++ b/pkg/cloudbroker/disks/resize.go @@ -2,30 +2,21 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for resize disk type ResizeRequest struct { // ID of the disk to resize // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // New size of the disk in GB // Required: true - Size uint64 `url:"size" json:"size"` -} - -func (drq ResizeRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if drq.Size == 0 { - return errors.New("validation-error: field Size must be set") - } - return nil + Size uint64 `url:"size" json:"size" validate:"required"` } // Resize resize disk @@ -33,9 +24,11 @@ func (drq ResizeRequest) validate() error { // in that case please stop and start your machine after changing the disk size, for your changes to be reflected. // This method will not be used for disks, assigned to computes. Only unassigned disks and disks, assigned with "old" virtual machines. func (d Disks) Resize(ctx context.Context, req ResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/resize" @@ -58,9 +51,11 @@ func (d Disks) Resize(ctx context.Context, req ResizeRequest) (bool, error) { // in that case please stop and start your machine after changing the disk size, for your changes to be reflected. // This method will not be used for disks, assigned to "old" virtual machines. Only unassigned disks and disks, assigned with computes. func (d Disks) Resize2(ctx context.Context, req ResizeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/resize2" diff --git a/pkg/cloudbroker/disks/restore.go b/pkg/cloudbroker/disks/restore.go index a09f717..69acb1c 100644 --- a/pkg/cloudbroker/disks/restore.go +++ b/pkg/cloudbroker/disks/restore.go @@ -2,38 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore a deleted unattached disk type RestoreRequest struct { // ID of the disk to restore // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Reason for restoring the disk // Required: true - Reason string `url:"reason" json:"reason"` -} - -func (drq RestoreRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if drq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil + Reason string `url:"reason" json:"reason" validate:"required"` } // Restore restore a deleted unattached disk from recycle bin func (d Disks) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/restore" diff --git a/pkg/cloudbroker/disks/share.go b/pkg/cloudbroker/disks/share.go index d7d6232..9f34d21 100644 --- a/pkg/cloudbroker/disks/share.go +++ b/pkg/cloudbroker/disks/share.go @@ -2,31 +2,26 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for share data disk type ShareRequest struct { // ID of the disk to share // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq ShareRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Share shares data disk func (d Disks) Share(ctx context.Context, req ShareRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/share" diff --git a/pkg/cloudbroker/disks/snapshot_delete.go b/pkg/cloudbroker/disks/snapshot_delete.go index 873dc76..6e0fddd 100644 --- a/pkg/cloudbroker/disks/snapshot_delete.go +++ b/pkg/cloudbroker/disks/snapshot_delete.go @@ -2,38 +2,30 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete snapshot type SnapshotDeleteRequest struct { // ID of disk to delete // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Label of the snapshot to delete - // Required: false - Label string `url:"label" json:"label"` -} - -func (drq SnapshotDeleteRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if drq.Label == "" { - return errors.New("validation-error: field Label must be set") - } - - return nil + // Required: true + Label string `url:"label" json:"label" validate:"required"` } // SnapshotDelete deletes a snapshot func (d Disks) SnapshotDelete(ctx context.Context, req SnapshotDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/snapshotDelete" diff --git a/pkg/cloudbroker/disks/snapshot_rollback.go b/pkg/cloudbroker/disks/snapshot_rollback.go index 433cbdf..b095a02 100644 --- a/pkg/cloudbroker/disks/snapshot_rollback.go +++ b/pkg/cloudbroker/disks/snapshot_rollback.go @@ -2,45 +2,34 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rollback snapshot type SnapshotRollbackRequest struct { // ID of the disk // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` // Label of the snapshot to rollback - // Required: true - Label string `url:"label" json:"label"` + // Required: false + Label string `url:"label,omitempty" json:"label,omitempty"` // Timestamp of the snapshot to rollback - // Required: true - TimeStamp uint64 `url:"timestamp" json:"timestamp"` -} - -func (drq SnapshotRollbackRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID must be set") - } - if drq.Label == "" { - return errors.New("validation-error: field Label must be set") - } - if drq.TimeStamp == 0 { - return errors.New("validation-error: field TimeStamp must be set") - } - - return nil + // Required: false + TimeStamp uint64 `url:"timestamp,omitempty" json:"timestamp,omitempty"` } // SnapshotRollback rollback an individual disk snapshot func (d Disks) SnapshotRollback(ctx context.Context, req SnapshotRollbackRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/snapshotRollback" diff --git a/pkg/cloudbroker/disks/unshare.go b/pkg/cloudbroker/disks/unshare.go index 60a08ec..68c26a8 100644 --- a/pkg/cloudbroker/disks/unshare.go +++ b/pkg/cloudbroker/disks/unshare.go @@ -2,31 +2,26 @@ package disks import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unshare data disk type UnshareRequest struct { // ID of the disk to unshare // Required: true - DiskID uint64 `url:"diskId" json:"diskId"` -} - -func (drq UnshareRequest) validate() error { - if drq.DiskID == 0 { - return errors.New("validation-error: field DiskID can not be empty or equal to 0") - } - - return nil + DiskID uint64 `url:"diskId" json:"diskId" validate:"required"` } // Unshare unshares data disk func (d Disks) Unshare(ctx context.Context, req UnshareRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/disks/unshare" diff --git a/pkg/cloudbroker/extnet/access_add.go b/pkg/cloudbroker/extnet/access_add.go index 88b9117..b74fad9 100644 --- a/pkg/cloudbroker/extnet/access_add.go +++ b/pkg/cloudbroker/extnet/access_add.go @@ -3,37 +3,29 @@ package extnet import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access type AccessAddRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Account ID // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (erq AccessAddRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if erq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // AccessAdd grant access to external network for account ID func (e ExtNet) AccessAdd(ctx context.Context, req AccessAddRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/accessAdd" diff --git a/pkg/cloudbroker/extnet/access_remove.go b/pkg/cloudbroker/extnet/access_remove.go index 5f6855f..42bb7c4 100644 --- a/pkg/cloudbroker/extnet/access_remove.go +++ b/pkg/cloudbroker/extnet/access_remove.go @@ -3,37 +3,29 @@ package extnet import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove access type AccessRemoveRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Account ID // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` -} - -func (erq AccessRemoveRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if erq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` } // AccessRemove remove access from external network for account ID func (e ExtNet) AccessRemove(ctx context.Context, req AccessRemoveRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/accessRemove" diff --git a/pkg/cloudbroker/extnet/create.go b/pkg/cloudbroker/extnet/create.go index 79de16c..d595714 100644 --- a/pkg/cloudbroker/extnet/create.go +++ b/pkg/cloudbroker/extnet/create.go @@ -2,33 +2,34 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create external network type CreateRequest struct { // External network name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // IP network CIDR // For example 192.168.0.0/24 // Required: true - IPCIDR string `url:"ipcidr" json:"ipcidr"` + IPCIDR string `url:"ipcidr" json:"ipcidr" validate:"required"` // External network gateway IP address // Required: true - Gateway string `url:"gateway" json:"gateway"` + Gateway string `url:"gateway" json:"gateway" validate:"required"` // VLAN ID // Required: true - VLANID uint64 `url:"vlanId" json:"vlanId"` + VLANID uint64 `url:"vlanId" json:"vlanId" validate:"required"` // List of DNS addresses // Required: false @@ -71,31 +72,13 @@ type CreateRequest struct { OVSBridge string `url:"ovsBridge,omitempty" json:"ovsBridge,omitempty"` } -func (erq CreateRequest) validate() error { - if erq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if erq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if erq.IPCIDR == "" { - return errors.New("validation-error: field IPCIDR must be set") - } - if erq.Gateway == "" { - return errors.New("validation-error: field Gateway must be set") - } - if erq.VLANID == 0 { - return errors.New("validation-error: field VLANID must be set") - } - - return nil -} - // Create creates new external network into platform func (e ExtNet) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/create" diff --git a/pkg/cloudbroker/extnet/default_qos_update.go b/pkg/cloudbroker/extnet/default_qos_update.go index c3fd2f4..2556993 100644 --- a/pkg/cloudbroker/extnet/default_qos_update.go +++ b/pkg/cloudbroker/extnet/default_qos_update.go @@ -2,16 +2,17 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update QOS type DefaultQOSUpdateRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Internal traffic, kbit // Required: false @@ -26,19 +27,13 @@ type DefaultQOSUpdateRequest struct { EgressRate uint64 `url:"egress_rate,omitempty" json:"egress_rate,omitempty"` } -func (erq DefaultQOSUpdateRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // DefaultQOSUpdate updates default qos values func (e ExtNet) DefaultQOSUpdate(ctx context.Context, req DefaultQOSUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/defaultQosUpdate" diff --git a/pkg/cloudbroker/extnet/destroy.go b/pkg/cloudbroker/extnet/destroy.go index 75988f5..f9f9907 100644 --- a/pkg/cloudbroker/extnet/destroy.go +++ b/pkg/cloudbroker/extnet/destroy.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for destroy type DestroyRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq DestroyRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // Destroy destroys external network func (e ExtNet) Destroy(ctx context.Context, req DestroyRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/destroy" diff --git a/pkg/cloudbroker/extnet/device_deploy.go b/pkg/cloudbroker/extnet/device_deploy.go index ec391fc..ec16623 100644 --- a/pkg/cloudbroker/extnet/device_deploy.go +++ b/pkg/cloudbroker/extnet/device_deploy.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for deploy network device type DeviceDeployRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq DeviceDeployRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // DeviceDeploy deploys network device for external network (make not virtual, "physical") func (e ExtNet) DeviceDeploy(ctx context.Context, req DeviceDeployRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/deviceDeploy" diff --git a/pkg/cloudbroker/extnet/device_migrate.go b/pkg/cloudbroker/extnet/device_migrate.go index 219b4bc..94aed76 100644 --- a/pkg/cloudbroker/extnet/device_migrate.go +++ b/pkg/cloudbroker/extnet/device_migrate.go @@ -2,37 +2,29 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for migrate VNF type DeviceMigrateRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Target stack ID to migrate to - StackID uint64 `url:"stackId" json:"stackId"` -} - -func (erq DeviceMigrateRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if erq.StackID == 0 { - return errors.New("validation-error: field StackID must be set") - } - - return nil + StackID uint64 `url:"stackId" json:"stackId" validate:"required"` } // DeviceMigrate migrate external network VNF device func (e ExtNet) DeviceMigrate(ctx context.Context, req DeviceMigrateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/deviceMigrate" diff --git a/pkg/cloudbroker/extnet/device_remove.go b/pkg/cloudbroker/extnet/device_remove.go index 2d4ec7d..b015cca 100644 --- a/pkg/cloudbroker/extnet/device_remove.go +++ b/pkg/cloudbroker/extnet/device_remove.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for remove network device type DeviceRemoveRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq DeviceRemoveRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // DeviceRemove removes network device of external network (make it virtual, not "physical") func (e ExtNet) DeviceRemove(ctx context.Context, req DeviceRemoveRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/deviceRemove" diff --git a/pkg/cloudbroker/extnet/device_restart.go b/pkg/cloudbroker/extnet/device_restart.go index f36b974..583f29b 100644 --- a/pkg/cloudbroker/extnet/device_restart.go +++ b/pkg/cloudbroker/extnet/device_restart.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart VNF device type DeviceRestartRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq DeviceRestartRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // DeviceRestart restarts external network VNF device func (e ExtNet) DeviceRestart(ctx context.Context, req DeviceRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/deviceRestart" diff --git a/pkg/cloudbroker/extnet/disable.go b/pkg/cloudbroker/extnet/disable.go index 09ac21a..9a1a736 100644 --- a/pkg/cloudbroker/extnet/disable.go +++ b/pkg/cloudbroker/extnet/disable.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable external network type DisableRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq DisableRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // Disable disables external network func (e ExtNet) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/disable" diff --git a/pkg/cloudbroker/extnet/dns_apply.go b/pkg/cloudbroker/extnet/dns_apply.go index 8b174b8..db73215 100644 --- a/pkg/cloudbroker/extnet/dns_apply.go +++ b/pkg/cloudbroker/extnet/dns_apply.go @@ -2,35 +2,30 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set new DNS type DNSApplyRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // List of DNS to apply // Required: false DNSList []string `url:"dns_list,omitempty" json:"dns_list,omitempty"` } -func (erq DNSApplyRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // DNSApply sets new DNS func (e ExtNet) DNSApply(ctx context.Context, req DNSApplyRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/dnsApply" diff --git a/pkg/cloudbroker/extnet/enable.go b/pkg/cloudbroker/extnet/enable.go index cb1db9b..7026080 100644 --- a/pkg/cloudbroker/extnet/enable.go +++ b/pkg/cloudbroker/extnet/enable.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable external network type EnableRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq EnableRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // Enable enables external networks func (e ExtNet) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/enable" diff --git a/pkg/cloudbroker/extnet/get.go b/pkg/cloudbroker/extnet/get.go index df7fda0..5ac3c39 100644 --- a/pkg/cloudbroker/extnet/get.go +++ b/pkg/cloudbroker/extnet/get.go @@ -3,30 +3,25 @@ package extnet import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about external network type GetRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq GetRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // Get gets external network details func (e ExtNet) Get(ctx context.Context, req GetRequest) (*RecordExtNet, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/get" diff --git a/pkg/cloudbroker/extnet/ips_exclude.go b/pkg/cloudbroker/extnet/ips_exclude.go index 8eb262a..6f3d1cb 100644 --- a/pkg/cloudbroker/extnet/ips_exclude.go +++ b/pkg/cloudbroker/extnet/ips_exclude.go @@ -2,38 +2,30 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for exclude list IPs type IPsExcludeRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // List of IPs for exclude from external network // Required: true - IPs []string `url:"ips" json:"ips"` -} - -func (erq IPsExcludeRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if len(erq.IPs) == 0 { - return errors.New("validation-error: field IPs must be set") - } - - return nil + IPs []string `url:"ips" json:"ips" validate:"min=1"` } // IPsExclude exclude list IPs from external network pool func (e ExtNet) IPsExclude(ctx context.Context, req IPsExcludeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/ipsExclude" diff --git a/pkg/cloudbroker/extnet/ips_exclude_range.go b/pkg/cloudbroker/extnet/ips_exclude_range.go index 596796a..2649c3d 100644 --- a/pkg/cloudbroker/extnet/ips_exclude_range.go +++ b/pkg/cloudbroker/extnet/ips_exclude_range.go @@ -2,45 +2,34 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for exclude range of IPs type IPsExcludeRangeRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Starting IP // Required: true - IPStart string `url:"ip_start" json:"ip_start"` + IPStart string `url:"ip_start" json:"ip_start" validate:"required"` // Ending IP // Required: true - IPEnd string `url:"ip_end" json:"ip_end"` -} - -func (erq IPsExcludeRangeRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if erq.IPStart == "" { - return errors.New("validation-error: field IPStart must be set") - } - if erq.IPEnd == "" { - return errors.New("validation-error: field IPEnd must be set") - } - - return nil + IPEnd string `url:"ip_end" json:"ip_end" validate:"required"` } // IPsExcludeRange exclude range of IPs from external network pool func (e ExtNet) IPsExcludeRange(ctx context.Context, req IPsExcludeRangeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/ipsExcludeRange" diff --git a/pkg/cloudbroker/extnet/ips_include.go b/pkg/cloudbroker/extnet/ips_include.go index 866e46b..bfd5cad 100644 --- a/pkg/cloudbroker/extnet/ips_include.go +++ b/pkg/cloudbroker/extnet/ips_include.go @@ -2,38 +2,30 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for include list IPs type IPsIncludeRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // List of IPs for include to external network // Required: true - IPs []string `url:"ips" json:"ips"` -} - -func (erq IPsIncludeRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if len(erq.IPs) == 0 { - return errors.New("validation-error: field IPs must be set") - } - - return nil + IPs []string `url:"ips" json:"ips" validate:"min=1"` } // IPsInclude include list IPs to external network pool func (e ExtNet) IPsInclude(ctx context.Context, req IPsIncludeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/ipsInclude" diff --git a/pkg/cloudbroker/extnet/ips_include_range.go b/pkg/cloudbroker/extnet/ips_include_range.go index bf91e43..538f153 100644 --- a/pkg/cloudbroker/extnet/ips_include_range.go +++ b/pkg/cloudbroker/extnet/ips_include_range.go @@ -2,45 +2,34 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for include range of IPs type IPsIncludeRangeRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // Starting IP // Required: true - IPStart string `url:"ip_start" json:"ip_start"` + IPStart string `url:"ip_start" json:"ip_start" validate:"required"` // Ending IP // Required: true - IPEnd string `url:"ip_end" json:"ip_end"` -} - -func (erq IPsIncludeRangeRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - if erq.IPStart == "" { - return errors.New("validation-error: field IPStart must be set") - } - if erq.IPEnd == "" { - return errors.New("validation-error: field IPEnd must be set") - } - - return nil + IPEnd string `url:"ip_end" json:"ip_end" validate:"required"` } // IPsIncludeRange include range of IPs to external network pool func (e ExtNet) IPsIncludeRange(ctx context.Context, req IPsIncludeRangeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/ipsIncludeRange" diff --git a/pkg/cloudbroker/extnet/ntp_apply.go b/pkg/cloudbroker/extnet/ntp_apply.go index 9c341e9..77b0a97 100644 --- a/pkg/cloudbroker/extnet/ntp_apply.go +++ b/pkg/cloudbroker/extnet/ntp_apply.go @@ -2,35 +2,30 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set new NTP type NTPApplyRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // List of NTP to apply // Required: false NTPList []string `url:"ntp_list,omitempty" json:"ntp_list,omitempty"` } -func (erq NTPApplyRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // NTPApply sets new NTP func (e ExtNet) NTPApply(ctx context.Context, req NTPApplyRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/ntpApply" diff --git a/pkg/cloudbroker/extnet/set_default.go b/pkg/cloudbroker/extnet/set_default.go index 87f4607..ffafdb1 100644 --- a/pkg/cloudbroker/extnet/set_default.go +++ b/pkg/cloudbroker/extnet/set_default.go @@ -2,31 +2,26 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set external network as default type SetDefaultRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` -} - -func (erq SetDefaultRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` } // SetDefault sets external network as default for platform func (e ExtNet) SetDefault(ctx context.Context, req SetDefaultRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/setDefault" diff --git a/pkg/cloudbroker/extnet/update.go b/pkg/cloudbroker/extnet/update.go index f249553..b189063 100644 --- a/pkg/cloudbroker/extnet/update.go +++ b/pkg/cloudbroker/extnet/update.go @@ -2,16 +2,17 @@ package extnet import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update external network type UpdateRequest struct { // ID of external network // Required: true - NetID uint64 `url:"net_id" json:"net_id"` + NetID uint64 `url:"net_id" json:"net_id" validate:"required"` // New external network name // Required: false @@ -22,19 +23,13 @@ type UpdateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (erq UpdateRequest) validate() error { - if erq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // Update updates external network parameters func (e ExtNet) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/extnet/update" diff --git a/pkg/cloudbroker/grid/add.go b/pkg/cloudbroker/grid/add.go index b6fd6af..dd2c1a1 100644 --- a/pkg/cloudbroker/grid/add.go +++ b/pkg/cloudbroker/grid/add.go @@ -2,45 +2,34 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for location code type AddRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Name of the location // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Location code typicly used in dns names // Required: true - LocationCode string `url:"locationcode" json:"locationcode"` -} - -func (grq AddRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if grq.LocationCode == "" { - return errors.New("validation-error: field LocationCode must be set") - } - - return nil + LocationCode string `url:"locationcode" json:"locationcode" validate:"required"` } // Add location code (e.g. DNS name of this grid) func (g Grid) Add(ctx context.Context, req AddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/add" diff --git a/pkg/cloudbroker/grid/change_settings.go b/pkg/cloudbroker/grid/change_settings.go index da5bdd2..c2a762d 100644 --- a/pkg/cloudbroker/grid/change_settings.go +++ b/pkg/cloudbroker/grid/change_settings.go @@ -2,38 +2,30 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for change grid settings type ChangeSettingsRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"id" json:"id"` + GID uint64 `url:"id" json:"id" validate:"required"` // Json data of the new settings will override old data // Required: true - Settings string `url:"settings" json:"settings"` -} - -func (grq ChangeSettingsRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Settings == "" { - return errors.New("validation-error: field Settings must be set") - } - - return nil + Settings string `url:"settings" json:"settings" validate:"required"` } // ChangeSettings changes grid settings func (g Grid) ChangeSettings(ctx context.Context, req ChangeSettingsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/changeSettings" diff --git a/pkg/cloudbroker/grid/check_vms.go b/pkg/cloudbroker/grid/check_vms.go index 3d768a4..82f5190 100644 --- a/pkg/cloudbroker/grid/check_vms.go +++ b/pkg/cloudbroker/grid/check_vms.go @@ -2,31 +2,26 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for check virtual machine type CheckVMsRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` -} - -func (grq CheckVMsRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - - return nil + GID uint64 `url:"gid" json:"gid" validate:"required"` } // CheckVMs run checkvms jumpscript func (g Grid) CheckVMs(ctx context.Context, req CheckVMsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/checkVMs" diff --git a/pkg/cloudbroker/grid/create_system_space.go b/pkg/cloudbroker/grid/create_system_space.go index 7a1f7ba..663225f 100644 --- a/pkg/cloudbroker/grid/create_system_space.go +++ b/pkg/cloudbroker/grid/create_system_space.go @@ -2,31 +2,32 @@ package grid import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create system space type CreateSystemSpaceRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"id" json:"id"` + GID uint64 `url:"id" json:"id" validate:"required"` // Name of the account/cloudspace to be created for the system // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of the specific image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of base volume // Required: true - BootSize uint64 `url:"bootsize" json:"bootsize"` + BootSize uint64 `url:"bootsize" json:"bootsize" validate:"required"` // Data disk size in gigabytes // Required: true - DataDiskSize uint64 `url:"dataDiskSize" json:"dataDiskSize"` + DataDiskSize uint64 `url:"dataDiskSize" json:"dataDiskSize" validate:"required"` // ID of the specific size // Required: false @@ -41,31 +42,13 @@ type CreateSystemSpaceRequest struct { Memory uint64 `url:"memory,omitempty" json:"memory,omitempty"` } -func (grq CreateSystemSpaceRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if grq.BootSize == 0 { - return errors.New("validation-error: field BootSize must be set") - } - if grq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if grq.DataDiskSize == 0 { - return errors.New("validation-error: field DataDiskSize must be set") - } - - return nil -} - // CreateSystemSpace creates system space func (g Grid) CreateSystemSpace(ctx context.Context, req CreateSystemSpaceRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/createSystemSpace" diff --git a/pkg/cloudbroker/grid/execute_maintenance_script.go b/pkg/cloudbroker/grid/execute_maintenance_script.go index 5533eeb..d234703 100644 --- a/pkg/cloudbroker/grid/execute_maintenance_script.go +++ b/pkg/cloudbroker/grid/execute_maintenance_script.go @@ -2,45 +2,34 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for execute script type ExecuteMaintenanceScriptRequest struct { // Grid (platform) ID // Required: true - GID string `url:"gid" json:"gid"` + GID string `url:"gid" json:"gid" validate:"required"` // Type of nodes you want to apply the action on // Required: true - NodesType string `url:"nodestype" json:"nodestype"` + NodesType string `url:"nodestype" json:"nodestype" validate:"required"` // The script you want to run // Required: true - Script string `url:"script" json:"script"` -} - -func (grq ExecuteMaintenanceScriptRequest) validate() error { - if grq.GID == "" { - return errors.New("validation-error: field GID must be set") - } - if grq.NodesType == "" { - return errors.New("validation-error: field NodesType must be set") - } - if grq.Script == "" { - return errors.New("validation-error: field Script must be set") - } - - return nil + Script string `url:"script" json:"script" validate:"required"` } // ExecuteMaintenanceScript executes maintenance script func (g Grid) ExecuteMaintenanceScript(ctx context.Context, req ExecuteMaintenanceScriptRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/executeMaintenanceScript" diff --git a/pkg/cloudbroker/grid/get.go b/pkg/cloudbroker/grid/get.go index 1e7051e..dbc7646 100644 --- a/pkg/cloudbroker/grid/get.go +++ b/pkg/cloudbroker/grid/get.go @@ -3,30 +3,25 @@ package grid import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get grid details type GetRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gridId" json:"gridId"` -} - -func (grq GetRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - - return nil + GID uint64 `url:"gridId" json:"gridId" validate:"required"` } // Get gets information about grid by ID func (g Grid) Get(ctx context.Context, req GetRequest) (*RecordGrid, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/get" diff --git a/pkg/cloudbroker/grid/get_backup.go b/pkg/cloudbroker/grid/get_backup.go index 83f4299..7f7d0e5 100644 --- a/pkg/cloudbroker/grid/get_backup.go +++ b/pkg/cloudbroker/grid/get_backup.go @@ -2,30 +2,25 @@ package grid import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get backup type GetBackupRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` -} - -func (grq GetBackupRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - - return nil + GID uint64 `url:"gid" json:"gid" validate:"required"` } // GetBackup gets platform backup func (g Grid) GetBackup(ctx context.Context, req GetBackupRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/getBackup" @@ -40,9 +35,11 @@ func (g Grid) GetBackup(ctx context.Context, req GetBackupRequest) (string, erro // GetBackupGET gets platform backup func (g Grid) GetBackupGET(ctx context.Context, req GetBackupRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/getBackup" diff --git a/pkg/cloudbroker/grid/get_diagnosis.go b/pkg/cloudbroker/grid/get_diagnosis.go index 0e72724..925c979 100644 --- a/pkg/cloudbroker/grid/get_diagnosis.go +++ b/pkg/cloudbroker/grid/get_diagnosis.go @@ -2,30 +2,25 @@ package grid import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get platform snapshot with additional diagnosis type GetDiagnosisRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` -} - -func (grq GetDiagnosisRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - - return nil + GID uint64 `url:"gid" json:"gid" validate:"required"` } // GetDiagnosis get platform snapshot with additional diagnosis info like a logs, etc func (g Grid) GetDiagnosis(ctx context.Context, req GetDiagnosisRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/getDiagnosis" @@ -40,9 +35,11 @@ func (g Grid) GetDiagnosis(ctx context.Context, req GetDiagnosisRequest) (string // GetDiagnosisGET get platform snapshot with additional diagnosis info like a logs, etc func (g Grid) GetDiagnosisGET(ctx context.Context, req GetDiagnosisRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/getDiagnosis" diff --git a/pkg/cloudbroker/grid/models.go b/pkg/cloudbroker/grid/models.go index 5826f3f..f70526d 100644 --- a/pkg/cloudbroker/grid/models.go +++ b/pkg/cloudbroker/grid/models.go @@ -15,7 +15,7 @@ type RecordResource struct { CPU uint64 `json:"cpu"` // Disk size - DiskSize uint64 `json:"disksize"` + DiskSize float64 `json:"disksize"` // Disk size max DiskSizeMax int64 `json:"disksizemax"` diff --git a/pkg/cloudbroker/grid/purge_logs.go b/pkg/cloudbroker/grid/purge_logs.go index 1873f44..680f751 100644 --- a/pkg/cloudbroker/grid/purge_logs.go +++ b/pkg/cloudbroker/grid/purge_logs.go @@ -2,39 +2,31 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for purge logs type PurgeLogsRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Age of the records to remove, e.g. -1h for records older than 1 hour, -1w - one week, etc // Required: true - Age string `url:"age" json:"age"` -} - -func (grq PurgeLogsRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Age == "" { - return errors.New("validation-error: field Age must be set") - } - - return nil + Age string `url:"age" json:"age" validate:"required"` } // PurgeLogs clear Log and ECO records that are older than the specified age. // By default, records older than one week are removed func (g Grid) PurgeLogs(ctx context.Context, req PurgeLogsRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/purgeLogs" diff --git a/pkg/cloudbroker/grid/rename.go b/pkg/cloudbroker/grid/rename.go index c6e5bd7..5728c91 100644 --- a/pkg/cloudbroker/grid/rename.go +++ b/pkg/cloudbroker/grid/rename.go @@ -2,38 +2,30 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rename grid type RenameRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // New name // Required: true - Name string `url:"Name" json:"Name"` -} - -func (grq RenameRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil + Name string `url:"Name" json:"Name" validate:"required"` } // Rename renames a grid func (g Grid) Rename(ctx context.Context, req RenameRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/rename" diff --git a/pkg/cloudbroker/grid/services_restart.go b/pkg/cloudbroker/grid/services_restart.go index f747829..dc386f6 100644 --- a/pkg/cloudbroker/grid/services_restart.go +++ b/pkg/cloudbroker/grid/services_restart.go @@ -2,38 +2,30 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart services type ServicesRestartRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Node ID // Required: true - NID uint64 `url:"nid" json:"nid"` -} - -func (grq ServicesRestartRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.NID == 0 { - return errors.New("validation-error: field NID must be set") - } - - return nil + NID uint64 `url:"nid" json:"nid" validate:"required"` } // ServicesRestart restarts decort services on the node func (g Grid) ServicesRestart(ctx context.Context, req ServicesRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/servicesRestart" diff --git a/pkg/cloudbroker/grid/set_cpu_allocation_ratio.go b/pkg/cloudbroker/grid/set_cpu_allocation_ratio.go index 04f375d..3ef3e41 100644 --- a/pkg/cloudbroker/grid/set_cpu_allocation_ratio.go +++ b/pkg/cloudbroker/grid/set_cpu_allocation_ratio.go @@ -2,38 +2,30 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set allocation type SetCPUAllocationRatioRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gridId" json:"gridId"` + GID uint64 `url:"gridId" json:"gridId" validate:"required"` // Allocation ratio // Required: true - Ratio float64 `url:"ratio" json:"ratio"` -} - -func (grq SetCPUAllocationRatioRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Ratio == 0.0 { - return errors.New("validation-error: field Ratio must be set") - } - - return nil + Ratio float64 `url:"ratio" json:"ratio" validate:"required"` } // SetCPUAllocationRatio sets CPU allocation ratio func (g Grid) SetCPUAllocationRatio(ctx context.Context, req SetCPUAllocationRatioRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/setCpuAllocationRatio" diff --git a/pkg/cloudbroker/grid/set_mem_allocation_ratio.go b/pkg/cloudbroker/grid/set_mem_allocation_ratio.go index bc67c5c..0d4ec6e 100644 --- a/pkg/cloudbroker/grid/set_mem_allocation_ratio.go +++ b/pkg/cloudbroker/grid/set_mem_allocation_ratio.go @@ -2,38 +2,30 @@ package grid import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set memory allocation type SetMemAllocationRatioRequest struct { // Grid (platform) ID // Required: true - GID uint64 `url:"gridId" json:"gridId"` + GID uint64 `url:"gridId" json:"gridId" validate:"required"` // Allocation ratio // Required: true - Ratio float64 `url:"ratio" json:"ratio"` -} - -func (grq SetMemAllocationRatioRequest) validate() error { - if grq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if grq.Ratio == 0.0 { - return errors.New("validation-error: field Ratio must be set") - } - - return nil + Ratio float64 `url:"ratio" json:"ratio" validate:"required"` } // SetMemAllocationRatio sets memory allocation ratio func (g Grid) SetMemAllocationRatio(ctx context.Context, req SetMemAllocationRatioRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/grid/setMemAllocationRatio" diff --git a/pkg/cloudbroker/image/computeci_set.go b/pkg/cloudbroker/image/computeci_set.go index 1a6cc70..c208d9b 100644 --- a/pkg/cloudbroker/image/computeci_set.go +++ b/pkg/cloudbroker/image/computeci_set.go @@ -2,38 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for set compute CI type ComputeCISetRequest struct { // ID of the image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // ID of the compute CI // Required: true - ComputeCIID uint64 `url:"computeciId" json:"computeciId"` -} - -func (irq ComputeCISetRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if irq.ComputeCIID == 0 { - return errors.New("validation-error: field ComputeCIID must be set") - } - - return nil + ComputeCIID uint64 `url:"computeciId" json:"computeciId" validate:"required"` } // ComputeCISet set compute CI ID for image func (i Image) ComputeCISet(ctx context.Context, req ComputeCISetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/computeciSet" diff --git a/pkg/cloudbroker/image/computeci_unset.go b/pkg/cloudbroker/image/computeci_unset.go index 859fae4..36882ff 100644 --- a/pkg/cloudbroker/image/computeci_unset.go +++ b/pkg/cloudbroker/image/computeci_unset.go @@ -2,31 +2,26 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for unset compute CI type ComputeCIUnsetRequest struct { // ID of the image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` -} - -func (irq ComputeCIUnsetRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` } // ComputeCIUnset unset compute CI ID from image func (i Image) ComputeCIUnset(ctx context.Context, req ComputeCIUnsetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/сomputeciUnset" diff --git a/pkg/cloudbroker/image/create_cdrom_image.go b/pkg/cloudbroker/image/create_cdrom_image.go index db4227d..8438e53 100644 --- a/pkg/cloudbroker/image/create_cdrom_image.go +++ b/pkg/cloudbroker/image/create_cdrom_image.go @@ -2,7 +2,6 @@ package image import ( "context" - "errors" "net/http" "strconv" @@ -13,15 +12,15 @@ import ( type CreateCDROMImageRequest struct { // Name of the rescue disk // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // URL where to download ISO from // Required: true - URL string `url:"url" json:"url"` + URL string `url:"url" json:"url" validate:"required,url"` // Grid (platform) ID where this CD-ROM image should be create in // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Account ID to make the image exclusive // Required: false @@ -53,37 +52,16 @@ type CreateCDROMImageRequest struct { // List of types of compute suitable for image. // Example: [ "KVM_X86" ] // Required: true - Drivers []string `url:"drivers" json:"drivers"` -} - -func (irq CreateCDROMImageRequest) validate() error { - if irq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if irq.URL == "" { - return errors.New("validation-error: field URL must be set") - } - if irq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if len(irq.Drivers) == 0 || len(irq.Drivers) > 1 { - return errors.New("validation-error: field Drivers can not be empty or have 2 or more elements") - } - for _, v := range irq.Drivers { - validate := validators.StringInSlice(v, []string{"KVM_X86"}) - if !validate { - return errors.New("validation-error: field Drivers can be KVM_X86 only") - } - } - - return nil + Drivers []string `url:"drivers" json:"drivers" validate:"min=1,max=2,imageDrivers"` } // CreateCDROMImage creates CD-ROM image from an ISO identified by URL func (i Image) CreateCDROMImage(ctx context.Context, req CreateCDROMImageRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/createCDROMImage" diff --git a/pkg/cloudbroker/image/create_image.go b/pkg/cloudbroker/image/create_image.go index 9b875f7..451175d 100644 --- a/pkg/cloudbroker/image/create_image.go +++ b/pkg/cloudbroker/image/create_image.go @@ -2,7 +2,6 @@ package image import ( "context" - "errors" "net/http" "strconv" @@ -13,22 +12,22 @@ import ( type CreateRequest struct { // Name of the rescue disk // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // URL where to download media from // Required: true - URL string `url:"url" json:"url"` + URL string `url:"url" json:"url" validate:"required,url"` // Grid (platform) ID where this template should be create in // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Boot type of image // Should be one of: // - bios // - UEFI // Required: true - BootType string `url:"boottype" json:"boottype"` + BootType string `url:"boottype" json:"boottype" validate:"imageBootType"` // Image type // Should be one of: @@ -36,7 +35,7 @@ type CreateRequest struct { // - windows // - or other // Required: true - ImageType string `url:"imagetype" json:"imagetype"` + ImageType string `url:"imagetype" json:"imagetype" validate:"imageType"` // Does this machine supports hot resize // Required: false @@ -80,46 +79,20 @@ type CreateRequest struct { // List of types of compute suitable for image // Example: [ "KVM_X86" ] // Required: true - Drivers []string `url:"drivers" json:"drivers"` + Drivers []string `url:"drivers" json:"drivers" validate:"min=1,max=2,imageDrivers"` // Bootable image or not // Required: false Bootable bool `url:"bootable,omitempty" json:"bootable,omitempty"` } -func (irq CreateRequest) validate() error { - if irq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if irq.URL == "" { - return errors.New("validation-error: field URL must be set") - } - if irq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if irq.BootType == "" { - return errors.New("validation-error: field BootType must be set") - } - if irq.ImageType == "" { - return errors.New("validation-error: field ImageType must be set") - } - validate := validators.StringInSlice(irq.BootType, []string{"bios", "uefi"}) - if !validate { - return errors.New("validation-error: field BootType can be bios or uefi") - } - validate = validators.StringInSlice(irq.ImageType, []string{"windows", "linux", "other"}) - if !validate { - return errors.New("validation-error: field ImageType can be windows, linux or other") - } - - return nil -} - // CreateImage creates image from a media identified by URL func (i Image) CreateImage(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/createImage" diff --git a/pkg/cloudbroker/image/create_virtual.go b/pkg/cloudbroker/image/create_virtual.go index 2cea138..748869e 100644 --- a/pkg/cloudbroker/image/create_virtual.go +++ b/pkg/cloudbroker/image/create_virtual.go @@ -2,38 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create virtual image type CreateVirtualRequest struct { // Name of the virtual image to create // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of real image to link this virtual image to upon creation // Required: true - TargetID uint64 `url:"targetId" json:"targetId"` -} - -func (irq CreateVirtualRequest) validate() error { - if irq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if irq.TargetID == 0 { - return errors.New("validation-error: field TargetID must be set") - } - - return nil + TargetID uint64 `url:"targetId" json:"targetId" validate:"required"` } // CreateVirtual creates virtual image func (i Image) CreateVirtual(ctx context.Context, req CreateVirtualRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/createVirtual" diff --git a/pkg/cloudbroker/image/delete.go b/pkg/cloudbroker/image/delete.go index b0e74ee..fdc3c90 100644 --- a/pkg/cloudbroker/image/delete.go +++ b/pkg/cloudbroker/image/delete.go @@ -2,42 +2,34 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete image type DeleteRequest struct { // ID of the image to delete // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Reason for action // Required: true - Reason string `url:"reason" json:"reason"` + Reason string `url:"reason" json:"reason" validate:"required"` // Whether to completely delete the image // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (irq DeleteRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if irq.Reason == "" { - return errors.New("validation-error: field Reason must be set") - } - - return nil -} - // Delete deletes image by ID func (i Image) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/delete" diff --git a/pkg/cloudbroker/image/delete_cdrom_image.go b/pkg/cloudbroker/image/delete_cdrom_image.go index 4bf22cf..ca5a906 100644 --- a/pkg/cloudbroker/image/delete_cdrom_image.go +++ b/pkg/cloudbroker/image/delete_cdrom_image.go @@ -2,35 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete CD-ROM image type DeleteCDROMImageRequest struct { // ID of the CD-ROM image to delete // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Whether to completely delete the CD-ROM image, needs to be unused - // Required: true - Permanently bool `url:"permanently" json:"permanently"` -} - -func (irq DeleteCDROMImageRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil + // Required: false + Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } // DeleteCDROMImage delete a CD-ROM image func (i Image) DeleteCDROMImage(ctx context.Context, req DeleteCDROMImageRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/deleteCDROMImage" diff --git a/pkg/cloudbroker/image/delete_images.go b/pkg/cloudbroker/image/delete_images.go index 87f183e..b9e52e3 100644 --- a/pkg/cloudbroker/image/delete_images.go +++ b/pkg/cloudbroker/image/delete_images.go @@ -2,39 +2,34 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete images type DeleteImagesRequest struct { // List of images to be deleted // Required: true - ImageIDs []uint64 `url:"imageIds" json:"imageIds"` + ImageIDs []uint64 `url:"imageIds" json:"imageIds" validate:"min=1"` // Reason for action - // Required: true + // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` // Whether to completely delete the images - // Required: true + // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (irq DeleteImagesRequest) validate() error { - if len(irq.ImageIDs) == 0 { - return errors.New("validation-error: field ImageIDs must be set") - } - - return nil -} - // DeleteImages deletes images func (i Image) DeleteImages(ctx context.Context, req DeleteImagesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/deleteImages" diff --git a/pkg/cloudbroker/image/disable.go b/pkg/cloudbroker/image/disable.go index 8fa1062..a3e2aa3 100644 --- a/pkg/cloudbroker/image/disable.go +++ b/pkg/cloudbroker/image/disable.go @@ -2,31 +2,26 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable image type DisableRequest struct { // ID of image to be disabled // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` -} - -func (irq DisableRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` } // Disable disables image func (i Image) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/disable" diff --git a/pkg/cloudbroker/image/edit.go b/pkg/cloudbroker/image/edit.go index 505f649..5526884 100644 --- a/pkg/cloudbroker/image/edit.go +++ b/pkg/cloudbroker/image/edit.go @@ -2,16 +2,17 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for edit image type EditRequest struct { // ID of the image to edit // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Name for the image // Required: false @@ -38,19 +39,13 @@ type EditRequest struct { Bootable bool `url:"bootable,omitempty" json:"bootable,omitempty"` } -func (irq EditRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // Edit edits an existing image func (i Image) Edit(ctx context.Context, req EditRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/edit" diff --git a/pkg/cloudbroker/image/enable.go b/pkg/cloudbroker/image/enable.go index 7f21edd..23565ee 100644 --- a/pkg/cloudbroker/image/enable.go +++ b/pkg/cloudbroker/image/enable.go @@ -2,31 +2,26 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable image type EnableRequest struct { // ID of image to be enabled // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` -} - -func (irq EnableRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must br set") - } - - return nil + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` } // Enable enables image func (i Image) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/enable" diff --git a/pkg/cloudbroker/image/get.go b/pkg/cloudbroker/image/get.go index 4b9b3fc..16a0560 100644 --- a/pkg/cloudbroker/image/get.go +++ b/pkg/cloudbroker/image/get.go @@ -3,30 +3,25 @@ package image import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get image details type GetRequest struct { // ID of image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` -} - -func (irq GetRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` } // Get get image details by ID func (i Image) Get(ctx context.Context, req GetRequest) (*RecordImage, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/get" diff --git a/pkg/cloudbroker/image/link.go b/pkg/cloudbroker/image/link.go index 00fcbd0..246fc65 100644 --- a/pkg/cloudbroker/image/link.go +++ b/pkg/cloudbroker/image/link.go @@ -2,38 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for link virtual image to another image type LinkRequest struct { // ID of the virtual image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // ID of real image to link this virtual image to // Required: true - TargetID uint64 `url:"targetId" json:"targetId"` -} - -func (irq LinkRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if irq.TargetID == 0 { - return errors.New("validation-error: field TargetID must be set") - } - - return nil + TargetID uint64 `url:"targetId" json:"targetId" validate:"required"` } // Link links virtual image to another image in the platform func (i Image) Link(ctx context.Context, req LinkRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/link" diff --git a/pkg/cloudbroker/image/list_stacks.go b/pkg/cloudbroker/image/list_stacks.go index c91d411..f40180e 100644 --- a/pkg/cloudbroker/image/list_stacks.go +++ b/pkg/cloudbroker/image/list_stacks.go @@ -3,15 +3,16 @@ package image import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list stack type ListStacksRequest struct { // Image ID // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Page number // Required: false @@ -22,19 +23,13 @@ type ListStacksRequest struct { Size uint64 `url:"size,omitempty" json:"size,omitempty"` } -func (irq ListStacksRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // ListStacks gets list stack by image ID func (i Image) ListStacks(ctx context.Context, req ListStacksRequest) (ListStacks, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/listStacks" diff --git a/pkg/cloudbroker/image/rename.go b/pkg/cloudbroker/image/rename.go index 90c2f19..49f3824 100644 --- a/pkg/cloudbroker/image/rename.go +++ b/pkg/cloudbroker/image/rename.go @@ -2,38 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for rename image type RenameRequest struct { // ID of the virtual image to rename // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // New name // Required: true - Name string `url:"name" json:"name"` -} - -func (irq RenameRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if irq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil + Name string `url:"name" json:"name" validate:"required"` } // Rename renames image by ID func (i Image) Rename(ctx context.Context, req RenameRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/rename" diff --git a/pkg/cloudbroker/image/share.go b/pkg/cloudbroker/image/share.go index 501cb57..1c75343 100644 --- a/pkg/cloudbroker/image/share.go +++ b/pkg/cloudbroker/image/share.go @@ -2,38 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for share image type ShareRequest struct { // ID of the image to share // Required: true - ImageId uint64 `url:"imageId" json:"imageId"` + ImageId uint64 `url:"imageId" json:"imageId" validate:"required"` // List of account IDs // Required: true - AccountIDs []uint64 `url:"accounts" json:"accounts"` -} - -func (irq ShareRequest) validate() error { - if irq.ImageId == 0 { - return errors.New("validation-error: field ImageID must be set") - } - if len(irq.AccountIDs) == 0 || irq.AccountIDs == nil { - return errors.New("validation-error: field must be set") - } - - return nil + AccountIDs []uint64 `url:"accounts" json:"accounts" validate:"min=1"` } // Share shares image with accounts func (i Image) Share(ctx context.Context, req ShareRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/share" diff --git a/pkg/cloudbroker/image/sync_create_image.go b/pkg/cloudbroker/image/sync_create_image.go index 0815d66..b093301 100644 --- a/pkg/cloudbroker/image/sync_create_image.go +++ b/pkg/cloudbroker/image/sync_create_image.go @@ -2,7 +2,6 @@ package image import ( "context" - "errors" "net/http" "strconv" @@ -13,22 +12,22 @@ import ( type SyncCreateRequest struct { // Name of the rescue disk // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // URL where to download media from // Required: true - URL string `url:"url" json:"url"` + URL string `url:"url" json:"url" validate:"required"` // Grid (platform) ID where this template should be create in // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Boot type of image // Should be one of: // - bios // - UEFI // Required: true - BootType string `url:"boottype" json:"boottype"` + BootType string `url:"boottype" json:"boottype" validate:"imageBootType"` // Image type // Should be one of: @@ -36,7 +35,7 @@ type SyncCreateRequest struct { // - windows // - or other // Required: true - ImageType string `url:"imagetype" json:"imagetype"` + ImageType string `url:"imagetype" json:"imagetype" validate:"imageType"` // Does this machine supports hot resize // Required: false @@ -80,55 +79,20 @@ type SyncCreateRequest struct { // List of types of compute suitable for image // Example: [ "KVM_X86" ] // Required: true - Drivers []string `url:"drivers" json:"drivers"` + Drivers []string `url:"drivers" json:"drivers" validate:"min=1,max=2,imageDrivers"` // Bootable image or not // Required: false Bootable bool `url:"bootable,omitempty" json:"bootable,omitempty"` } -func (irq SyncCreateRequest) validate() error { - if irq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if irq.URL == "" { - return errors.New("validation-error: field URL must be set") - } - if irq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if irq.BootType == "" { - return errors.New("validation-error: field BootType must be set") - } - if irq.ImageType == "" { - return errors.New("validation-error: field ImageType must be set") - } - validate := validators.StringInSlice(irq.BootType, []string{"bios", "uefi"}) - if !validate { - return errors.New("validation-error: field BootType can be bios or uefi") - } - validate = validators.StringInSlice(irq.ImageType, []string{"windows", "linux", "other"}) - if !validate { - return errors.New("validation-error: field ImageType can be windows, linux or other") - } - if len(irq.Drivers) == 0 || len(irq.Drivers) > 1 { - return errors.New("validation-error: field Drivers can not be empty or have 2 or more elements") - } - for _, v := range irq.Drivers { - validate := validators.StringInSlice(v, []string{"KVM_X86"}) - if !validate { - return errors.New("validation-error: field Drivers can be KVM_X86 only") - } - } - - return nil -} - // SyncCreate creates image from a media identified by URL (in synchronous mode) func (i Image) SyncCreate(ctx context.Context, req SyncCreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/syncCreateImage" diff --git a/pkg/cloudbroker/image/update_nodes.go b/pkg/cloudbroker/image/update_nodes.go index 1d4e307..7b186e9 100644 --- a/pkg/cloudbroker/image/update_nodes.go +++ b/pkg/cloudbroker/image/update_nodes.go @@ -2,35 +2,30 @@ package image import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update nodes type UpdateNodesRequest struct { // Image ID // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // List of stacks // Required: false EnabledStacks []uint64 `url:"enabledStacks,omitempty" json:"enabledStacks,omitempty"` } -func (irq UpdateNodesRequest) validate() error { - if irq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // UpdateNodes udates image availability on nodes func (i Image) UpdateNodes(ctx context.Context, req UpdateNodesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/image/updateNodes" diff --git a/pkg/cloudbroker/k8ci/create.go b/pkg/cloudbroker/k8ci/create.go index 722d1bb..f30ff5a 100644 --- a/pkg/cloudbroker/k8ci/create.go +++ b/pkg/cloudbroker/k8ci/create.go @@ -2,7 +2,6 @@ package k8ci import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type CreateRequest struct { // Name of catalog item // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Version tag // Required: true - Version string `url:"version" json:"version"` + Version string `url:"version" json:"version" validate:"required"` // Optional description // Required: false @@ -25,7 +24,7 @@ type CreateRequest struct { // Image ID for master K8S node // Required: true - MasterImageID uint64 `url:"masterImageId" json:"masterImageId"` + MasterImageID uint64 `url:"masterImageId" json:"masterImageId" validate:"required"` // Compute driver // Should be one of: @@ -33,11 +32,11 @@ type CreateRequest struct { // - KVM_PPC // - etc // Required: true - MasterDriver string `url:"masterDriver" json:"masterDriver"` + MasterDriver string `url:"masterDriver" json:"masterDriver" validate:"driver"` // Image ID for worker K8S node // Required: true - WorkerImageID uint64 `url:"workerImageId" json:"workerImageId"` + WorkerImageID uint64 `url:"workerImageId" json:"workerImageId" validate:"required"` // Compute driver // Should be one of @@ -45,11 +44,11 @@ type CreateRequest struct { // - KVM_PPC // - etc // Required: true - WorkerDriver string `url:"workerDriver" json:"workerDriver"` + WorkerDriver string `url:"workerDriver" json:"workerDriver" validate:"driver"` // Image ID for load balancer node // Required: true - LBImageID uint64 `url:"lbImageId" json:"lbImageId"` + LBImageID uint64 `url:"lbImageId" json:"lbImageId" validate:"required"` // List of account IDs, which have access to this item. // If empty, any account has access @@ -65,51 +64,13 @@ type CreateRequest struct { MaxWorkerCount uint64 `url:"maxWorkerCount" json:"maxWorkerCount"` } -func (krq CreateRequest) validate() error { - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.Version == "" { - return errors.New("validation-error: field Version must be set") - } - if krq.MasterImageID == 0 { - return errors.New("validation-error: field MasterImageID must be set") - } - if krq.MasterDriver == "" { - return errors.New("validation-error: field MasterDriver must be set") - } - if krq.WorkerImageID == 0 { - return errors.New("validation-error: field WorkerImageID must be set") - } - if krq.WorkerDriver == "" { - return errors.New("validation-error: field WorkerDriver must be set") - } - if krq.LBImageID == 0 { - return errors.New("validation-error: field LBImageID must be set") - } - if krq.MaxMasterCount == 0 { - return errors.New("validation-error: field MaxMasterCount must be set") - } - if krq.MaxWorkerCount == 0 { - return errors.New("validation-error: field MaxWorkerCount must be set") - } - validate := validators.StringInSlice(krq.MasterDriver, []string{"KVM_X86", "KVM_PPC"}) - if !validate { - return errors.New("validation-error: field MasterDriver must be one of KVM_X86, KVM_PPC") - } - validate = validators.StringInSlice(krq.WorkerDriver, []string{"KVM_X86", "KVM_PPC"}) - if !validate { - return errors.New("validation-error: field WorkerDriver must be one of KVM_X86, KVM_PPC") - } - - return nil -} - // Create creates a new K8CI instance func (k K8CI) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/create" diff --git a/pkg/cloudbroker/k8ci/delete.go b/pkg/cloudbroker/k8ci/delete.go index 9a280a9..aa4b21e 100644 --- a/pkg/cloudbroker/k8ci/delete.go +++ b/pkg/cloudbroker/k8ci/delete.go @@ -2,35 +2,30 @@ package k8ci import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete K8CI type DeleteRequest struct { // K8CI ID // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` // Delete permanently or not // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (krq DeleteRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - - return nil -} - // Delete deletes K8CI by ID func (k K8CI) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/delete" diff --git a/pkg/cloudbroker/k8ci/disable.go b/pkg/cloudbroker/k8ci/disable.go index ff7fc15..2b89430 100644 --- a/pkg/cloudbroker/k8ci/disable.go +++ b/pkg/cloudbroker/k8ci/disable.go @@ -2,31 +2,26 @@ package k8ci import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable K8CI type DisableRequest struct { // K8CI ID // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` -} - -func (krq DisableRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - - return nil + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` } // Disable disables K8CI func (k K8CI) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/disable" diff --git a/pkg/cloudbroker/k8ci/enable.go b/pkg/cloudbroker/k8ci/enable.go index 57eff11..22b75f9 100644 --- a/pkg/cloudbroker/k8ci/enable.go +++ b/pkg/cloudbroker/k8ci/enable.go @@ -2,31 +2,26 @@ package k8ci import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable K8CI type EnableRequest struct { // K8CI ID // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` -} - -func (krq EnableRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - - return nil + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` } // Enable enables K8CI func (k K8CI) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/enable" diff --git a/pkg/cloudbroker/k8ci/get.go b/pkg/cloudbroker/k8ci/get.go index 32d4aa1..1e6c049 100644 --- a/pkg/cloudbroker/k8ci/get.go +++ b/pkg/cloudbroker/k8ci/get.go @@ -3,30 +3,25 @@ package k8ci import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about K8CI type GetRequest struct { // ID of the K8 catalog item to get // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` -} - -func (krq GetRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - - return nil + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` } // Get gets details of the specified K8 catalog item func (k K8CI) Get(ctx context.Context, req GetRequest) (*RecordK8CI, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/get" diff --git a/pkg/cloudbroker/k8ci/restore.go b/pkg/cloudbroker/k8ci/restore.go index 803510d..a75242b 100644 --- a/pkg/cloudbroker/k8ci/restore.go +++ b/pkg/cloudbroker/k8ci/restore.go @@ -2,31 +2,26 @@ package k8ci import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore K8CI type RestoreRequest struct { // K8CI ID // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` -} - -func (krq RestoreRequest) validate() error { - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - - return nil + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` } // Restore restores K8CI func (k K8CI) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8ci/restore" diff --git a/pkg/cloudbroker/k8s/create.go b/pkg/cloudbroker/k8s/create.go index ae8887e..45f17d3 100644 --- a/pkg/cloudbroker/k8s/create.go +++ b/pkg/cloudbroker/k8s/create.go @@ -2,28 +2,29 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create K8S type CreateRequest struct { // Name of kubernetes cluster // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Resource group ID for cluster placement // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // ID of kubernetes catalog item (K8CI) for cluster // Required: true - K8CIID uint64 `url:"k8ciId" json:"k8ciId"` + K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"` // Name for first worker group created with cluster // Required: true - WorkerGroupName string `url:"workerGroupName" json:"workerGroupName"` + WorkerGroupName string `url:"workerGroupName" json:"workerGroupName" validate:"required"` // ID of SEP to create boot disks for master nodes. // Uses images SEP ID if not set @@ -104,28 +105,13 @@ type CreateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.K8CIID == 0 { - return errors.New("validation-error: field K8CIID must be set") - } - if krq.WorkerGroupName == "" { - return errors.New("validation-error: field WorkerGroupName must be set") - } - - return nil -} - // Create creates a new kubernetes cluster in the specified resource group func (k K8S) Create(ctx context.Context, req CreateRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/create" diff --git a/pkg/cloudbroker/k8s/delete.go b/pkg/cloudbroker/k8s/delete.go index 88d23b6..bbbcce6 100644 --- a/pkg/cloudbroker/k8s/delete.go +++ b/pkg/cloudbroker/k8s/delete.go @@ -2,36 +2,31 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete kubernetes cluster type DeleteRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // True if cluster is destroyed permanently. // Otherwise it can be restored from recycle bin - // Required: true - Permanently bool `url:"permanently" json:"permanently"` -} - -func (krq DeleteRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + // Required: false + Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } // Delete deletes kubernetes cluster func (k K8S) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/delete" diff --git a/pkg/cloudbroker/k8s/delete_master_from_group.go b/pkg/cloudbroker/k8s/delete_master_from_group.go index 0d67c43..a05ddad 100644 --- a/pkg/cloudbroker/k8s/delete_master_from_group.go +++ b/pkg/cloudbroker/k8s/delete_master_from_group.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete master from group type DeleteMasterFromGroupRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the masters compute group // Required: true - MasterGroupID uint64 `url:"masterGroupId" json:"masterGroupId"` + MasterGroupID uint64 `url:"masterGroupId" json:"masterGroupId" validate:"required"` // List of Compute IDs of master nodes to delete // Required: true - MasterIDs []string `url:"masterIds" json:"masterIds"` -} - -func (krq DeleteMasterFromGroupRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.MasterGroupID == 0 { - return errors.New("validation-error: field MasterGroupID must be set") - } - if len(krq.MasterIDs) == 0 { - return errors.New("validation-error: field MasterIDs must be set") - } - - return nil + MasterIDs []string `url:"masterIds" json:"masterIds" validate:"min=1"` } // DeleteMasterFromGroup deletes compute from masters group in selected kubernetes cluster func (k K8S) DeleteMasterFromGroup(ctx context.Context, req DeleteMasterFromGroupRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/deleteMasterFromGroup" diff --git a/pkg/cloudbroker/k8s/delete_worker_from_group.go b/pkg/cloudbroker/k8s/delete_worker_from_group.go index 3ac9b99..0b72503 100644 --- a/pkg/cloudbroker/k8s/delete_worker_from_group.go +++ b/pkg/cloudbroker/k8s/delete_worker_from_group.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete worker from group type DeleteWorkerFromGroupRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to delete // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq DeleteWorkerFromGroupRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkerGroupID must be set") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerIDs must be set") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // DeleteWorkerFromGroup deletes worker compute from workers group in selected kubernetes cluster func (k K8S) DeleteWorkerFromGroup(ctx context.Context, req DeleteWorkerFromGroupRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/deleteWorkerFromGroup" diff --git a/pkg/cloudbroker/k8s/disable.go b/pkg/cloudbroker/k8s/disable.go index 2da7305..ed931da 100644 --- a/pkg/cloudbroker/k8s/disable.go +++ b/pkg/cloudbroker/k8s/disable.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable kubernetes cluster type DisableRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq DisableRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Disable disables kubernetes cluster by ID func (k K8S) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/disable" diff --git a/pkg/cloudbroker/k8s/enable.go b/pkg/cloudbroker/k8s/enable.go index edbaa11..7c733a0 100644 --- a/pkg/cloudbroker/k8s/enable.go +++ b/pkg/cloudbroker/k8s/enable.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable kubernetes cluster type EnableRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq EnableRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Enable enables kubernetes cluster by ID func (k K8S) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/enable" diff --git a/pkg/cloudbroker/k8s/find_group_by_label.go b/pkg/cloudbroker/k8s/find_group_by_label.go index 82542a3..4905ca9 100644 --- a/pkg/cloudbroker/k8s/find_group_by_label.go +++ b/pkg/cloudbroker/k8s/find_group_by_label.go @@ -3,42 +3,34 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about group of kubernetes cluster type FindGroupByLabelRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // List of labels to search // Required: true - Labels []string `url:"labels" json:"labels"` + Labels []string `url:"labels" json:"labels" validate:"min=1"` // If true and more than one label provided, select only groups that have all provided labels. // If false - groups that have at least one label - // Required: true - Strict bool `url:"strict" json:"strict"` -} - -func (krq FindGroupByLabelRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if len(krq.Labels) == 0 { - return errors.New("validation-error: field Labels must be set") - } - - return nil + // Required: false + Strict bool `url:"strict,omitempty" json:"strict,omitempty"` } // FindGroupByLabel find worker group information by one on more labels func (k K8S) FindGroupByLabel(ctx context.Context, req FindGroupByLabelRequest) (ListK8SGroup, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/findGroupByLabel" diff --git a/pkg/cloudbroker/k8s/get.go b/pkg/cloudbroker/k8s/get.go index cedb34c..a0a1677 100644 --- a/pkg/cloudbroker/k8s/get.go +++ b/pkg/cloudbroker/k8s/get.go @@ -3,30 +3,25 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about kubernetes cluster type GetRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq GetRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Get gets information about kubernetes cluster func (k K8S) Get(ctx context.Context, req GetRequest) (*RecordK8S, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/get" diff --git a/pkg/cloudbroker/k8s/get_config.go b/pkg/cloudbroker/k8s/get_config.go index bd9d8c3..2929a50 100644 --- a/pkg/cloudbroker/k8s/get_config.go +++ b/pkg/cloudbroker/k8s/get_config.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get configuration of kubernetes cluster type GetConfigRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq GetConfigRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // GetConfig gets configuration data to access kubernetes cluster func (k K8S) GetConfig(ctx context.Context, req GetConfigRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/getConfig" diff --git a/pkg/cloudbroker/k8s/get_node_annotations.go b/pkg/cloudbroker/k8s/get_node_annotations.go index da05ac2..738dc3b 100644 --- a/pkg/cloudbroker/k8s/get_node_annotations.go +++ b/pkg/cloudbroker/k8s/get_node_annotations.go @@ -2,38 +2,30 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get node annotations type GetNodeAnnotationsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Node ID // Required: true - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeAnnotationsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeAnnotations gets kubernetes cluster worker node annotations func (k K8S) GetNodeAnnotations(ctx context.Context, req GetNodeAnnotationsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/getNodeAnnotations" diff --git a/pkg/cloudbroker/k8s/get_node_labels.go b/pkg/cloudbroker/k8s/get_node_labels.go index 2268ee7..864ac97 100644 --- a/pkg/cloudbroker/k8s/get_node_labels.go +++ b/pkg/cloudbroker/k8s/get_node_labels.go @@ -2,38 +2,30 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get worker node labels type GetNodeLabelsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Compute ID of worker node // Required: true - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeLabelsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeLabels gets kubernetes cluster worker node labels func (k K8S) GetNodeLabels(ctx context.Context, req GetNodeLabelsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/getNodeLabels" diff --git a/pkg/cloudbroker/k8s/get_node_taints.go b/pkg/cloudbroker/k8s/get_node_taints.go index a301b70..6bf57f9 100644 --- a/pkg/cloudbroker/k8s/get_node_taints.go +++ b/pkg/cloudbroker/k8s/get_node_taints.go @@ -2,38 +2,30 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get node taints type GetNodeTaintsRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Node ID - // Required: false - NodeID uint64 `url:"nodeId" json:"nodeId"` -} - -func (krq GetNodeTaintsRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID can not be empty or equal to 0") - } - if krq.NodeID == 0 { - return errors.New("validation-error: field NodeID can not be empty or equal to 0") - } - - return nil + // Required: true + NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"` } // GetNodeTaints gets kubernetes cluster worker node taints func (k K8S) GetNodeTaints(ctx context.Context, req GetNodeTaintsRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/getNodeTaints" diff --git a/pkg/cloudbroker/k8s/restore.go b/pkg/cloudbroker/k8s/restore.go index 6bd3a65..419041a 100644 --- a/pkg/cloudbroker/k8s/restore.go +++ b/pkg/cloudbroker/k8s/restore.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore kubernetes cluster type RestoreRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq RestoreRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Restore restore kubernetes cluster from recycle bin func (k K8S) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/restore" diff --git a/pkg/cloudbroker/k8s/start.go b/pkg/cloudbroker/k8s/start.go index e25d31f..8aa2ef5 100644 --- a/pkg/cloudbroker/k8s/start.go +++ b/pkg/cloudbroker/k8s/start.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start kubernetes cluster type StartRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq StartRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Start starts kubernetes cluster by ID func (k K8S) Start(ctx context.Context, req StartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/start" diff --git a/pkg/cloudbroker/k8s/stop.go b/pkg/cloudbroker/k8s/stop.go index d1d585c..d7baa2d 100644 --- a/pkg/cloudbroker/k8s/stop.go +++ b/pkg/cloudbroker/k8s/stop.go @@ -2,31 +2,26 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop kubernetes cluster type StopRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` -} - -func (krq StopRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` } // Stop stops kubernetes cluster by ID func (k K8S) Stop(ctx context.Context, req StopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/stop" diff --git a/pkg/cloudbroker/k8s/update.go b/pkg/cloudbroker/k8s/update.go index c2b0dc4..6f80a3f 100644 --- a/pkg/cloudbroker/k8s/update.go +++ b/pkg/cloudbroker/k8s/update.go @@ -2,16 +2,17 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update kubernetes cluster type UpdateRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // New name to set. // If empty string is passed, name is not updated @@ -24,19 +25,13 @@ type UpdateRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq UpdateRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - - return nil -} - // Update updates name or description of kubernetes cluster func (k K8S) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/update" diff --git a/pkg/cloudbroker/k8s/worker_add.go b/pkg/cloudbroker/k8s/worker_add.go index 4cd0475..f78082c 100644 --- a/pkg/cloudbroker/k8s/worker_add.go +++ b/pkg/cloudbroker/k8s/worker_add.go @@ -3,41 +3,33 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add worker to a kubernetes cluster type WorkerAddRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // How many worker nodes to add - // Required: true - Num uint64 `url:"num" json:"num"` -} - -func (krq WorkerAddRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID must be set") - } - - return nil + // Required: false + Num uint64 `url:"num,omitempty" json:"num,omitempty"` } // WorkerAdd adds worker nodes to a kubernetes cluster func (k K8S) WorkerAdd(ctx context.Context, req WorkerAddRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workerAdd" diff --git a/pkg/cloudbroker/k8s/worker_reset.go b/pkg/cloudbroker/k8s/worker_reset.go index c8257e8..e5e3999 100644 --- a/pkg/cloudbroker/k8s/worker_reset.go +++ b/pkg/cloudbroker/k8s/worker_reset.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for hard reset kubernetes cluster type WorkerResetRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to reset // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq WorkerResetRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID must be set") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerID must be set") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // WorkerReset hard reset (compute start + stop) worker node of the kubernetes cluster func (k K8S) WorkerReset(ctx context.Context, req WorkerResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workerReset" diff --git a/pkg/cloudbroker/k8s/worker_restart.go b/pkg/cloudbroker/k8s/worker_restart.go index f336200..4cd87b9 100644 --- a/pkg/cloudbroker/k8s/worker_restart.go +++ b/pkg/cloudbroker/k8s/worker_restart.go @@ -2,45 +2,34 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart worker node type WorkerRestartRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"requireD"` // ID of the workers compute group // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` // Compute ID of worker node to restart // Required: true - WorkerID uint64 `url:"workerId" json:"workerId"` -} - -func (krq WorkerRestartRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID must be set") - } - if krq.WorkerID == 0 { - return errors.New("validation-error: field WorkerID must be set") - } - - return nil + WorkerID uint64 `url:"workerId" json:"workerId" validate:"required"` } // WorkerRestart soft restart (reboot OS) worker node of the kubernetes cluster func (k8s K8S) WorkerRestart(ctx context.Context, req WorkerRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workerRestart" diff --git a/pkg/cloudbroker/k8s/workers_group_add.go b/pkg/cloudbroker/k8s/workers_group_add.go index dda4fdd..3f2516f 100644 --- a/pkg/cloudbroker/k8s/workers_group_add.go +++ b/pkg/cloudbroker/k8s/workers_group_add.go @@ -2,20 +2,21 @@ package k8s import ( "context" - "errors" "net/http" "strings" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add workers group type WorkersGroupAddRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of SEP to create boot disks for default worker nodes group. // Uses images SEP ID if not set @@ -59,22 +60,13 @@ type WorkersGroupAddRequest struct { WorkerDisk uint64 `url:"workerDisk,omitempty" json:"workerDisk,omitempty"` } -func (krq WorkersGroupAddRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - - return nil -} - // WorkersGroupAdd adds workers group to kubernetes cluster func (k K8S) WorkersGroupAdd(ctx context.Context, req WorkersGroupAddRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workersGroupAdd" diff --git a/pkg/cloudbroker/k8s/workers_group_delete.go b/pkg/cloudbroker/k8s/workers_group_delete.go index ac6775e..aa90947 100644 --- a/pkg/cloudbroker/k8s/workers_group_delete.go +++ b/pkg/cloudbroker/k8s/workers_group_delete.go @@ -2,38 +2,30 @@ package k8s import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete workers group type WorkersGroupDeleteRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group ID // Required: true - WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId"` -} - -func (krq WorkersGroupDeleteRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.WorkersGroupID == 0 { - return errors.New("validation-error: field WorkersGroupID must be set") - } - - return nil + WorkersGroupID uint64 `url:"workersGroupId" json:"workersGroupId" validate:"required"` } // WorkersGroupDelete deletes workers group from kubernetes cluster func (k K8S) WorkersGroupDelete(ctx context.Context, req WorkersGroupDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workersGroupDelete" diff --git a/pkg/cloudbroker/k8s/workers_group_get_by_name.go b/pkg/cloudbroker/k8s/workers_group_get_by_name.go index 6113df2..d2362b9 100644 --- a/pkg/cloudbroker/k8s/workers_group_get_by_name.go +++ b/pkg/cloudbroker/k8s/workers_group_get_by_name.go @@ -3,37 +3,29 @@ package k8s import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about worker group type WorkersGroupGetByNameRequest struct { // Kubernetes cluster ID // Required: true - K8SID uint64 `url:"k8sId" json:"k8sId"` + K8SID uint64 `url:"k8sId" json:"k8sId" validate:"required"` // Worker group name // Required: true - GroupName string `url:"groupName" json:"groupName"` -} - -func (krq WorkersGroupGetByNameRequest) validate() error { - if krq.K8SID == 0 { - return errors.New("validation-error: field K8SID must be set") - } - if krq.GroupName == "" { - return errors.New("validation-error: field WorkersGroupID must be set") - } - - return nil + GroupName string `url:"groupName" json:"groupName" validate:"required"` } // WorkersGroupGetByName gets worker group metadata by name func (k K8S) WorkersGroupGetByName(ctx context.Context, req WorkersGroupGetByNameRequest) (*RecordK8SGroup, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/k8s/workersGroupGetByName" diff --git a/pkg/cloudbroker/kvmppc/create.go b/pkg/cloudbroker/kvmppc/create.go index cd37bdf..f08df09 100644 --- a/pkg/cloudbroker/kvmppc/create.go +++ b/pkg/cloudbroker/kvmppc/create.go @@ -2,34 +2,35 @@ package kvmppc import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM PowerPC VM type CreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // ID of the OS image to base this VM on; // Could be boot disk image or CD-ROM image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB // Required: false @@ -50,7 +51,7 @@ type CreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -92,31 +93,13 @@ type CreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // Create creates KVM PowerPC VM based on specified OS image func (k KVMPPC) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmppc/create" diff --git a/pkg/cloudbroker/kvmppc/create_blank.go b/pkg/cloudbroker/kvmppc/create_blank.go index fbca0c8..ac41a26 100644 --- a/pkg/cloudbroker/kvmppc/create_blank.go +++ b/pkg/cloudbroker/kvmppc/create_blank.go @@ -2,42 +2,43 @@ package kvmppc import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM PowerPC VM from scratch type CreateBlankRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Size of the boot disk in GB // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + BootDisk uint64 `url:"bootDisk" json:"bootDisk" validate:"required"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + SEPID uint64 `url:"sepId" json:"sepId" validate:"required"` // Pool to use if SEP ID is set, can be also empty if needed to be chosen by system // Required: true - Pool string `url:"pool" json:"pool"` + Pool string `url:"pool" json:"pool" validate:"required"` // Network type // Should be one of: @@ -45,7 +46,7 @@ type CreateBlankRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -63,37 +64,13 @@ type CreateBlankRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateBlankRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.BootDisk == 0 { - return errors.New("validation-error: field BootDisk must be set") - } - if krq.SEPID == 0 { - return errors.New("validation-error: field SepID must be set") - } - if krq.Pool == "" { - return errors.New("validation-error: field Pool must be set") - } - - return nil -} - // CreateBlank creates KVM PowerPC VM from scratch func (k KVMPPC) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmppc/createBlank" diff --git a/pkg/cloudbroker/kvmppc/mass_create.go b/pkg/cloudbroker/kvmppc/mass_create.go index e55997b..92b66c3 100644 --- a/pkg/cloudbroker/kvmppc/mass_create.go +++ b/pkg/cloudbroker/kvmppc/mass_create.go @@ -3,49 +3,50 @@ package kvmppc import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for mass create KVM PowerPC type MassCreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number of VMs // Required: true - Count uint64 `url:"count" json:"count"` + Count uint64 `url:"count" json:"count" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Image ID // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB - // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + // Required: false + BootDisk uint64 `url:"bootDisk,omitempty" json:"bootDisk,omitempty"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set - // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + // Required: false + SEPID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"` // Pool to use if SEP ID is set, can be also empty if needed to be chosen by system - // Required: true - Pool string `url:"pool" json:"pool"` + // Required: false + Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Network type // Should be one of: @@ -53,7 +54,7 @@ type MassCreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -79,34 +80,13 @@ type MassCreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (krq MassCreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.Count == 0 { - return errors.New("validation-error: field Count must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // MassCreate creates KVM PPC computes based on specified OS image func (k KVMPPC) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmppc/massCreate" diff --git a/pkg/cloudbroker/kvmx86/create.go b/pkg/cloudbroker/kvmx86/create.go index 1d5127a..fe6a546 100644 --- a/pkg/cloudbroker/kvmx86/create.go +++ b/pkg/cloudbroker/kvmx86/create.go @@ -2,34 +2,35 @@ package kvmx86 import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM x86 VM type CreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // ID of the OS image to base this VM on; // Could be boot disk image or CD-ROM image // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB // Required: false @@ -50,7 +51,7 @@ type CreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -92,31 +93,13 @@ type CreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (krq CreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // Create creates KVM PowerPC VM based on specified OS image func (k KVMX86) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmx86/create" diff --git a/pkg/cloudbroker/kvmx86/create_blank.go b/pkg/cloudbroker/kvmx86/create_blank.go index 35ed7f8..6fe3a96 100644 --- a/pkg/cloudbroker/kvmx86/create_blank.go +++ b/pkg/cloudbroker/kvmx86/create_blank.go @@ -2,42 +2,43 @@ package kvmx86 import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create KVM x86 VM from scratch type CreateBlankRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Size of the boot disk in GB // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + BootDisk uint64 `url:"bootDisk" json:"bootDisk" validate:"required"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + SEPID uint64 `url:"sepId" json:"sepId" validate:"required"` // Pool to use if sepId is set, can be also empty if needed to be chosen by system // Required: true - Pool string `url:"pool" json:"pool"` + Pool string `url:"pool" json:"pool" validate:"required"` // Network type // Should be one of: @@ -45,7 +46,7 @@ type CreateBlankRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -63,37 +64,13 @@ type CreateBlankRequest struct { Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (krq CreateBlankRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.BootDisk == 0 { - return errors.New("validation-error: field BootDisk must be set") - } - if krq.SEPID == 0 { - return errors.New("validation-error: field SepID must be set") - } - if krq.Pool == "" { - return errors.New("validation-error: field Pool must be set") - } - - return nil -} - // CreateBlank creates KVM x86 VM from scratch func (k KVMX86) CreateBlank(ctx context.Context, req CreateBlankRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmx86/createBlank" diff --git a/pkg/cloudbroker/kvmx86/mass_create.go b/pkg/cloudbroker/kvmx86/mass_create.go index 5b84f75..ad5fbaf 100644 --- a/pkg/cloudbroker/kvmx86/mass_create.go +++ b/pkg/cloudbroker/kvmx86/mass_create.go @@ -3,49 +3,50 @@ package kvmx86 import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for mass create KVM x86 type MassCreateRequest struct { // ID of the resource group, which will own this VM // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of this VM. // Must be unique among all VMs (including those in DELETED state) in target resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Number of VMs // Required: true - Count uint64 `url:"count" json:"count"` + Count uint64 `url:"count" json:"count" validate:"required"` // Number CPUs to allocate to this VM // Required: true - CPU uint64 `url:"cpu" json:"cpu"` + CPU uint64 `url:"cpu" json:"cpu" validate:"required"` // Volume of RAM in MB to allocate to this VM // Required: true - RAM uint64 `url:"ram" json:"ram"` + RAM uint64 `url:"ram" json:"ram" validate:"required"` // Image ID // Required: true - ImageID uint64 `url:"imageId" json:"imageId"` + ImageID uint64 `url:"imageId" json:"imageId" validate:"required"` // Size of the boot disk in GB - // Required: true - BootDisk uint64 `url:"bootDisk" json:"bootDisk"` + // Required: false + BootDisk uint64 `url:"bootDisk,omitempty" json:"bootDisk,omitempty"` // ID of SEP to create boot disk on. // Uses images SEP ID if not set - // Required: true - SEPID uint64 `url:"sepId" json:"sepId"` + // Required: false + SEPID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"` // Pool to use if SEP ID is set, can be also empty if needed to be chosen by system - // Required: true - Pool string `url:"pool" json:"pool"` + // Required: false + Pool string `url:"pool,omitempty" json:"pool,omitempty"` // Network type // Should be one of: @@ -53,7 +54,7 @@ type MassCreateRequest struct { // - EXTNET // - NONE // Required: false - NetType string `url:"netType,omitempty" json:"netType,omitempty"` + NetType string `url:"netType,omitempty" json:"netType,omitempty" validate:"omitempty,kvmNetType"` // Network ID for connect to, // for EXTNET - external network ID, @@ -83,34 +84,13 @@ type MassCreateRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (krq MassCreateRequest) validate() error { - if krq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if krq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if krq.Count == 0 { - return errors.New("validation-error: field Count must be set") - } - if krq.CPU == 0 { - return errors.New("validation-error: field CPU must be set") - } - if krq.RAM == 0 { - return errors.New("validation-error: field RAM must be set") - } - if krq.ImageID == 0 { - return errors.New("validation-error: field ImageID must be set") - } - - return nil -} - // MassCreate creates KVM x86 computes based on specified OS image func (k KVMX86) MassCreate(ctx context.Context, req MassCreateRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/kvmx86/massCreate" diff --git a/pkg/cloudbroker/lb/backend_create.go b/pkg/cloudbroker/lb/backend_create.go index 4334fa9..2356ebc 100644 --- a/pkg/cloudbroker/lb/backend_create.go +++ b/pkg/cloudbroker/lb/backend_create.go @@ -2,7 +2,6 @@ package lb import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type BackendCreateRequest struct { // ID of the load balancer instance to backendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all backends of this load balancer - name of the new backend to create // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Algorithm // Should be one of: @@ -25,7 +24,7 @@ type BackendCreateRequest struct { // - static-rr // - leastconn // Required: false - Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty"` + Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty" validate:"omitempty,lbAlgorithm"` // Interval in milliseconds between two consecutive availability // checks of the server that is considered available @@ -68,26 +67,13 @@ type BackendCreateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendCreateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - validate := validators.StringInSlice(lbrq.Algorithm, []string{"roundrobin", "static-rr", "leastconn"}) - if !validate { - return errors.New("validation-error: field Algorithm must be one of roundrobin, static-rr, leastconn") - } - - return nil -} - // BackendCreate creates new backend on the specified load balancer func (lb LB) BackendCreate(ctx context.Context, req BackendCreateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendCreate" diff --git a/pkg/cloudbroker/lb/backend_delete.go b/pkg/cloudbroker/lb/backend_delete.go index 3cf6347..0009168 100644 --- a/pkg/cloudbroker/lb/backend_delete.go +++ b/pkg/cloudbroker/lb/backend_delete.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete backend type BackendDeleteRequest struct { // ID of the load balancer instance to BackendDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Cannot be emtpy string - name of the backend to delete // Required: true - BackendName string `url:"backendName" json:"backendName"` -} - -func (lbrq BackendDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - - return nil + BackendName string `url:"backendName" json:"backendName" validate:"required"` } // BackendDelete deletes backend from the specified load balancer. // Warning: you cannot undo this action! func (lb LB) BackendDelete(ctx context.Context, req BackendDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendDelete" diff --git a/pkg/cloudbroker/lb/backend_server_add.go b/pkg/cloudbroker/lb/backend_server_add.go index 098a248..b856394 100644 --- a/pkg/cloudbroker/lb/backend_server_add.go +++ b/pkg/cloudbroker/lb/backend_server_add.go @@ -2,32 +2,33 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add server definition to the backend type BackendServerAddRequest struct { // ID of the load balancer instance to BackendServerAdd // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` + ServerName string `url:"serverName" json:"serverName" validate:"required"` // IP address of the server // Required: true - Address string `url:"address" json:"address"` + Address string `url:"address" json:"address" validate:"required"` // Port number on the server // Required: true - Port uint64 `url:"port" json:"port"` + Port uint64 `url:"port" json:"port" validate:"required"` // Set to disabled if this server should be used regardless of its state // Required: false @@ -71,31 +72,13 @@ type BackendServerAddRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendServerAddRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName must be set") - } - if lbrq.Address == "" { - return errors.New("validation-error: field Address must be set") - } - if lbrq.Port == 0 { - return errors.New("validation-error: field Port must be set") - } - - return nil -} - // BackendServerAdd adds server definition to the backend on the specified load balancer func (lb LB) BackendServerAdd(ctx context.Context, req BackendServerAddRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendServerAdd" diff --git a/pkg/cloudbroker/lb/backend_server_delete.go b/pkg/cloudbroker/lb/backend_server_delete.go index b2e240d..f6fafe3 100644 --- a/pkg/cloudbroker/lb/backend_server_delete.go +++ b/pkg/cloudbroker/lb/backend_server_delete.go @@ -2,48 +2,35 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete server definition type BackendServerDeleteRequest struct { // ID of the load balancer instance to BackendServerDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` -} - -func (lbrq BackendServerDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName must be set") - } - - return nil + ServerName string `url:"serverName" json:"serverName" validate:"required"` } // BackendServerDelete deletes server definition from the backend on the specified load balancer. // Warning: you cannot undo this action! func (lb LB) BackendServerDelete(ctx context.Context, req BackendServerDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendServerDelete" diff --git a/pkg/cloudbroker/lb/backend_server_update.go b/pkg/cloudbroker/lb/backend_server_update.go index 4ec6672..5a9bd77 100644 --- a/pkg/cloudbroker/lb/backend_server_update.go +++ b/pkg/cloudbroker/lb/backend_server_update.go @@ -2,32 +2,33 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update server type BackendServerUpdateRequest struct { // ID of the load balancer instance to BackendServerAdd // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must match one of the existing backens - name of the backend to add servers to // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Must be unique among all servers defined for this backend - name of the server definition to add // Required: true - ServerName string `url:"serverName" json:"serverName"` + ServerName string `url:"serverName" json:"serverName" validate:"required"` // IP address of the server // Required: true - Address string `url:"address" json:"address"` + Address string `url:"address" json:"address" validate:"required"` // Port number on the server // Required: true - Port uint64 `url:"port" json:"port"` + Port uint64 `url:"port" json:"port" validate:"required"` // Set to disabled if this server should be used regardless of its state // Required: false @@ -71,31 +72,13 @@ type BackendServerUpdateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendServerUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - if lbrq.ServerName == "" { - return errors.New("validation-error: field ServerName must be set") - } - if lbrq.Address == "" { - return errors.New("validation-error: field Address must be set") - } - if lbrq.Port == 0 { - return errors.New("validation-error: field Port must be set") - } - - return nil -} - // BackendServerUpdate updates server definition on the backend of load balancer func (lb LB) BackendServerUpdate(ctx context.Context, req BackendServerUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendServerUpdate" diff --git a/pkg/cloudbroker/lb/backend_update.go b/pkg/cloudbroker/lb/backend_update.go index ee6f0d8..55fbde8 100644 --- a/pkg/cloudbroker/lb/backend_update.go +++ b/pkg/cloudbroker/lb/backend_update.go @@ -2,20 +2,21 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update backend type BackendUpdateRequest struct { // ID of the load balancer instance to backendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all backends of this load balancer - name of the new backend to create // Required: true - BackendName string `url:"backendName" json:"backendName"` + BackendName string `url:"backendName" json:"backendName" validate:"required"` // Algorithm // Should be one of: @@ -23,7 +24,7 @@ type BackendUpdateRequest struct { // - static-rr // - leastconn // Required: false - Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty"` + Algorithm string `url:"algorithm,omitempty" json:"algorithm,omitempty" validate:"omitempty,lbAlgorithm"` // Interval in milliseconds between two consecutive availability // checks of the server that is considered available @@ -66,22 +67,13 @@ type BackendUpdateRequest struct { Weight uint64 `url:"weight,omitempty" json:"weight,omitempty"` } -func (lbrq BackendUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - - return nil -} - // BackendUpdate updates existing backend on the specified load balancer. Note that backend name cannot be changed func (lb LB) BackendUpdate(ctx context.Context, req BackendUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/backendUpdate" diff --git a/pkg/cloudbroker/lb/config_reset.go b/pkg/cloudbroker/lb/config_reset.go index 316d9ac..4819e22 100644 --- a/pkg/cloudbroker/lb/config_reset.go +++ b/pkg/cloudbroker/lb/config_reset.go @@ -2,32 +2,27 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reset config type ConfigResetRequest struct { // ID of the load balancer instance to ConfigReset // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq ConfigResetRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // ConfigReset reset current software configuration of the specified load balancer. // Warning: this action cannot be undone! func (lb LB) ConfigReset(ctx context.Context, req ConfigResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/configReset" diff --git a/pkg/cloudbroker/lb/create.go b/pkg/cloudbroker/lb/create.go index 5bdc574..7e6e56a 100644 --- a/pkg/cloudbroker/lb/create.go +++ b/pkg/cloudbroker/lb/create.go @@ -2,21 +2,22 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create load balancer type CreateRequest struct { // ID of the resource group where this load balancer instance will be located // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Name of the load balancer. // Must be unique among all load balancers in this resource group // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // OS image ID to create load balancer from // Required: false @@ -24,43 +25,28 @@ type CreateRequest struct { // External network to connect this load balancer to // Required: true - ExtNetID uint64 `url:"extnetId" json:"extnetId"` + ExtNetID uint64 `url:"extnetId" json:"extnetId" validate:"required"` // Internal network (VINS) to connect this load balancer to // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Start now Load balancer // Required: false - Start bool `url:"start" json:"start"` + Start bool `url:"start" json:"start" validate:"required"` // Text description of this load balancer // Required: false Description string `url:"desc,omitempty" json:"desc,omitempty"` } -func (lbrq CreateRequest) validate() error { - if lbrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if lbrq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if lbrq.ExtNetID == 0 { - return errors.New("validation-error: field ExtNetID must be set") - } - if lbrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Create method will create a new load balancer instance func (lb LB) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/create" diff --git a/pkg/cloudbroker/lb/delete.go b/pkg/cloudbroker/lb/delete.go index 4f5d352..f2704af 100644 --- a/pkg/cloudbroker/lb/delete.go +++ b/pkg/cloudbroker/lb/delete.go @@ -2,35 +2,30 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete load balancer type DeleteRequest struct { // ID of the load balancer instance to delete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Set to true to delete load balancer immediately bypassing recycle bin // Required: false Permanently bool `url:"permanently,omitempty" json:"permanently,omitempty"` } -func (lbrq DeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil -} - // Delete deletes specified load balancer func (lb LB) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/delete" diff --git a/pkg/cloudbroker/lb/disable.go b/pkg/cloudbroker/lb/disable.go index 51e95bb..f241fa6 100644 --- a/pkg/cloudbroker/lb/disable.go +++ b/pkg/cloudbroker/lb/disable.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable load balancer type DisableRequest struct { // ID of the load balancer instance to disable // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq DisableRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Disable disables specified load balancer instance func (lb LB) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/disable" diff --git a/pkg/cloudbroker/lb/enable.go b/pkg/cloudbroker/lb/enable.go index 6c38fe2..c5781e8 100644 --- a/pkg/cloudbroker/lb/enable.go +++ b/pkg/cloudbroker/lb/enable.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable load balancer type EnableRequest struct { // ID of the load balancer instance to enable // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq EnableRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Enable enables specified load balancer instance func (lb LB) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/enable" diff --git a/pkg/cloudbroker/lb/filter.go b/pkg/cloudbroker/lb/filter.go index 774f0a2..b3a3ca4 100644 --- a/pkg/cloudbroker/lb/filter.go +++ b/pkg/cloudbroker/lb/filter.go @@ -1,5 +1,12 @@ package lb +import ( + "context" + + "repository.basistech.ru/BASIS/decort-golang-sdk/interfaces" + "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s" +) + // FilterByID returns ListLB with specified ID. func (ll ListLB) FilterByID(id uint64) ListLB { predicate := func(rlb RecordLB) bool { @@ -36,6 +43,26 @@ func (ll ListLB) FilterByImageID(imageID uint64) ListLB { return ll.FilterFunc(predicate) } +// FilterByK8SID returns ListLB used by specified K8S cluster. +func (ll ListLB) FilterByK8SID(ctx context.Context, k8sID uint64, decortClient interfaces.Caller) (ListLB, error) { + caller := k8s.New(decortClient) + + req := k8s.GetRequest{ + K8SID: k8sID, + } + + cluster, err := caller.Get(ctx, req) + if err != nil { + return nil, err + } + + predicate := func(rlb RecordLB) bool { + return cluster.LBID == rlb.ID + } + + return ll.FilterFunc(predicate), nil +} + // FilterFunc allows filtering ListLB based on a user-specified predicate. func (ll ListLB) FilterFunc(predicate func(RecordLB) bool) ListLB { var result ListLB diff --git a/pkg/cloudbroker/lb/frontend_bind.go b/pkg/cloudbroker/lb/frontend_bind.go index aec71f2..5ee7b48 100644 --- a/pkg/cloudbroker/lb/frontend_bind.go +++ b/pkg/cloudbroker/lb/frontend_bind.go @@ -2,62 +2,45 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for frontend bind type FrontendBindRequest struct { // ID of the load balancer instance to FrontendBind // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to update // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to update // Required: true - BindingName string `url:"bindingName" json:"bindingName"` + BindingName string `url:"bindingName" json:"bindingName" validate:"required"` // If specified must be within the IP range of either Ext Net or ViNS, // where this load balancer is connected - new IP address to use for this binding. // If omitted, current IP address is retained - // Required: false - BindingAddress string `url:"bindingAddress,omitempty" json:"bindingAddress,omitempty"` + // Required: true + BindingAddress string `url:"bindingAddress" json:"bindingAddress" validate:"required"` // New port number to use for this binding. // If omitted, current port number is retained - // Required: false - BindingPort uint64 `url:"bindingPort,omitempty" json:"bindingPort,omitempty"` -} - -func (lbrq FrontendBindRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName must be set") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName must be set") - } - if lbrq.BindingAddress == "" { - return errors.New("validation-error: field BindingAddress must be set") - } - if lbrq.BindingPort == 0 { - return errors.New("validation-error: field BindingPort must be set") - } - - return nil + // Required: true + BindingPort uint64 `url:"bindingPort" json:"bindingPort" validate:"required"` } // FrontendBind bind frontend from specified load balancer instance func (lb LB) FrontendBind(ctx context.Context, req FrontendBindRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/frontendBind" diff --git a/pkg/cloudbroker/lb/frontend_bind_delete.go b/pkg/cloudbroker/lb/frontend_bind_delete.go index b16ae1e..fc9e8bd 100644 --- a/pkg/cloudbroker/lb/frontend_bind_delete.go +++ b/pkg/cloudbroker/lb/frontend_bind_delete.go @@ -2,45 +2,34 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete bind type FrontendBindDeleteRequest struct { // ID of the load balancer instance to FrontendBindDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to delete // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to delete // Required: true - BindingName string `url:"bindingName" json:"bindingName"` -} - -func (lbrq FrontendBindDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName must be set") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName must be set") - } - - return nil + BindingName string `url:"bindingName" json:"bindingName" validate:"required"` } // FrontendBindDelete deletes binding from the specified load balancer frontend func (lb LB) FrontendBindDelete(ctx context.Context, req FrontendBindDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/frontendBindDelete" diff --git a/pkg/cloudbroker/lb/frontend_binding_update.go b/pkg/cloudbroker/lb/frontend_binding_update.go index 915e613..1ce9a76 100644 --- a/pkg/cloudbroker/lb/frontend_binding_update.go +++ b/pkg/cloudbroker/lb/frontend_binding_update.go @@ -2,62 +2,45 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update binding type FrontendBindUpdateRequest struct { // ID of the load balancer instance to FrontendBindUpdate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to update // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Name of the binding to update // Required: true - BindingName string `url:"bindingName" json:"bindingName"` + BindingName string `url:"bindingName" json:"bindingName" validate:"required"` // If specified must be within the IP range of either Ext Net or ViNS, // where this load balancer is connected - new IP address to use for this binding. // If omitted, current IP address is retained - // Required: false - BindingAddress string `url:"bindingAddress,omitempty" json:"bindingAddress,omitempty"` + // Required: true + BindingAddress string `url:"bindingAddress" json:"bindingAddress" validate:"required"` // New port number to use for this binding. // If omitted, current port number is retained - // Required: false - BindingPort uint64 `url:"bindingPort,omitempty" json:"bindingPort,omitempty"` -} - -func (lbrq FrontendBindUpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName must be set") - } - if lbrq.BindingName == "" { - return errors.New("validation-error: field BindingName must be set") - } - if lbrq.BindingAddress == "" { - return errors.New("validation-error: field BindingAddress must be set") - } - if lbrq.BindingPort == 0 { - return errors.New("validation-error: field BindingPort must be set") - } - - return nil + // Required: true + BindingPort uint64 `url:"bindingPort" json:"bindingPort" validate:"required"` } // FrontendBindUpdate updates binding for the specified load balancer frontend func (lb LB) FrontendBindUpdate(ctx context.Context, req FrontendBindUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/frontendBindingUpdate" diff --git a/pkg/cloudbroker/lb/frontend_create.go b/pkg/cloudbroker/lb/frontend_create.go index c1299c2..dfcb3b3 100644 --- a/pkg/cloudbroker/lb/frontend_create.go +++ b/pkg/cloudbroker/lb/frontend_create.go @@ -2,47 +2,36 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create frontend type FrontendCreateRequest struct { // ID of the load balancer instance to FrontendCreate // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Must be unique among all frontends of // this load balancer - name of the new frontend to create // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` // Should be one of the backends existing on // this load balancer - name of the backend to use // Required: true - BackendName string `url:"backendName" json:"backendName"` -} - -func (lbrq FrontendCreateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName must be set") - } - if lbrq.BackendName == "" { - return errors.New("validation-error: field BackendName must be set") - } - - return nil + BackendName string `url:"backendName" json:"backendName" validate:"required"` } // FrontendCreate creates new frontend on the specified load balancer func (l LB) FrontendCreate(ctx context.Context, req FrontendCreateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/frontendCreate" diff --git a/pkg/cloudbroker/lb/frontend_delete.go b/pkg/cloudbroker/lb/frontend_delete.go index 8b8c856..d9e28d0 100644 --- a/pkg/cloudbroker/lb/frontend_delete.go +++ b/pkg/cloudbroker/lb/frontend_delete.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete frontend type FrontendDeleteRequest struct { // ID of the load balancer instance to FrontendDelete // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // Name of the frontend to delete // Required: true - FrontendName string `url:"frontendName" json:"frontendName"` -} - -func (lbrq FrontendDeleteRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.FrontendName == "" { - return errors.New("validation-error: field FrontendName must be set") - } - - return nil + FrontendName string `url:"frontendName" json:"frontendName" validate:"required"` } // FrontendDelete deletes frontend from the specified load balancer. // Warning: you cannot undo this action! func (lb LB) FrontendDelete(ctx context.Context, req FrontendDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/frontendDelete" diff --git a/pkg/cloudbroker/lb/get.go b/pkg/cloudbroker/lb/get.go index 0cc8377..b751a56 100644 --- a/pkg/cloudbroker/lb/get.go +++ b/pkg/cloudbroker/lb/get.go @@ -3,30 +3,25 @@ package lb import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about load balancer type GetRequest struct { // ID of the load balancer to get details for // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq GetRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Get gets detailed information about load balancer func (lb LB) Get(ctx context.Context, req GetRequest) (*RecordLB, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/get" diff --git a/pkg/cloudbroker/lb/restart.go b/pkg/cloudbroker/lb/restart.go index 182f884..f2d1dd9 100644 --- a/pkg/cloudbroker/lb/restart.go +++ b/pkg/cloudbroker/lb/restart.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restart load balancer type RestartRequest struct { // ID of the load balancer instance to restart // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq RestartRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Restart restarts specified load balancer instance func (lb LB) Restart(ctx context.Context, req RestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/restart" diff --git a/pkg/cloudbroker/lb/restore.go b/pkg/cloudbroker/lb/restore.go index af48e34..2e5c80d 100644 --- a/pkg/cloudbroker/lb/restore.go +++ b/pkg/cloudbroker/lb/restore.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore load balancer type RestoreRequest struct { // ID of the load balancer instance to restore // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq RestoreRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Restore restore load balancer from recycle bin func (lb LB) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/restore" diff --git a/pkg/cloudbroker/lb/start.go b/pkg/cloudbroker/lb/start.go index e04a3ca..9a79c94 100644 --- a/pkg/cloudbroker/lb/start.go +++ b/pkg/cloudbroker/lb/start.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start load balancer type StartRequest struct { // ID of the LB instance to start // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq StartRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Start starts specified load balancer instance func (lb LB) Start(ctx context.Context, req StartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/start" diff --git a/pkg/cloudbroker/lb/stop.go b/pkg/cloudbroker/lb/stop.go index c878b65..5251937 100644 --- a/pkg/cloudbroker/lb/stop.go +++ b/pkg/cloudbroker/lb/stop.go @@ -2,31 +2,26 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop load balancer type StopRequest struct { // ID of the LB instance to stop // Required: true - LBID uint64 `url:"lbId" json:"lbId"` -} - -func (lbrq StopRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - - return nil + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` } // Stop stops specified load balancer instance func (lb LB) Stop(ctx context.Context, req StopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/stop" diff --git a/pkg/cloudbroker/lb/update.go b/pkg/cloudbroker/lb/update.go index aa4ded3..b72ad88 100644 --- a/pkg/cloudbroker/lb/update.go +++ b/pkg/cloudbroker/lb/update.go @@ -2,39 +2,31 @@ package lb import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update load balancer type UpdateRequest struct { // ID of the load balancer to update // Required: true - LBID uint64 `url:"lbId" json:"lbId"` + LBID uint64 `url:"lbId" json:"lbId" validate:"required"` // New description of this load balancer. // If omitted, current description is retained // Required: true - Description string `url:"desc" json:"desc"` -} - -func (lbrq UpdateRequest) validate() error { - if lbrq.LBID == 0 { - return errors.New("validation-error: field LBID must be set") - } - if lbrq.Description == "" { - return errors.New("validation-error: field Description must be set") - } - - return nil + Description string `url:"desc" json:"desc" validate:"required"` } // Update updates some of load balancer attributes func (lb LB) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/lb/update" diff --git a/pkg/cloudbroker/rg/access_grant.go b/pkg/cloudbroker/rg/access_grant.go index 600c5c8..6e90dbe 100644 --- a/pkg/cloudbroker/rg/access_grant.go +++ b/pkg/cloudbroker/rg/access_grant.go @@ -2,7 +2,6 @@ package rg import ( "context" - "errors" "net/http" "strconv" @@ -13,11 +12,11 @@ import ( type AccessGrantRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // User or group name to grant access // Required: true - User string `url:"user" json:"user"` + User string `url:"user" json:"user" validate:"required"` // Access rights to set, // Should be one of: @@ -25,33 +24,20 @@ type AccessGrantRequest struct { // - "RCX" // - "ARCXDU" // Required: true - Right string `url:"right" json:"right"` + Right string `url:"right" json:"right" validate:"accessType"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq AccessGrantRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if rgrq.User == "" { - return errors.New("validation-error: field User must be set") - } - validate := validators.StringInSlice(rgrq.Right, []string{"R", "RCX", "ARCXDU"}) - if !validate { - return errors.New("field Right can only be one of 'R', 'RCX' or 'ARCXDU'") - } - - return nil -} - // AccessGrant grants user or group access to the resource group as specified func (r RG) AccessGrant(ctx context.Context, req AccessGrantRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/accessGrant" diff --git a/pkg/cloudbroker/rg/access_revoke.go b/pkg/cloudbroker/rg/access_revoke.go index 717cb67..e6a8e45 100644 --- a/pkg/cloudbroker/rg/access_revoke.go +++ b/pkg/cloudbroker/rg/access_revoke.go @@ -2,42 +2,34 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access type AccessRevokeRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // User or group name to revoke access // Required: true - User string `url:"user" json:"user"` + User string `url:"user" json:"user" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq AccessRevokeRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if rgrq.User == "" { - return errors.New("validation-error: field User must be set") - } - - return nil -} - // AccessRevoke revokes specified user or group access from the resource group func (r RG) AccessRevoke(ctx context.Context, req AccessRevokeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/accessRevoke" diff --git a/pkg/cloudbroker/rg/affinity_group_computes.go b/pkg/cloudbroker/rg/affinity_group_computes.go index 3e69540..cd4601f 100644 --- a/pkg/cloudbroker/rg/affinity_group_computes.go +++ b/pkg/cloudbroker/rg/affinity_group_computes.go @@ -3,37 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of all computes with their relationships type AffinityGroupComputesRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Affinity group label // Required: true - AffinityGroup string `url:"affinityGroup" json:"affinityGroup"` -} - -func (rgrq AffinityGroupComputesRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if rgrq.AffinityGroup == "" { - return errors.New("validation-error: field AffinityGroup must be set") - } - - return nil + AffinityGroup string `url:"affinityGroup" json:"affinityGroup" validate:"required"` } // AffinityGroupComputes gets list of all computes with their relationships to another computes func (r RG) AffinityGroupComputes(ctx context.Context, req AffinityGroupComputesRequest) (ListAffinityGroupCompute, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/affinityGroupComputes" diff --git a/pkg/cloudbroker/rg/affinity_groups_get.go b/pkg/cloudbroker/rg/affinity_groups_get.go index 3a0c55e..ea181b9 100644 --- a/pkg/cloudbroker/rg/affinity_groups_get.go +++ b/pkg/cloudbroker/rg/affinity_groups_get.go @@ -3,37 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list computes from affinity group type AffinityGroupsGetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Label affinity group // Required: true - AffinityGroup string `url:"affinityGroup" json:"affinityGroup"` -} - -func (rgrq AffinityGroupsGetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if rgrq.AffinityGroup == "" { - return errors.New("validation-error: field AffinityGroup must be set") - } - - return nil + AffinityGroup string `url:"affinityGroup" json:"affinityGroup" validate:"required"` } // AffinityGroupsGet gets list computes in the specified affinity group func (r RG) AffinityGroupsGet(ctx context.Context, req AffinityGroupsGetRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/affinityGroupsGet" diff --git a/pkg/cloudbroker/rg/affinity_groups_list.go b/pkg/cloudbroker/rg/affinity_groups_list.go index f69b41a..9426da3 100644 --- a/pkg/cloudbroker/rg/affinity_groups_list.go +++ b/pkg/cloudbroker/rg/affinity_groups_list.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of affinity groups from resource group type AffinityGroupsListRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq AffinityGroupsListRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // AffinityGroupsList gets all currently defined affinity groups in this resource group with compute IDs func (r RG) AffinityGroupsList(ctx context.Context, req AffinityGroupsListRequest) (map[string][]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/affinityGroupsList" diff --git a/pkg/cloudbroker/rg/audits.go b/pkg/cloudbroker/rg/audits.go index 776c6b7..59bda95 100644 --- a/pkg/cloudbroker/rg/audits.go +++ b/pkg/cloudbroker/rg/audits.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audit type AuditsRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq AuditsRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // Audits gets audit records for the specified resource group object func (r RG) Audits(ctx context.Context, req AuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/audits" diff --git a/pkg/cloudbroker/rg/create.go b/pkg/cloudbroker/rg/create.go index c586029..29dddaa 100644 --- a/pkg/cloudbroker/rg/create.go +++ b/pkg/cloudbroker/rg/create.go @@ -2,24 +2,25 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create resource group type CreateRequest struct { // Account, which will own this resource group // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Grid ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // Name of this resource group. Must be unique within the account // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Max size of memory in MB // Required: false @@ -53,7 +54,7 @@ type CreateRequest struct { // - PUBLIC // - NONE // Required: false - DefNet string `url:"def_net,omitempty" json:"def_net,omitempty"` + DefNet string `url:"def_net,omitempty" json:"def_net,omitempty" validate:"omitempty,rgDefNet"` // Private network IP CIDR if default network PRIVATE // Required: false @@ -84,25 +85,13 @@ type CreateRequest struct { UniqPools []string `url:"unuqPools,omitempty" json:"unuqPools,omitempty"` } -func (rgrq CreateRequest) validate() error { - if rgrq.AccountID == 0 { - return errors.New("field AccountID can not be empty or equal to 0") - } - if rgrq.GID == 0 { - return errors.New("field GID can not be empty or equal to 0") - } - if len(rgrq.Name) < 2 { - return errors.New("field Name can not be shorter than two bytes") - } - - return nil -} - // Create creates resource group func (r RG) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/create" diff --git a/pkg/cloudbroker/rg/delete.go b/pkg/cloudbroker/rg/delete.go index 9337bac..31813dd 100644 --- a/pkg/cloudbroker/rg/delete.go +++ b/pkg/cloudbroker/rg/delete.go @@ -2,16 +2,17 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete resource group type DeleteRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Set to True if you want force delete non-empty resource group // Required: false @@ -27,19 +28,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq DeleteRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Delete deletes resource group func (r RG) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/delete" diff --git a/pkg/cloudbroker/rg/disable.go b/pkg/cloudbroker/rg/disable.go index e7609ab..56d7b9f 100644 --- a/pkg/cloudbroker/rg/disable.go +++ b/pkg/cloudbroker/rg/disable.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable resource group type DisableRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq DisableRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Disable disables resource group by ID func (r RG) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/disable" diff --git a/pkg/cloudbroker/rg/enable.go b/pkg/cloudbroker/rg/enable.go index 7afc7e0..7a4b03f 100644 --- a/pkg/cloudbroker/rg/enable.go +++ b/pkg/cloudbroker/rg/enable.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable resource group type EnableRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq EnableRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Enable enables resource group by ID func (r RG) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/enable" diff --git a/pkg/cloudbroker/rg/get.go b/pkg/cloudbroker/rg/get.go index c308597..5c9c10a 100644 --- a/pkg/cloudbroker/rg/get.go +++ b/pkg/cloudbroker/rg/get.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get detailed information about resource group type GetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq GetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Get gets current configuration of the resource group func (r RG) Get(ctx context.Context, req GetRequest) (*RecordRG, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/get" diff --git a/pkg/cloudbroker/rg/list_computes.go b/pkg/cloudbroker/rg/list_computes.go index 4378d9e..8928c3d 100644 --- a/pkg/cloudbroker/rg/list_computes.go +++ b/pkg/cloudbroker/rg/list_computes.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of computes type ListComputesRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq ListComputesRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // ListComputes gets list of all compute instances under specified resource group, accessible by the user func (r RG) ListComputes(ctx context.Context, req ListComputesRequest) (ListComputes, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/listComputes" diff --git a/pkg/cloudbroker/rg/list_lb.go b/pkg/cloudbroker/rg/list_lb.go index a8b33a4..c5800e9 100644 --- a/pkg/cloudbroker/rg/list_lb.go +++ b/pkg/cloudbroker/rg/list_lb.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list load balancers type ListLBRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq ListLBRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // ListLB gets list all load balancers in the specified resource group, accessible by the user func (r RG) ListLB(ctx context.Context, req ListLBRequest) (ListLB, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/listLb" diff --git a/pkg/cloudbroker/rg/list_pfw.go b/pkg/cloudbroker/rg/list_pfw.go index c5eef61..2dd13eb 100644 --- a/pkg/cloudbroker/rg/list_pfw.go +++ b/pkg/cloudbroker/rg/list_pfw.go @@ -3,30 +3,25 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list port forward rules type ListPFWRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` -} - -func (rgrq ListPFWRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` } // ListPFW gets list port forward rules for the specified resource group func (r RG) ListPFW(ctx context.Context, req ListPFWRequest) (ListPFW, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/listPFW" diff --git a/pkg/cloudbroker/rg/list_vins.go b/pkg/cloudbroker/rg/list_vins.go index f26d940..f8c6121 100644 --- a/pkg/cloudbroker/rg/list_vins.go +++ b/pkg/cloudbroker/rg/list_vins.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINSes type ListVINSRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq ListVINSRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // ListVINS gets list all ViNSes under specified resource group, accessible by the user func (r RG) ListVINS(ctx context.Context, req ListVINSRequest) (ListVINS, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/listVins" diff --git a/pkg/cloudbroker/rg/mass_delete.go b/pkg/cloudbroker/rg/mass_delete.go index 2e8a8a6..8b17d65 100644 --- a/pkg/cloudbroker/rg/mass_delete.go +++ b/pkg/cloudbroker/rg/mass_delete.go @@ -2,15 +2,16 @@ package rg import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete several resource groups type MassDeleteRequest struct { // IDs of the resource groups // Required: true - RGIDs []uint64 `url:"rgIds" json:"rgIds"` + RGIDs []uint64 `url:"rgIds" json:"rgIds" validate:"min=1"` // Set to true if you want force delete non-empty resource groups // Required: false @@ -28,19 +29,13 @@ type MassDeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq MassDeleteRequest) validate() error { - if len(rgrq.RGIDs) == 0 { - return errors.New("validation-error: field RGIDs must be set") - } - - return nil -} - // MassDelete starts jobs to delete several resource groups func (r RG) MassDelete(ctx context.Context, req MassDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/massDelete" diff --git a/pkg/cloudbroker/rg/mass_disable.go b/pkg/cloudbroker/rg/mass_disable.go index efcfb56..b7c11f7 100644 --- a/pkg/cloudbroker/rg/mass_disable.go +++ b/pkg/cloudbroker/rg/mass_disable.go @@ -2,34 +2,29 @@ package rg import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable several resource groups type MassDisableRequest struct { // IDs of the resource groups // Required: true - RGIDs []uint64 `url:"rgIds" json:"rgIds"` + RGIDs []uint64 `url:"rgIds" json:"rgIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq MassDisableRequest) validate() error { - if len(rgrq.RGIDs) == 0 { - return errors.New("validation-error: field RGIDs must be set") - } - - return nil -} - // MassDisable start jobs to disable several resource groups func (r RG) MassDisable(ctx context.Context, req MassDisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/massDisable" diff --git a/pkg/cloudbroker/rg/mass_enable.go b/pkg/cloudbroker/rg/mass_enable.go index 9bdf874..42f677d 100644 --- a/pkg/cloudbroker/rg/mass_enable.go +++ b/pkg/cloudbroker/rg/mass_enable.go @@ -2,34 +2,29 @@ package rg import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable several resource groups type MassEnableRequest struct { // IDs of the resource groups // Required: true - RGIDs []uint64 `url:"rgIds" json:"rgIds"` + RGIDs []uint64 `url:"rgIds" json:"rgIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq MassEnableRequest) validate() error { - if len(rgrq.RGIDs) == 0 { - return errors.New("validation-error: field RGIDs must be set") - } - - return nil -} - // MassEnable start jobs to enable several resource groups func (r RG) MassEnable(ctx context.Context, req MassEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/massEnable" diff --git a/pkg/cloudbroker/rg/restore.go b/pkg/cloudbroker/rg/restore.go index 0ee0082..bb0aedc 100644 --- a/pkg/cloudbroker/rg/restore.go +++ b/pkg/cloudbroker/rg/restore.go @@ -2,35 +2,30 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore resource group type RestoreRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq RestoreRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Restore restores resource group from recycle bin func (r RG) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/restore" diff --git a/pkg/cloudbroker/rg/set_def_net.go b/pkg/cloudbroker/rg/set_def_net.go index 675de7b..2820222 100644 --- a/pkg/cloudbroker/rg/set_def_net.go +++ b/pkg/cloudbroker/rg/set_def_net.go @@ -2,7 +2,6 @@ package rg import ( "context" - "errors" "net/http" "strconv" @@ -13,41 +12,31 @@ import ( type SetDefNetRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Network type // Should be one of: // - "PUBLIC" // - "PRIVATE" // Required: true - NetType string `url:"netType" json:"netType"` + NetType string `url:"netType" json:"netType" validate:"rgNetType"` // Network ID // Required: false - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId,omitempty" json:"netId,omitempty"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq SetDefNetRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - validate := validators.StringInSlice(rgrq.NetType, []string{"PUBLIC", "PRIVATE"}) - if !validate { - return errors.New("validation-error: field NetType must be one of PRIVATE or PUBLIC") - } - - return nil -} - // SetDefNet sets default network for attach associated virtual machines func (r RG) SetDefNet(ctx context.Context, req SetDefNetRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/setDefNet" diff --git a/pkg/cloudbroker/rg/update.go b/pkg/cloudbroker/rg/update.go index 8c32acf..2208a97 100644 --- a/pkg/cloudbroker/rg/update.go +++ b/pkg/cloudbroker/rg/update.go @@ -2,16 +2,17 @@ package rg import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update resource group type UpdateRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // New name // Required: false @@ -54,19 +55,13 @@ type UpdateRequest struct { UniqPools []string `url:"uniqPools,omitempty" json:"uniqPools,omitempty"` } -func (rgrq UpdateRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Update updates resource group func (r RG) Update(ctx context.Context, req UpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/update" diff --git a/pkg/cloudbroker/rg/update_resource_types.go b/pkg/cloudbroker/rg/update_resource_types.go index 376274b..988595b 100644 --- a/pkg/cloudbroker/rg/update_resource_types.go +++ b/pkg/cloudbroker/rg/update_resource_types.go @@ -2,7 +2,6 @@ package rg import ( "context" - "errors" "net/http" "strconv" @@ -13,7 +12,7 @@ import ( type UpdateResourceTypesRequest struct { // ID of resource group // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Resource types available to create in this resource group // Each element in a resource type slice must be one of: @@ -24,29 +23,15 @@ type UpdateResourceTypesRequest struct { // - lb // - flipgroup // Required: true - ResTypes []string `url:"resourceTypes" json:"resourceTypes"` -} - -func (rgrq UpdateResourceTypesRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - if len(rgrq.ResTypes) > 0 { - for _, value := range rgrq.ResTypes { - validate := validators.StringInSlice(value, []string{"compute", "vins", "k8s", "openshift", "lb", "flipgroup"}) - if !validate { - return errors.New("validation-error: Every resource type specified should be one of [compute, vins, k8s, openshift, lb, flipgroup]") - } - } - } - - return nil + ResTypes []string `url:"resourceTypes" json:"resourceTypes" validate:"min=1,resTypes"` } func (r RG) UpdateResourceTypes(ctx context.Context, req UpdateResourceTypesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/updateResourceTypes" diff --git a/pkg/cloudbroker/rg/usage.go b/pkg/cloudbroker/rg/usage.go index bbece0e..609f6af 100644 --- a/pkg/cloudbroker/rg/usage.go +++ b/pkg/cloudbroker/rg/usage.go @@ -3,34 +3,29 @@ package rg import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get report of resource usage type UsageRequest struct { // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (rgrq UsageRequest) validate() error { - if rgrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // Usage gets report resource usage on the resource group func (r RG) Usage(ctx context.Context, req UsageRequest) (*Reservation, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/rg/usage" diff --git a/pkg/cloudbroker/sep/access_grant.go b/pkg/cloudbroker/sep/access_grant.go index 8fcd304..22d32fc 100644 --- a/pkg/cloudbroker/sep/access_grant.go +++ b/pkg/cloudbroker/sep/access_grant.go @@ -2,39 +2,31 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to SEP type AccessGrantRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Account ID to grant access to the specified SEP. If 0, // the SEP will be available for all accounts with no exceptions // Required: true - AccountID uint64 `url:"account_id" json:"account_id"` -} - -func (srq AccessGrantRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"account_id" json:"account_id" validate:"required"` } // AccessGrant grant access to SEP func (s SEP) AccessGrant(ctx context.Context, req AccessGrantRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/accessGrant" diff --git a/pkg/cloudbroker/sep/access_grant_to_pool.go b/pkg/cloudbroker/sep/access_grant_to_pool.go index cc4622b..9bdefce 100644 --- a/pkg/cloudbroker/sep/access_grant_to_pool.go +++ b/pkg/cloudbroker/sep/access_grant_to_pool.go @@ -2,20 +2,21 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for grant access to pool SEP type AccessGrantToPoolRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Pool name // Required: true - PoolName string `url:"pool_name" json:"pool_name"` + PoolName string `url:"pool_name" json:"pool_name" validate:"required"` // Account ID to grant access to the specified pool SEP // Required: false @@ -26,22 +27,13 @@ type AccessGrantToPoolRequest struct { RGID uint64 `url:"resgroup_id,omitempty" json:"resgroup_id,omitempty"` } -func (srq AccessGrantToPoolRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.PoolName == "" { - return errors.New("validation-error: field PoolName must be set") - } - - return nil -} - // AccessGrantToPool grant access to pool SEP func (s SEP) AccessGrantToPool(ctx context.Context, req AccessGrantToPoolRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/accessGrantToPool" diff --git a/pkg/cloudbroker/sep/access_revoke.go b/pkg/cloudbroker/sep/access_revoke.go index 5187dd1..d0ba4bc 100644 --- a/pkg/cloudbroker/sep/access_revoke.go +++ b/pkg/cloudbroker/sep/access_revoke.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access to SEP type AccessRevokeRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Account ID to revoke access to the specified SEP // Required: true - AccountID uint64 `url:"account_id" json:"account_id"` -} - -func (srq AccessRevokeRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil + AccountID uint64 `url:"account_id" json:"account_id" validate:"required"` } // AccessRevoke revoke access to SEP func (s SEP) AccessRevoke(ctx context.Context, req AccessRevokeRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/accessRevoke" diff --git a/pkg/cloudbroker/sep/access_revoke_to_pool.go b/pkg/cloudbroker/sep/access_revoke_to_pool.go index 5d64378..2ebc1fe 100644 --- a/pkg/cloudbroker/sep/access_revoke_to_pool.go +++ b/pkg/cloudbroker/sep/access_revoke_to_pool.go @@ -2,20 +2,21 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for revoke access to pool SEP type AccessRevokeToPoolRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Pool name // Required: true - PoolName string `url:"pool_name" json:"pool_name"` + PoolName string `url:"pool_name" json:"pool_name" validate:"required"` // Account ID to grant access to the specified pool SEP // Required: false @@ -26,22 +27,13 @@ type AccessRevokeToPoolRequest struct { RGID uint64 `url:"resgroup_id,omitempty" json:"resgroup_id,omitempty"` } -func (srq AccessRevokeToPoolRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.PoolName == "" { - return errors.New("validation-error: field PoolName must be set") - } - - return nil -} - // AccessRevokeToPool revoke access to pool SEP func (s SEP) AccessRevokeToPool(ctx context.Context, req AccessRevokeToPoolRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/accessRevokeToPool" diff --git a/pkg/cloudbroker/sep/add_consumer_nodes.go b/pkg/cloudbroker/sep/add_consumer_nodes.go index fcd6cef..9dfe1a8 100644 --- a/pkg/cloudbroker/sep/add_consumer_nodes.go +++ b/pkg/cloudbroker/sep/add_consumer_nodes.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add consumer nodes type AddConsumerNodesRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // List of nodes IDs // Required: true - ConsumerNIDs []uint64 `url:"consumer_nids" json:"consumer_nids"` -} - -func (srq AddConsumerNodesRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if len(srq.ConsumerNIDs) == 0 { - return errors.New("validation-error: field ConsumerNIDs must be set") - } - - return nil + ConsumerNIDs []uint64 `url:"consumer_nids" json:"consumer_nids" validate:"min=1"` } // AddConsumerNodes add consumer nodes to SEP parameters func (s SEP) AddConsumerNodes(ctx context.Context, req AddConsumerNodesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/addConsumerNodes" diff --git a/pkg/cloudbroker/sep/add_provider_nodes.go b/pkg/cloudbroker/sep/add_provider_nodes.go index f28b4c7..219775e 100644 --- a/pkg/cloudbroker/sep/add_provider_nodes.go +++ b/pkg/cloudbroker/sep/add_provider_nodes.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for add provider nodes type AddProviderNodesRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // List of node IDs // Required: true - ProviderNIDs []uint64 `url:"provider_nids" json:"provider_nids"` -} - -func (srq AddProviderNodesRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if len(srq.ProviderNIDs) == 0 { - return errors.New("validation-error: field ProviderNIDs must be set") - } - - return nil + ProviderNIDs []uint64 `url:"provider_nids" json:"provider_nids" validate:"min=1"` } // AddProviderNodes add provider nodes to SEP parameters func (s SEP) AddProviderNodes(ctx context.Context, req AddProviderNodesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/addProviderNodes" diff --git a/pkg/cloudbroker/sep/config_field_edit.go b/pkg/cloudbroker/sep/config_field_edit.go index 472079d..641f769 100644 --- a/pkg/cloudbroker/sep/config_field_edit.go +++ b/pkg/cloudbroker/sep/config_field_edit.go @@ -2,7 +2,6 @@ package sep import ( "context" - "errors" "net/http" "strconv" @@ -13,15 +12,15 @@ import ( type ConfigFieldEditRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Field name // Required: true - FieldName string `url:"field_name" json:"field_name"` + FieldName string `url:"field_name" json:"field_name" validate:"required"` // Field value // Required: true - FieldValue string `url:"field_value" json:"field_value"` + FieldValue string `url:"field_value" json:"field_value" validate:"required"` // Field type // Should be one of: @@ -31,35 +30,16 @@ type ConfigFieldEditRequest struct { // - list // - dict // Required: true - FieldType string `url:"field_type" json:"field_type"` -} - -func (srq ConfigFieldEditRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.FieldName == "" { - return errors.New("validation-error: field FieldName must be set") - } - if srq.FieldValue == "" { - return errors.New("validation-error: field FieldValue must be set") - } - if srq.FieldType == "" { - return errors.New("validation-error: field FieldType must be set") - } - validate := validators.StringInSlice(srq.FieldType, []string{"int", "str", "bool", "list", "dict"}) - if !validate { - return errors.New("validation-error: field FieldType must be one of int, str, bool, list, dict") - } - - return nil + FieldType string `url:"field_type" json:"field_type" validate:"sepFieldType"` } // ConfigFieldEdit edit SEP config field value func (s SEP) ConfigFieldEdit(ctx context.Context, req ConfigFieldEditRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/configFieldEdit" diff --git a/pkg/cloudbroker/sep/config_insert.go b/pkg/cloudbroker/sep/config_insert.go index a2126de..6f7eb94 100644 --- a/pkg/cloudbroker/sep/config_insert.go +++ b/pkg/cloudbroker/sep/config_insert.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for insert config type ConfigInsertRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Storage provider config // Required: true - Config string `url:"config" json:"config"` -} - -func (srq ConfigInsertRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.Config == "" { - return errors.New("validation-error: feold Config must be set") - } - - return nil + Config string `url:"config" json:"config" validate:"required"` } // ConfigInsert insert config to SEP func (s SEP) ConfigInsert(ctx context.Context, req ConfigInsertRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/configInsert" diff --git a/pkg/cloudbroker/sep/config_validate.go b/pkg/cloudbroker/sep/config_validate.go index 3ad68fd..dfef548 100644 --- a/pkg/cloudbroker/sep/config_validate.go +++ b/pkg/cloudbroker/sep/config_validate.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for validate config type ConfigValidateRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Storage provider config // Required: true - Config string `url:"config" json:"config"` -} - -func (srq ConfigValidateRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.Config == "" { - return errors.New("validation-error: feold Config must be set") - } - - return nil + Config string `url:"config" json:"config" validate:"required"` } // ConfigValidate verify config for the SEP func (s SEP) ConfigValidate(ctx context.Context, req ConfigValidateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/configValidate" diff --git a/pkg/cloudbroker/sep/consumption.go b/pkg/cloudbroker/sep/consumption.go index 25b10c3..433ae67 100644 --- a/pkg/cloudbroker/sep/consumption.go +++ b/pkg/cloudbroker/sep/consumption.go @@ -3,30 +3,25 @@ package sep import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get consumption info type ConsumptionRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq ConsumptionRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // Consumption get SEP consumption info func (s SEP) Consumption(ctx context.Context, req ConsumptionRequest) (*RecordConsumption, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/consumption" diff --git a/pkg/cloudbroker/sep/create.go b/pkg/cloudbroker/sep/create.go index 99faba6..2c86210 100644 --- a/pkg/cloudbroker/sep/create.go +++ b/pkg/cloudbroker/sep/create.go @@ -2,24 +2,25 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create SEP object type CreateRequest struct { // Grid ID // Required: true - GID uint64 `url:"gid" json:"gid"` + GID uint64 `url:"gid" json:"gid" validate:"required"` // SEP name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Type of storage // Required: true - SEPType string `url:"sep_type" json:"sep_type"` + SEPType string `url:"sep_type" json:"sep_type" validate:"required"` // Description // Required: false @@ -42,25 +43,13 @@ type CreateRequest struct { Enable bool `url:"enable,omitempty" json:"enable,omitempty"` } -func (srq CreateRequest) validate() error { - if srq.GID == 0 { - return errors.New("validation-error: field GID must be set") - } - if srq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if srq.SEPType == "" { - return errors.New("validation-error: field SEPType must be set") - } - - return nil -} - // Create creates SEP object func (s SEP) Create(ctx context.Context, req CreateRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/create" diff --git a/pkg/cloudbroker/sep/decommission.go b/pkg/cloudbroker/sep/decommission.go index 034e2d2..da6136d 100644 --- a/pkg/cloudbroker/sep/decommission.go +++ b/pkg/cloudbroker/sep/decommission.go @@ -2,35 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for decommission type DecommissionRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Clear disks and images physically // Required: false ClearPhisically bool `url:"clear_physically,omitempty" json:"clear_physically,omitempty"` } -func (srq DecommissionRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil -} - // Decommission unlink everything that exists from SEP func (s SEP) Decommission(ctx context.Context, req DecommissionRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/decommission" diff --git a/pkg/cloudbroker/sep/del_consumer_nodes.go b/pkg/cloudbroker/sep/del_consumer_nodes.go index d59f577..7fc8f1c 100644 --- a/pkg/cloudbroker/sep/del_consumer_nodes.go +++ b/pkg/cloudbroker/sep/del_consumer_nodes.go @@ -2,38 +2,30 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for exclude consumer nodes type DelConsumerNodesRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // List of consumer node IDs // Required: true - ConsumerNIDs []uint64 `url:"consumer_nids" json:"consumer_nids"` -} - -func (srq DelConsumerNodesRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if len(srq.ConsumerNIDs) == 0 { - return errors.New("validation-error: field ConsumerNIDs must be set") - } - - return nil + ConsumerNIDs []uint64 `url:"consumer_nids" json:"consumer_nids" validate:"min=1"` } // DelConsumerNodes exclude consumer nodes from SEP parameters func (s SEP) DelConsumerNodes(ctx context.Context, req DelConsumerNodesRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/delConsumerNodes" diff --git a/pkg/cloudbroker/sep/delete.go b/pkg/cloudbroker/sep/delete.go index 3fb4529..b51b748 100644 --- a/pkg/cloudbroker/sep/delete.go +++ b/pkg/cloudbroker/sep/delete.go @@ -2,31 +2,26 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete SEP type DeleteRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq DeleteRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // Delete deletes SEP by ID func (s SEP) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/delete" diff --git a/pkg/cloudbroker/sep/disable.go b/pkg/cloudbroker/sep/disable.go index 87b4678..7abd2d7 100644 --- a/pkg/cloudbroker/sep/disable.go +++ b/pkg/cloudbroker/sep/disable.go @@ -2,31 +2,26 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable SEP type DisableRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq DisableRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // Disable disables SEP by ID func (s SEP) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/disable" diff --git a/pkg/cloudbroker/sep/disk_list.go b/pkg/cloudbroker/sep/disk_list.go index ffcf96d..4b112bc 100644 --- a/pkg/cloudbroker/sep/disk_list.go +++ b/pkg/cloudbroker/sep/disk_list.go @@ -3,34 +3,29 @@ package sep import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of disk IDs type DiskListRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Pool name // Required: false PoolName string `url:"pool_name,omitempty" json:"pool_name,omitempty"` } -func (srq DiskListRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil -} - // DiskList get list of disk IDs, who use this SEP and pool (if provided) func (s SEP) DiskList(ctx context.Context, req DiskListRequest) ([]uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/diskList" diff --git a/pkg/cloudbroker/sep/enable.go b/pkg/cloudbroker/sep/enable.go index ea21b2c..f2aaddc 100644 --- a/pkg/cloudbroker/sep/enable.go +++ b/pkg/cloudbroker/sep/enable.go @@ -2,31 +2,26 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable SEP type EnableRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq EnableRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // Enable enables SEP by ID func (s SEP) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/enable" diff --git a/pkg/cloudbroker/sep/get.go b/pkg/cloudbroker/sep/get.go index b82e8aa..ea14a98 100644 --- a/pkg/cloudbroker/sep/get.go +++ b/pkg/cloudbroker/sep/get.go @@ -3,30 +3,25 @@ package sep import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get SEP parameters type GetRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq GetRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // Get gets SEP parameters func (s SEP) Get(ctx context.Context, req GetRequest) (*RecordSEP, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/get" diff --git a/pkg/cloudbroker/sep/get_config.go b/pkg/cloudbroker/sep/get_config.go index 02382ca..2b72470 100644 --- a/pkg/cloudbroker/sep/get_config.go +++ b/pkg/cloudbroker/sep/get_config.go @@ -3,30 +3,25 @@ package sep import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get SEP config type GetConfigRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq GetConfigRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // GetConfig gets SEP config func (s SEP) GetConfig(ctx context.Context, req GetConfigRequest) (*SEPConfig, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/getConfig" diff --git a/pkg/cloudbroker/sep/get_pool.go b/pkg/cloudbroker/sep/get_pool.go index 6a9dd9a..cfcef93 100644 --- a/pkg/cloudbroker/sep/get_pool.go +++ b/pkg/cloudbroker/sep/get_pool.go @@ -3,37 +3,29 @@ package sep import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get SEP pool config by name type GetPoolRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` // Pool name // Required: true - PoolName string `url:"pool_name" json:"pool_name"` -} - -func (srq GetPoolRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - if srq.PoolName == "" { - return errors.New("validation-error: PoolName must be set") - } - - return nil + PoolName string `url:"pool_name" json:"pool_name" validate:"required"` } // GetPool gets SEP pool config by name func (s SEP) GetPool(ctx context.Context, req GetPoolRequest) (*RecordPool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/getPool" diff --git a/pkg/cloudbroker/sep/update_capacity_limit.go b/pkg/cloudbroker/sep/update_capacity_limit.go index c7a1394..f7b93ec 100644 --- a/pkg/cloudbroker/sep/update_capacity_limit.go +++ b/pkg/cloudbroker/sep/update_capacity_limit.go @@ -2,31 +2,26 @@ package sep import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update capacity limits type UpdateCapacityLimitRequest struct { // Storage endpoint provider ID // Required: true - SEPID uint64 `url:"sep_id" json:"sep_id"` -} - -func (srq UpdateCapacityLimitRequest) validate() error { - if srq.SEPID == 0 { - return errors.New("validation-error: field SEPID must be set") - } - - return nil + SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"` } // UpdateCapacityLimit updates SEP capacity limit func (s SEP) UpdateCapacityLimit(ctx context.Context, req UpdateCapacityLimitRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/sep/updateCapacityLimit" diff --git a/pkg/cloudbroker/tasks/get.go b/pkg/cloudbroker/tasks/get.go index 7ddefc4..4175411 100644 --- a/pkg/cloudbroker/tasks/get.go +++ b/pkg/cloudbroker/tasks/get.go @@ -3,30 +3,25 @@ package tasks import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get background API task status and result type GetRequest struct { // ID of audit GUID // Required: true - AuditID string `url:"auditId" json:"auditId"` -} - -func (trq GetRequest) validate() error { - if trq.AuditID == "" { - return errors.New("validation-error: field AuditID must be set") - } - - return nil + AuditID string `url:"auditId" json:"auditId" validate:"required"` } // Get gets background API task status and result func (t Tasks) Get(ctx context.Context, req GetRequest) (*RecordTask, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/tasks/get" diff --git a/pkg/cloudbroker/tasks/models.go b/pkg/cloudbroker/tasks/models.go index a8a6ac0..7709926 100644 --- a/pkg/cloudbroker/tasks/models.go +++ b/pkg/cloudbroker/tasks/models.go @@ -32,6 +32,12 @@ func (r *InfoResult) UnmarshalJSON(b []byte) error { } else { return fmt.Errorf("could not unmarshal %v into int", res[0]) } + } else { + n, err := strconv.Atoi(string(b)) + if err != nil { + return err + } + *r = InfoResult(n) } return nil diff --git a/pkg/cloudbroker/vins/audits.go b/pkg/cloudbroker/vins/audits.go index 14f13ac..abf2f58 100644 --- a/pkg/cloudbroker/vins/audits.go +++ b/pkg/cloudbroker/vins/audits.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get audits type AuditsRequest struct { // ID of the VINS // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq AuditsRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // Audits gets audit records for the specified VINS object func (v VINS) Audits(ctx context.Context, req AuditsRequest) (ListAudits, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/audits" diff --git a/pkg/cloudbroker/vins/create_in_account.go b/pkg/cloudbroker/vins/create_in_account.go index ad12d41..d4bbfac 100644 --- a/pkg/cloudbroker/vins/create_in_account.go +++ b/pkg/cloudbroker/vins/create_in_account.go @@ -2,20 +2,21 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create VINS in account type CreateInAccountRequest struct { // VINS name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // ID of account // Required: true - AccountID uint64 `url:"accountId" json:"accountId"` + AccountID uint64 `url:"accountId" json:"accountId" validate:"required"` // Grid ID // Required: false @@ -38,22 +39,13 @@ type CreateInAccountRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq CreateInAccountRequest) validate() error { - if vrq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if vrq.AccountID == 0 { - return errors.New("validation-error: field AccountID must be set") - } - - return nil -} - // CreateInAccount creates VINS in account level func (v VINS) CreateInAccount(ctx context.Context, req CreateInAccountRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/createInAccount" diff --git a/pkg/cloudbroker/vins/create_in_rg.go b/pkg/cloudbroker/vins/create_in_rg.go index 0d350fb..01232f9 100644 --- a/pkg/cloudbroker/vins/create_in_rg.go +++ b/pkg/cloudbroker/vins/create_in_rg.go @@ -2,20 +2,21 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create VINS in resource group type CreateInRGRequest struct { // VINS name // Required: true - Name string `url:"name" json:"name"` + Name string `url:"name" json:"name" validate:"required"` // Resource group ID // Required: true - RGID uint64 `url:"rgId" json:"rgId"` + RGID uint64 `url:"rgId" json:"rgId" validate:"required"` // Private network IP CIDR // Required: false @@ -42,22 +43,13 @@ type CreateInRGRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq CreateInRGRequest) validate() error { - if vrq.Name == "" { - return errors.New("validation-error: field Name must be set") - } - if vrq.RGID == 0 { - return errors.New("validation-error: field RGID must be set") - } - - return nil -} - // CreateInRG creates VINS in resource group level func (v VINS) CreateInRG(ctx context.Context, req CreateInRGRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/createInRG" diff --git a/pkg/cloudbroker/vins/default_qos_update.go b/pkg/cloudbroker/vins/default_qos_update.go index 43f1a68..be8ec64 100644 --- a/pkg/cloudbroker/vins/default_qos_update.go +++ b/pkg/cloudbroker/vins/default_qos_update.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update QOS type DefaultQOSUpdateRequest struct { // ID of VINS // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Internal traffic, kbit // Required: false @@ -26,19 +27,13 @@ type DefaultQOSUpdateRequest struct { EgressRate uint64 `url:"egress_rate,omitempty" json:"egress_rate,omitempty"` } -func (vrq DefaultQOSUpdateRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // DefaultQOSUpdate update default QOS values func (v VINS) DefaultQOSUpdate(ctx context.Context, req DefaultQOSUpdateRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/defaultQosUpdate" diff --git a/pkg/cloudbroker/vins/delete.go b/pkg/cloudbroker/vins/delete.go index 7b15b64..73e5cff 100644 --- a/pkg/cloudbroker/vins/delete.go +++ b/pkg/cloudbroker/vins/delete.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete VINS type DeleteRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Set to True if you want force delete non-empty VINS. // Primarily, VINS is considered non-empty if it has virtual machines connected to it, @@ -30,19 +31,13 @@ type DeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq DeleteRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Delete deletes VINS func (v VINS) Delete(ctx context.Context, req DeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/delete" diff --git a/pkg/cloudbroker/vins/disable.go b/pkg/cloudbroker/vins/disable.go index f46f0ae..c25deae 100644 --- a/pkg/cloudbroker/vins/disable.go +++ b/pkg/cloudbroker/vins/disable.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable VINS type DisableRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq DisableRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Disable disables VINS by ID func (v VINS) Disable(ctx context.Context, req DisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/disable" diff --git a/pkg/cloudbroker/vins/enable.go b/pkg/cloudbroker/vins/enable.go index 69d54ec..c9478eb 100644 --- a/pkg/cloudbroker/vins/enable.go +++ b/pkg/cloudbroker/vins/enable.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable VINS type EnableRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq EnableRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Enable enables VINS by ID func (v VINS) Enable(ctx context.Context, req EnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/enable" diff --git a/pkg/cloudbroker/vins/extnet_connect.go b/pkg/cloudbroker/vins/extnet_connect.go index 7c7d91f..c85de26 100644 --- a/pkg/cloudbroker/vins/extnet_connect.go +++ b/pkg/cloudbroker/vins/extnet_connect.go @@ -2,20 +2,21 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for connect external network type ExtNetConnectRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // External network ID // Required: true - NetID uint64 `url:"netId" json:"netId"` + NetID uint64 `url:"netId" json:"netId" validate:"required"` // Directly set IP address // Required: false @@ -26,22 +27,13 @@ type ExtNetConnectRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq ExtNetConnectRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - if vrq.NetID == 0 { - return errors.New("validation-error: field NetID must be set") - } - - return nil -} - // ExtNetConnect connect VINS to external network func (v VINS) ExtNetConnect(ctx context.Context, req ExtNetConnectRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/extNetConnect" diff --git a/pkg/cloudbroker/vins/extnet_disconnect.go b/pkg/cloudbroker/vins/extnet_disconnect.go index cf5d0f0..5f63bfd 100644 --- a/pkg/cloudbroker/vins/extnet_disconnect.go +++ b/pkg/cloudbroker/vins/extnet_disconnect.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disconnect VINS from external network type ExtNetDisconnectRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq ExtNetDisconnectRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // ExtNetDisconnect disconnect VINS from external network func (v VINS) ExtNetDisconnect(ctx context.Context, req ExtNetDisconnectRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/extNetDisconnect" diff --git a/pkg/cloudbroker/vins/extnet_list.go b/pkg/cloudbroker/vins/extnet_list.go index 123bd74..b2bc394 100644 --- a/pkg/cloudbroker/vins/extnet_list.go +++ b/pkg/cloudbroker/vins/extnet_list.go @@ -3,34 +3,29 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list VINS external network connections type ExtNetListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq ExtNetListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // ExtNetList show list of VINS external network connections func (v VINS) ExtNetList(ctx context.Context, req ExtNetListRequest) (ListExtNets, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/extNetList" diff --git a/pkg/cloudbroker/vins/get.go b/pkg/cloudbroker/vins/get.go index fbd9913..71078df 100644 --- a/pkg/cloudbroker/vins/get.go +++ b/pkg/cloudbroker/vins/get.go @@ -3,34 +3,29 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get information about VINS type GetRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq GetRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Get gets information about VINS by ID func (v VINS) Get(ctx context.Context, req GetRequest) (*RecordVINS, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/get" diff --git a/pkg/cloudbroker/vins/ip_list.go b/pkg/cloudbroker/vins/ip_list.go index d2651f0..16697f4 100644 --- a/pkg/cloudbroker/vins/ip_list.go +++ b/pkg/cloudbroker/vins/ip_list.go @@ -3,30 +3,25 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for DHCP IP type IPListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` -} - -func (vrq IPListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` } // IPList show DHCP IP reservations on VINS func (v VINS) IPList(ctx context.Context, req IPListRequest) (ListIPs, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/ipList" diff --git a/pkg/cloudbroker/vins/ip_release.go b/pkg/cloudbroker/vins/ip_release.go index 41d4300..b6a1db6 100644 --- a/pkg/cloudbroker/vins/ip_release.go +++ b/pkg/cloudbroker/vins/ip_release.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for IP relese type IPReleaseRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // IP address // Required: false @@ -22,20 +23,14 @@ type IPReleaseRequest struct { MAC string `url:"mac,omitempty" json:"mac,omitempty"` } -func (vrq IPReleaseRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // IPRelese delete IP reservation matched by specified IP & MAC address combination. // If both IP and MAC address are empty strings, all IP reservations will be deleted. func (v VINS) IPRelease(ctx context.Context, req IPReleaseRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/ipRelease" diff --git a/pkg/cloudbroker/vins/ip_reserve.go b/pkg/cloudbroker/vins/ip_reserve.go index cab686d..001ed41 100644 --- a/pkg/cloudbroker/vins/ip_reserve.go +++ b/pkg/cloudbroker/vins/ip_reserve.go @@ -2,7 +2,6 @@ package vins import ( "context" - "errors" "net/http" "strings" @@ -13,7 +12,7 @@ import ( type IPReserveRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Type of the reservation // Should be one of: @@ -21,7 +20,7 @@ type IPReserveRequest struct { // - VIP // - EXCLUDE // Required: true - Type string `url:"type" json:"type"` + Type string `url:"type" json:"type" validate:"vinsType"` // IP address to use. Non-empty string is required for type "EXCLUDE". // Ignored for types "DHCP" and "VIP". @@ -44,26 +43,13 @@ type IPReserveRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq IPReserveRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - if vrq.Type == "" { - return errors.New("validation-error: field Type must be set") - } - validate := validators.StringInSlice(vrq.Type, []string{"DHCP", "VIP", "EXCLUDED"}) - if !validate { - return errors.New("'type' should be 'DHCP', 'VIP' or 'EXCLUDED'") - } - - return nil -} - // IPReserve creates reservation on ViNS DHCP func (v VINS) IPReserve(ctx context.Context, req IPReserveRequest) (string, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return "", err + for _, validationError := range validators.GetErrors(err) { + return "", validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/ipReserve" diff --git a/pkg/cloudbroker/vins/mass_delete.go b/pkg/cloudbroker/vins/mass_delete.go index 4026b80..0a55be4 100644 --- a/pkg/cloudbroker/vins/mass_delete.go +++ b/pkg/cloudbroker/vins/mass_delete.go @@ -2,15 +2,16 @@ package vins import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete several VINSes type MassDeleteRequest struct { // VINS IDs // Required: true - VINSIDs []uint64 `url:"vinsIds" json:"vinsIds"` + VINSIDs []uint64 `url:"vinsIds" json:"vinsIds" validate:"min=1"` // Set to true if you want force delete non-empty VINS. Primarily, // VINS is considered non-empty if it has VMs connected to it, @@ -30,19 +31,13 @@ type MassDeleteRequest struct { Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq MassDeleteRequest) validate() error { - if len(vrq.VINSIDs) == 0 { - return errors.New("validation-error: field VINSIDs must be set") - } - - return nil -} - // MassDelete start jobs to delete several VINSes func (v VINS) MassDelete(ctx context.Context, req MassDeleteRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/massDelete" diff --git a/pkg/cloudbroker/vins/mass_disable.go b/pkg/cloudbroker/vins/mass_disable.go index 6b29ed8..bd54b12 100644 --- a/pkg/cloudbroker/vins/mass_disable.go +++ b/pkg/cloudbroker/vins/mass_disable.go @@ -2,34 +2,29 @@ package vins import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for disable several VINSes type MassDisableRequest struct { // VINS IDs // Required: true - VINSIDs []uint64 `url:"vinsIds" json:"vinsIds"` + VINSIDs []uint64 `url:"vinsIds" json:"vinsIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq MassDisableRequest) validate() error { - if len(vrq.VINSIDs) == 0 { - return errors.New("validation-error: field VINSIDs must be set") - } - - return nil -} - // MassDisable start jobs to disable several VINSes func (v VINS) MassDisable(ctx context.Context, req MassDisableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/massDisable" diff --git a/pkg/cloudbroker/vins/mass_enable.go b/pkg/cloudbroker/vins/mass_enable.go index 4b9b13b..565445c 100644 --- a/pkg/cloudbroker/vins/mass_enable.go +++ b/pkg/cloudbroker/vins/mass_enable.go @@ -2,34 +2,29 @@ package vins import ( "context" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for enable several VINSes type MassEnableRequest struct { // VINS IDs // Required: true - VINSIDs []uint64 `url:"vinsIds" json:"vinsIds"` + VINSIDs []uint64 `url:"vinsIds" json:"vinsIds" validate:"min=1"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq MassEnableRequest) validate() error { - if len(vrq.VINSIDs) == 0 { - return errors.New("validation-error: field VINSIDs must be set") - } - - return nil -} - // MassEnable start jobs to enable several VINSes func (v VINS) MassEnable(ctx context.Context, req MassEnableRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/massEnable" diff --git a/pkg/cloudbroker/vins/nat_rule_add.go b/pkg/cloudbroker/vins/nat_rule_add.go index 9a502ad..ab07f6e 100644 --- a/pkg/cloudbroker/vins/nat_rule_add.go +++ b/pkg/cloudbroker/vins/nat_rule_add.go @@ -2,67 +2,53 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for create NAT rules type NATRuleAddRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Internal IP address to apply this rule to // Required: true - IntIP string `url:"intIp " json:"intIp "` + IntIP string `url:"intIp " json:"intIp " validate:"required"` // Internal IP port number to use for this rule // Required: true - IntPort uint `url:"intPort" json:"intPort"` + IntPort uint64 `url:"intPort" json:"intPort" validate:"required"` // External IP start port to use for this rule // Required: true - ExtPortStart uint `url:"extPortStart" json:"extPortStart"` + ExtPortStart uint64 `url:"extPortStart" json:"extPortStart" validate:"required"` // External IP end port to use for this rule // Required: false - ExtPortEnd uint `url:"extPortEnd,omitempty" json:"extPortEnd,omitempty"` + ExtPortEnd uint64 `url:"extPortEnd,omitempty" json:"extPortEnd,omitempty"` // IP protocol type // Should be one of: // - "tcp" // - "udp" // Required: false - Proto string `url:"proto,omitempty" json:"proto,omitempty"` + Proto string `url:"proto,omitempty" json:"proto,omitempty" validate:"omitempty,proto"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq NATRuleAddRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - if vrq.IntIP == "" { - return errors.New("validation-error: field IntIP must be set") - } - if vrq.IntPort == 0 { - return errors.New("validation-error: field IntPort must be set") - } - if vrq.ExtPortStart == 0 { - return errors.New("validation-error: field ExtPortStart must be set") - } - - return nil -} - // NATRuleAdd create NAT (port forwarding) rule on VINS func (v VINS) NATRuleAdd(ctx context.Context, req NATRuleAddRequest) (uint64, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return 0, err + for _, validationError := range validators.GetErrors(err) { + return 0, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/natRuleAdd" diff --git a/pkg/cloudbroker/vins/nat_rule_del.go b/pkg/cloudbroker/vins/nat_rule_del.go index 75317be..ff1dca0 100644 --- a/pkg/cloudbroker/vins/nat_rule_del.go +++ b/pkg/cloudbroker/vins/nat_rule_del.go @@ -2,43 +2,35 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for delete NAT rule type NATRuleDelRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // ID of the rule to delete. // Pass -1 to clear all rules at once // Required: true - RuleID uint64 `url:"ruleId" json:"ruleId"` + RuleID uint64 `url:"ruleId" json:"ruleId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq NATRuleDelRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - if vrq.RuleID == 0 { - return errors.New("validation-error: field RuleID must be set") - } - - return nil -} - // NATRuleDel delete NAT (port forwarding) rule on VINS func (v VINS) NATRuleDel(ctx context.Context, req NATRuleDelRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/natRuleDel" diff --git a/pkg/cloudbroker/vins/nat_rule_list.go b/pkg/cloudbroker/vins/nat_rule_list.go index 3bb91a3..fc0312a 100644 --- a/pkg/cloudbroker/vins/nat_rule_list.go +++ b/pkg/cloudbroker/vins/nat_rule_list.go @@ -3,34 +3,29 @@ package vins import ( "context" "encoding/json" - "errors" "net/http" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for get list of NAT rules type NATRuleListRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq NATRuleListRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // NATRuleList gets list of NAT (port forwarding) rules func (v VINS) NATRuleList(ctx context.Context, req NATRuleListRequest) (ListNATRules, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return nil, err + for _, validationError := range validators.GetErrors(err) { + return nil, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/natRuleList" diff --git a/pkg/cloudbroker/vins/net_qos.go b/pkg/cloudbroker/vins/net_qos.go index 9cca46d..7181f2d 100644 --- a/pkg/cloudbroker/vins/net_qos.go +++ b/pkg/cloudbroker/vins/net_qos.go @@ -2,16 +2,17 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for update all VINS interfaces QOS type NetQOSRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Internal traffic, kbit // Required: false @@ -26,19 +27,13 @@ type NetQOSRequest struct { EgressRate uint64 `url:"egress_rate,omitempty" json:"egress_rate,omitempty"` } -func (vrq NetQOSRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // NetQOS update all VINS interfaces QOS func (v VINS) NetQOS(ctx context.Context, req NetQOSRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/netQos" diff --git a/pkg/cloudbroker/vins/restore.go b/pkg/cloudbroker/vins/restore.go index d74094f..2dd0c45 100644 --- a/pkg/cloudbroker/vins/restore.go +++ b/pkg/cloudbroker/vins/restore.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for restore type RestoreRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq RestoreRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: field VINSID must be set") - } - - return nil -} - // Restore restores VINS from recycle bin func (v VINS) Restore(ctx context.Context, req RestoreRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/restore" diff --git a/pkg/cloudbroker/vins/vnfdev_redeploy.go b/pkg/cloudbroker/vins/vnfdev_redeploy.go index 1f42ddf..2d9b6e0 100644 --- a/pkg/cloudbroker/vins/vnfdev_redeploy.go +++ b/pkg/cloudbroker/vins/vnfdev_redeploy.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for redeploy VNF devices type VNFDevRedeployRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq VNFDevRedeployRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: fiels VINSID must be set") - } - - return nil -} - // VNFDevRedeploy redeploy VINS VNFDevs func (v VINS) VNFDevRedeploy(ctx context.Context, req VNFDevRedeployRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/vnfdevRedeploy" diff --git a/pkg/cloudbroker/vins/vnfdev_reset.go b/pkg/cloudbroker/vins/vnfdev_reset.go index d94a923..6e82a41 100644 --- a/pkg/cloudbroker/vins/vnfdev_reset.go +++ b/pkg/cloudbroker/vins/vnfdev_reset.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reset VNF device type VNFDevResetRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq VNFDevResetRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: fiels VINSID must be set") - } - - return nil -} - // VNFDevReset reset VINSes primary VNF device func (v VINS) VNFDevReset(ctx context.Context, req VNFDevResetRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/vnfdevReset" diff --git a/pkg/cloudbroker/vins/vnfdev_restart.go b/pkg/cloudbroker/vins/vnfdev_restart.go index 4280168..9bd35e2 100644 --- a/pkg/cloudbroker/vins/vnfdev_restart.go +++ b/pkg/cloudbroker/vins/vnfdev_restart.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for reboot VINSes primary VNF device type VNFDevRestartRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action - // Required: fal + // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq VNFDevRestartRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: fiels VINSID must be set") - } - - return nil -} - // VNFDevRestart reboot VINSes primary VNF device func (v VINS) VNFDevRestart(ctx context.Context, req VNFDevRestartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/vnfdevRestart" diff --git a/pkg/cloudbroker/vins/vnfdev_start.go b/pkg/cloudbroker/vins/vnfdev_start.go index 32c51db..c43493d 100644 --- a/pkg/cloudbroker/vins/vnfdev_start.go +++ b/pkg/cloudbroker/vins/vnfdev_start.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for start VNF devices type VNFDevStartRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq VNFDevStartRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: fiels VINSID must be set") - } - - return nil -} - // VNFDevStart starts VINSes primary VNF device func (v VINS) VNFDevStart(ctx context.Context, req VNFDevStartRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/vnfdevStart" diff --git a/pkg/cloudbroker/vins/vnfdev_stop.go b/pkg/cloudbroker/vins/vnfdev_stop.go index 3281d9d..22c60e7 100644 --- a/pkg/cloudbroker/vins/vnfdev_stop.go +++ b/pkg/cloudbroker/vins/vnfdev_stop.go @@ -2,35 +2,30 @@ package vins import ( "context" - "errors" "net/http" "strconv" + + "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" ) // Request struct for stop VNF devices type VNFDevStopRequest struct { // VINS ID // Required: true - VINSID uint64 `url:"vinsId" json:"vinsId"` + VINSID uint64 `url:"vinsId" json:"vinsId" validate:"required"` // Reason for action - // Required: true + // Required: false Reason string `url:"reason,omitempty" json:"reason,omitempty"` } -func (vrq VNFDevStopRequest) validate() error { - if vrq.VINSID == 0 { - return errors.New("validation-error: fiels VINSID must be set") - } - - return nil -} - // VNFDevStop stop VINSes primary VNF device func (v VINS) VNFDevStop(ctx context.Context, req VNFDevStopRequest) (bool, error) { - err := req.validate() + err := validators.ValidateRequest(req) if err != nil { - return false, err + for _, validationError := range validators.GetErrors(err) { + return false, validators.ValidationError(validationError) + } } url := "/cloudbroker/vins/vnfdevStop" diff --git a/samples/config/config.json b/samples/config/config.json new file mode 100644 index 0000000..b35c237 --- /dev/null +++ b/samples/config/config.json @@ -0,0 +1,8 @@ +{ + "appId": "", + "appSecret": "", + "ssoUrl": "https://sso.digitalenergy.online", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "sslSkipVerify": false +} diff --git a/samples/config/config.yml b/samples/config/config.yml new file mode 100644 index 0000000..181c139 --- /dev/null +++ b/samples/config/config.yml @@ -0,0 +1,6 @@ +appId: +appSecret: +ssoUrl: https://sso.digitalenergy.online +decortUrl: https://mr4.digitalenergy.online +retries: 5 +sslSkipVerify: false diff --git a/samples/config/legacy-config.json b/samples/config/legacy-config.json new file mode 100644 index 0000000..14dfcaf --- /dev/null +++ b/samples/config/legacy-config.json @@ -0,0 +1,7 @@ +{ + "username": "", + "password": "", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "sslSkipVerify": true +} diff --git a/samples/config/legacy-config.yml b/samples/config/legacy-config.yml new file mode 100644 index 0000000..dcb7eca --- /dev/null +++ b/samples/config/legacy-config.yml @@ -0,0 +1,5 @@ +username: +password: +decortUrl: https://mr4.digitalenergy.online +retries: 5 +sslSkipVerify: true