diff options
author | Dylan Griffith <dyl.griffith@gmail.com> | 2019-07-10 08:33:09 +0300 |
---|---|---|
committer | Dylan Griffith <dyl.griffith@gmail.com> | 2019-07-11 10:22:58 +0300 |
commit | dacd0ee18b617f0c81c4a478a4d801b3c37e0c56 (patch) | |
tree | b129e20413cefe21c2ad33c890b223d0a37a4928 /app/services | |
parent | 4efc8574cb45369797cf902d4e9676506a6e4cf7 (diff) |
Refactor: model errors for multi cluster validation
The current approach requires catching exceptions to handle these errors
and callers are already handling model validations so it seems more
appropriate. Also it seemed to convoluted to add this logic directly to
the model since the model needs to check too many possible associations
to determine whether or not there are more than one cluster since the
model doesn't know what it's being created on. Additionally we only
wanted to validate during create to avoid the risk of existing models
becoming invalid by many different edge cases.
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/clusters/create_service.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/app/services/clusters/create_service.rb b/app/services/clusters/create_service.rb index 886e484caaf..5fb5e15c32d 100644 --- a/app/services/clusters/create_service.rb +++ b/app/services/clusters/create_service.rb @@ -10,24 +10,27 @@ module Clusters def execute(access_token: nil) raise ArgumentError, 'Unknown clusterable provided' unless clusterable - raise ArgumentError, _('Instance does not support multiple Kubernetes clusters') unless can_create_cluster? cluster_params = params.merge(user: current_user).merge(clusterable_params) cluster_params[:provider_gcp_attributes].try do |provider| provider[:access_token] = access_token end - create_cluster(cluster_params).tap do |cluster| - ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted? + cluster = Clusters::Cluster.new(cluster_params) + + unless can_create_cluster? + cluster.errors.add(:base, _('Instance does not support multiple Kubernetes clusters')) end - end - private + return cluster if cluster.errors.present? - def create_cluster(cluster_params) - Clusters::Cluster.create(cluster_params) + cluster.tap do |cluster| + cluster.save && ClusterProvisionWorker.perform_async(cluster.id) + end end + private + def clusterable @clusterable ||= params.delete(:clusterable) end |