-
Notifications
You must be signed in to change notification settings - Fork 669
/
resource_ibm_cr_retention_policy.go
169 lines (137 loc) · 5.72 KB
/
resource_ibm_cr_retention_policy.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package registry
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/container-registry-go-sdk/containerregistryv1"
)
func ResourceIBMCrRetentionPolicy() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMCrRetentionPolicyCreate,
ReadContext: resourceIBMCrRetentionPolicyRead,
UpdateContext: resourceIBMCrRetentionPolicyUpdate,
DeleteContext: resourceIBMCrRetentionPolicyDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "The namespace to which the retention policy is attached.",
},
"images_per_repo": {
Type: schema.TypeInt,
Required: true,
Description: "Determines how many images will be retained for each repository when the retention policy is executed. The value -1 denotes 'Unlimited' (all images are retained).",
},
"retain_untagged": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Determines if untagged images are retained when executing the retention policy. This is false by default meaning untagged images will be deleted when the policy is executed.",
},
},
}
}
func resourceIBMCrRetentionPolicyCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
containerRegistryClient, err := meta.(conns.ClientSession).ContainerRegistryV1()
if err != nil {
return diag.FromErr(err)
}
setRetentionPolicyOptions := &containerregistryv1.SetRetentionPolicyOptions{}
setRetentionPolicyOptions.SetNamespace(d.Get("namespace").(string))
setRetentionPolicyOptions.SetImagesPerRepo(int64(d.Get("images_per_repo").(int)))
if _, ok := d.GetOk("retain_untagged"); ok {
setRetentionPolicyOptions.SetRetainUntagged(d.Get("retain_untagged").(bool))
}
response, err := containerRegistryClient.SetRetentionPolicyWithContext(context, setRetentionPolicyOptions)
if err != nil {
log.Printf("[DEBUG] SetRetentionPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(d.Get("namespace").(string))
return resourceIBMCrRetentionPolicyRead(context, d, meta)
}
func resourceIBMCrRetentionPolicyRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
containerRegistryClient, err := meta.(conns.ClientSession).ContainerRegistryV1()
if err != nil {
return diag.FromErr(err)
}
getRetentionPolicyOptions := &containerregistryv1.GetRetentionPolicyOptions{}
getRetentionPolicyOptions.SetNamespace(d.Id())
retentionPolicy, response, err := containerRegistryClient.GetRetentionPolicyWithContext(context, getRetentionPolicyOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetRetentionPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
// A retention policy "does not exist" if `imagesPerRepo` is -1 `retainUntagged` is true
if *retentionPolicy.ImagesPerRepo == -1 && *retentionPolicy.RetainUntagged {
d.SetId("")
return nil
}
if err = d.Set("namespace", retentionPolicy.Namespace); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting namespace: %s", err))
}
if err = d.Set("images_per_repo", flex.IntValue(retentionPolicy.ImagesPerRepo)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting images_per_repo: %s", err))
}
if err = d.Set("retain_untagged", retentionPolicy.RetainUntagged); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting retain_untagged: %s", err))
}
return nil
}
func resourceIBMCrRetentionPolicyUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
containerRegistryClient, err := meta.(conns.ClientSession).ContainerRegistryV1()
if err != nil {
return diag.FromErr(err)
}
setRetentionPolicyOptions := &containerregistryv1.SetRetentionPolicyOptions{}
setRetentionPolicyOptions.SetNamespace(d.Id())
hasChange := false
if d.HasChange("namespace") {
setRetentionPolicyOptions.SetNamespace(d.Get("namespace").(string))
hasChange = true
}
if d.HasChange("images_per_repo") {
setRetentionPolicyOptions.SetImagesPerRepo(int64(d.Get("images_per_repo").(int)))
hasChange = true
}
if d.HasChange("retain_untagged") {
setRetentionPolicyOptions.SetRetainUntagged(d.Get("retain_untagged").(bool))
hasChange = true
}
if hasChange {
response, err := containerRegistryClient.SetRetentionPolicyWithContext(context, setRetentionPolicyOptions)
if err != nil {
log.Printf("[DEBUG] SetRetentionPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
}
return resourceIBMCrRetentionPolicyRead(context, d, meta)
}
func resourceIBMCrRetentionPolicyDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
containerRegistryClient, err := meta.(conns.ClientSession).ContainerRegistryV1()
if err != nil {
return diag.FromErr(err)
}
setRetentionPolicyOptions := &containerregistryv1.SetRetentionPolicyOptions{}
setRetentionPolicyOptions.SetNamespace(d.Id())
setRetentionPolicyOptions.SetImagesPerRepo(-1)
setRetentionPolicyOptions.SetRetainUntagged(true)
response, err := containerRegistryClient.SetRetentionPolicyWithContext(context, setRetentionPolicyOptions)
if err != nil {
log.Printf("[DEBUG] SetRetentionPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId("")
return nil
}