Class: ForemanAnsibleDirector::LifecycleEnvironmentPathService

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_ansible_director/lifecycle_environment_path_service.rb

Class Method Summary collapse

Class Method Details

.create_path(name:, description:, organization_id:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/foreman_ansible_director/lifecycle_environment_path_service.rb', line 6

def create_path(name:,
                description:,
                organization_id:)
  ActiveRecord::Base.transaction do
    ::ForemanAnsibleDirector::LifecycleEnvironmentPath.create!(
      name: name,
      description: description,
      organization_id: organization_id
    )
  end
end

.destroy_path(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/foreman_ansible_director/lifecycle_environment_path_service.rb', line 29

def destroy_path(path)
  ActiveRecord::Base.transaction do # TODO: Rollback if any LCE is used by host; Setting
    # raise ActiveRecord::Rollback if path.lifecycle_environments.any? { |lce| lce.hosts.positive? }
    path.update!(root_environment_id: nil)
    # I am disabling the linter rule here, because in this case, I do not care about callbacks.
    # Associated lifecycle environments will be deleted anyway and update_all is faster than manual iteration.
    # rubocop:disable Rails/SkipsModelValidations
    path.lifecycle_environments.update_all(parent_id: nil, child_id: nil)
    # rubocop:enable Rails/SkipsModelValidations
    path.destroy!
  end
end

.edit_path(lce_path:, name:, description:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/services/foreman_ansible_director/lifecycle_environment_path_service.rb', line 18

def edit_path(lce_path:,
              name:,
              description:)
  ActiveRecord::Base.transaction do
    lce_path.update!(
      name: name,
      description: description
    )
  end
end

.insert_environment(path, environment, position) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/foreman_ansible_director/lifecycle_environment_path_service.rb', line 42

def insert_environment(path, environment, position)
  ActiveRecord::Base.transaction do
    environment.lifecycle_environment_path = path

    old_lce = environment_at_position(path, position)

    case position
    when 0
      insert_at_beginning(path, environment)
    when -1
      if old_lce
        insert_at_end(path, environment)
      else
        insert_at_beginning(path, environment) # Empty path
      end
    else
      if old_lce
        environment.parent = old_lce.parent
        environment.child = old_lce

        old_lce.parent.update(child: environment)
        old_lce.parent = environment

        environment.position = position

        ::ForemanAnsibleDirector::LifecycleEnvironmentService.increment_position old_lce
      else
        insert_at_end(path, environment)
      end

    end

    environment.save!
  end
end

.promote(lce_path:, source_environment_id:, target_environment_id:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/services/foreman_ansible_director/lifecycle_environment_path_service.rb', line 78

def promote(lce_path:,
            source_environment_id:,
            target_environment_id:)
  success = false

  ActiveRecord::Base.transaction do
    source_env = lce_path.lifecycle_environments.find_by(id: source_environment_id)
    target_env = lce_path.lifecycle_environments.find_by(id: target_environment_id)

    unless source_env
      # errors.add(:source_environment_id, 'Source environment not found')
      raise ActiveRecord::Rollback
    end

    unless target_env
      # errors.add(:target_environment_id, 'Target environment not found')
      raise ActiveRecord::Rollback
    end

    valid_promotion?(source_env, target_env)

    if source_env.using_snapshot_content?
      source_env.content_snapshot.increment_references
      target_env.content_snapshot&.decrement_references
      target_env.update!(content_snapshot: source_env.content_snapshot)
    else
      snapshot = create_snapshot source_env
      target_env.content_snapshot&.decrement_references
      target_env.update!(content_snapshot: snapshot)
    end

    if (ee_id = source_env.execution_environment&.id)
      target_env.update!(execution_environment_id: ee_id)
    end

    success = true
  end
  success
end