Skip to content

Commit

Permalink
new resource azurerm_synapse_sql_pool (#8095)
Browse files Browse the repository at this point in the history
the third resource of #7406

it needs to take a long time to run test for PointInTime restore (8h according to the doc) and recovery (1 day), so I don't add test case for these two create mode. I have tested it offline and there is no problem
  • Loading branch information
njuCZ authored Sep 11, 2020
1 parent ee8e5db commit 87114b2
Show file tree
Hide file tree
Showing 14 changed files with 1,149 additions and 29 deletions.
47 changes: 47 additions & 0 deletions azurerm/internal/services/sql/parse/sql_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SqlDatabaseId struct {
ResourceGroup string
ServerName string
Name string
}

func NewSqlDatabaseID(resourceGroup, serverName, name string) SqlDatabaseId {
return SqlDatabaseId{
ResourceGroup: resourceGroup,
ServerName: serverName,
Name: name,
}
}

func (id SqlDatabaseId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Sql/servers/%s/databases/%s", subscriptionId, id.ResourceGroup, id.ServerName, id.Name)
}

func SqlDatabaseID(input string) (*SqlDatabaseId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Synapse Sql Pool ID %q: %+v", input, err)
}

sqlDatabaseId := SqlDatabaseId{
ResourceGroup: id.ResourceGroup,
}
if sqlDatabaseId.ServerName, err = id.PopSegment("servers"); err != nil {
return nil, err
}
if sqlDatabaseId.Name, err = id.PopSegment("databases"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &sqlDatabaseId, nil
}
88 changes: 88 additions & 0 deletions azurerm/internal/services/sql/parse/sql_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = SqlDatabaseId{}

func TestSqlDatabaseId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *SqlDatabaseId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing servers Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/servers/",
Expected: nil,
},
{
Name: "Sql Servers ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/servers/server1",
Expected: nil,
},
{
Name: "Missing databases Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/servers/server1/databases",
Expected: nil,
},
{
Name: "Sql Database ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/servers/server1/databases/db1",
Expected: &SqlDatabaseId{
Name: "db1",
ServerName: "server1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/servers/server1/Databases/db1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := SqlDatabaseID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
37 changes: 16 additions & 21 deletions azurerm/internal/services/sql/resource_arm_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/sql/helper"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/sql/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -28,9 +30,10 @@ func resourceArmSqlDatabase() *schema.Resource {
Update: resourceArmSqlDatabaseCreateUpdate,
Delete: resourceArmSqlDatabaseDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.SqlDatabaseID(id)
return err
}),

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Expand Down Expand Up @@ -523,41 +526,37 @@ func resourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.SqlDatabaseID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
serverName := id.Path["servers"]
name := id.Path["databases"]

resp, err := client.Get(ctx, resourceGroup, serverName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.ServerName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading SQL Database %q - removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Sql Database %s: %+v", name, err)
return fmt.Errorf("Error making Read request on Sql Database %s: %+v", id.Name, err)
}

threatClient := meta.(*clients.Client).Sql.DatabaseThreatDetectionPoliciesClient
threat, err := threatClient.Get(ctx, resourceGroup, serverName, name)
threat, err := threatClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err == nil {
if err := d.Set("threat_detection_policy", flattenArmSqlServerThreatDetectionPolicy(d, threat)); err != nil {
return fmt.Errorf("Error setting `threat_detection_policy`: %+v", err)
}
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

d.Set("server_name", serverName)
d.Set("server_name", id.ServerName)

if props := resp.DatabaseProperties; props != nil {
// TODO: set `create_mode` & `source_database_id` once this issue is fixed:
Expand Down Expand Up @@ -599,9 +598,9 @@ func resourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error
}

auditingClient := meta.(*clients.Client).Sql.DatabaseExtendedBlobAuditingPoliciesClient
auditingResp, err := auditingClient.Get(ctx, resourceGroup, serverName, name)
auditingResp, err := auditingClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
return fmt.Errorf("failure in reading SQL Database %q: %v Blob Auditing Policies", name, err)
return fmt.Errorf("failure in reading SQL Database %q: %v Blob Auditing Policies", id.Name, err)
}

