/
delete_project.go
263 lines (217 loc) · 7.71 KB
/
delete_project.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package projectService
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/dotenx/dotenx/ao-api/config"
"github.com/dotenx/dotenx/ao-api/services/crudService"
"github.com/dotenx/dotenx/ao-api/services/databaseService"
"github.com/dotenx/dotenx/ao-api/services/uibuilderService"
"github.com/sirupsen/logrus"
)
/*
DeleteProject deletes a project and all its associated resources:
If the project has domain:
If the project has an external domain:
1. Deletes the CloudFront distribution if it exists
2. Deletes the S3 bucket if it exists
3. Deletes the Route53 hosted zone if it exists
4. Deletes the ACM certificate if it exists
Else:
Deletes the S3 folder
Deletes the UI pages records from the database
Deletes the pipeline records from the database
Deletes the project record from the database
TODO: Create a scheduled job to delete the actual database associated with the project if there is one (not implemented for now)
TODO: Delete the uploaded files with a scheduled job (it's currently happening here)
ISSUE: With current implementation, as the database is not deleted immediately, if the user creates another database with the same name it'll see the data from the deleted project!
*/
// DeleteProject deletes a project and all its associated resources
func (ps *projectService) DeleteProject(accountId, projectTag string, ubService uibuilderService.UIbuilderService, dbService databaseService.DatabaseService, cService crudService.CrudService) error {
/// Delete the resources first
// Get the project domain to understand if the UI pages are published and if yes, where
_, err := ps.GetProjectDomain(accountId, projectTag)
hasDomain := true
if err != nil {
if err.Error() == "project_domain not found" {
hasDomain = false
} else {
return err
}
}
if !config.Configs.App.RunLocally && hasDomain {
// Delete the project domain and its dependencies
err = ps.DeleteProjectDomain(accountId, projectTag, ubService)
if err != nil && err.Error() != "entity not found" {
logrus.Error(err.Error())
return err
}
}
// Delete the uploaded files
// TODO: After implementing the necessary changes in the objecstore implement this
// NOTE: We don't drop the database here and we will do it with a scheduled job.
// TODO: Implement a scheduled job that finds the list of databases that don't have a database and drops them (hint: utils.GetDbInstance())
// CAVEAT: If the user deletes a project and creates a new one with the same name, the database will be used if it happens before the scheduled job runs
// todo: delete the uploaded files with a scheduled job
/// Delete the records from the database
// Delete all the pipelines of the project
project, err := ps.GetProjectByTag(projectTag)
if err != nil {
logrus.Error(err.Error())
return err
}
err = cService.DeleteAllPipelines(accountId, project.Name)
if err != nil {
logrus.Error(err.Error())
return err
}
// Delete all the UI pages from the database
err = ubService.DeleteAllPages(accountId, projectTag)
if err != nil {
if err.Error() != "entity not found" {
logrus.Error(err.Error())
return err
}
}
if project.HasDatabase {
// Drop database of project
err = dbService.DeleteDatabase(accountId, project.Name)
if err != nil {
logrus.Error(err.Error())
return err
}
// Delete corresponding database user
err = dbService.DeleteDatabaseUser(accountId, project.Name)
if err != nil {
logrus.Error(err.Error())
return err
}
}
err = ps.Store.DeleteProjectByTag(context.Background(), projectTag)
if err != nil {
logrus.Error(err.Error())
}
return err
}
// Utility functions - not much business logic here
func deleteS3Bucket(bucket string) error {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
svc := s3.New(session.New(), cfg)
_, err := svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
logrus.Error(err.Error())
}
return err
}
func deleteCertificate(certificateArn string) error {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
svc := acm.New(session.New(), cfg)
_, err := svc.DeleteCertificate(&acm.DeleteCertificateInput{
CertificateArn: aws.String(certificateArn),
})
if err != nil {
logrus.Error(err.Error())
}
return err
}
func deleteHostedZone(hostedZoneId string) error {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
svc := route53.New(session.New(), cfg)
_, err := svc.DeleteHostedZone(&route53.DeleteHostedZoneInput{
Id: aws.String(hostedZoneId),
})
if err != nil {
logrus.Error(err.Error())
}
return err
}
func deleteRoute53Record(domain, value, hostedZoneId, recordType string) error {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
svc := route53.New(session.New(), cfg)
var resourceRecordSet *route53.ResourceRecordSet
if recordType == "Alias" {
resourceRecordSet = &route53.ResourceRecordSet{
AliasTarget: &route53.AliasTarget{
DNSName: aws.String(value),
EvaluateTargetHealth: aws.Bool(false),
HostedZoneId: aws.String("Z2FDTNDATAQYW2"), // CloudFront hosted zone id. DOESN'T CHANGE
},
Name: aws.String(domain + "."),
Type: aws.String("A"),
}
} else {
resourceRecordSet = &route53.ResourceRecordSet{
Name: aws.String(domain + "."),
Type: aws.String("CNAME"),
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(value),
},
},
TTL: aws.Int64(300),
}
}
upsert := []*route53.Change{{
Action: aws.String("DELETE"),
ResourceRecordSet: resourceRecordSet,
}}
params := route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: upsert,
},
HostedZoneId: aws.String(hostedZoneId),
}
_, err := svc.ChangeResourceRecordSets(¶ms)
if err != nil {
logrus.Error(err.Error())
}
return err
}
func deleteCloudFrontDistribution(distributionId string) error {
cfg := &aws.Config{
Region: aws.String(config.Configs.Upload.S3Region),
}
if config.Configs.App.RunLocally {
creds := credentials.NewStaticCredentials(config.Configs.Secrets.AwsAccessKeyId, config.Configs.Secrets.AwsSecretAccessKey, "")
cfg = aws.NewConfig().WithRegion(config.Configs.Upload.S3Region).WithCredentials(creds)
}
cfSvc := cloudfront.New(session.New(), cfg)
_, err := cfSvc.DeleteDistribution(&cloudfront.DeleteDistributionInput{
Id: aws.String(distributionId),
})
if err != nil {
logrus.Error(err.Error())
}
return err
}