-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource
azurerm_mssql_server_extended_auditing_policy
(#8447)
- Loading branch information
Showing
7 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
azurerm/internal/services/mssql/mssql_server_extended_auditing_policy_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package mssql | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql" | ||
"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/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql/validate" | ||
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" | ||
) | ||
|
||
func resourceArmMsSqlServerExtendedAuditingPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmMsSqlServerExtendedAuditingPolicyCreateUpdate, | ||
Read: resourceArmMsSqlServerExtendedAuditingPolicyRead, | ||
Update: resourceArmMsSqlServerExtendedAuditingPolicyCreateUpdate, | ||
Delete: resourceArmMsSqlServerExtendedAuditingPolicyDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := parse.MssqlServerExtendedAuditingPolicyID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"server_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.MsSqlServerID, | ||
}, | ||
|
||
"storage_endpoint": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.IsURLWithHTTPS, | ||
}, | ||
|
||
"storage_account_access_key": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Sensitive: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"storage_account_access_key_is_secondary": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
|
||
"retention_in_days": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 0, | ||
ValidateFunc: validation.IntBetween(0, 3285), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmMsSqlServerExtendedAuditingPolicyCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Printf("[INFO] preparing arguments for MsSql Server Extended Auditing Policy creation.") | ||
|
||
serverId, err := parse.MsSqlServerID(d.Get("server_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.IsNewResource() { | ||
existing, err := client.Get(ctx, serverId.ResourceGroup, serverId.Name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Failed to check for presence of existing Server %q Sql Auditing (Resource Group %q): %s", serverId.Name, serverId.ResourceGroup, err) | ||
} | ||
} | ||
|
||
// if state is not disabled, we should import it. | ||
if existing.ID != nil && *existing.ID != "" && existing.ExtendedServerBlobAuditingPolicyProperties != nil && existing.ExtendedServerBlobAuditingPolicyProperties.State != sql.BlobAuditingPolicyStateDisabled { | ||
return tf.ImportAsExistsError("azurerm_mssql_server_extended_auditing_policy", *existing.ID) | ||
} | ||
} | ||
|
||
params := sql.ExtendedServerBlobAuditingPolicy{ | ||
ExtendedServerBlobAuditingPolicyProperties: &sql.ExtendedServerBlobAuditingPolicyProperties{ | ||
State: sql.BlobAuditingPolicyStateEnabled, | ||
StorageEndpoint: utils.String(d.Get("storage_endpoint").(string)), | ||
IsStorageSecondaryKeyInUse: utils.Bool(d.Get("storage_account_access_key_is_secondary").(bool)), | ||
RetentionDays: utils.Int32(int32(d.Get("retention_in_days").(int))), | ||
}, | ||
} | ||
|
||
if v, ok := d.GetOk("storage_account_access_key"); ok { | ||
params.ExtendedServerBlobAuditingPolicyProperties.StorageAccountAccessKey = utils.String(v.(string)) | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, serverId.ResourceGroup, serverId.Name, params) | ||
if err != nil { | ||
return fmt.Errorf("creating MsSql Server %q Extended Auditing Policy (Resource Group %q): %+v", serverId.Name, serverId.ResourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for creation of MsSql Server %q Extended Auditing Policy (Resource Group %q): %+v", serverId.Name, serverId.ResourceGroup, err) | ||
} | ||
|
||
read, err := client.Get(ctx, serverId.ResourceGroup, serverId.Name) | ||
if err != nil { | ||
return fmt.Errorf("retrieving MsSql Server %q Extended Auditing Policy (Resource Group %q): %+v", serverId.Name, serverId.ResourceGroup, err) | ||
} | ||
|
||
if read.ID == nil || *read.ID == "" { | ||
return fmt.Errorf("reading MsSql Server %q Extended Auditing Policy (Resource Group %q) ID is empty or nil", serverId.Name, serverId.ResourceGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmMsSqlServerExtendedAuditingPolicyRead(d, meta) | ||
} | ||
|
||
func resourceArmMsSqlServerExtendedAuditingPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient | ||
serverClient := meta.(*clients.Client).MSSQL.ServersClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.MssqlServerExtendedAuditingPolicyID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.MsSqlServer) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("reading MsSql Server %s Extended Auditing Policy (Resource Group %q): %s", id.MsSqlServer, id.ResourceGroup, err) | ||
} | ||
|
||
serverResp, err := serverClient.Get(ctx, id.ResourceGroup, id.MsSqlServer) | ||
if err != nil || serverResp.ID == nil || *serverResp.ID == "" { | ||
return fmt.Errorf("reading MsSql Server %q ID is empty or nil(Resource Group %q): %s", id.MsSqlServer, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("server_id", serverResp.ID) | ||
|
||
if props := resp.ExtendedServerBlobAuditingPolicyProperties; props != nil { | ||
d.Set("storage_endpoint", props.StorageEndpoint) | ||
d.Set("storage_account_access_key_is_secondary", props.IsStorageSecondaryKeyInUse) | ||
d.Set("retention_in_days", props.RetentionDays) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmMsSqlServerExtendedAuditingPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.MssqlServerExtendedAuditingPolicyID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
params := sql.ExtendedServerBlobAuditingPolicy{ | ||
ExtendedServerBlobAuditingPolicyProperties: &sql.ExtendedServerBlobAuditingPolicyProperties{ | ||
State: sql.BlobAuditingPolicyStateDisabled, | ||
}, | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.MsSqlServer, params) | ||
if err != nil { | ||
return fmt.Errorf("deleting MsSql Server %q Extended Auditing Policy(Resource Group %q): %+v", id.MsSqlServer, id.ResourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for deletion of MsSql Server %q Extended Auditing Policy (Resource Group %q): %+v", id.MsSqlServer, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.