-
Notifications
You must be signed in to change notification settings - Fork 1
/
getphysical.go
86 lines (74 loc) · 2.16 KB
/
getphysical.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
package cit
import (
"context"
"encoding/json"
"errors"
log "github.com/sirupsen/logrus"
"strings"
aws "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
)
// PhysicalIDfromCID get AWS physical id from CDK contruct ID
// stack - Stackname
// constructID - awssns.NewTopic(stack, jsii.String("MyTopic") << MyTopic
func PhysicalIDfromCID(client CloudFormationInterface, stack *string, constructId *string) (*string, error) {
// Get Stack
parameterStack := &cloudformation.GetTemplateInput{
StackName: stack,
}
resGetTemplate, err := client.GetTemplate(context.TODO(), parameterStack)
if err != nil {
log.Fatal("Template "+*stack+" not found")
return nil, err
}
template := resGetTemplate.TemplateBody
// Find LogicalID
logicalID,err := LogicalIDfromCID(template, constructId)
if err != nil {
return nil, err
}
// get stackresources
parameterResource := &cloudformation.DescribeStackResourceInput{
LogicalResourceId: logicalID,
StackName: stack,
}
resourceDetail,err := client.DescribeStackResource(context.TODO(), parameterResource)
if err != nil {
return nil, err
}
// find physicalid
physicalId := resourceDetail.StackResourceDetail.PhysicalResourceId
return physicalId,nil
}
// LogicalIDfromCID - get logicalID
func LogicalIDfromCID(stackContent *string, constructID *string) (*string, error) {
stack := &Template{}
err := json.Unmarshal([]byte(*stackContent), stack)
if err != nil {
log.Fatal("There was an error processing the template: ", err)
return nil, err
}
for key, resource := range stack.Resources {
if resource.Metadata != nil {
if resource.Metadata["aws:cdk:path"] != "" {
meta := resource.Metadata["aws:cdk:path"]
log.Debug("Path: ",meta)
templateConstructID := ExtractConstructID(&meta)
if templateConstructID == *constructID {
return &key, nil
}
}
}
}
return aws.String(""), errors.New("ConstructID not found")
}
// ExtractCounstructid
// "aws:cdk:path": "vpc/baseVPC/Resource"
func ExtractConstructID(path *string) string {
var parts = strings.Split(*path, "/")
if parts[2] == "Resource"{
return parts[1]
}else{
return ""
}
}