Skip to content

Commit a7a08e1

Browse files
committed
Adding delete all layer versions functionality for layer_version resource
1 parent 2771d6a commit a7a08e1

File tree

6 files changed

+98
-2
lines changed

6 files changed

+98
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-12-13T23:07:54Z"
2+
build_date: "2023-01-03T21:18:25Z"
33
build_hash: 12246c7da82841b351ec7a9e1f139f9338f2784b
44
go_version: go1.19
55
version: v0.20.1-14-g12246c7
66
api_directory_checksum: 90c9337a64415662f698cf6cd270706cd560bd16
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: e007d88ecf6ad1c45cb6451f5fda9401af9d7305
10+
file_checksum: e3b0b7baf90c92126b784c7147765f29847c303b
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ resources:
153153
operation: ReadOne
154154
tags:
155155
ignore: true
156+
hooks:
157+
sdk_delete_pre_build_request:
158+
template_path: hooks/layerversion/sdk_delete_pre_build_request.go.tpl
156159
renames:
157160
operations:
158161
GetLayerVersion:

generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ resources:
153153
operation: ReadOne
154154
tags:
155155
ignore: true
156+
hooks:
157+
sdk_delete_pre_build_request:
158+
template_path: hooks/layerversion/sdk_delete_pre_build_request.go.tpl
156159
renames:
157160
operations:
158161
GetLayerVersion:

pkg/resource/layer_version/hooks.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package layer_version
15+
16+
import (
17+
"context"
18+
"sort"
19+
20+
// "github.com/Jeffail/gabs/v2"
21+
svcapitypes "github.com/aws-controllers-k8s/lambda-controller/apis/v1alpha1"
22+
svcsdk "github.com/aws/aws-sdk-go/service/lambda"
23+
)
24+
25+
var (
26+
_ = &svcsdk.Lambda{}
27+
_ = &svcapitypes.LayerVersion{}
28+
)
29+
30+
// customPreDelete deletes all the previous versions of a
31+
//LayerVersion except the latest version
32+
33+
// This function is used as a sdk_delete hook, to delete
34+
// all the previous versions of a LayerVersion when delete API call is made
35+
func customPreDelete(
36+
r *resource,
37+
rm *resourceManager,
38+
ctx context.Context,
39+
) error {
40+
var response *svcsdk.ListLayerVersionsOutput
41+
var err error
42+
var version_list []*svcsdk.LayerVersionsListItem
43+
var totalVersion int
44+
var _ *svcsdk.DeleteLayerVersionOutput
45+
46+
// Getting the list of all the versions of a LayerVersion
47+
input := &svcsdk.ListLayerVersionsInput{
48+
LayerName: r.ko.Spec.LayerName,
49+
}
50+
response, err = rm.sdkapi.ListLayerVersionsWithContext(ctx, input)
51+
if err != nil {
52+
return err
53+
}
54+
55+
// The above API call returns output containing list of versions as
56+
//LayerVersions and a pagination token as NextMarker
57+
58+
// Extracting the list of versions and assigning it to a new variable
59+
version_list = response.LayerVersions
60+
totalVersion = len(version_list) // Number of total versions
61+
62+
// sorting the list in ascending order
63+
sort.Slice(version_list, func(i, j int) bool {
64+
return *version_list[i].Version < *version_list[j].Version
65+
})
66+
67+
for i := 0; i < (totalVersion - 1); i++ {
68+
input := &svcsdk.DeleteLayerVersionInput{
69+
LayerName: r.ko.Spec.LayerName,
70+
VersionNumber: version_list[i].Version,
71+
}
72+
// Delete API call to delete the versions one by one
73+
_, err = rm.sdkapi.DeleteLayerVersionWithContext(ctx, input)
74+
if err != nil {
75+
return err
76+
}
77+
}
78+
79+
return nil
80+
}

pkg/resource/layer_version/sdk.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
err = customPreDelete(r,rm,ctx)
2+
if err != nil{
3+
return nil, err
4+
}
5+

0 commit comments

Comments
 (0)