Skip to content

Commit

Permalink
Merge pull request #7 from pulumiverse/feat/typescript-example
Browse files Browse the repository at this point in the history
feat: Typescript example
  • Loading branch information
tmeckel authored Jun 9, 2023
2 parents db44955 + 4f54ea1 commit c4f2292
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
1 change: 1 addition & 0 deletions examples/typescript/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions examples/typescript/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"--install.no-lockfile" true
12 changes: 12 additions & 0 deletions examples/typescript/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions examples/typescript/README.md
Original file line number Diff line number Diff line change
@@ -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 <your-external-ip>
pulumi config set sqladmin <aad-upn>
pulumi config set resourcegroup-location <azure-region>
pulumi config set resourcegroup-name <name-of-resource-group>
pulumi config set db-name <database-name>
```
1. Run `pulumi up` to preview and deploy changes:
```bash
pulumi up
Previewing changes:
...
143 changes: 143 additions & 0 deletions examples/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "mssql-test",
"devDependencies": {
"@types/node": "^14"
},
"dependencies": {
"typescript": "^4.0.0",
"@pulumi/pulumi": "^3",
"@pulumi/azure": "^5",
"@pulumi/azuread": "^3",
"@pulumi/random": "^4",
"@pulumiverse/mssql": "^0.0.6"
}
}
18 changes: 18 additions & 0 deletions examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
]
}

0 comments on commit c4f2292

Please sign in to comment.