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

POST /v1/objects: allow array of attrs to undo modifications of #9783

Merged
merged 1 commit into from
Jun 20, 2023

Conversation

Al2Klimov
Copy link
Member

@Al2Klimov Al2Klimov commented Jun 6, 2023

in addition to the mapping of ones to modify.

Restoration is also a kind of modification I guess.

See changed docs.

fixes #8709
closes #7986

TODO

@Al2Klimov Al2Klimov added enhancement New feature or request area/configuration DSL, parser, compiler, error handling area/api REST API labels Jun 6, 2023
@Al2Klimov Al2Klimov added this to the 2.14.0 milestone Jun 6, 2023
@Al2Klimov Al2Klimov requested a review from julianbrost June 6, 2023 15:24
@cla-bot cla-bot bot added the cla/signed label Jun 6, 2023
lib/remote/modifyobjecthandler.cpp Outdated Show resolved Hide resolved
lib/remote/modifyobjecthandler.cpp Show resolved Hide resolved
doc/12-icinga2-api.md Show resolved Hide resolved
doc/12-icinga2-api.md Show resolved Hide resolved
@Al2Klimov Al2Klimov force-pushed the restore_attrs branch 2 times, most recently from b7606df to 57f7f76 Compare June 7, 2023 11:21
@Al2Klimov Al2Klimov requested a review from julianbrost June 7, 2023 11:21
@julianbrost
Copy link
Contributor

Looks fine, so you can go ahead with that:

@Al2Klimov
Copy link
Member Author

Invalid usages

➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1}'
HTTP/1.1 400 Bad Request
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 166

{
    "error": 400,
    "status": "Missing both 'attrs' and 'restore_attrs'. Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?"
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":[]}'
HTTP/1.1 400 Bad Request
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 198

{
    "error": 400,
    "status": "Invalid type for 'attrs' attribute specified. Dictionary type is required.Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?"
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"restore_attrs":{}}'
HTTP/1.1 400 Bad Request
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 116

{
    "error": 400,
    "status": "Invalid type for 'restore_attrs' attribute specified. Array type is required."
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"vars.lolcat":42}'
HTTP/1.1 400 Bad Request
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 166

{
    "error": 400,
    "status": "Missing both 'attrs' and 'restore_attrs'. Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?"
}
➜  icinga2 git:(restore_attrs)

Valid usages

➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":{}}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 197

{
    "results": [
        {
            "code": 200,
            "name": "alexandersmbp2.int.netways.de",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":{"vars.lolcat":42}}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 197

{
    "results": [
        {
            "code": 200,
            "name": "alexandersmbp2.int.netways.de",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":{"vars.lolcat":777},"restore_attrs":["vars.lolcat"]}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 197

{
    "results": [
        {
            "code": 200,
            "name": "alexandersmbp2.int.netways.de",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X GET -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":["vars"]}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 932

{
    "results": [
        {
            "attrs": {
                "vars": {
                    "disks": {
                        "disk": {},
                        "disk /": {
                            "disk_partitions": "/"
                        }
                    },
                    "http_vhosts": {
                        "http": {
                            "http_uri": "/"
                        }
                    },
                    "lolcat": 777,
                    "notification": {
                        "mail": {
                            "groups": [
                                "icingaadmins"
                            ]
                        }
                    },
                    "os": "Linux"
                }
            },
            "joins": {},
            "meta": {},
            "name": "alexandersmbp2.int.netways.de",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X POST -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"restore_attrs":["vars.lolcat"]}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 197

{
    "results": [
        {
            "code": 200,
            "name": "alexandersmbp2.int.netways.de",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs) curl -sSiku root:123456 -X GET -H 'Accept: application/json' 'https://127.0.0.1:5665/v1/objects/hosts' -d '{"pretty":1,"attrs":["vars","original_attributes"]}'
HTTP/1.1 200 OK
Server: Icinga/v2.13.0-720-g57f7f76dd
Content-Type: application/json
Content-Length: 976

{
    "results": [
        {
            "attrs": {
                "original_attributes": {},
                "vars": {
                    "disks": {
                        "disk": {},
                        "disk /": {
                            "disk_partitions": "/"
                        }
                    },
                    "http_vhosts": {
                        "http": {
                            "http_uri": "/"
                        }
                    },
                    "lolcat": null,
                    "notification": {
                        "mail": {
                            "groups": [
                                "icingaadmins"
                            ]
                        }
                    },
                    "os": "Linux"
                }
            },
            "joins": {},
            "meta": {},
            "name": "alexandersmbp2.int.netways.de",
            "type": "Host"
        }
    ]
}
➜  icinga2 git:(restore_attrs)

Admittedly the latter doesn’t work exactly as intended, leaving vars.lolcat=null, but only till the next reload.

@Al2Klimov Al2Klimov marked this pull request as ready for review June 7, 2023 13:14
doc/12-icinga2-api.md Outdated Show resolved Hide resolved
doc/12-icinga2-api.md Show resolved Hide resolved
@julianbrost
Copy link
Contributor

Temporarily, I was worried whether this would actually work properly in clusters, but seems like this was actually also implemented for v2.4 (that's when ConfigObject::RestoreAttribute() was added):

/* check whether original attributes changed and restore them locally */
Array::Ptr newOriginalAttributes = params->Get("original_attributes");
Dictionary::Ptr objOriginalAttributes = object->GetOriginalAttributes();
if (newOriginalAttributes && objOriginalAttributes) {
std::vector<String> restoreAttrs;
{
ObjectLock xlock(objOriginalAttributes);
for (const Dictionary::Pair& kv : objOriginalAttributes) {
/* original attribute was removed, restore it */
if (!newOriginalAttributes->Contains(kv.first))
restoreAttrs.push_back(kv.first);
}
}
for (const String& key : restoreAttrs) {
/* do not update the object version yet. */
object->RestoreAttribute(key, false);
}
}

And it looks like this is actually working fine after being mostly unused for years. I guess you can also get lucky sometimes.

@julianbrost
Copy link
Contributor

@Al2Klimov This should probably fix (or at least ref) some issues. Can you please add that before merging?

@Al2Klimov Al2Klimov requested a review from julianbrost June 19, 2023 16:00
@julianbrost julianbrost merged commit eddd4c7 into master Jun 20, 2023
@julianbrost julianbrost deleted the restore_attrs branch June 20, 2023 08:16
Al2Klimov added a commit that referenced this pull request Jul 3, 2023
…ibutes, i.e. to restore the config files' values. #9783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api REST API area/configuration DSL, parser, compiler, error handling cla/signed enhancement New feature or request
Projects
None yet
2 participants