codify-plugin-lib 1.0.97 → 1.0.99

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.
@@ -115,10 +115,26 @@ export class ParsedResourceSettings {
115
115
  'determine the prompt to ask users during imports. It can\'t parse which parameters are needed when ' +
116
116
  'required is declared conditionally.');
117
117
  }
118
- const importRequiredParameterNotInSchema = this.settings.import?.requiredParameters?.filter((p) => !(schema?.properties[p]));
119
- if (schema && importRequiredParameterNotInSchema && importRequiredParameterNotInSchema.length > 0) {
120
- throw new Error(`The following properties were declared in settings.import.requiredParameters but were not found in the schema:
121
- ${JSON.stringify(importRequiredParameterNotInSchema, null, 2)}`);
118
+ if (this.settings.import) {
119
+ const { requiredParameters, refreshKeys, defaultRefreshValues } = this.settings.import;
120
+ const requiredParametersNotInSchema = requiredParameters
121
+ ?.filter((p) => schema && !(schema.properties[p]));
122
+ if (schema && requiredParametersNotInSchema && requiredParametersNotInSchema.length > 0) {
123
+ throw new Error(`The following properties were declared in settings.import.requiredParameters but were not found in the schema:
124
+ ${JSON.stringify(requiredParametersNotInSchema, null, 2)}`);
125
+ }
126
+ const refreshKeyNotInSchema = refreshKeys
127
+ ?.filter((k) => schema && !(schema.properties[k]));
128
+ if (schema && refreshKeyNotInSchema && refreshKeyNotInSchema.length > 0) {
129
+ throw new Error(`The following properties were declared in settings.import.refreshKeys but were not found in the schema:
130
+ ${JSON.stringify(requiredParametersNotInSchema, null, 2)}`);
131
+ }
132
+ const refreshValueNotInRefreshKey = Object.entries(defaultRefreshValues ?? {})
133
+ .filter(([k]) => schema && !(schema.properties[k])).map(([k]) => k);
134
+ if (schema && refreshValueNotInRefreshKey.length > 0) {
135
+ throw new Error(`Properties declared in defaultRefreshValues must already be declared in refreshKeys:
136
+ ${JSON.stringify(refreshValueNotInRefreshKey, null, 2)}`);
137
+ }
122
138
  }
123
139
  }
