Skip to content

Commit

Permalink
Port two Azure examples to Go. (pulumi#542)
Browse files Browse the repository at this point in the history
And fix the Gopkg.toml files for AWS examples.
  • Loading branch information
pgavlin committed Feb 7, 2020
1 parent d65f682 commit 2a6323a
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aws-go-fargate/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

[[constraint]]
name = "github.com/pulumi/pulumi-aws"
branch = "tfgo"
version = "1.20.0"

[prune]
go-tests = true
Expand Down
2 changes: 1 addition & 1 deletion aws-go-s3-folder/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

[[constraint]]
name = "github.com/pulumi/pulumi-aws"
branch = "tfgo"
version = "1.20.0"

[prune]
go-tests = true
Expand Down
2 changes: 1 addition & 1 deletion aws-go-webserver/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

[[constraint]]
name = "github.com/pulumi/pulumi-aws"
branch = "tfgo"
version = "1.20.0"

[prune]
go-tests = true
Expand Down
38 changes: 38 additions & 0 deletions azure-go-appservice/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/pulumi/pulumi"
version = "1.9.1"

[[constraint]]
name = "github.com/pulumi/pulumi-azure"
version = "1.13.0"

[prune]
go-tests = true
unused-packages = true
11 changes: 11 additions & 0 deletions azure-go-appservice/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: azure-appservice
runtime: go
description: Creates Azure App Service with SQL Database and Application Insights
template:
config:
azure:environment:
description: The Azure environment to use (`public`, `usgovernment`, `german`, `china`)
default: public
sqlPassword:
description: SQL Server password (complex enough to satisfy Azure policy)
secret: true
61 changes: 61 additions & 0 deletions azure-go-appservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Azure App Service with SQL Database and Application Insights

Starting point for building web application hosted in Azure App Service.

Provisions Azure SQL Database and Azure Application Insights to be used in combination
with App Service.

## Running the App

1. Create a new stack:

```
$ pulumi stack init azure-appservice
```

1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):

```
$ az login
```

1. Set the azure location in which to run the test:

```
$ pulumi config set azure:location westus2
```

1. Define SQL Server password (make it complex enough to satisfy Azure policy):

```
pulumi config set --secret sqlPassword <value>
```

1. Run `pulumi up` to preview and deploy changes:

```
$ pulumi up
Previewing changes:
...
Performing changes:
...
info: 10 changes performed:
+ 10 resources created
Update duration: 1m14.59910109s
```

1. Check the deployed website endpoint:

```
$ pulumi stack output endpoint
https://azpulumi-as0ef47193.azurewebsites.net
$ curl "$(pulumi stack output endpoint)"
<html>
<body>
<h1>Greetings from Azure App Service!</h1>
</body>
</html>
```
162 changes: 162 additions & 0 deletions azure-go-appservice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"fmt"
"strings"

"github.com/pulumi/pulumi-azure/sdk/go/azure/appinsights"
"github.com/pulumi/pulumi-azure/sdk/go/azure/appservice"
"github.com/pulumi/pulumi-azure/sdk/go/azure/core"
"github.com/pulumi/pulumi-azure/sdk/go/azure/sql"
"github.com/pulumi/pulumi-azure/sdk/go/azure/storage"
"github.com/pulumi/pulumi/sdk/go/pulumi"
"github.com/pulumi/pulumi/sdk/go/pulumi/config"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// use first 10 characters of the stackname as prefix for resource names
prefix := ctx.Stack()
if len(prefix) > 10 {
prefix = prefix[:10]
}

resourceGroup, err := core.NewResourceGroup(ctx, prefix+"-rg", nil)
if err != nil {
return err
}

storageAccountName := strings.Replace(strings.ToLower(prefix), "-", "", -1) + "sa"
storageAccount, err := storage.NewAccount(ctx, storageAccountName, &storage.AccountArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
AccountKind: pulumi.String("StorageV2"),
AccountTier: pulumi.String("Standard"),
AccountReplicationType: pulumi.String("LRS"),
})
if err != nil {
return err
}

appServicePlan, err := appservice.NewPlan(ctx, prefix+"-asp", &appservice.PlanArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
Kind: pulumi.String("App"),
Sku: appservice.PlanSkuInputs{
Tier: pulumi.String("Basic"),
Size: pulumi.String("B1"),
},
})
if err != nil {
return err
}

storageContainer, err := storage.NewContainer(ctx, prefix+"-c", &storage.ContainerArgs{
StorageAccountName: storageAccount.Name,
ContainerAccessType: pulumi.String("private"),
})
if err != nil {
return err
}

