/
deleteawsresourcewithpost.go
120 lines (103 loc) · 4.63 KB
/
deleteawsresourcewithpost.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
/*
Copyright 2023 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package awsproxy
import (
"context"
http "net/http"
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
armrpc_controller "github.com/radius-project/radius/pkg/armrpc/frontend/controller"
armrpc_rest "github.com/radius-project/radius/pkg/armrpc/rest"
"github.com/radius-project/radius/pkg/to"
ucpaws "github.com/radius-project/radius/pkg/ucp/aws"
"github.com/radius-project/radius/pkg/ucp/aws/servicecontext"
"github.com/radius-project/radius/pkg/ucp/datamodel"
"github.com/radius-project/radius/pkg/ucp/ucplog"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/google/uuid"
)
var _ armrpc_controller.Controller = (*DeleteAWSResourceWithPost)(nil)
// DeleteAWSResourceWithPost is the controller implementation to delete an AWS resource.
type DeleteAWSResourceWithPost struct {
armrpc_controller.Operation[*datamodel.AWSResource, datamodel.AWSResource]
awsClients ucpaws.Clients
}
// NewDeleteAWSResourceWithPost creates a new DeleteAWSResourceWithPost.
//
// NewDeleteAWSResourceWithPost creates a new DeleteAWSResourceWithPost controller which is used to delete an AWS resource
// using a POST request.
func NewDeleteAWSResourceWithPost(opts armrpc_controller.Options, awsClients ucpaws.Clients) (armrpc_controller.Controller, error) {
return &DeleteAWSResourceWithPost{
Operation: armrpc_controller.NewOperation(opts, armrpc_controller.ResourceOptions[datamodel.AWSResource]{}),
awsClients: awsClients,
}, nil
}
// Run() reads the region from the request, reads properties from the body, gets the primary
// identifier from the properties, logs the resource to be deleted, deletes the resource, and returns an async operation
// response. If the resource is not found, it returns a no content response. If an error occurs, it returns an error response.
func (p *DeleteAWSResourceWithPost) Run(ctx context.Context, w http.ResponseWriter, req *http.Request) (armrpc_rest.Response, error) {
logger := ucplog.FromContextOrDiscard(ctx)
serviceCtx := servicecontext.AWSRequestContextFromContext(ctx)
region, errResponse := readRegionFromRequest(req.URL.Path, p.Options().PathBase)
if errResponse != nil {
return errResponse, nil
}
cloudControlOpts := []func(*cloudcontrol.Options){CloudControlRegionOption(region)}
properties, err := readPropertiesFromBody(req)
if err != nil {
e := v1.ErrorResponse{
Error: v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: err.Error(),
},
}
return armrpc_rest.NewBadRequestARMResponse(e), nil
}
cloudFormationOpts := []func(*cloudformation.Options){CloudFormationWithRegionOption(region)}
describeTypeOutput, err := p.awsClients.CloudFormation.DescribeType(ctx, &cloudformation.DescribeTypeInput{
Type: types.RegistryTypeResource,
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()),
}, cloudFormationOpts...)
if err != nil {
return ucpaws.HandleAWSError(err)
}
awsResourceIdentifier, err := getPrimaryIdentifierFromMultiIdentifiers(properties, *describeTypeOutput.Schema)
if err != nil {
e := v1.ErrorResponse{
Error: v1.ErrorDetails{
Code: v1.CodeInvalid,
Message: err.Error(),
},
}
return armrpc_rest.NewBadRequestARMResponse(e), nil
}
logger.Info("Deleting resource", "resourceType", serviceCtx.ResourceTypeInAWSFormat(), "resourceID", awsResourceIdentifier)
response, err := p.awsClients.CloudControl.DeleteResource(ctx, &cloudcontrol.DeleteResourceInput{
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()),
Identifier: aws.String(awsResourceIdentifier),
}, cloudControlOpts...)
if err != nil {
if ucpaws.IsAWSResourceNotFoundError(err) {
return armrpc_rest.NewNoContentResponse(), nil
}
return ucpaws.HandleAWSError(err)
}
operation, err := uuid.Parse(*response.ProgressEvent.RequestToken)
if err != nil {
return nil, err
}
resp := armrpc_rest.NewAsyncOperationResponse(map[string]any{}, v1.LocationGlobal, 202, serviceCtx.ResourceID, operation, "", serviceCtx.ResourceID.RootScope(), p.Options().PathBase)
return resp, nil
}