diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2017-10-03 23:26:26 +0200 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2017-10-03 23:26:26 +0200 |
commit | c471430a13c79a82555ee171fecdf45b882a3c19 (patch) | |
tree | 6c0fc8b3257e1dd023d7e844a0e89b98b6fc831f /app/models | |
parent | 7e3e8ae81b7d20db718ff3b3afb27e6b92496af7 (diff) | |
download | gitlab-ce-c471430a13c79a82555ee171fecdf45b882a3c19.tar.gz |
Gcp::Cluster improvements
- introduce state machine
- use before transitions to clear data
- introduce namespace validation
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/gcp/cluster.rb | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/app/models/gcp/cluster.rb b/app/models/gcp/cluster.rb index 8b39bedad55..543505c24c2 100644 --- a/app/models/gcp/cluster.rb +++ b/app/models/gcp/cluster.rb @@ -22,49 +22,52 @@ module Gcp algorithm: 'aes-256-cbc' enum status: { - unknown: nil, scheduled: 1, creating: 2, created: 3, errored: 4 } + state_machine :status, initial: :scheduled do + event :creating do + transition any - [:creating] => :creating + end + + event :created do + transition any - [:created] => :created + end + + event :errored do + transition any - [:errored] => :errored + end + + before_transition any => [:errored, :created] do |cluster| + cluster.gcp_token = nil + cluster.gcp_operation_id = nil + end + + before_transition any => [:errored] do |cluster| + status_reason = transition.args.first + cluster.status_reason = status_reason + end + end + validates :gcp_project_id, presence: true validates :gcp_cluster_zone, presence: true validates :gcp_cluster_name, presence: true validates :gcp_cluster_size, presence: true, numericality: { only_integer: true, greater_than: 0 } - validate :restrict_modification, on: :update - - def errored!(reason) - self.status = :errored - self.status_reason = reason - self.gcp_token = nil - - save!(validate: false) - end - - def creating!(gcp_operation_id) - self.status = :creating - self.gcp_operation_id = gcp_operation_id - save!(validate: false) - end + validates :project_namespace, + allow_blank: true, + length: 1..63, + format: { + with: Gitlab::Regex.kubernetes_namespace_regex, + message: Gitlab::Regex.kubernetes_namespace_regex_message + } - def created!(endpoint, ca_cert, kubernetes_token, username, password) - self.status = :created - self.enabled = true - self.endpoint = endpoint - self.ca_cert = ca_cert - self.kubernetes_token = kubernetes_token - self.username = username - self.password = password - self.service = project.find_or_initialize_service('kubernetes') - self.gcp_token = nil - self.gcp_operation_id = nil - - save! - end + # if we do not do status transition we prevent change + validate :restrict_modification, on: :update, unless: :status_changed? def on_creation? scheduled? || creating? |