Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]- New Azure SQL Managed resources #8835

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
90f564a
added initial commit for asmi
PriyankaGR Aug 12, 2020
a70606e
added ASMI files for v3.0 along with examples
PriyankaGR Aug 12, 2020
654da81
added data source to tf files and uncommented code lines
PriyankaGR Aug 12, 2020
8d8a589
Added update fixes for create mode
PriyankaGR Aug 13, 2020
3e8cbfe
go fmt commit
sureshpatamsetty Aug 17, 2020
94fa64c
moved exapmles to a folder in mssql
sureshpatamsetty Aug 19, 2020
769bd9a
added tests for datasource and resource - asmi
sureshpatamsetty Aug 19, 2020
1cb0eda
Updated ASMI Data source files
PriyankaGR Aug 19, 2020
38004fe
added dns zone partner test
sureshpatamsetty Aug 20, 2020
12fb42b
terraform fmt apply
sureshpatamsetty Aug 20, 2020
5e13bd6
Merge branch 'master' into feature/asmi
PriyankaGR Aug 21, 2020
6cb5b03
updated accepetence template
sureshpatamsetty Aug 24, 2020
fc2f357
Merge branch 'feature/asmi' of https://github.com/Microsoft-Salesforc…
sureshpatamsetty Aug 24, 2020
2881dcd
changed data_point_enabled to public_data_point_enabled
PriyankaGR Aug 24, 2020
e5213cf
Merge branch 'feature/asmi' of https://github.com/Microsoft-Salesforc…
PriyankaGR Aug 24, 2020
0079372
updated examples
PriyankaGR Aug 24, 2020
3795c9a
initial commit for managed databases
PriyankaGR Aug 31, 2020
ea7e93e
formatted code
PriyankaGR Sep 3, 2020
673c021
added formatting
PriyankaGR Sep 8, 2020
45857d0
Merge branch 'master' into feature/asmi
PriyankaRanganath Sep 10, 2020
60ad72b
removed depreciated features
PriyankaRanganath Sep 10, 2020
9523e8e
merged latest master branch from upstream repo
Sep 23, 2020
5f947b6
initial commit
Sep 24, 2020
91248ed
Added managed instance keys and encryption
Sep 24, 2020
f2bd80e
Merge branch 'master' into feature/asmi
Sep 25, 2020
d36819f
Added test cases for MI transport data encryption
Sep 30, 2020
0b78b99
fixed changes
Oct 5, 2020
cbb01e3
refactored code
Oct 8, 2020
8d2846d
added documentation and corrections
Oct 10, 2020
fce12b0
fixed comments
Oct 10, 2020
fbe93ed
more formatting added
Oct 10, 2020
c7e2403
added formatting
Oct 10, 2020
6618a7f
merge conflicts resolved
Oct 10, 2020
c8d2057
added more formatting
Oct 10, 2020
8de855c
formatting mistakes corrected
Oct 10, 2020
8b5bff2
refactored code
PriyankaRanganath Oct 14, 2020
b988e59
fixed formatting
PriyankaRanganath Oct 14, 2020
f2a16ed
corrected documentation
PriyankaRanganath Oct 14, 2020
b892c99
updated asmi more
PriyankaRanganath Oct 16, 2020
d8d6850
Merge branch 'master' into feature/asmi
PriyankaRanganath Oct 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions azurerm/helpers/azure/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package azure

