From e3e5aa88fbc20f3bb5ff3c7436f50416c485ef20 Mon Sep 17 00:00:00 2001 From: Thomas Meckel Date: Wed, 7 Jun 2023 17:15:45 +0000 Subject: [PATCH 1/2] Added a typescript example --- examples/typescript/.gitignore | 2 + examples/typescript/.npmrc | 1 + examples/typescript/.yarnrc | 1 + examples/typescript/Pulumi.yaml | 12 +++ examples/typescript/README.md | 53 +++++++++++ examples/typescript/index.ts | 143 ++++++++++++++++++++++++++++++ examples/typescript/package.json | 14 +++ examples/typescript/tsconfig.json | 18 ++++ 8 files changed, 244 insertions(+) create mode 100644 examples/typescript/.gitignore create mode 100644 examples/typescript/.npmrc create mode 100644 examples/typescript/.yarnrc create mode 100644 examples/typescript/Pulumi.yaml create mode 100644 examples/typescript/README.md create mode 100644 examples/typescript/index.ts create mode 100644 examples/typescript/package.json create mode 100644 examples/typescript/tsconfig.json diff --git a/examples/typescript/.gitignore b/examples/typescript/.gitignore new file mode 100644 index 0000000..dc902b5 --- /dev/null +++ b/examples/typescript/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/node_modules/ \ No newline at end of file diff --git a/examples/typescript/.npmrc b/examples/typescript/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/examples/typescript/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/examples/typescript/.yarnrc b/examples/typescript/.yarnrc new file mode 100644 index 0000000..b4460e3 --- /dev/null +++ b/examples/typescript/.yarnrc @@ -0,0 +1 @@ +"--install.no-lockfile" true diff --git a/examples/typescript/Pulumi.yaml b/examples/typescript/Pulumi.yaml new file mode 100644 index 0000000..8e9e3cf --- /dev/null +++ b/examples/typescript/Pulumi.yaml @@ -0,0 +1,12 @@ +name: mssql-test +runtime: nodejs +description: A Pulumi YAML project to test the new MSSQL provider +config: + local-ip: + type: string + resourcegroup-location: + type: string + resourcegroup-name: + type: string + sqladmin: + type: string diff --git a/examples/typescript/README.md b/examples/typescript/README.md new file mode 100644 index 0000000..9e27de6 --- /dev/null +++ b/examples/typescript/README.md @@ -0,0 +1,53 @@ +# Pulumi MSSQL + +### Prerequisites + +1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/) + +### Steps + +1. Install packages + + ```bash + yarn install + ``` + +1. Pulumi login + + ```bash + pulumi login + ``` + +1. Create a new stack: + + ```bash + pulumi stack init mssql-dev + pulumi stack select mssql-dev + ``` + +1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step): + + ``` + $ az login + ``` + + > ***Note:*** + > you can use Service Principal Login as well + > https://www.pulumi.com/registry/packages/azure/installation-configuration/#authenticate-using-a-service-principal + +1. Configure the required settings: + + ```bash + pulumi config set local-ip + pulumi config set sqladmin + pulumi config set resourcegroup-location + pulumi config set resourcegroup-name + pulumi config set db-name + ``` + +1. Run `pulumi up` to preview and deploy changes: + + ```bash + pulumi up + Previewing changes: + ... diff --git a/examples/typescript/index.ts b/examples/typescript/index.ts new file mode 100644 index 0000000..8f13a2a --- /dev/null +++ b/examples/typescript/index.ts @@ -0,0 +1,143 @@ +import * as pulumi from "@pulumi/pulumi"; +import * as azure from "@pulumi/azure"; +import * as azuread from "@pulumi/azuread"; +import * as mssql from "@pulumiverse/mssql"; +import * as random from "@pulumi/random"; + +const res = (async () => { + const provider = new azure.Provider("provider", { skipProviderRegistration: true }); + const providerAzuread = new azuread.Provider("provider-azuread", {}); + + const config = new pulumi.Config(); + const resourcegroupLocation = config.require("resourcegroup-location"); + const resourcegroupName = config.require("resourcegroup-name"); + const localIp = config.require("local-ip"); + const sqladmin = config.require("sqladmin"); + const dbName = config.require("db-name"); + const roleName = "db_owner"; + + const randomPrefix = new random.RandomId("random-prefix", { + byteLength: 8, + keepers: { + keep: resourcegroupName, + }, + }); + + const serverName = pulumi.interpolate`sql${randomPrefix.hex}`; + const current = await azure.core.getClientConfig({ + provider: provider + }); + const aadSqladmin = await azuread.getUser({ + userPrincipalName: sqladmin, + }, { + provider: providerAzuread + }); + const aadGroupSqladmins = new azuread.Group("aad-group-sqladmins", { + displayName: pulumi.interpolate`AZ.RESOURCES.${serverName}.Admins`, + securityEnabled: true, + members: [ + current.objectId, + aadSqladmin.objectId, + ], + }, { + provider: providerAzuread, + }); + const resourceGroup = new azure.core.ResourceGroup("resource-group", { + name: resourcegroupName, + location: resourcegroupLocation, + }, { + provider: provider, + }); + const serverPassword = new random.RandomPassword("server-password", { + length: 32, + special: true, + }); + const server = new azure.mssql.Server("server", { + name: serverName, + resourceGroupName: resourceGroup.name, + location: resourceGroup.location, + version: "12.0", + administratorLogin: "sadmin", + administratorLoginPassword: serverPassword.result, + minimumTlsVersion: "1.2", + azureadAdministrator: { + azureadAuthenticationOnly: true, + loginUsername: aadGroupSqladmins.displayName, + objectId: aadGroupSqladmins.objectId, + tenantId: current.tenantId, + }, + }, { + provider: provider, + parent: resourceGroup, + }); + + const database = new azure.mssql.Database("database", { + name: dbName, + serverId: server.id, + licenseType: "LicenseIncluded", + maxSizeGb: 2, + skuName: "Basic", + }, { + provider: provider, + parent: server, + }); + + const databaseFirewallRule = new azure.mssql.FirewallRule("database-firewall-rule", { + name: "client", + serverId: server.id, + startIpAddress: localIp, + endIpAddress: localIp, + }, { + provider: provider, + parent: server, + }); + + const providerMssql = new mssql.Provider("provider-mssql", { + hostname: server.fullyQualifiedDomainName, + azureAuth: {}, + }, { + dependsOn: [ + database, + databaseFirewallRule, + ], + }); + + const databaseId = await mssql.getDatabase({ + name: dbName, + }, { + provider: providerMssql, + parent: database, + }); + const databaseRoleOwner = await mssql.getDatabaseRole({ + databaseId: databaseId.id, + name: roleName, + }, { + provider: providerMssql, + parent: database, + }); + + const aadGroupDbOwner = new azuread.Group("aad-group-db-owner", { + displayName: pulumi.interpolate`AZ.RESOURCES.${server.name}.Database.${database.name}.${roleName}`, + securityEnabled: true, + }, { + provider: providerAzuread, + parent: database, + }); + const dbUser = new mssql.AzureadUser("db-user", { + databaseId: databaseId.id || "0", + name: aadGroupDbOwner.displayName, + userObjectId: aadGroupDbOwner.objectId, + }, { + provider: providerMssql, + parent: database, + }); + const dbRoleMember = new mssql.DatabaseRoleMember("db-role-member", { + roleId: databaseRoleOwner.id || "0", + memberId: dbUser.id, + }, { + provider: providerMssql, + parent: database, + }); +})() // async-wrapper + +export default res diff --git a/examples/typescript/package.json b/examples/typescript/package.json new file mode 100644 index 0000000..67caa77 --- /dev/null +++ b/examples/typescript/package.json @@ -0,0 +1,14 @@ +{ + "name": "mssql-test", + "devDependencies": { + "@types/node": "^14" + }, + "dependencies": { + "typescript": "^4.0.0", + "@pulumi/pulumi": "^3.0.0", + "@pulumi/azure": "5.38.0", + "@pulumi/azuread": "5.30.0", + "@pulumiverse/mssql": "0.0.6", + "@pulumi/random": "4.3.1" + } +} diff --git a/examples/typescript/tsconfig.json b/examples/typescript/tsconfig.json new file mode 100644 index 0000000..2ed7ea9 --- /dev/null +++ b/examples/typescript/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": "bin", + "target": "es2016", + "module": "commonjs", + "moduleResolution": "node", + "sourceMap": true, + "experimentalDecorators": true, + "pretty": true, + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.ts", + ] +} \ No newline at end of file From 4f54ea1d51aff486a81f778cbfad001eef1683f1 Mon Sep 17 00:00:00 2001 From: Thomas Meckel Date: Wed, 7 Jun 2023 18:30:06 +0000 Subject: [PATCH 2/2] Updated version pinnings in examples/typescript/package.json --- examples/typescript/package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/typescript/package.json b/examples/typescript/package.json index 67caa77..6df8f2a 100644 --- a/examples/typescript/package.json +++ b/examples/typescript/package.json @@ -5,10 +5,10 @@ }, "dependencies": { "typescript": "^4.0.0", - "@pulumi/pulumi": "^3.0.0", - "@pulumi/azure": "5.38.0", - "@pulumi/azuread": "5.30.0", - "@pulumiverse/mssql": "0.0.6", - "@pulumi/random": "4.3.1" + "@pulumi/pulumi": "^3", + "@pulumi/azure": "^5", + "@pulumi/azuread": "^3", + "@pulumi/random": "^4", + "@pulumiverse/mssql": "^0.0.6" } }