Skip to content

Commit

Permalink
add tests and fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
saragluna committed Jan 2, 2025
1 parent 48559e1 commit d56aa1f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cli/azd/.vscode/cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ words:
- runcontext
- unmarshals
- usgovcloudapi
- datasource
- jdbc
- passwordless
- postgre
languageSettings:
- languageId: go
ignoreRegExpList:
Expand Down
50 changes: 50 additions & 0 deletions cli/azd/internal/repository/app_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,56 @@ func TestInitializer_prjConfigFromDetect(t *testing.T) {
},
},
},
{
name: "api with postgresql",
detect: detectConfirm{
Services: []appdetect.Project{
{
Language: appdetect.Java,
Path: "java",
DatabaseDeps: []appdetect.DatabaseDep{
appdetect.DbPostgres,
},
},
},
Databases: map[appdetect.DatabaseDep]EntryKind{
appdetect.DbPostgres: EntryKindDetected,
},
},
interactions: []string{
"postgresql-db",
// prompt for auth type
// todo cannot use umi here for it will check the source code
"Username and password",
},
want: project.ProjectConfig{
Services: map[string]*project.ServiceConfig{
"java": {
Language: project.ServiceLanguageJava,
Host: project.ContainerAppTarget,
RelativePath: "java",
},
},
Resources: map[string]*project.ResourceConfig{
"java": {
Type: project.ResourceTypeHostContainerApp,
Name: "java",
Props: project.ContainerAppProps{
Port: 8080,
},
Uses: []string{"postgresql"},
},
"postgresql": {
Type: project.ResourceTypeDbPostgres,
Name: "postgresql",
Props: project.PostgresProps{
DatabaseName: "postgresql-db",
AuthType: internal.AuthTypePassword,
},
},
},
},
},
{
name: "api and web",
detect: detectConfirm{
Expand Down
24 changes: 24 additions & 0 deletions cli/azd/internal/repository/detect_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ func Test_detectConfirm_confirm(t *testing.T) {
},
},
},
{
name: "confirm single with postgresql resource",
detection: []appdetect.Project{
{
Language: appdetect.Java,
Path: javaDir,
DatabaseDeps: []appdetect.DatabaseDep{
appdetect.DbPostgres,
},
},
},
interactions: []string{
"Confirm and continue initializing my app",
},
want: []appdetect.Project{
{
Language: appdetect.Java,
Path: javaDir,
DatabaseDeps: []appdetect.DatabaseDep{
appdetect.DbPostgres,
},
},
},
},
{
name: "add a language",
detection: []appdetect.Project{
Expand Down
21 changes: 8 additions & 13 deletions cli/azd/internal/repository/infra_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,16 @@ func (i *Initializer) infraSpecFromDetect(
continue
}

dbPrompt:
for {
dbName, err := promptDbName(i.console, ctx, database)
if err != nil {
return scaffold.InfraSpec{}, err
}
dbName, err := promptDbName(i.console, ctx, database)
if err != nil {
return scaffold.InfraSpec{}, err
}

switch database {
case appdetect.DbMongo:
spec.DbCosmosMongo = &scaffold.DatabaseCosmosMongo{
DatabaseName: dbName,
}
break dbPrompt
switch database {
case appdetect.DbMongo:
spec.DbCosmosMongo = &scaffold.DatabaseCosmosMongo{
DatabaseName: dbName,
}
break dbPrompt
}
}

Expand Down
3 changes: 3 additions & 0 deletions cli/azd/internal/scaffold/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scaffold

import (
"context"
"github.com/azure/azure-dev/cli/azd/internal"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -126,13 +127,15 @@ func TestExecInfra(t *testing.T) {
DbPostgres: &DatabasePostgres{
DatabaseName: "appdb",
DatabaseUser: "appuser",
AuthType: internal.AuthTypeUserAssignedManagedIdentity,
},
Services: []ServiceSpec{
{
Name: "api",
Port: 3100,
DbPostgres: &DatabasePostgres{
DatabaseName: "appdb",
AuthType: internal.AuthTypeUserAssignedManagedIdentity,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/resources/scaffold/base/abbreviations.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dataMigrationServices": "dms-",
"dBforMySQLServers": "mysql-",
"dBforPostgreSQLServers": "psql-",
"deploymentScript": "dc-",
"devicesIotHubs": "iot-",
"devicesProvisioningServices": "provs-",
"devicesProvisioningServicesCertificates": "pcert-",
Expand Down Expand Up @@ -132,4 +133,4 @@
"webSitesAppServiceEnvironment": "ase-",
"webSitesFunctions": "func-",
"webStaticSites": "stapp-"
}
}
14 changes: 11 additions & 3 deletions cli/azd/resources/scaffold/templates/resources.bicept
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ module cosmos 'br/public:avm/res/document-db/database-account:0.8.1' = {
{{- end}}

{{- if .DbPostgres}}

var postgreSqlDatabaseName = '{{ .DbPostgres.DatabaseName }}'
var postgreSqlDatabaseUser = '{{ .DbPostgres.DatabaseUser }}'
module postgreServer 'br/public:avm/res/db-for-postgre-sql/flexible-server:0.1.4' = {
Expand All @@ -137,12 +136,21 @@ module postgreServer 'br/public:avm/res/db-for-postgre-sql/flexible-server:0.1.4
name: postgreSqlDatabaseName
}
]
location: location
{{- if (and .DbPostgres (eq .DbPostgres.AuthType "userAssignedManagedIdentity")) }}
roleAssignments: [
{
principalId: connectionCreatorIdentity.outputs.principalId
principalType: 'ServicePrincipal'
roleDefinitionIdOrName: 'b24988ac-6180-42a0-ab88-20f7382dd24c'
}
]
{{- end}}
}
}
{{- end}}

{{- if (and .DbPostgres (eq .DbPostgres.AuthType "userAssignedManagedIdentity"))}}

module connectionCreatorIdentity 'br/public:avm/res/managed-identity/user-assigned-identity:0.2.1' = {
name: 'connectionCreatorIdentity'
params: {
Expand All @@ -151,9 +159,9 @@ module connectionCreatorIdentity 'br/public:avm/res/managed-identity/user-assign
}
}
{{- end}}

{{- range .Services}}
{{- if (and .DbPostgres (eq .DbPostgres.AuthType "userAssignedManagedIdentity")) }}
var {{bicepName .Name}}PostgresConnectionName = 'connection_${uniqueString(subscription().id, resourceGroup().id, location, '{{bicepName .Name}}', 'Postgres')}'
module {{bicepName .Name}}CreateConnectionToPostgreSql 'br/public:avm/res/resources/deployment-script:0.4.0' = {
name: '{{bicepName .Name}}CreateConnectionToPostgreSql'
params: {
Expand Down

0 comments on commit d56aa1f

Please sign in to comment.