import (
"fmt"
"net/url"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

Expand Down Expand Up @@ -43,6 +47,216 @@ func ValidateMsSqlElasticPoolName(i interface{}, k string) (_ []string, errors [
return nil, nil
}

// Managed instance name can only be made up of lowercase letters 'a'-'z', the numbers 0-9 and the hyphen.
// The hyphen may not lead or trail in the instance name.
// Maximum length of instace name can not exceed 260 characters
func ValidateManagedInstanceName(i interface{}, k string) (_ []string, errors []error) {
if m, regexErrs := validate.RegExHelper(i, k, `^[0-9a-z]([-0-9a-z]{0,259}[0-9a-z])?$`); !m {
return nil, append(regexErrs, fmt.Errorf("%q can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 260 characters.", k))
}

return nil, nil
}

func ValidateManagedInstanceTimeZones() schema.SchemaValidateFunc {
// accepted timezones are listed here: https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/timezones-overview
acceptedTimeZones := []string{
"Dateline Standard Time",
"UTC-11",
"Aleutian Standard Time",
"Hawaiian Standard Time",
"Marquesas Standard Time",
"Alaskan Standard Time",
"UTC-09",
"Pacific Standard Time (Mexico)",
"UTC-08",
"Pacific Standard Time",
"US Mountain Standard Time",
"Mountain Standard Time (Mexico)",
"Mountain Standard Time",
"Central America Standard Time",
"Central Standard Time",
"Easter Island Standard Time",
"Central Standard Time (Mexico)",
"Canada Central Standard Time",
"SA Pacific Standard Time",
"Eastern Standard Time (Mexico)",
"Eastern Standard Time",
"Haiti Standard Time",
"Cuba Standard Time",
"US Eastern Standard Time",
"Turks And Caicos Standard Time",
"Paraguay Standard Time",
"Atlantic Standard Time",
"Venezuela Standard Time",
"Central Brazilian Standard Time",
"SA Western Standard Time",
"Pacific SA Standard Time",
"Newfoundland Standard Time",
"Tocantins Standard Time",
"E. South America Standard Time",
"SA Eastern Standard Time",
"Argentina Standard Time",
"Greenland Standard Time",
"Montevideo Standard Time",
"Magallanes Standard Time",
"Saint Pierre Standard Time",
"Bahia Standard Time",
"UTC-02",
"Mid-Atlantic Standard Time",
"Azores Standard Time",
"Cape Verde Standard Time",
"UTC",
"GMT Standard Time",
"Greenwich Standard Time",
"W. Europe Standard Time",
"Central Europe Standard Time",
"Romance Standard Time",
"Morocco Standard Time",
"Sao Tome Standard Time",
"Central European Standard Time",
"W. Central Africa Standard Time",
"Jordan Standard Time",
"GTB Standard Time",
"Middle East Standard Time",
"Egypt Standard Time",
"E. Europe Standard Time",
"Syria Standard Time",
"West Bank Standard Time",
"South Africa Standard Time",
"FLE Standard Time",
"Israel Standard Time",
"Kaliningrad Standard Time",
"Sudan Standard Time",
"Libya Standard Time",
"Namibia Standard Time",
"Arabic Standard Time",
"Turkey Standard Time",
"Arab Standard Time",
"Belarus Standard Time",
"Russian Standard Time",
"E. Africa Standard Time",
"Iran Standard Time",
"Arabian Standard Time",
"Astrakhan Standard Time",
"Azerbaijan Standard Time",
"Russia Time Zone 3",
"Mauritius Standard Time",
"Saratov Standard Time",
"Georgian Standard Time",
"Volgograd Standard Time",
"Caucasus Standard Time",
"Afghanistan Standard Time",
"West Asia Standard Time",
"Ekaterinburg Standard Time",
"Pakistan Standard Time",
"India Standard Time",
"Sri Lanka Standard Time",
"Nepal Standard Time",
"Central Asia Standard Time",
"Bangladesh Standard Time",
"Omsk Standard Time",
"Myanmar Standard Time",
"SE Asia Standard Time",
"Altai Standard Time",
"W. Mongolia Standard Time",
"North Asia Standard Time",
"N. Central Asia Standard Time",
"Tomsk Standard Time",
"China Standard Time",
"North Asia East Standard Time",
"Singapore Standard Time",
"W. Australia Standard Time",
"Taipei Standard Time",
"Ulaanbaatar Standard Time",
"Aus Central W. Standard Time",
"Transbaikal Standard Time",
"Tokyo Standard Time",
"North Korea Standard Time",
"Korea Standard Time",
"Yakutsk Standard Time",
"Cen. Australia Standard Time",
"AUS Central Standard Time",
"E. Australia Standard Time",
"AUS Eastern Standard Time",
"West Pacific Standard Time",
"Tasmania Standard Time",
"Vladivostok Standard Time",
"Lord Howe Standard Tim",
"Bougainville Standard Time",
"Russia Time Zone 10",
"Magadan Standard Time",
"Norfolk Standard Time",
"Sakhalin Standard Time",
"Central Pacific Standard Time",
"Russia Time Zone 11",
"New Zealand Standard Time",
"UTC+12",
"Fiji Standard Time",
"Kamchatka Standard Time",
"Chatham Islands Standard Time",
"UTC+13",
"Tonga Standard Time",
"Samoa Standard Time",
"Line Islands Standard Time",
}
return validation.StringInSlice(acceptedTimeZones, true)
}

func GetSQLResourceParentId(id string) (*string, error) {
idURL, err := url.ParseRequestURI(id)
if err != nil {
return nil, fmt.Errorf("Cannot parse Azure ID: %s", err)
}

path := idURL.Path

path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")

components := strings.Split(path, "/")

// We should have an even number of key-value pairs.
if len(components)%2 != 0 {
return nil, fmt.Errorf("The number of path segments is not divisible by 2 in %q", path)
}

var subscriptionID string
var resourceGroup string
var providers string
var server string
var managedInstance string

// Put the constituent key-value pairs into a map
for current := 0; current < len(components); current += 2 {
key := components[current]
value := components[current+1]

switch key {
case "subscriptions":
subscriptionID = value
case "resourceGroups":
resourceGroup = value
case "providers":
providers = value
case "managedInstances":
managedInstance = value
case "servers":
server = value
default:
return nil, fmt.Errorf("Key/Value cannot be empty strings. Key: '%s', Value: '%s'", key, value)
}
}

var databaseParentComponents = []string{"/subscriptions", subscriptionID, "resourceGroups", resourceGroup, "providers", providers, "managedInstances", managedInstance}
if server != "" {
databaseParentComponents[6] = "servers"
databaseParentComponents[7] = server
}
var parentId = strings.Join(databaseParentComponents, "/")
return &parentId, nil
}

func ValidateLongTermRetentionPoliciesIsoFormat(i interface{}, k string) (_ []string, errors []error) {
if m, regexErrs := validate.RegExHelper(i, k, `^P[0-9]*[YMWD]`); !m {
return nil, append(regexErrs, fmt.Errorf(`%q has to be a valid Duration format, starting with "P" and ending with either of the letters "YMWD"`, k))
Expand Down
25 changes: 25 additions & 0 deletions azurerm/internal/services/mssql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type Client struct {
ServerSecurityAlertPoliciesClient *sql.ServerSecurityAlertPoliciesClient
ServerVulnerabilityAssessmentsClient *sql.ServerVulnerabilityAssessmentsClient
VirtualMachinesClient *sqlvirtualmachine.SQLVirtualMachinesClient
ManagedInstancesClient *sql.ManagedInstancesClient
ManagedInstanceAdministratorsClient *sql.ManagedInstanceAdministratorsClient
ManagedDatabasesClient *sql.ManagedDatabasesClient
ManagedInstanceKeysClient *sql.ManagedInstanceKeysClient
ManagedInstanceEncryptionProtectorsClient *sql.ManagedInstanceEncryptionProtectorsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand Down Expand Up @@ -70,6 +75,21 @@ func NewClient(o *common.ClientOptions) *Client {
sqlVirtualMachinesClient := sqlvirtualmachine.NewSQLVirtualMachinesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sqlVirtualMachinesClient.Client, o.ResourceManagerAuthorizer)

managedInstancesClient := sql.NewManagedInstancesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&managedInstancesClient.Client, o.ResourceManagerAuthorizer)

managedInstanceAdministratorsClient := sql.NewManagedInstanceAdministratorsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&managedInstanceAdministratorsClient.Client, o.ResourceManagerAuthorizer)

managedDatabaseClient := sql.NewManagedDatabasesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&managedDatabaseClient.Client, o.ResourceManagerAuthorizer)

managedInstanceKeysClient := sql.NewManagedInstanceKeysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&managedInstanceKeysClient.Client, o.ResourceManagerAuthorizer)

managedInstanceEncryptionProtectorsClient := sql.NewManagedInstanceEncryptionProtectorsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&managedInstanceEncryptionProtectorsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
BackupLongTermRetentionPoliciesClient: &BackupLongTermRetentionPoliciesClient,
BackupShortTermRetentionPoliciesClient: &BackupShortTermRetentionPoliciesClient,
Expand All @@ -86,5 +106,10 @@ func NewClient(o *common.ClientOptions) *Client {
ServerSecurityAlertPoliciesClient: &serverSecurityAlertPoliciesClient,
ServerVulnerabilityAssessmentsClient: &serverVulnerabilityAssessmentsClient,
VirtualMachinesClient: &sqlVirtualMachinesClient,
ManagedInstancesClient: &managedInstancesClient,
ManagedInstanceAdministratorsClient: &managedInstanceAdministratorsClient,
ManagedDatabasesClient: &managedDatabaseClient,
ManagedInstanceKeysClient: &managedInstanceKeysClient,
ManagedInstanceEncryptionProtectorsClient: &managedInstanceEncryptionProtectorsClient,
}
}
125 changes: 125 additions & 0 deletions azurerm/internal/services/mssql/mssql_managed_database_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package mssql

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
)

func dataSourceArmMSSQLManagedDatabase() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmMSSQLManagedDatabaseRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"managed_instance_name": {
Type: schema.TypeString,
Required: true,
},

"resource_group": {
Type: schema.TypeString,
Required: true,
},

"managed_instance_id": {
Type: schema.TypeString,
Computed: true,
},

"location": {
Type: schema.TypeString,
Computed: true,
},

"type": {
Type: schema.TypeString,
Computed: true,
},

"collation": {
Type: schema.TypeString,
Computed: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
},

"creation_date": {
Type: schema.TypeString,
Computed: true,
},

"earliest_restore_point": {
Type: schema.TypeString,
Computed: true,
},

"default_secondary_location": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.Schema(),
},
}
}

func dataSourceArmMSSQLManagedDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.ManagedDatabasesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
managedInstanceName := d.Get("managed_instance_name").(string)
resourceGroup := d.Get("resource_group").(string)

resp, err := client.Get(ctx, resourceGroup, managedInstanceName, name)
if err != nil {
return fmt.Errorf("while reading managed SQL Database %s: %v", name, err)
}

if id := resp.ID; id != nil {
d.SetId(*id)
}

managedInstanceId, _ := azure.GetSQLResourceParentId(d.Id())

d.Set("name", name)
d.Set("resource_group", resourceGroup)
d.Set("type", resp.Type)
d.Set("managed_instance_id", managedInstanceId)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.ManagedDatabaseProperties; props != nil {
d.Set("collation", props.Collation)
d.Set("status", props.Status)
if props.CreationDate != nil && props.CreationDate.String() != "" {
d.Set("creation_date", props.CreationDate.String())
}
if props.EarliestRestorePoint != nil && props.EarliestRestorePoint.String() != "" {
d.Set("earliest_restore_point", props.EarliestRestorePoint.String())
}
d.Set("default_secondary_location", props.DefaultSecondaryLocation)
}

return tags.FlattenAndSet(d, resp.Tags)
}
Loading