PGSSoft / terraform-provider-mssql

MS SQL Terraform provider
MIT License
17 stars 9 forks source link

feature request: sql contained auth and new db parameters #108

Open Krad-dono opened 1 year ago

Krad-dono commented 1 year ago

Hello,

I'm having some missing feature in current provider version when wanting to setup SQL users that use contained auth. Current "mssql_sql_user" resource can't do that and creating with script resource limits what you can do with that user. So maybe you can please add additional parameters to "mssql_sql_user" resource for contained auth? Like password. CREATE USER [<user_name>] WITH PASSWORD=N'<password>'

As well if we're talking about contained auth, maybe parameter in "mssql_database" to run alter database would be nice too like: ALTER DATABASE [<database name>] SET CONTAINMENT = PARTIAL WITH NO_WAIT or any other ALTER DATABASE.

Krad-dono commented 1 year ago

Another issue I discovered relates to how database creation resource works. My use case:

  1. create a new db using "mssql_database".
  2. because "mssql_database" has no params I run "mssql_script" resource with: ALTER DATABASE [${each.key}] SET DISABLE_BROKER WITH ROLLBACK IMMEDIATE;
  3. This works fine when db server is in single node or failover config. But fails with message below in AlwaysOn config: The operation cannot be performed on database "new_db" because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.

Maybe it is possible to add few parameters to "mssql_database" resource like from preferred db creation script below?

DECLARE @query     varchar(max)
DECLARE @database  varchar(500)
DECLARE @script    VarChar(MAX)

SET @database='database_name'  -- <<< Change database name

set @query='
CREATE DATABASE ['+@database+']
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'+@database+', FILENAME = N''D:\rdsdbdata\DATA\'+@database+'.mdf'' , SIZE = 65536KB , FILEGROWTH = 1048576KB )
 LOG ON 
( NAME = N'+@database+'_log, FILENAME = N''D:\rdsdbdata\DATA\'+@database+'_log.ldf'' , SIZE = 65536KB , FILEGROWTH = 524288KB )
GO
ALTER DATABASE ['+@database+'] SET CONTAINMENT = PARTIAL WITH NO_WAIT
GO
ALTER DATABASE ['+@database+'] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE ['+@database+'] SET  DISABLE_BROKER 
GO
ALTER DATABASE ['+@database+'] SET RECOVERY FULL 
GO
'
SET @script = 'EXEC (''' + REPLACE(REPLACE(@query, '''', ''''''), 'GO', '''); EXEC(''') + ''');'--Just add this one line.
exec(@script)