124
140
  validateParameterEqualsFn(parameter, key) {
@@ -133,11 +133,18 @@ export class ResourceController {
133
133
  async import(config) {
134
134
  this.addDefaultValues(config);
135
135
  await this.applyTransformParameters(config);
136
- // Try to refresh as many parameters as possible here
137
- const parametersToRefresh = {
138
- ...Object.fromEntries(this.getAllParameterKeys().map((k) => [k, null])),
139
- ...config,
140
- };
136
+ // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
137
+ const parametersToRefresh = this.settings.import?.refreshKeys
138
+ ? {
139
+ ...Object.fromEntries(this.settings.import?.refreshKeys.map((k) => [k, null])),
140
+ ...this.settings.import?.defaultRefreshValues,
141
+ ...config,
142
+ }
143
+ : {
144
+ ...Object.fromEntries(this.getAllParameterKeys().map((k) => [k, null])),
145
+ ...this.settings.import?.defaultRefreshValues,
146
+ ...config,
147
+ };
141
148
  // Parse data from the user supplied config
142
149
  const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters);
143
150
  const { allNonStatefulParameters, allStatefulParameters, coreParameters, } = parsedConfig;
@@ -53,7 +53,9 @@ export interface ResourceSettings<T extends StringIndexedObject> {
53
53
  */
54
54
  inputTransformation?: (desired: Partial<T>) => Promise<unknown> | unknown;
55
55
  import?: {
56
- requiredParameters: Array<Partial<keyof T>>;
56
+ requiredParameters?: Array<Partial<keyof T>>;
57
+ refreshKeys?: Array<Partial<keyof T>>;
58
+ defaultRefreshValues?: Partial<T>;
57
59
  };
58
60
  }
59
61
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.97",
3
+ "version": "1.0.99",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -63,19 +63,16 @@ describe('Resource options parser tests', () => {
63
63
  schema,
64
64
  }
65
65
 
66
- expect(() => new ParsedResourceSettings(option)).throws;
66
+ expect(() => new ParsedResourceSettings(option)).toThrowError();
67
67
  })
68
68
 
69
69
  it('Throws an error when an import.requiredParameters is declared improperly', () => {
70
70
  const schema = {
71
71
  '$schema': 'http://json-schema.org/draft-07/schema',
72
- '$id': 'https://www.codifycli.com/git-clone.json',
73
- 'title': 'Git-clone resource',
74
72
  'type': 'object',
75
73
  'properties': {
76
74
  'remote': {
77
75
  'type': 'string',
78
- 'description': 'Remote tracking url to clone repo from. Equivalent to repository and only one should be specified'
79
76
  },
80
77
  },
81
78
  'additionalProperties': false,
@@ -89,6 +86,77 @@ describe('Resource options parser tests', () => {
89
86
  }
90
87
  }
91
88
 
92
- expect(() => new ParsedResourceSettings(option)).throws;
89
+ expect(() => new ParsedResourceSettings(option)).toThrowError();
90
+ })
91
+
92
+ it('Throws an error when an import.refreshKeys is declared improperly', () => {
93
+ const schema = {
94
+ '$schema': 'http://json-schema.org/draft-07/schema',
95
+ 'type': 'object',
96
+ 'properties': {
97
+ 'remote': {
98
+ 'type': 'string',
99
+ },
100
+ },
101
+ 'additionalProperties': false,
102
+ }
103
+
104
+ const option: ResourceSettings<TestConfig> = {
105
+ id: 'typeId',
106
+ schema,
107
+ import: {
108
+ refreshKeys: ['import-error']
109
+ }
110
+ }
111
+
112
+ expect(() => new ParsedResourceSettings(option)).toThrowError();
113
+ })
114
+
115
+ it('Doesn\'t throw an error when an import.refreshValues is declared properly', () => {
116
+ const schema = {
117
+ '$schema': 'http://json-schema.org/draft-07/schema',
118
+ 'type': 'object',
119
+ 'properties': {
120
+ 'remote': {
121
+ 'type': 'string',
122
+ },
123
+ },
124
+ 'additionalProperties': false,
125
+ }
126
+
127
+ const option: ResourceSettings<TestConfig> = {
128
+ id: 'typeId',
129
+ schema,
130
+ import: {
131
+ refreshKeys: ['remote'],
132
+ }
133
+ }
134
+
135
+ expect(() => new ParsedResourceSettings(option)).not.toThrowError()
136
+ })
137
+
138
+ it('Throws an error if defaultRefreshValue is not found in refreshKeys', () => {
139
+ const schema = {
140
+ '$schema': 'http://json-schema.org/draft-07/schema',
141
+ 'type': 'object',
142
+ 'properties': {
143
+ 'remote': {
144
+ 'type': 'string',
145
+ },
146
+ },
147
+ 'additionalProperties': false,
148
+ }
149
+
150
+ const option: ResourceSettings<TestConfig> = {
151
+ id: 'typeId',
152
+ schema,
153
+ import: {
154
+ defaultRefreshValues: {
155
+ repository: 'abc'
156
+ }
157
+ }
158
+ }
159
+
160
+ expect(() => new ParsedResourceSettings(option)).toThrowError()
93
161
  })
94
162
  })
@@ -167,11 +167,37 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
167
167
  )
168
168
  }
169
169
 
