Skip to content

Commit

Permalink
New Resource: azurerm_synapse_workspace_sql_aad_admin (#14341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu authored Dec 16, 2021
1 parent 3fe9778 commit 2c96545
Show file tree
Hide file tree
Showing 11 changed files with 695 additions and 8 deletions.
75 changes: 75 additions & 0 deletions internal/services/synapse/parse/workspace_sql_aad_admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type WorkspaceSqlAADAdminId struct {
SubscriptionId string
ResourceGroup string
WorkspaceName string
SqlAdministratorName string
}

func NewWorkspaceSqlAADAdminID(subscriptionId, resourceGroup, workspaceName, sqlAdministratorName string) WorkspaceSqlAADAdminId {
return WorkspaceSqlAADAdminId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
WorkspaceName: workspaceName,
SqlAdministratorName: sqlAdministratorName,
}
}

func (id WorkspaceSqlAADAdminId) String() string {
segments := []string{
fmt.Sprintf("Sql Administrator Name %q", id.SqlAdministratorName),
fmt.Sprintf("Workspace Name %q", id.WorkspaceName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Workspace Sql A A D Admin", segmentsStr)
}

func (id WorkspaceSqlAADAdminId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Synapse/workspaces/%s/sqlAdministrators/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.WorkspaceName, id.SqlAdministratorName)
}

// WorkspaceSqlAADAdminID parses a WorkspaceSqlAADAdmin ID into an WorkspaceSqlAADAdminId struct
func WorkspaceSqlAADAdminID(input string) (*WorkspaceSqlAADAdminId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := WorkspaceSqlAADAdminId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.WorkspaceName, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if resourceId.SqlAdministratorName, err = id.PopSegment("sqlAdministrators"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
128 changes: 128 additions & 0 deletions internal/services/synapse/parse/workspace_sql_aad_admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

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

var _ resourceid.Formatter = WorkspaceSqlAADAdminId{}

func TestWorkspaceSqlAADAdminIDFormatter(t *testing.T) {
actual := NewWorkspaceSqlAADAdminID("12345678-1234-9876-4563-123456789012", "resourceGroup1", "workspace1", "activeDirectory").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/workspace1/sqlAdministrators/activeDirectory"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestWorkspaceSqlAADAdminID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *WorkspaceSqlAADAdminId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing WorkspaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/",
Error: true,
},

{
// missing value for WorkspaceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/",
Error: true,
},

{
// missing SqlAdministratorName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/workspace1/",
Error: true,
},

{
// missing value for SqlAdministratorName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/workspace1/sqlAdministrators/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/workspace1/sqlAdministrators/activeDirectory",
Expected: &WorkspaceSqlAADAdminId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resourceGroup1",
WorkspaceName: "workspace1",
SqlAdministratorName: "activeDirectory",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESOURCEGROUP1/PROVIDERS/MICROSOFT.SYNAPSE/WORKSPACES/WORKSPACE1/SQLADMINISTRATORS/ACTIVEDIRECTORY",
Error: true,
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.WorkspaceName != v.Expected.WorkspaceName {
t.Fatalf("Expected %q but got %q for WorkspaceName", v.Expected.WorkspaceName, actual.WorkspaceName)
}
if actual.SqlAdministratorName != v.Expected.SqlAdministratorName {
t.Fatalf("Expected %q but got %q for SqlAdministratorName", v.Expected.SqlAdministratorName, actual.SqlAdministratorName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/synapse/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
"azurerm_synapse_workspace_extended_auditing_policy": resourceSynapseWorkspaceExtendedAuditingPolicy(),
"azurerm_synapse_workspace_key": resourceSynapseWorkspaceKey(),
"azurerm_synapse_workspace_security_alert_policy": resourceSynapseWorkspaceSecurityAlertPolicy(),
"azurerm_synapse_workspace_sql_aad_admin": resourceSynapseWorkspaceSqlAADAdmin(),
"azurerm_synapse_workspace_vulnerability_assessment": resourceSynapseWorkspaceVulnerabilityAssessment(),
}
}
1 change: 1 addition & 0 deletions internal/services/synapse/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ package synapse
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WorkspaceExtendedAuditingPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/extendedAuditingSettings/default
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WorkspaceKeys -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/keys/key1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WorkspaceSecurityAlertPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/securityAlertPolicies/Default
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WorkspaceSqlAADAdmin -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resourceGroup1/providers/Microsoft.Synapse/workspaces/workspace1/sqlAdministrators/activeDirectory
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WorkspaceVulnerabilityAssessment -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/workspaces/workspace1/vulnerabilityAssessments/default
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ func resourceSynapseWorkspaceAADAdmin() *pluginsdk.Resource {
},

"login": {
Type: pluginsdk.TypeString,
Required: true,
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"object_id": {
Expand Down
13 changes: 7 additions & 6 deletions internal/services/synapse/synapse_workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ func resourceSynapseWorkspace() *pluginsdk.Resource {
},

"sql_aad_admin": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
ConflictsWith: []string{"customer_managed_key"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"login": {
Expand Down Expand Up @@ -307,7 +308,7 @@ func resourceSynapseWorkspace() *pluginsdk.Resource {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"aad_admin"},
ConflictsWith: []string{"aad_admin", "sql_aad_admin"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"key_versionless_id": {
Expand Down
Loading

0 comments on commit 2c96545

Please sign in to comment.