Skip to content

Commit

Permalink
synapse_workspace supports sql_aad_admin (#13659)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu authored Nov 2, 2021
1 parent 63623c1 commit 1d3964c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/services/synapse/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Client struct {
WorkspaceExtendedBlobAuditingPoliciesClient *synapse.WorkspaceManagedSQLServerExtendedBlobAuditingPoliciesClient
WorkspaceManagedIdentitySQLControlSettingsClient *synapse.WorkspaceManagedIdentitySQLControlSettingsClient
WorkspaceSecurityAlertPolicyClient *synapse.WorkspaceManagedSQLServerSecurityAlertPolicyClient
WorkspaceSQLAadAdminsClient *synapse.WorkspaceSQLAadAdminsClient
WorkspaceVulnerabilityAssessmentsClient *synapse.WorkspaceManagedSQLServerVulnerabilityAssessmentsClient

synapseAuthorizer autorest.Authorizer
Expand Down Expand Up @@ -87,6 +88,9 @@ func NewClient(o *common.ClientOptions) *Client {
workspaceSecurityAlertPolicyClient := synapse.NewWorkspaceManagedSQLServerSecurityAlertPolicyClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&workspaceSecurityAlertPolicyClient.Client, o.ResourceManagerAuthorizer)

workspaceSQLAadAdminsClient := synapse.NewWorkspaceSQLAadAdminsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&workspaceSQLAadAdminsClient.Client, o.ResourceManagerAuthorizer)

workspaceVulnerabilityAssessmentsClient := synapse.NewWorkspaceManagedSQLServerVulnerabilityAssessmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&workspaceVulnerabilityAssessmentsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -108,6 +112,7 @@ func NewClient(o *common.ClientOptions) *Client {
WorkspaceExtendedBlobAuditingPoliciesClient: &workspaceExtendedBlobAuditingPoliciesClient,
WorkspaceManagedIdentitySQLControlSettingsClient: &workspaceManagedIdentitySQLControlSettingsClient,
WorkspaceSecurityAlertPolicyClient: &workspaceSecurityAlertPolicyClient,
WorkspaceSQLAadAdminsClient: &workspaceSQLAadAdminsClient,
WorkspaceVulnerabilityAssessmentsClient: &workspaceVulnerabilityAssessmentsClient,

synapseAuthorizer: o.SynapseAuthorizer,
Expand Down
75 changes: 75 additions & 0 deletions internal/services/synapse/synapse_workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,34 @@ func resourceSynapseWorkspace() *pluginsdk.Resource {
},
},

"sql_aad_admin": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"login": {
Type: pluginsdk.TypeString,
Required: true,
},

"object_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},

"tenant_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
},
},
},

"connectivity_endpoints": {
Type: pluginsdk.TypeMap,
Computed: true,
Expand Down Expand Up @@ -304,6 +332,7 @@ func resourceSynapseWorkspace() *pluginsdk.Resource {
func resourceSynapseWorkspaceCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.WorkspaceClient
aadAdminClient := meta.(*clients.Client).Synapse.WorkspaceAadAdminsClient
sqlAdminClient := meta.(*clients.Client).Synapse.WorkspaceSQLAadAdminsClient
identitySQLControlClient := meta.(*clients.Client).Synapse.WorkspaceManagedIdentitySQLControlSettingsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -397,6 +426,18 @@ func resourceSynapseWorkspaceCreate(d *pluginsdk.ResourceData, meta interface{})
}
}

sqlAdmin := expandArmWorkspaceAadAdmin(d.Get("sql_aad_admin").([]interface{}))
if sqlAdmin != nil {
workspaceSqlAdminsCreateOrUpdateFuture, err := sqlAdminClient.CreateOrUpdate(ctx, resourceGroup, name, *aadAdmin)
if err != nil {
return fmt.Errorf("updating Synapse Workspace %q Sql Admin (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = workspaceSqlAdminsCreateOrUpdateFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on updating for Synapse Workspace %q Sql Admin (Resource Group %q): %+v", name, resourceGroup, err)
}
}

