-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"Name": "/terraform/config/listeners/us-east-2/123456789012", | ||
"Type": "String", | ||
"Value": "https://sqs.us-east-1.amazonaws.com/123456789012/mcal-core-use2-acct1-tf-ssm-sync-orchestrator.fifo", | ||
"Version": 1, | ||
"LastModifiedDate": "2023-12-08T23:59:13.856000-05:00", | ||
"ARN": "arn:aws:ssm:us-east-2:112233445566:parameter/terraform/config/us-east-2/123456789012", | ||
"DataType": "text" | ||
}, | ||
{ | ||
"Name": "/terraform/config/listeners/us-east-2/112233445566", | ||
"Type": "String", | ||
"Value": "https://sqs.us-east-1.amazonaws.com/112233445566/mcal-core-use2-acct2-tf-ssm-sync-orchestrator.fifo", | ||
"Version": 1, | ||
"LastModifiedDate": "2023-12-08T23:59:13.856000-05:00", | ||
"ARN": "arn:aws:ssm:us-east-2:112233445566:parameter/terraform/config/us-east-2/112233445566", | ||
"DataType": "text" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ssm" | ||
"github.com/cloudposse/ssm-syncronizer/internal/model" | ||
"github.com/cloudposse/ssm-syncronizer/internal/service/aws/awssvciface" | ||
"github.com/cloudposse/ssm-syncronizer/internal/util" | ||
) | ||
|
||
type OrchestratorService struct { | ||
AccountService awssvciface.AccountInfoService | ||
ConfigPath string | ||
SSMService awssvciface.SSMService | ||
SQSService awssvciface.SQSService | ||
} | ||
|
||
type enabledAccount struct { | ||
Account string `json:"account"` | ||
QueueUrl string `json:"queue_url"` | ||
} | ||
|
||
func NewOrchestratorService(accountService awssvciface.AccountInfoService, configPath string, ssmService awssvciface.SSMService, sqsService awssvciface.SQSService) *OrchestratorService { | ||
return &OrchestratorService{ | ||
AccountService: accountService, | ||
ConfigPath: configPath, | ||
SSMService: ssmService, | ||
SQSService: sqsService, | ||
} | ||
} | ||
|
||
func (s *OrchestratorService) getEnabledListenerAccounts() ([]enabledAccount, error) { | ||
region, err := s.AccountService.GetRegion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
accountsEnabledPath := strings.Join([]string{s.ConfigPath, "listeners", region}, "/") | ||
|
||
var accounts []enabledAccount | ||
|
||
input := &ssm.GetParametersByPathInput{ | ||
Path: aws.String(accountsEnabledPath), | ||
} | ||
|
||
params, err := s.SSMService.GetParametersByPathPages(input) | ||
for _, param := range params { | ||
account := strings.Split(*param.Name, fmt.Sprintf("%s/", accountsEnabledPath))[1] | ||
accounts = append(accounts, enabledAccount{Account: account, QueueUrl: *param.Value}) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return accounts, nil | ||
} | ||
|
||
func (s *OrchestratorService) sendToRemoteListener(account string, queue string, event model.ParameterStoreChangeEvent) error { | ||
message, err := util.Marshal(event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.SQSService.SetQueueUrl(queue) | ||
s.SQSService.SendMessage(string(message), event.Detail.Name) | ||
return nil | ||
} | ||
|
||
func (s *OrchestratorService) Sync(event model.ParameterStoreChangeEvent) error { | ||
accounts, err := s.getEnabledListenerAccounts() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, account := range accounts { | ||
err := s.sendToRemoteListener(account.Account, account.QueueUrl, event) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/ssm" | ||
"github.com/cloudposse/ssm-syncronizer/internal/model" | ||
"github.com/cloudposse/ssm-syncronizer/internal/util" | ||
"github.com/cloudposse/ssm-syncronizer/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func getParametersValue(t *testing.T) []*ssm.Parameter { | ||
file := "../../fixtures/get-parameters-by-path-output.json" | ||
param, err := util.UnmarshalFile[[]*ssm.Parameter](file) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal %s", file) | ||
} | ||
return param | ||
} | ||
|
||
func TestOrchestratorService_Sync(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
eventFile string | ||
currentAccount string | ||
currentRegion string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Foo", | ||
eventFile: "../../fixtures/param-store-event-same-account-and-region.json", | ||
currentAccount: "111111222222", | ||
currentRegion: "us-east-2", | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
event, err := util.UnmarshalFile[model.ParameterStoreChangeEvent](tt.eventFile) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal event %s", tt.eventFile) | ||
} | ||
|
||
getParametersValue := getParametersValue(t) | ||
|
||
accountSvc := &mocks.AccountServiceMock{GetAccountResponse: tt.currentAccount, GetRegionResponse: tt.currentRegion} | ||
ssmSvc := &mocks.SSMServiceMock{GetParametersByPathPagesOutput: getParametersValue} | ||
sqsSvc := &mocks.SQSServiceMock{} | ||
|
||
svc := &OrchestratorService{AccountService: accountSvc, ConfigPath: "/terraform/config", SSMService: ssmSvc, SQSService: sqsSvc} | ||
|
||
err = svc.Sync(event) | ||
if (err != nil) != tt.expectError { | ||
t.Errorf("parseArn() error = %v, expectedErr %v", err, tt.expectError) | ||
return | ||
} | ||
|
||
assert.Equal(t, 2, sqsSvc.SendMessageCalls) | ||
assert.Equal(t, 2, sqsSvc.SetQueueUrlCalls) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testStruct struct { | ||
Name string `json:"name"` | ||
Count int `json:"count"` | ||
} | ||
|
||
func TestMarshal(t *testing.T) { | ||
test := testStruct{ | ||
Name: "test", | ||
Count: 1, | ||
} | ||
|
||
output, err := Marshal(test) | ||
assert.NoError(t, err) | ||
assert.Equal(t, `{"name":"test","count":1}`, string(output)) | ||
} | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
input := []byte(`{"name":"test","count":1}`) | ||
|
||
output, err := Unmarshal(input) | ||
assert.NoError(t, err) | ||
assert.Equal(t, map[string]interface{}{"name": "test", "count": 1.0}, output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters