Warning: count(): Parameter must be an array or an object that implements Countable in /home/customer/www/vmguru.com/public_html/wp-content/plugins/amp-plugin-filter/amp-plugin-filter.php on line 24

Warning: Illegal string offset 'width' in /home/customer/www/vmguru.com/public_html/wp-content/plugins/amp-plugin-filter/amp-plugin-filter.php on line 28

Warning: Illegal string offset 'height' in /home/customer/www/vmguru.com/public_html/wp-content/plugins/amp-plugin-filter/amp-plugin-filter.php on line 28

Warning: Illegal string offset 'width' in /home/customer/www/vmguru.com/public_html/wp-content/plugins/amp-plugin-filter/amp-plugin-filter.php on line 28
Create a new database user using vRealize Automation XaaS

Create a new database user using vRealize Automation XaaS

Recently I described how to deliver Database-as-a-Service using XaaS (Anything-as-a-Service) in vRealize Automation. In the meantime I added some add-ons to this.

It’s nice to have the ability to create a new database using self-service from vRealize Automation but when the database is running, users may want to edit it’s properties. User may want to add a user to a database to log in with or give users additional rights on the database. So I created a few XaaS services in vRealize Automation to do just that.

Running the script inside the guest OS with vRealize Orchestrator.

Next we need a script which creates a database on an existing SQL Server instance. I used the PowerShell script below:

#----------------------------------------------------------------------
# Database as a Service add-on - MS SQL Server
# Create a new database user with db role
# Erik Scholten (VMGuru)
# January 2017 - Version 1.0
#
# Variables:
# [COMPUTERNAME] is SQL Server name
# [DBUSER] is new dbuser account name
# [DBPASSWORD] is password for new dbuser account
# [DATABASE] is the database on which the account receives the rights
# [DBROLE] is the role the account gets on the database
#-----------------------------------------------------------------------

# Import SQL Server module
Import-Module SQLPS -DisableNameChecking
 
# Replace this with your instance name
$instanceName = $env:[COMPUTERNAME]
$dbUserName = '[DBUSER]'
$dbPassword = '[DBPASSWORD]'
$Databases = '[DATABASE]'
$roleName = '[DBROLE]'
 
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
 
# Login already exists?
if ($server.Logins.Contains($dbUserName))  
{   
    Write-Host("Create $dbUserName failed because username already exists.")
}
else
{ 
    # Define user type and create the user
    $Login = New-Object Microsoft.SqlServer.Management.Smo.Login($server, $dbUserName)
    $Login.LoginType = 'SqlLogin'
    $Login.Create($dbPassword)
    $login.PasswordExpirationEnabled = $false
    Write-Host("Login $dbUserName created successfully.")

    foreach($databaseToMap in $Databases)  
    {
        $database = $server.Databases[$databaseToMap]
        if ($database.Users[$dbUserName])
        {
            Write-Host("Dropping user $dbUserName on $database.")
            $database.Users[$dbUserName].Drop()
        }

        $dbUser = New-Object `
        -TypeName Microsoft.SqlServer.Management.Smo.User `
        -ArgumentList $database, $dbUserName
        $dbUser.Login = $loginName
        $dbUser.Create()
        Write-Host("User $dbUser created successfully.")

        #assign database role for a new user
        $dbrole = $Database.Roles[$roleName]
        $dbrole.AddMember($dbUserName)
        $dbrole.Alter()
        Write-Host("User $dbUser successfully added to $roleName role.")
    }
}

Just like the ‘Database-as-a-Service’ we’re going to use the VM Guest Script Manager to run this script inside the database server. You can download the Guest Script Manager package from the VMware Development Center. Once downloaded, import the package into your Orchestrator instance.

Now we need to add the script above to the vRealize Orchestrator by running the ‘Guest Script Manager/Script Management/Add script configuration‘ workflow. Name your imported script ‘Create a database user (code)‘ (I added ‘code’ to the name to distinguish between the code and the actual workflow). Select ‘PowerShell’ as the script type and copy+paste this code in the script content window.

How to call the script?

So we’ve imported the script, now we need to create a workflow to run this script and input the variables which are mentioned in the script comments. This workflow is the actual workflow we’re going to call as XaaS service from vRealize Automation.

So create a new workflow called ‘Create a database user‘.

The result should look like this.

Create the XaaS workflow and blueprint

Now we have to retrieve input to fill the script variables mentioned in the script comments. The following input variables need to be provided:

The For each of these variables we need to add an Action element to the workflow which maps a workflow input. This results in an input prompt when running this workflow.

To do this:

Do the same for [DBUSER], [DBPASSWORD], [DATABASE] and [DBROLE].

Each mapping should look like this (Example: [DATABASE]):

Your completed workflow should look like this:

Now we need to create an XaaS service in vRealize Automation which calls the workflow that we’ve just created.

Go to ‘vRealize Automation/Design/XaaS Blueprints‘ and select ‘New Blueprint‘. Select the Orchestrator workflow you’ve just created.

Name your XaaS blueprint e.g. ‘Create a database user‘  and version it (1.0.0). Give the inputs nice user friendly input values so users who request this will know what they need to enter in which field. I also created some nice dropdown lists so users do not have to guess the values but select them from a predefined list. For now these are all static values but you can populate the field values dynamically for a more agile approach.

The last step is publishing the XaaS Blueprint, set the correct entitlements and enjoy a new Anything-as-a-Service. You can now request a new database administrator account without bothering the database administrators or ever opening SQL Server Management Studio.

 

Related posts

Gain insight in Kubernetes

by Dimitri de Swart
3 years ago

Monitor Windows services with the End-Point Operations Agent

by Sander Martijn
8 years ago

The (Un)official VMware VCIX-NV Study Guide

by Martijn Smit
9 years ago
Exit mobile version