Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icinga Director REST API : Import Source and Sync Rules #2944

Open
DannyMelguizo opened this issue Dec 11, 2024 · 0 comments
Open

Icinga Director REST API : Import Source and Sync Rules #2944

DannyMelguizo opened this issue Dec 11, 2024 · 0 comments

Comments

@DannyMelguizo
Copy link

Hi I'm trying to add a new Import Source and a new Sync Rule using the Icinga Director's REST API.

Following the documentation provided at https://icinga.com/docs/icinga-director/latest/doc/70-REST-API/ and using the script given there.

METHOD=$1
URL=$2
BODY="$3"
USERNAME="demo"
PASSWORD="***"
test -z "$PASSWORD" || USERNAME="$USERNAME:$PASSWORD"

test -z "$BODY" && curl -u "$USERNAME" \
  -i https://icingaweb/icingaweb/$URL \
  -H 'Accept: application/json' \
  -X $METHOD

test -z "$BODY" || curl -u "$USERNAME" \
  -i https://icingaweb/icingaweb/$URL \
  -H 'Accept: application/json' \
  -X $METHOD \
  -d "$BODY"

echo

Current Behavior

I'm trying to create the source by doing POST director/importsources '{"source_name": "test"}' (I don't even know if this is the right endpoint) and I get the following message.

HTTP/1.1 500 Internal Server Error
Date: Wed, 11 Dec 2024 11:57:56 GMT
Server: Apache
X-Powered-By: PHP/7.3.29
X-Frame-Options: SAMEORIGIN
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

{"status":"error","message":"Uncaught TypeError: Argument 2 passed to Icinga\\Module\\Director\\Data\\ObjectImporter::import() must be an instance of stdClass, string given, called in \/usr\/share\/icingaweb2\/modules\/director\/library\/Director\/DirectorObject\/Automation\/ImportExport.php on line 131 and defined in \/usr\/share\/icingaweb2\/modules\/director\/library\/Director\/Data\/ObjectImporter.php:42\nStack trace:\n#0 \/usr\/share\/icingaweb2\/modules\/director\/library\/Director\/DirectorObject\/Automation\/ImportExport.php(131): Icinga\\Module\\Director\\Data\\ObjectImporter->import('Icinga\\\\Module\\\\D...', 'test')\n#1 \/usr\/share\/icingaweb2\/modules\/director\/library\/Director\/Db.php(43): Icinga\\Module\\Director\\DirectorObject\\Automation\\ImportExport->Icinga\\Module\\Director\\DirectorObject\\Automation\\{closure}()\n#2 \/usr\/share\/icingaweb2\/modules\/director\/library\/Director\/DirectorObject\/Automation\/ImportExport.php(134): Icinga\\Module\\Director\\Db->runFailSafeTransaction(Object(Closure))\n#3 \/usr\/share\/icingaweb2\/modules\/director\/application\/controllers\/ImportsourcesController."}

Checking in the files where the error occurs, I think it happens because the unserializeImportSources function in the /library/Director/DirectorObject/Automation/ImportExport.php file is passing the arguments in the wrong order to the import function in the /library/Director/Data/ObjectImporter.php file.

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

    return $count;
}
public function import(string $implementation, stdClass $plain): DbObject
{
    $this->assertTemplate($implementation, $plain);
    $this->fixRelations($implementation, $plain);
    $this->applyOtherWorkarounds($implementation, $plain);
    $this->fixLegacyBaskets($implementation, $plain);
    $this->fixSubObjects($implementation, $plain);

    $object = $this->loadExistingObject($implementation, $plain);
    if ($object === null) {
        $object = $implementation::create([], $this->db);
    }

    $properties = (array) $plain;
    unset($properties['fields']);
    unset($properties['originalId']);
    if ($implementation === Basket::class) {
        if (isset($properties['objects']) && is_string($properties['objects'])) {
            $properties['objects'] = JsonString::decode($properties['objects']);
        }
    }
    $object->setProperties($properties);

    return $object;
}

Possible Solution

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

    return $count;
}

I would also like to know if there is a way to know the current endpoints or how can I add a new Sync Rule, because the following does not work.

POST director/syncrules '{
    "object_type": "host",
    "purge_action": "delete",
    "purge_existing": true,
    "rule_name": "test",
    "update_policy": "override"
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant