-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds metadata versioning for terraform modules (#2041)
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 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,11 @@ | ||
exports['metadata.yaml updateContent updates version in metadata.yaml 1'] = ` | ||
apiVersion: blueprints.cloud.google.com/v1alpha1 | ||
kind: BlueprintMetadata | ||
metadata: | ||
name: foo | ||
spec: | ||
info: | ||
title: bar | ||
version: 2.1.0 | ||
` |
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,37 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {logger as defaultLogger, Logger} from '../../util/logger'; | ||
import {DefaultUpdater} from '../default'; | ||
|
||
/** | ||
* Updates a Terraform metadata.yaml or metadata.display.yaml file(s). | ||
*/ | ||
export class MetadataVersion extends DefaultUpdater { | ||
/** | ||
* Given initial file contents, return updated contents. | ||
* @param {string} content The initial content | ||
* @returns {string} The updated content | ||
*/ | ||
updateContent(content: string, logger: Logger = defaultLogger): string { | ||
const oldVersion = content.match(/version: [0-9]+\.[0-9]+\.[0-9]+(-\w+)?/); | ||
if (oldVersion) { | ||
logger.info(`updating from ${oldVersion} to v${this.version}`); | ||
} | ||
return content.replace( | ||
/version: [0-9]+\.[0-9]+\.[0-9]+(-\w+)?/g, | ||
`version: ${this.version}` | ||
); | ||
} | ||
} |
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,8 @@ | ||
apiVersion: blueprints.cloud.google.com/v1alpha1 | ||
kind: BlueprintMetadata | ||
metadata: | ||
name: foo | ||
spec: | ||
info: | ||
title: bar | ||
version: 2.0.0 |
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,38 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {readFileSync} from 'fs'; | ||
import {resolve} from 'path'; | ||
import * as snapshot from 'snap-shot-it'; | ||
import {describe, it} from 'mocha'; | ||
import {MetadataVersion} from '../../src/updaters/terraform/metadata-version'; | ||
import {Version} from '../../src/version'; | ||
|
||
const fixturesPath = './test/updaters/fixtures'; | ||
|
||
describe('metadata.yaml', () => { | ||
describe('updateContent', () => { | ||
it('updates version in metadata.yaml', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './metadata.yaml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const version = new MetadataVersion({ | ||
version: Version.parse('2.1.0'), | ||
}); | ||
const newContent = version.updateContent(oldContent); | ||
snapshot(newContent); | ||
}); | ||
}); | ||
}); |