@@ -45,6 +45,87 @@ func addIDToDeleteRequest(r *resource,
45
45
return nil
46
46
}
47
47
48
+ // syncAllowedPrincipals adds & removes allowed principals with the 'ModifyVpcEndpointServicePermissions' API call
49
+ func (rm * resourceManager ) syncAllowedPrincipals (
50
+ ctx context.Context ,
51
+ desired * resource ,
52
+ latest * resource ,
53
+ ) (updated * resource , err error ) {
54
+ rlog := ackrtlog .FromContext (ctx )
55
+ exit := rlog .Trace ("updateAllowedPrincipals" )
56
+ defer func (err error ) {
57
+ exit (err )
58
+ }(err )
59
+
60
+ var listOfPrincipalsToAdd []* string
61
+ var listOfPrincipalsToRemove []* string
62
+
63
+ // If the latest list of principals is empty, we want to add all principals
64
+ if len (latest .ko .Spec .AllowedPrincipals ) == 0 && len (desired .ko .Spec .AllowedPrincipals ) > 0 {
65
+ listOfPrincipalsToAdd = desired .ko .Spec .AllowedPrincipals
66
+
67
+ // If the desired list of principals is empty, we want to remove all principals
68
+ } else if len (desired .ko .Spec .AllowedPrincipals ) == 0 && len (latest .ko .Spec .AllowedPrincipals ) > 0 {
69
+ listOfPrincipalsToRemove = latest .ko .Spec .AllowedPrincipals
70
+ // Otherwise, we'll compare the two lists and add/remove principals as needed
71
+ } else {
72
+ // Add any 'desired' principal that is not on the allowed list
73
+ for _ , desiredPrincipal := range desired .ko .Spec .AllowedPrincipals {
74
+ principalToAddAlreadyFound := false
75
+ for _ , latestPrincipal := range latest .ko .Spec .AllowedPrincipals {
76
+ if * desiredPrincipal == * latestPrincipal {
77
+ // Principal already in Allow List, skip
78
+ principalToAddAlreadyFound = true
79
+ break
80
+ }
81
+ }
82
+ if ! principalToAddAlreadyFound {
83
+ // Desired Principal is not in the Allowed List, add it to the list of those to add
84
+ listOfPrincipalsToAdd = append (listOfPrincipalsToAdd , desiredPrincipal )
85
+ }
86
+ }
87
+
88
+ // Remove any 'latest' principal that is not on the allowed list anymore
89
+ for _ , latestPrincipal := range latest .ko .Spec .AllowedPrincipals {
90
+ principalToRemoveAlreadyFound := false
91
+ for _ , desiredPrincipal := range desired .ko .Spec .AllowedPrincipals {
92
+ if * desiredPrincipal == * latestPrincipal {
93
+ // Principal still in Allow List, skip
94
+ principalToRemoveAlreadyFound = true
95
+ break
96
+ }
97
+ }
98
+ if ! principalToRemoveAlreadyFound {
99
+ // Latest Principal is not in the Allowed List, add it to the list of those to remove
100
+ listOfPrincipalsToRemove = append (listOfPrincipalsToRemove , latestPrincipal )
101
+ }
102
+ }
103
+
104
+ }
105
+
106
+ // Make the AWS API call to update the allowed principals
107
+ if len (listOfPrincipalsToAdd ) > 0 || len (listOfPrincipalsToRemove ) > 0 {
108
+ modifyPermissionsInput := & svcsdk.ModifyVpcEndpointServicePermissionsInput {
109
+ ServiceId : latest .ko .Status .ServiceID ,
110
+ }
111
+
112
+ if len (listOfPrincipalsToAdd ) > 0 {
113
+ modifyPermissionsInput .AddAllowedPrincipals = listOfPrincipalsToAdd
114
+ }
115
+
116
+ if len (listOfPrincipalsToRemove ) > 0 {
117
+ modifyPermissionsInput .RemoveAllowedPrincipals = listOfPrincipalsToRemove
118
+ }
119
+
120
+ _ , err := rm .sdkapi .ModifyVpcEndpointServicePermissions (modifyPermissionsInput )
121
+ rm .metrics .RecordAPICall ("UPDATE" , "ModifyVpcEndpointServicePermissions" , err )
122
+ if err != nil {
123
+ return desired , err
124
+ }
125
+ }
126
+ return desired , nil
127
+ }
128
+
48
129
// syncTags used to keep tags in sync by calling Create and Delete API's
49
130
func (rm * resourceManager ) syncTags (
50
131
ctx context.Context ,
0 commit comments