sqlControlSettings := expandIdentityControlSQLSettings(d.Get("sql_identity_control_enabled").(bool))
if _, err = identitySQLControlClient.CreateOrUpdate(ctx, resourceGroup, name, *sqlControlSettings); err != nil {
return fmt.Errorf("Granting workspace identity control for SQL pool: %+v", err)
Expand All @@ -412,6 +453,7 @@ func resourceSynapseWorkspaceCreate(d *pluginsdk.ResourceData, meta interface{})
func resourceSynapseWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.WorkspaceClient
aadAdminClient := meta.(*clients.Client).Synapse.WorkspaceAadAdminsClient
sqlAdminClient := meta.(*clients.Client).Synapse.WorkspaceSQLAadAdminsClient
identitySQLControlClient := meta.(*clients.Client).Synapse.WorkspaceManagedIdentitySQLControlSettingsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand All @@ -437,6 +479,12 @@ func resourceSynapseWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) e
return fmt.Errorf("retrieving Synapse Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
sqlAdmin, err := sqlAdminClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(sqlAdmin.Response) {
return fmt.Errorf("retrieving Synapse Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}

sqlControlSettings, err := identitySQLControlClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
Expand Down Expand Up @@ -490,6 +538,9 @@ func resourceSynapseWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) e
if err := d.Set("aad_admin", flattenArmWorkspaceAadAdmin(aadAdmin.AadAdminProperties)); err != nil {
return fmt.Errorf("setting `aad_admin`: %+v", err)
}
if err := d.Set("sql_aad_admin", flattenArmWorkspaceAadAdmin(sqlAdmin.AadAdminProperties)); err != nil {
return fmt.Errorf("setting `sql_aad_admin`: %+v", err)
}
if err := d.Set("sql_identity_control_enabled", flattenIdentityControlSQLSettings(sqlControlSettings)); err != nil {
return fmt.Errorf("setting `sql_identity_control_enabled`: %+v", err)
}
Expand All @@ -500,6 +551,7 @@ func resourceSynapseWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) e
func resourceSynapseWorkspaceUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.WorkspaceClient
aadAdminClient := meta.(*clients.Client).Synapse.WorkspaceAadAdminsClient
sqlAdminClient := meta.(*clients.Client).Synapse.WorkspaceSQLAadAdminsClient
identitySQLControlClient := meta.(*clients.Client).Synapse.WorkspaceManagedIdentitySQLControlSettingsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -570,6 +622,29 @@ func resourceSynapseWorkspaceUpdate(d *pluginsdk.ResourceData, meta interface{})
}
}

if d.HasChange("sql_aad_admin") {
sqlAdmin := expandArmWorkspaceAadAdmin(d.Get("sql_aad_admin").([]interface{}))
if sqlAdmin != nil {
workspaceSqlAdminsCreateOrUpdateFuture, err := sqlAdminClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, *sqlAdmin)
if err != nil {
return fmt.Errorf("updating Synapse Workspace %q Sql Admin (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = workspaceSqlAdminsCreateOrUpdateFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on updating for Synapse Workspace %q Sql Admin (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
} else {
workspaceSqlAdminsDeleteFuture, err := sqlAdminClient.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("setting empty Synapse Workspace %q Sql Admin (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = workspaceSqlAdminsDeleteFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on setting empty Synapse Workspace %q Sql Admin (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
}

if d.HasChange("sql_identity_control_enabled") {
sqlControlSettings := expandIdentityControlSQLSettings(d.Get("sql_identity_control_enabled").(bool))
if _, err = identitySQLControlClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, *sqlControlSettings); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/services/synapse/synapse_workspace_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ resource "azurerm_synapse_workspace" "test" {
tenant_id = data.azurerm_client_config.current.tenant_id
}
sql_aad_admin {
login = "AzureAD Admin"
object_id = data.azurerm_client_config.current.object_id
tenant_id = data.azurerm_client_config.current.tenant_id
}
tags = {
ENV = "Test2"
}
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/synapse_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ The following arguments are supported:

* `customer_managed_key` - (Optional) A `customer_managed_key` block as defined below.

* `sql_aad_admin` - (Optional) An `sql_aad_admin` block as defined below.

* `tags` - (Optional) A mapping of tags which should be assigned to the Synapse Workspace.

---
Expand All @@ -107,6 +109,16 @@ An `aad_admin` block supports the following:

---

An `sql_aad_admin` block supports the following:

* `login` - (Required) The login name of the Azure AD Administrator of this Synapse Workspace SQL.

* `object_id` - (Required) The object id of the Azure AD Administrator of this Synapse Workspace SQL.

* `tenant_id` - (Required) The tenant id of the Azure AD Administrator of this Synapse Workspace SQL.

---

An `azure_devops_repo` block supports the following:

* `account_name` - (Required) Specifies the Azure DevOps account name.
Expand Down

0 comments on commit 1d3964c

Please sign in to comment.