Skip to content

Commit

Permalink
fix: Moved Tag generator into core so it can be used everywhere in th…
Browse files Browse the repository at this point in the history
…e ecosystem (#31)

fix: Added more built-in tags for ingredient version/tool version
  • Loading branch information
whilke authored Mar 19, 2019
1 parent f81e8ce commit 1dcef96
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 23 deletions.
19 changes: 3 additions & 16 deletions arm-helper/src/arm-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IIngredient, Logger, DeploymentContext, BakeVariable } from '@azbake/core';
import { IIngredient, Logger, DeploymentContext, BakeVariable, TagGenerator } from '@azbake/core';
import { ResourceManagementClient } from '@azure/arm-resources';
import { Deployment, DeploymentProperties } from '@azure/arm-resources/esm/models';
import { stringify } from 'querystring';
Expand Down Expand Up @@ -102,20 +102,7 @@ export class ARMHelper {

public GenerateTags(extraTags: Map<string,string> | null = null) : any
{
let tags: any = {}
if (extraTags){
extraTags.forEach((v,n)=>{
tags[n] = v
})
}

tags.envcode = this._ctx.Environment.environmentCode
tags.environment = this._ctx.Environment.environmentName
tags.region = this._ctx.Region.name
tags.recipe = this._ctx.Config.name
tags.version = this._ctx.Config.version
tags.ingredient = this._ctx.Ingredient.properties.type || ""

return tags
var tagGen = new TagGenerator(this._ctx)
return tagGen.GenerateTags(extraTags)
}
}
1 change: 1 addition & 0 deletions core/src/bake-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface IBaseUtilityType {
export interface IIngredient {
properties: IIngredientProperties,
dependsOn: Array<string>
pluginVersion: string
}

export interface IBakeConfig {
Expand Down
3 changes: 2 additions & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {IBakeAuthentication, IBakeConfig, IBakeEnvironment, IBakePackage, IBakeR
import {IngredientManager} from './ingredient-manager'
import {BaseIngredient} from './base-ingredient'
import {BaseUtility} from './base-utility'
import {TagGenerator} from './tag-generator'

export {BakeEval, BakeVariable, BaseUtility, DeploymentContext,Logger,
IBakeAuthentication, IBakeConfig, IBakeEnvironment, IBakePackage, IBakeRegion, IIngredient, IIngredientProperties,
IngredientManager, BaseIngredient
IngredientManager, BaseIngredient, TagGenerator
}
13 changes: 11 additions & 2 deletions core/src/ingredient-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ export class IngredientManager {
= new Map<string, IIngredientType>()
private static ingredientUtilTypes : Map<string,IBaseUtilityType>
= new Map<string, IBaseUtilityType>()
private static ingredientTypesVersions : Map<string, string>
= new Map<string, string>()

public static Register(moduleName: string):void {

var module = require(moduleName)

if (module.plugin){
let module_version = require(moduleName + '/package.json').version

IngredientManager.ingredientTypes.set(module.pluginNS, module.plugin)
IngredientManager.ingredientTypesVersions.set(module.pluginNS, module_version)
}

if (module.functions){
Expand All @@ -27,9 +33,12 @@ export class IngredientManager {
public static CreateIngredient(ingredientType: string, name: string, ingredient: IIngredient, ctx: DeploymentContext ): BaseIngredient | null {

let type = IngredientManager.ingredientTypes.get(ingredientType)

if (type){
return new type(name,ingredient,ctx)
let plugin = new type(name,ingredient,ctx)

//this is a bit smelly, but allows us to inject package version into the ingredient instance
plugin._ingredient.pluginVersion = IngredientManager.ingredientTypesVersions.get(ingredientType) || "0.0.0"
return plugin
}
else {
return null
Expand Down
35 changes: 35 additions & 0 deletions core/src/tag-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { DeploymentContext } from "./deployment-context";

export class TagGenerator {
constructor(ctx: DeploymentContext) {
this._ctx = ctx
}
_ctx : DeploymentContext

public GenerateTags(extraTags: Map<string,string> | null = null) : any
{
let tags: any = {}
if (extraTags){
extraTags.forEach((v,n)=>{
tags[n] = v
})
}

tags.envcode = this._ctx.Environment.environmentCode
tags.environment = this._ctx.Environment.environmentName
tags.region = this._ctx.Region.name
tags.recipe = this._ctx.Config.name
tags.package_version = this._ctx.Config.version
tags.bake_version = this._ctx.Environment.toolVersion

//check against pluginVersion be seting, since Ingredient is always set as a default object
if (this._ctx.Ingredient.pluginVersion){
tags.ing_version = this._ctx.Ingredient.pluginVersion
tags.ingredient = this._ctx.Ingredient.properties.type || ""
}

tags.deployment_ts = new Date().toISOString()

return tags
}
}
2 changes: 1 addition & 1 deletion ingredient/ingredient-app-insights/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ingredient/ingredient-traffic-manager/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ingredient/ingredient-webapp-container/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion system/src/bake-runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBakePackage, IBakeRegion, IIngredient, IBakeAuthentication, BakeEval, IBakeConfig, IngredientManager} from "@azbake/core";
import { IBakePackage, IBakeRegion, IIngredient, IBakeAuthentication, BakeEval, IBakeConfig, IngredientManager, TagGenerator} from "@azbake/core";
import {IngredientFactory} from './ingredients'
import {red, cyan} from 'colors'
import { DeploymentContext, Logger } from "@azbake/core"
Expand Down Expand Up @@ -97,8 +97,11 @@ export class BakeRunner {

if (!rgExists){

let tagGenerator = new TagGenerator(ctx)

ctx.Logger.log('Setting up resource group ' + cyan(rg_name))
await client.resourceGroups.createOrUpdate(rg_name, <ResourceGroup>{
tags: tagGenerator.GenerateTags(),
location: region_name
})
}
Expand Down

0 comments on commit 1dcef96

Please sign in to comment.