summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Automation/ImportExport.php
blob: a5e72fae3417cfdfe9a16d1f2ae679e9c41f7cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
77
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

namespace Icinga\Module\Director\DirectorObject\Automation;

use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\DirectorDatalist;
use Icinga\Module\Director\Objects\DirectorJob;
use Icinga\Module\Director\Objects\IcingaHostGroup;
use Icinga\Module\Director\Objects\IcingaServiceGroup;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Objects\IcingaTemplateChoiceHost;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Objects\SyncRule;

class ImportExport
{
    /** @var Db */
    protected $connection;

    /** @var Exporter */
    protected $exporter;

    public function __construct(Db $connection)
    {
        $this->connection = $connection;
        $this->exporter = new Exporter($connection);
    }

    public function serializeAllServiceSets()
    {
        $res = [];
        foreach (IcingaServiceSet::loadAll($this->connection) as $object) {
            if ($object->get('host_id')) {
                continue;
            }
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllHostTemplateChoices()
    {
        $res = [];
        foreach (IcingaTemplateChoiceHost::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllHostGroups()
    {
        $res = [];
        foreach (IcingaHostGroup::loadAll($this->connection) as $object) {
            $res[] = $object->toPlainObject();
        }

        return $res;
    }

    public function serializeAllServiceGroups()
    {
        $res = [];
        foreach (IcingaServiceGroup::loadAll($this->connection) as $object) {
            $res[] = $object->toPlainObject();
        }

        return $res;
    }

    public function serializeAllDataFields()
    {
        $res = [];
        foreach (DirectorDatafield::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllDataLists()
    {
        $res = [];
        foreach (DirectorDatalist::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllJobs()
    {
        $res = [];
        foreach (DirectorJob::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllImportSources()
    {
        $res = [];
        foreach (ImportSource::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function serializeAllSyncRules()
    {
        $res = [];
        foreach (SyncRule::loadAll($this->connection) as $object) {
            $res[] = $this->exporter->export($object);
        }

        return $res;
    }

    public function unserializeImportSources($objects)
    {
        $count = 0;
        $this->connection->runFailSafeTransaction(function () use ($objects, &$count) {
            foreach ($objects as $object) {
                ImportSource::import($object, $this->connection)->store();
                $count++;
            }
        });

        return $count;
    }

    public function unserializeSyncRules($objects)
    {
        $count = 0;
        $this->connection->runFailSafeTransaction(function () use ($objects, &$count) {
            foreach ($objects as $object) {
                SyncRule::import($object, $this->connection)->store();
            }
            $count++;
        });

        return $count;
    }
}