From 7b6603ed5fa9a31495fc38dd208d8084a17bc693 Mon Sep 17 00:00:00 2001 From: fraliv13 <5892139+fraliv13@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:36:07 +0200 Subject: [PATCH] Tests and readme --- README.md | 48 +++++++++++++++++++ .../pulumi/eazure_powershell_script_test.go | 38 +++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 internal/controllers/provisioning/provisioners/pulumi/eazure_powershell_script_test.go diff --git a/README.md b/README.md index 9628362..530b919 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,54 @@ spec: workspaceFriendlyName: Charisma ``` +### AzurePowershellScript +`AzurePowershellScript` is a Custom Resource Definition (CRD) that represents an Azure PowerShell deployment script. + +Definition can be found [here](./helm/crds/provisioning.totalsoft.ro_azurepowershellscripts.yaml) + +#### Spec +The `AzurePowershellScript` spec has the following fields: + +- `scriptContent`: The content of the PowerShell script to be executed. +- `scriptArguments`: The arguments to be passed to the PowerShell script. These should match the parameters defined in the scriptContent. +- `managedIdentity`: The Azure Resource Manager (ARM) identifier of the managed identity used to run the script. +- `domainRef`: The reference to the domain that the user belongs to. +- `platformRef`: The reference to the platform that the user belongs to. +- `forceUpdateTag`: Update this value to trigger the script even if the content or args are unchanged + +Example: +```yaml +apiVersion: provisioning.totalsoft.ro/v1alpha1 +kind: AzurePowerShellScript +metadata: + name: createresourcegroup + namespace: provisioning-test +spec: + domainRef: domain2 + exports: + - scriptOutputs: + toConfigMap: + keyTemplate: MultiTenancy__Tenants__{{ .Tenant.Code }}__ScriptOutputs + managedIdentity: >- + /subscriptions/15b38e46-ef41-4f5b-bdba-7d9354568c2d/resourceGroups/global/providers/Microsoft.ManagedIdentity/userAssignedIdentities/scriptidentity + platformRef: provisioning.test + scriptContent: |- + param([string] $name) + + $output = "RG name: {0}" -f $name + Write-Output $output + + $DeploymentScriptOutputs = @{} + $DeploymentScriptOutputs['text'] = $output + + New-AzResourceGroup $name "West Europe" + scriptArguments: "-name testrg-{{ .Platform }}-{{ .Tenant.Code }}" + target: + category: Tenant +``` + + + ### EntraUser `EntraUser` is a Custom Resource Definition (CRD) that represents a user for Entra Id. diff --git a/internal/controllers/provisioning/provisioners/pulumi/eazure_powershell_script_test.go b/internal/controllers/provisioning/provisioners/pulumi/eazure_powershell_script_test.go new file mode 100644 index 0000000..269a17e --- /dev/null +++ b/internal/controllers/provisioning/provisioners/pulumi/eazure_powershell_script_test.go @@ -0,0 +1,38 @@ +package pulumi + +import ( + "testing" + + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + provisioningv1 "totalsoft.ro/platform-controllers/pkg/apis/provisioning/v1alpha1" +) + +func TestDeployAzurePowerShellScript(t *testing.T) { + t.Run("maximal entra user spec", func(t *testing.T) { + platform := "dev" + tenant := newTenant("tenant1", platform) + script := &provisioningv1.AzurePowerShellScript{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-pwsh-script", + }, + Spec: provisioningv1.AzurePowerShellScriptSpec{ + ScriptContent: "Write-Host 'Hello, World!'", + ManagedIdentity: "my-managed-identity", + ProvisioningMeta: provisioningv1.ProvisioningMeta{ + DomainRef: "example-domain", + }, + }, + } + + err := pulumi.RunErr(func(ctx *pulumi.Context) error { + script, err := deployAzurePowerShellScript(tenant, pulumi.String("rg").ToStringOutput(), script, []pulumi.Resource{}, ctx) + assert.NoError(t, err) + assert.NotNil(t, script) + return nil + + }, pulumi.WithMocks("project", "stack", mocks(0))) + assert.NoError(t, err) + }) +}