@defra/grants-config-utils 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @defra/grants-config-utils
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 512774f: Status defaults to active and is now configurable
8
+
9
+ ## 0.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - dabf7f1: feat: Add SNS client
14
+
3
15
  ## 0.1.2
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-config-utils",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -31,6 +31,7 @@
31
31
  "exports": {
32
32
  ".": "./src/service/config-store-and-inform.js",
33
33
  "./s3-interactions": "./src/s3/s3-interactions.js",
34
+ "./sns-client": "./src/sns/sns-client.js",
34
35
  "./config": "./src/config/config.js",
35
36
  "./broker": "./src/broker/broker-auth-helper.js"
36
37
  },
@@ -41,6 +42,7 @@
41
42
  "homepage": "https://github.com/DEFRA/grants-config-utils#readme",
42
43
  "dependencies": {
43
44
  "@aws-sdk/client-s3": "3.1047.0",
45
+ "@aws-sdk/client-sns": "3.1047.0",
44
46
  "convict": "6.2.5",
45
47
  "convict-format-with-validator": "6.2.0"
46
48
  },
@@ -39,6 +39,14 @@ export const config = convict({
39
39
  env: "FORCE_PATH_STYLE",
40
40
  },
41
41
  },
42
+ sns: {
43
+ configVersionTopicArn: {
44
+ doc: "ARN of the SNS topic to publish new config version events",
45
+ format: String,
46
+ default: "arn:aws:sns:eu-west-2:000000000000:gfr__sns___config_version",
47
+ env: "GFR__SNS__CONFIG_VERSION_ARN",
48
+ },
49
+ },
42
50
  },
43
51
  configBroker: {
44
52
  auth: {
@@ -64,6 +72,14 @@ export const config = convict({
64
72
  env: "GRANTS_CONFIG_BROKER_API_ENDPOINT",
65
73
  },
66
74
  },
75
+ configPublish: {
76
+ status: {
77
+ doc: "Config publish status",
78
+ format: String,
79
+ default: "active",
80
+ env: "GRANTS_CONFIG_PUBLISH_STATUS",
81
+ },
82
+ },
67
83
  });
68
84
 
69
85
  config.validate({ allowed: "strict" });
