Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit dd5de2a

Browse files
authored
feat: add opensearch domains (#910)
1 parent 56f6304 commit dd5de2a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package resources
2+
3+
import (
4+
"time"
5+
6+
"github.com/aws/aws-sdk-go/aws/session"
7+
"github.com/aws/aws-sdk-go/service/opensearchservice"
8+
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
9+
)
10+
11+
type OSDomain struct {
12+
svc *opensearchservice.OpenSearchService
13+
domainName *string
14+
lastUpdatedTime *time.Time
15+
tagList []*opensearchservice.Tag
16+
}
17+
18+
func init() {
19+
register("OSDomain", ListOSDomains)
20+
}
21+
22+
func ListOSDomains(sess *session.Session) ([]Resource, error) {
23+
svc := opensearchservice.New(sess)
24+
25+
listResp, err := svc.ListDomainNames(&opensearchservice.ListDomainNamesInput{})
26+
if err != nil {
27+
return nil, err
28+
}
29+
var domainNames []*string
30+
for _, domain := range listResp.DomainNames {
31+
domainNames = append(domainNames, domain.DomainName)
32+
}
33+
34+
resources := make([]Resource, 0)
35+
36+
// early return to prevent the `missing required field, DescribeDomainsInput.DomainNames.` error
37+
if len(domainNames) == 0 {
38+
return resources, nil
39+
}
40+
41+
descResp, err := svc.DescribeDomains(
42+
&opensearchservice.DescribeDomainsInput{
43+
DomainNames: domainNames,
44+
})
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
for _, domain := range descResp.DomainStatusList {
50+
configResp, err := svc.DescribeDomainConfig(&opensearchservice.DescribeDomainConfigInput{DomainName: domain.DomainName})
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
lto, err := svc.ListTags(&opensearchservice.ListTagsInput{ARN: domain.ARN})
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
resources = append(resources, &OSDomain{
61+
svc: svc,
62+
domainName: domain.DomainName,
63+
lastUpdatedTime: configResp.DomainConfig.ClusterConfig.Status.UpdateDate,
64+
tagList: lto.TagList,
65+
})
66+
}
67+
68+
return resources, nil
69+
}
70+
71+
func (o *OSDomain) Remove() error {
72+
_, err := o.svc.DeleteDomain(&opensearchservice.DeleteDomainInput{
73+
DomainName: o.domainName,
74+
})
75+
76+
return err
77+
}
78+
79+
func (o *OSDomain) Properties() types.Properties {
80+
properties := types.NewProperties().
81+
Set("LastUpdatedTime", o.lastUpdatedTime.Format(time.RFC3339))
82+
for _, t := range o.tagList {
83+
properties.SetTag(t.Key, t.Value)
84+
}
85+
return properties
86+
}
87+
88+
func (o *OSDomain) String() string {
89+
return *o.domainName
90+
}

0 commit comments

Comments
 (0)