Skip to content

Commit

Permalink
PulumiBicepConverter v0.15 update to latest Pulumi SDK to use the arg…
Browse files Browse the repository at this point in the history
…s in conversion requests and fix parameterizing tenantId and subscriptionId
  • Loading branch information
Zaid-Ajaj committed Sep 23, 2023
1 parent 82066c9 commit 4f22f31
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pulumi plugin install converter bicep --server github://api.github.com/Zaid-Ajaj
```

### Usage
In a directory with a single Bicep file, run the following command:
Run the following command in the directory where your Bicep files are located
```
pulumi convert --from bicep --language <language> --out pulumi
pulumi convert --from bicep --language <language> --out pulumi --entry <entry-file>
```
Will convert Bicep code into your language of choice: `typescript`, `csharp`, `python`, `go`, `java` or `yaml`

Expand Down
6 changes: 3 additions & 3 deletions integration_tests/keyvault/pulumi/main.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
currentResourceGroup = invoke("azure-native:resources:getResourceGroup", {
resourceGroupName = resourceGroupName
})
config tenantId "string" {
}
currentClientConfig = invoke("azure-native:authorization:getClientConfig", {
})
config adminPassword "string" {
}
resource kv "azure-native:keyvault:Vault" {
Expand All @@ -15,7 +15,7 @@
family = "A"
name = "standard"
}
tenantId = tenantId
tenantId = currentClientConfig.tenantId
}
resourceGroupName = currentResourceGroup.name
vaultName = "kv-contoso"
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/keyvault/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const resourceGroupName = config.require("resourceGroupName");
const currentResourceGroup = azure_native.resources.getResourceGroupOutput({
resourceGroupName: resourceGroupName,
});
const tenantId = config.require("tenantId");
const currentClientConfig = azure_native.authorization.getClientConfig({});
const adminPassword = config.require("adminPassword");
const kv = new azure_native.keyvault.Vault("kv-contoso", {
properties: {
sku: {
family: "A",
name: azure_native.keyvault.SkuName.Standard,
},
tenantId: tenantId,
tenantId: currentClientConfig.then(currentClientConfig => currentClientConfig.tenantId),
},
resourceGroupName: currentResourceGroup.apply(currentResourceGroup => currentResourceGroup.name),
vaultName: "kv-contoso",
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/keyvault/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dependencies": {
"typescript": "^4.0.0",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/azure-native": "2.0.0"
"@pulumi/azure-native": "2.4.0"
}
}
2 changes: 1 addition & 1 deletion integration_tests/keyvault/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
"index.ts",
]
}
2 changes: 1 addition & 1 deletion integration_tests/simpleModule/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dependencies": {
"typescript": "^4.0.0",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/azure-native": "2.0.0"
"@pulumi/azure-native": "2.4.0"
}
}
2 changes: 1 addition & 1 deletion integration_tests/simpleModule/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
},
"files": [
"index.ts",
"storage.ts",
"storage.ts"
]
}
2 changes: 1 addition & 1 deletion integration_tests/simpleStorage/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dependencies": {
"typescript": "^4.0.0",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/azure-native": "2.0.0"
"@pulumi/azure-native": "2.4.0"
}
}
2 changes: 1 addition & 1 deletion integration_tests/storageAccount/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dependencies": {
"typescript": "^4.0.0",
"@pulumi/pulumi": "^3.0.0",
"@pulumi/azure-native": "2.0.0"
"@pulumi/azure-native": "2.4.0"
}
}
41 changes: 20 additions & 21 deletions src/Converter/BicepProgram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ let parameterizeByResourceGroup (bicepProgram: BicepProgram) : BicepProgram =

programWithAddedDeclarations

let parameterizeByTenantId (bicepProgram: BicepProgram) : BicepProgram =
let parameterizeByTenantAndSubscriptionId (bicepProgram: BicepProgram) : BicepProgram =
let containsImplicitSubscriptionTenantId =
bicepProgram
|> programContains (function
Expand All @@ -407,37 +407,36 @@ let parameterizeByTenantId (bicepProgram: BicepProgram) : BicepProgram =
| BicepSyntax.PropertyAccess(BicepSyntax.FunctionCall("tenant", [ ]), "tenantId") -> true
| _ -> false)

let tenantIdParameter = BicepDeclaration.Parameter {
name = "tenantId"
parameterType = Some "string"
decorators = [ ]
defaultValue =
match bicepProgram.programKind with
| ProgramKind.EntryPoint -> None
| ProgramKind.Module -> Some (BicepSyntax.String "")
}
let containsImplicitSubscriptionId =
bicepProgram
|> programContains (function
| BicepSyntax.PropertyAccess(BicepSyntax.FunctionCall("subscription", [ ]), "subscriptionId") -> true
| _ -> false)

if not containsImplicitSubscriptionTenantId && not containsImplicitTenantId then
if not containsImplicitSubscriptionTenantId && not containsImplicitTenantId && not containsImplicitSubscriptionId then
bicepProgram
else
let currentClientConfig = BicepDeclaration.Variable {
name = "currentClientConfig"
value = BicepSyntax.FunctionCall("getClientConfig", [ ])
}

let replacements = Map.ofList [
BicepSyntax.PropertyAccess(
BicepSyntax.FunctionCall("subscription", [ ]), "tenantId"), BicepSyntax.VariableAccess "tenantId"
BicepSyntax.PropertyAccess(BicepSyntax.FunctionCall("subscription", [ ]), "tenantId"),
BicepSyntax.PropertyAccess(BicepSyntax.VariableAccess "currentClientConfig", "tenantId")

BicepSyntax.PropertyAccess(BicepSyntax.FunctionCall("subscription", [ ]), "subscriptionId"),
BicepSyntax.PropertyAccess(BicepSyntax.VariableAccess "currentClientConfig", "subscriptionId")

BicepSyntax.PropertyAccess(
BicepSyntax.FunctionCall("tenant", [ ]), "tenantId"), BicepSyntax.VariableAccess "tenantId"
BicepSyntax.PropertyAccess(BicepSyntax.FunctionCall("tenant", [ ]), "tenantId"),
BicepSyntax.PropertyAccess(BicepSyntax.VariableAccess "currentClientConfig", "tenantId")
]

let modifiedProgram = programReplace replacements bicepProgram
let programWithAddedDeclarations = {
bicepProgram with
declarations = [
match tryFindParameter "tenantId" modifiedProgram with
| None ->
// add the parameter
yield tenantIdParameter
| _ ->
()
yield currentClientConfig
yield! modifiedProgram.declarations
]
}
Expand Down
4 changes: 2 additions & 2 deletions src/Converter/Compile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let compileProgramWithComponents (args: CompilationArgs) =
bicepProgram
|> BicepProgram.dropResourceUnknowns
|> BicepProgram.reduceScopeParameter
|> BicepProgram.parameterizeByTenantId
|> BicepProgram.parameterizeByTenantAndSubscriptionId
|> BicepProgram.addResourceGroupNameParameterToModules
|> BicepProgram.parameterizeByResourceGroup
|> Transform.bicepProgramToPulumi
Expand All @@ -41,7 +41,7 @@ let compileProgramWithComponents (args: CompilationArgs) =
bicepProgram
|> BicepProgram.dropResourceUnknowns
|> BicepProgram.reduceScopeParameter
|> BicepProgram.parameterizeByTenantId
|> BicepProgram.parameterizeByTenantAndSubscriptionId
|> BicepProgram.addResourceGroupNameParameterToModules
|> BicepProgram.parameterizeByResourceGroupName
|> Transform.bicepProgramToPulumi
Expand Down
3 changes: 3 additions & 0 deletions src/Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ let transformFunction name args program =
invoke "azure-native:resources:getResourceGroup" [
"resourceGroupName", fromBicep arg program
]

| "getClientConfig", _ ->
invoke "azure-native:authorization:getClientConfig" [ ]

| "getExistingResource", [ BicepSyntax.String resourceToken; BicepSyntax.Object properties ] ->
let invokeToken = ResourceTokens.fromAzureSpecToExistingResourceToken resourceToken
Expand Down
19 changes: 16 additions & 3 deletions src/PulumiBicepConverter/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ let errorResponse (message: string) =

let convertProgram (request: ConvertProgramRequest): ConvertProgramResponse =
let bicepFile =
Directory.EnumerateFiles(request.SourceDirectory)
|> Seq.tryFind (fun file -> Path.GetExtension(file) = ".bicep")
request.Args
|> Seq.pairwise
|> Seq.tryFind (fun (argKey, argValue) -> argKey = "--entry")
|> Option.map (fun (_, entry) ->
if not (entry.EndsWith ".bicep")
then entry
else entry + ".bicep")
|> Option.map (fun entryBicep ->
if Path.IsPathRooted(entryBicep)
then entryBicep
else Path.Combine(request.SourceDirectory, entryBicep))
|> Option.orElse (
Directory.EnumerateFiles(request.SourceDirectory)
|> Seq.tryFind (fun file -> Path.GetExtension(file) = ".bicep")
)

match bicepFile with
| None ->
errorResponse "No Bicep file found in the source directory"
errorResponse "Could not find the entry bicep file from the source directory"
| Some entryBicepFile ->
let storageOptions = FolderFileStorageOptions(Folder=request.SourceDirectory)
let storage = new FolderFileStorage(storageOptions)
Expand Down
4 changes: 2 additions & 2 deletions src/PulumiBicepConverter/PulumiBicepConverter.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Version>0.14.0</Version>
<Version>0.15.0</Version>
<AssemblyName>pulumi-converter-bicep</AssemblyName>
</PropertyGroup>

Expand All @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Pulumi" Version="3.56.0" />
<PackageReference Include="Pulumi" Version="3.57.0" />
</ItemGroup>

</Project>

0 comments on commit 4f22f31

Please sign in to comment.