unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 5 forks source link

Add endpoint for Create Retention Policy #44

Closed nikolasj closed 1 year ago

nikolasj commented 1 year ago

Now I have not found an endpoint for creating a retention policy. Can you add? I would also like to see an example.

pederhan commented 1 year ago

I have not added that yet. I will add it some time in the next couple of days.

nikolasj commented 1 year ago

Thanks, I'll wait. A very necessary endpoint.

pederhan commented 1 year ago

This has been added in v0.15.0. There is some rudimentary documentation for how to interact with this endpoint on the Recipes page. Also check out the API reference for the different endpoints here.

Try it out and see how it works for you.

pederhan commented 1 year ago

I've played with it a bit myself now, and I have to say the API endpoints are a bit of an undocumented mess... You can definitely reverse-engineer it by creating retention rules in the web interface, then inspecting the resulting policy data from the API, but it's not ideal.

The API relies on magic string variables to denote the different retention rules and strategies, none of which are documented in the official API spec. So while these new methods should definitely work, it's not really feasible for me to document all the possible values that can be assigned to a RetentionPolicy object.

Take for instance this rule in the web interface:

image

Results in this policy (JSON representation):

{
  "id": 1,
  "algorithm": "or",
  "rules": [
    {
      "id": null,
      "priority": null,
      "disabled": null,
      "action": "retain",
      "template": "latestPushedK",
      "params": {
        "latestPushedK": 5
      },
      "tag_selectors": [
        {
          "kind": "doublestar",
          "decoration": "matches",
          "pattern": "**",
          "extras": "{\"untagged\":true}"
        }
      ],
      "scope_selectors": {
        "repository": [
          {
            "kind": "doublestar",
            "decoration": "repoMatches",
            "pattern": "**",
            "extras": null
          }
        ]
      }
    }
  ],
  "trigger": {
    "kind": "Schedule",
    "settings": {
      "cron": ""
    },
    "references": null
  },
  "scope": {
    "level": "project",
    "ref": 7
  }
}

You can see stuff like "doublestar", "matches" and "repoMatches". None of which are documented in the API spec.

nikolasj commented 1 year ago

Thank you! Now I will check. Could you tell me in your example the scope section refers to the project? i.e. ref: 7 is the project id?

pederhan commented 1 year ago

ref: 7 is the project id?

Yes. Call get_retention_policy() with the retention ID you get from get_project_retention_id(), and you should get a similar policy object.

Just call .json() on the returned policy object if you want a similar JSON representation.

pederhan commented 1 year ago

I'll try to add some syntactic sugar to the retention endpoint methods in the future if Harbor doesn't provide a better API schema for it, but I don't have time right now. So I'm closing this issue for now, as the general API spec compliant methods are now implemented.