@@ -101,22 +101,26 @@ const notifyConfigBrokerServiceVersionAvailable = async (
101
101
  logger,
102
102
  ) => {
103
103
  const configBrokerEndpoint = config.get("configBroker.apiEndpoint");
104
+ const configPublishStatus = config.get("configPublish.status");
104
105
 
105
106
  // iterate each grant configuration, notify config available at current service version
106
107
  for (const configAtServiceVersion of configsAtServiceVersion) {
107
- await callReleaseConfigEndpoint(
108
+ await sendConfigMessageToBroker(
108
109
  configBrokerEndpoint,
109
110
  configAtServiceVersion,
111
+ configPublishStatus,
110
112
  logger,
111
113
  );
112
114
  }
113
115
  };
114
116
 
115
- const callReleaseConfigEndpoint = async (
117
+ const sendConfigMessageToBroker = async (
116
118
  configBrokerEndpoint,
117
119
  configAtServiceVersion,
120
+ configPublishStatus,
118
121
  logger,
119
122
  ) => {
123
+ // TODO BH check service topic set instead
120
124
  if (!configBrokerEndpoint?.length) {
121
125
  logger.warn(
122
126
  `config broker endpoint not set, so skipping release config call`,
@@ -132,7 +136,7 @@ const callReleaseConfigEndpoint = async (
132
136
  grant,
133
137
  version,
134
138
  files: s3Paths,
135
- status: "draft", // hardcode status for now
139
+ status: configPublishStatus,
136
140
  };
137
141
 
138
142
  const url = new URL(`/api/release-config`, configBrokerEndpoint);
@@ -16,6 +16,7 @@ vi.mock("../config/config.js", () => ({
16
16
  serviceVersion: "1.2.3",
17
17
  serviceName: "grants-config-playground",
18
18
  "configBroker.apiEndpoint": "https://broker.unit.test",
19
+ "configPublish.status": "active",
19
20
  };
20
21
  return values[key];
21
22
  },
@@ -105,7 +106,22 @@ describe("storeConfigVersionAndInformBroker", () => {
105
106
  expect(listFiles).toHaveBeenCalledTimes(1);
106
107
  expect(readFileSync).toHaveBeenCalledTimes(1);
107
108
  expect(uploadBlob).toHaveBeenCalledTimes(1);
108
- expect(fetch).toHaveBeenCalledTimes(1);
109
+ expect(fetch).toHaveBeenCalledWith(
110
+ "https://broker.unit.test/api/release-config",
111
+ {
112
+ method: "POST",
113
+ headers: {
114
+ "Content-Type": "application/json",
115
+ authorization: "Bearer token",
116
+ },
117
+ body: JSON.stringify({
118
+ grant: "grant-1",
119
+ version: "1.2.3",
120
+ files: ["grant-1/1.2.3/main.json"],
121
+ status: "active",
122
+ }),
123
+ },
124
+ );
109
125
  });
110
126
 
111
127
  it("should not store or inform when already published by config broker", async () => {
@@ -0,0 +1,66 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { config } from "../config/config.js";
3
+
4
+ let defaultTopic;
5
+ let loggerInstance;
6
+ let clientSetup = false;
7
+
8
+ let snsClient;
9
+
10
+ export const isClientSetup = () => clientSetup;
11
+
12
+ export const setupClient = (
13
+ logger,
14
+ {
15
+ region = config.get("aws.region"),
16
+ awsEndpointUrl = config.get("aws.endpointUrl"),
17
+ publishToTopic = config.get("aws.sns.configVersionTopicArn"),
18
+ } = {},
19
+ ) => {
20
+ snsClient = new SNSClient({
21
+ region,
22
+ endpoint: awsEndpointUrl,
23
+ });
24
+ loggerInstance = logger;
25
+ defaultTopic = publishToTopic;
26
+ clientSetup = true;
27
+ };
28
+
29
+ export const publishMessage = async (
30
+ data,
31
+ messageAttributes = {},
32
+ topic = defaultTopic,
33
+ ) => {
34
+ if (!clientSetup) {
35
+ throw new Error(
36
+ "SNS client not setup. Call setupClient() before publishing messages.",
37
+ );
38
+ }
39
+ loggerInstance.info(`Publish command ${topic}`);
40
+ await snsClient.send(
41
+ new PublishCommand({
42
+ TopicArn: topic,
43
+ Message: JSON.stringify(data),
44
+ MessageAttributes: convertMessageAttributes(messageAttributes),
45
+ }),
46
+ );
47
+ };
48
+
49
+ /* Converts a simple key-value object into the format required by SNS for message attributes.
50
+ Note only supports string values currently
51
+ Example input: { key1: "value1", key2: "value2" }
52
+ Example output: {
53
+ key1: { DataType: "String", StringValue: "value1" },
54
+ key2: { DataType: "String", StringValue: "value2" }
55
+ }
56
+ */
57
+ const convertMessageAttributes = (attributes) => {
58
+ const convertedAttributes = {};
59
+ for (const key in attributes) {
60
+ convertedAttributes[key] = {
61
+ DataType: "String",
62
+ StringValue: attributes[key],
63
+ };
64
+ }
65
+ return convertedAttributes;
66
+ };
@@ -0,0 +1,154 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { isClientSetup, publishMessage, setupClient } from "./sns-client.js";
3
+ import { config } from "../config/config.js";
4
+
5
+ vi.mock("./metrics.js");
6
+ vi.mock("@aws-sdk/client-sns", () => ({
7
+ SNSClient: vi.fn(),
8
+ PublishCommand: vi.fn(),
9
+ }));
10
+
11
+ const mockLogger = {
12
+ info: vi.fn(),
13
+ error: vi.fn(),
14
+ };
15
+
16
+ const topicArn = "arn:aws:sns:us-east-1:123456789012:MyTopic";
17
+
18
+ describe("publish", () => {
19
+ it("error thrown if client not set up", async () => {
20
+ const message = {
21
+ key: "value",
22
+ };
23
+
24
+ const send = vi.fn();
25
+
26
+ SNSClient.mockReturnValue({
27
+ send,
28
+ });
29
+
30
+ expect(isClientSetup()).toBe(false);
31
+
32
+ await expect(publishMessage(message)).rejects.toThrow(
33
+ "SNS client not setup. Call setupClient() before publishing messages.",
34
+ );
35
+
36
+ expect(PublishCommand).toHaveBeenCalledTimes(0);
37
+
38
+ expect(send).toHaveBeenCalledTimes(0);
39
+ });
40
+
41
+ it("publishes a message to a topic", async () => {
42
+ const message = {
43
+ key: "value",
44
+ };
45
+
46
+ const send = vi.fn();
47
+
48
+ SNSClient.mockImplementation(function () {
49
+ return { send };
50
+ });
51
+
52
+ PublishCommand.mockImplementation(function (params) {
53
+ return params;
54
+ });
55
+ setupClient(mockLogger, {
56
+ region: "us-east-1",
57
+ awsEndpointUrl: "http://localhost:4566",
58
+ publishToTopic: topicArn,
59
+ });
60
+
61
+ expect(isClientSetup()).toBe(true);
62
+
63
+ await publishMessage(message);
64
+
65
+ expect(PublishCommand).toHaveBeenCalledWith({
66
+ TopicArn: topicArn,
67
+ Message: '{"key":"value"}',
68
+ MessageAttributes: {},
69
+ });
70
+
71
+ expect(send).toHaveBeenCalledWith({
72
+ TopicArn: topicArn,
73
+ Message: '{"key":"value"}',
74
+ MessageAttributes: {},
75
+ });
76
+ });
77
+
78
+ it("publishes a message including custom message attribute", async () => {
79
+ const message = {
80
+ key: "value",
81
+ };
82
+
83
+ const send = vi.fn();
84
+
85
+ SNSClient.mockImplementation(function () {
86
+ return { send };
87
+ });
88
+
89
+ PublishCommand.mockImplementation(function (params) {
90
+ return params;
91
+ });
92
+ setupClient(mockLogger, {
93
+ region: "us-east-1",
94
+ awsEndpointUrl: "http://localhost:4566",
95
+ });
96
+
97
+ await publishMessage(
98
+ message,
99
+ { customAttribute: "customValue" },
100
+ "specialTopicArn",
101
+ );
102
+
103
+ expect(PublishCommand).toHaveBeenCalledWith({
104
+ TopicArn: "specialTopicArn",
105
+ Message: '{"key":"value"}',
106
+ MessageAttributes: {
107
+ customAttribute: {
108
+ DataType: "String",
109
+ StringValue: "customValue",
110
+ },
111
+ },
112
+ });
113
+
114
+ expect(send).toHaveBeenCalledWith({
115
+ TopicArn: "specialTopicArn",
116
+ Message: '{"key":"value"}',
117
+ MessageAttributes: {
118
+ customAttribute: {
119
+ DataType: "String",
120
+ StringValue: "customValue",
121
+ },
122
+ },
123
+ });
124
+ });
125
+
126
+ it("sets up client with defaults if config object is not supplied", async () => {
127
+ const send = vi.fn();
128
+ SNSClient.mockImplementation(function () {
129
+ return { send };
130
+ });
131
+
132
+ config.set("aws.region", "eu-west-1");
133
+ config.set("aws.endpointUrl", "http://localhost:4566");
134
+ config.set("aws.sns.configVersionTopicArn", "default-topic-arn");
135
+
136
+ setupClient(mockLogger);
137
+
138
+ expect(SNSClient).toHaveBeenCalledWith(
139
+ expect.objectContaining({
140
+ region: "eu-west-1",
141
+ endpoint: "http://localhost:4566",
142
+ }),
143
+ );
144
+
145
+ const message = { key: "value" };
146
+ await publishMessage(message);
147
+
148
+ expect(PublishCommand).toHaveBeenCalledWith(
149
+ expect.objectContaining({
150
+ TopicArn: "default-topic-arn",
151
+ }),
152
+ );
153
+ });
154
+ });