170
- const importRequiredParameterNotInSchema =
171
- this.settings.import?.requiredParameters?.filter((p) => !(schema?.properties[p]))
172
- if(schema && importRequiredParameterNotInSchema && importRequiredParameterNotInSchema.length > 0) {
173
- throw new Error(`The following properties were declared in settings.import.requiredParameters but were not found in the schema:
174
- ${JSON.stringify(importRequiredParameterNotInSchema, null, 2)}`)
170
+ if (this.settings.import) {
171
+ const { requiredParameters, refreshKeys, defaultRefreshValues } = this.settings.import;
172
+
173
+ const requiredParametersNotInSchema = requiredParameters
174
+ ?.filter(
175
+ (p) => schema && !(schema.properties[p])
176
+ )
177
+ if (schema && requiredParametersNotInSchema && requiredParametersNotInSchema.length > 0) {
178
+ throw new Error(`The following properties were declared in settings.import.requiredParameters but were not found in the schema:
179
+ ${JSON.stringify(requiredParametersNotInSchema, null, 2)}`)
180
+ }
181
+
182
+ const refreshKeyNotInSchema = refreshKeys
183
+ ?.filter(
184
+ (k) => schema && !(schema.properties[k])
185
+ )
186
+ if (schema && refreshKeyNotInSchema && refreshKeyNotInSchema.length > 0) {
187
+ throw new Error(`The following properties were declared in settings.import.refreshKeys but were not found in the schema:
188
+ ${JSON.stringify(requiredParametersNotInSchema, null, 2)}`)
189
+ }
190
+
191
+ const refreshValueNotInRefreshKey =
192
+ Object.entries(defaultRefreshValues ?? {})
193
+ .filter(
194
+ ([k]) => schema && !(schema.properties[k])
195
+ ).map(([k]) => k)
196
+
197
+ if (schema && refreshValueNotInRefreshKey.length > 0) {
198
+ throw new Error(`Properties declared in defaultRefreshValues must already be declared in refreshKeys:
199
+ ${JSON.stringify(refreshValueNotInRefreshKey, null, 2)}`)
200
+ }
175
201
  }
176
202
  }
177
203
 
@@ -189,13 +189,22 @@ export class ResourceController<T extends StringIndexedObject> {
189
189
  this.addDefaultValues(config);
190
190
  await this.applyTransformParameters(config);
191
191
 
192
- // Try to refresh as many parameters as possible here
193
- const parametersToRefresh = {
194
- ...Object.fromEntries(
195
- this.getAllParameterKeys().map((k) => [k, null])
196
- ),
197
- ...config,
198
- };
192
+ // Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
193
+ const parametersToRefresh = this.settings.import?.refreshKeys
194
+ ? {
195
+ ...Object.fromEntries(
196
+ this.settings.import?.refreshKeys.map((k) => [k, null])
197
+ ),
198
+ ...this.settings.import?.defaultRefreshValues,
199
+ ...config,
200
+ }
201
+ : {
202
+ ...Object.fromEntries(
203
+ this.getAllParameterKeys().map((k) => [k, null])
204
+ ),
205
+ ...this.settings.import?.defaultRefreshValues,
206
+ ...config,
207
+ };
199
208
 
200
209
  // Parse data from the user supplied config
201
210
  const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters)
@@ -612,4 +612,21 @@ describe('Resource parameter tests', () => {
612
612
 
613
613
  expect(plan.changeSet.operation).to.eq(ResourceOperation.NOOP);
614
614
  })
615
+
616
+ it('Accepts an input parameters for imports', () => {
617
+ const resource = new class extends TestResource {
618
+ getSettings(): ResourceSettings<TestConfig> {
619
+ return {
620
+ id: 'resourceType',
621
+ import: {
622
+ requiredParameters: ['propA'],
623
+ refreshKeys: ['propB', 'propA'],
624
+ defaultRefreshValues: {
625
+ propB: 6,
626
+ }
627
+ }
628
+ }
629
+ }
630
+ };
631
+ })
615
632
  })
@@ -66,7 +66,11 @@ export interface ResourceSettings<T extends StringIndexedObject> {
66
66
  inputTransformation?: (desired: Partial<T>) => Promise<unknown> | unknown;
67
67
 
68
68
  import?: {
69
- requiredParameters: Array<Partial<keyof T>>;
69
+ requiredParameters?: Array<Partial<keyof T>>;
70
+
71
+ refreshKeys?: Array<Partial<keyof T>>;
72
+
73
+ defaultRefreshValues?: Partial<T>
70
74
  }
71
75
  }
72
76