blob, err := storage.NewZipBlob(ctx, prefix+"-b", &storage.ZipBlobArgs{
StorageAccountName: storageAccount.Name,
StorageContainerName: storageContainer.Name,
Type: pulumi.String("block"),
Content: pulumi.NewFileArchive("wwwroot"),
})
if err != nil {
return err
}

codeBlobURL := signedBlobReadURL(ctx, blob, storageAccount)

appInsights, err := appinsights.NewInsights(ctx, prefix+"-i", &appinsights.InsightsArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
ApplicationType: pulumi.String("Web"),
})
if err != nil {
return err
}

username := "pulumi"

// Get the password to use for SQL from config.
passwd := config.New(ctx, "").Require("sqlPassword")

sqlServer, err := sql.NewSqlServer(ctx, prefix+"-sql", &sql.SqlServerArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
AdministratorLogin: pulumi.String(username),
AdministratorLoginPassword: pulumi.String(passwd),
Version: pulumi.String("12.0"),
})
if err != nil {
return err
}

database, err := sql.NewDatabase(ctx, prefix+"-db", &sql.DatabaseArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
ServerName: sqlServer.Name,
RequestedServiceObjectiveName: pulumi.String("S0"),
})
if err != nil {
return err
}

app, err := appservice.NewAppService(ctx, prefix+"-as", &appservice.AppServiceArgs{
ResourceGroupName: resourceGroup.Name,
Location: resourceGroup.Location,
AppServicePlanId: appServicePlan.ID(),
AppSettings: pulumi.StringMap{
"WEBSITE_RUN_FROM_ZIP": codeBlobURL,
"ApplicationInsights:InstrumentationKey": appInsights.InstrumentationKey,
"APPINSIGHTS_INSTRUMENTATIONKEY": appInsights.InstrumentationKey,
},
ConnectionStrings: appservice.AppServiceConnectionStringsArray{
appservice.AppServiceConnectionStringsInputs{
Name: pulumi.String("db"),
Value: pulumi.Sprintf("Server=tcp:%s.database.windows.net;initial catalog=%s;user ID=%s;password=%s;Min Pool Size=0;Max Pool Size=30;Persist Security Info=true;",
sqlServer.Name, database.Name, username, passwd),
Type: pulumi.String("SQLAzure"),
},
},
})
if err != nil {
return err
}

ctx.Export("endpoint", pulumi.Sprintf("https://%s", app.DefaultSiteHostname))
return nil
})
}

func signedBlobReadURL(ctx *pulumi.Context, blob *storage.ZipBlob, account *storage.Account) pulumi.StringOutput {
// Choose a fixed, far-future expiration date for signed blob URLs. The shared access signature
// (SAS) we generate for the Azure storage blob must remain valid for as long as the Function
// App is deployed, since new instances will download the code on startup. By using a fixed
// date, rather than (e.g.) "today plus ten years", the signing operation is idempotent.
const signatureExpiration = "2100-01-01"

return pulumi.All(account.Name, account.PrimaryConnectionString, blob.StorageContainerName, blob.Name).
ApplyT(func(args []interface{}) (string, error) {
accountName := args[0].(string)
connectionString := args[1].(string)
containerName := args[2].(string)
blobName := args[3].(string)

sas, err := storage.LookupAccountBlobContainerSAS(ctx, &storage.GetAccountBlobContainerSASArgs{
ConnectionString: connectionString,
ContainerName: containerName,
Start: "2019-01-01",
Expiry: signatureExpiration,
Permissions: storage.GetAccountBlobContainerSASPermissionsArgs{Read: true},
})
if err != nil {
return "", err
}
return fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s%s", accountName, containerName, blobName, sas.Sas), nil
}).(pulumi.StringOutput)
}
5 changes: 5 additions & 0 deletions azure-go-appservice/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Greetings from Azure App Service!</h1>
</body>
</html>
38 changes: 38 additions & 0 deletions azure-go-webserver-component/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/pulumi/pulumi"
version = "1.9.1"

[[constraint]]
name = "github.com/pulumi/pulumi-azure"
version = "1.13.0"

[prune]
go-tests = true
unused-packages = true
13 changes: 13 additions & 0 deletions azure-go-webserver-component/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: ws-go-azure-comp
runtime: go
description: Basic example of an Azure web server accessible over HTTP
template:
config:
azure:location:
description: The Azure location to use
default: westus
username:
description: The username used to configure the Virtual Machine
password:
description: The password used to configure the Virtual Machine
secret: true
Loading

0 comments on commit 2a6323a

Please sign in to comment.