Skip to content

Commit 2e0a674

Browse files
committed
[Feat] SWR: Add image retention policy APIs
1 parent 793ddb6 commit 2e0a674

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

openstack/swr/v2/policy/Create.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package policy
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
6+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
7+
)
8+
9+
type CreateOpts struct {
10+
// Retention policy matching algorithm. The value is "or".
11+
Algorithm string `json:"algorithm" required:"true"`
12+
// List of retention rules.
13+
Rules []Rule `json:"rules" required:"true"`
14+
}
15+
16+
// This function is used to create an image retention policy.
17+
func Create(client *golangsdk.ServiceClient, organization string, repository string, opts CreateOpts) (string, error) {
18+
b, err := build.RequestBody(opts, "")
19+
if err != nil {
20+
return "", err
21+
}
22+
23+
// POST /v2/manage/namespaces/{namespace}/repos/{repository}/retentions
24+
raw, err := client.Post(client.ServiceURL("manage", "namespaces", organization, "repos", repository, "retentions"), b, nil, &golangsdk.RequestOpts{
25+
OkCodes: []int{201},
26+
})
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
var res struct {
32+
ID string `json:"id"`
33+
}
34+
err = extract.Into(raw.Body, &res)
35+
return res.ID, err
36+
}

openstack/swr/v2/policy/Delete.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package policy
2+
3+
import golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
4+
5+
// This function is used to delete an image retention policy.
6+
func Delete(client *golangsdk.ServiceClient, organization, repository, policy string) (err error) {
7+
// DELETE /v2/manage/namespaces/{namespace}/repos/{repository}/retentions/{retention_id}
8+
_, err = client.Delete(client.ServiceURL("manage", "namespaces", organization, "repos", repository, "retentions", policy), nil)
9+
return err
10+
}

openstack/swr/v2/policy/Get.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package policy
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
6+
)
7+
8+
// This function is used to query details about an image retention policy.
9+
func Get(client *golangsdk.ServiceClient, organization, repository, policy string) (*ImageRetentionPolicy, error) {
10+
// GET /v2/manage/namespaces/{namespace}/repos/{repository}/retentions/{retention_id}
11+
raw, err := client.Get(client.ServiceURL("manage", "namespaces", organization, "repos", repository, "retentions", policy), nil, nil)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
var res ImageRetentionPolicy
17+
err = extract.Into(raw.Body, &res)
18+
return &res, err
19+
}
20+
21+
type ImageRetentionPolicy struct {
22+
// Retention policy matching rule. The value is or.
23+
Algorithm string `json:"algorithm"`
24+
// ID.
25+
ID int `json:"id"`
26+
// Image retention policy.
27+
Rules []Rule `json:"rules"`
28+
// Reserved field.
29+
Scope string `json:"scope"`
30+
}

openstack/swr/v2/policy/List.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package policy
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
6+
)
7+
8+
// This function is used to query image retention policies.
9+
func List(client *golangsdk.ServiceClient, organization, repository string) ([]ImageRetentionPolicy, error) {
10+
// GET /v2/manage/namespaces/{namespace}/repos/{repository}/retentions}
11+
raw, err := client.Get(client.ServiceURL("manage", "namespaces", organization, "repos", repository, "retentions"), nil, nil)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
var res []ImageRetentionPolicy
17+
err = extract.Into(raw.Body, &res)
18+
return res, err
19+
}

openstack/swr/v2/policy/Update.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package policy
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
6+
)
7+
8+
type UpdateOpts struct {
9+
// Retention policy matching algorithm. The value is "or".
10+
Algorithm string `json:"algorithm" required:"true"`
11+
// List of retention rules.
12+
Rules []Rule `json:"rules" required:"true"`
13+
}
14+
15+
// This function is used to modify an image retention policy.
16+
func Update(client *golangsdk.ServiceClient, organization, repository, policy string, opts UpdateOpts) (err error) {
17+
b, err := build.RequestBody(opts, "")
18+
if err != nil {
19+
return
20+
}
21+
22+
// PATCH /v2/manage/namespaces/{namespace}/repos/{repository}/retentions/{retention_id}
23+
_, err = client.Patch(client.ServiceURL("manage", "namespaces", organization, "repos", repository, "retentions", policy), b, nil, &golangsdk.RequestOpts{
24+
OkCodes: []int{201},
25+
})
26+
return
27+
}

openstack/swr/v2/policy/common.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package policy
2+
3+
type Rule struct {
4+
// Retention policy type.
5+
// The value can be "date_rule" (number of days) or "tag_rule" (number of tags).
6+
Template string `json:"template" required:"true"`
7+
// Parameters for the retention rule.
8+
// If Template is "date_rule", Params should contain {"days": "xxx"}.
9+
// If Template is "tag_rule", Params should contain {"num": "xxx"}.
10+
Params map[string]string `json:"params" required:"true"`
11+
// Exception image selectors that should not be affected by retention.
12+
TagSelectors []TagSelector `json:"tag_selectors" required:"true"`
13+
}
14+
15+
type TagSelector struct {
16+
// Matching rule type for tags.
17+
// The value can be "label" or "regexp".
18+
Kind string `json:"kind" required:"true"`
19+
// If Kind is "label", the tag to match.
20+
// If Kind is "regexp", the regular expression to match.
21+
Pattern string `json:"pattern" required:"true"`
22+
}

0 commit comments

Comments
 (0)