flattenBlobAuditing := helper.FlattenAzureRmSqlDBBlobAuditingPolicies(&auditingResp, d)
Expand All @@ -617,16 +616,12 @@ func resourceArmSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) erro
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.SqlDatabaseID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
serverName := id.Path["servers"]
name := id.Path["databases"]

resp, err := client.Delete(ctx, resourceGroup, serverName, name)
resp, err := client.Delete(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
Expand Down
22 changes: 22 additions & 0 deletions azurerm/internal/services/sql/validate/sql_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/sql/parse"
)

func SqlDatabaseID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := parse.SqlDatabaseID(v); err != nil {
errors = append(errors, fmt.Errorf("can not parse %q as a Sql Database resource id: %v", k, err))
return
}

return warnings, errors
}
26 changes: 18 additions & 8 deletions azurerm/internal/services/synapse/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
)

type Client struct {
FirewallRulesClient *synapse.IPFirewallRulesClient
SparkPoolClient *synapse.BigDataPoolsClient
WorkspaceClient *synapse.WorkspacesClient
WorkspaceAadAdminsClient *synapse.WorkspaceAadAdminsClient
FirewallRulesClient *synapse.IPFirewallRulesClient
SparkPoolClient *synapse.BigDataPoolsClient
SqlPoolClient *synapse.SQLPoolsClient
SqlPoolTransparentDataEncryptionClient *synapse.SQLPoolTransparentDataEncryptionsClient
WorkspaceClient *synapse.WorkspacesClient
WorkspaceAadAdminsClient *synapse.WorkspaceAadAdminsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -20,16 +22,24 @@ func NewClient(o *common.ClientOptions) *Client {
sparkPoolClient := synapse.NewBigDataPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sparkPoolClient.Client, o.ResourceManagerAuthorizer)

sqlPoolClient := synapse.NewSQLPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sqlPoolClient.Client, o.ResourceManagerAuthorizer)

sqlPoolTransparentDataEncryptionClient := synapse.NewSQLPoolTransparentDataEncryptionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sqlPoolTransparentDataEncryptionClient.Client, o.ResourceManagerAuthorizer)

workspaceClient := synapse.NewWorkspacesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&workspaceClient.Client, o.ResourceManagerAuthorizer)

workspaceAadAdminsClient := synapse.NewWorkspaceAadAdminsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&workspaceAadAdminsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
FirewallRulesClient: &firewallRuleClient,
SparkPoolClient: &sparkPoolClient,
WorkspaceClient: &workspaceClient,
WorkspaceAadAdminsClient: &workspaceAadAdminsClient,
FirewallRulesClient: &firewallRuleClient,
SparkPoolClient: &sparkPoolClient,
SqlPoolClient: &sqlPoolClient,
SqlPoolTransparentDataEncryptionClient: &sqlPoolTransparentDataEncryptionClient,
WorkspaceClient: &workspaceClient,
WorkspaceAadAdminsClient: &workspaceAadAdminsClient,
}
}
37 changes: 37 additions & 0 deletions azurerm/internal/services/synapse/parse/synapse_sql_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SynapseSqlPoolId struct {
Workspace *SynapseWorkspaceId
Name string
}

func SynapseSqlPoolID(input string) (*SynapseSqlPoolId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Synapse Sql Pool ID %q: %+v", input, err)
}

synapseSqlPool := SynapseSqlPoolId{
Workspace: &SynapseWorkspaceId{
SubscriptionID: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
},
}
if synapseSqlPool.Workspace.Name, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if synapseSqlPool.Name, err = id.PopSegment("sqlPools"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &synapseSqlPool, nil
}
Loading

0 comments on commit 87114b2

Please sign in to comment.