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

New resource: azurerm_sentinel_data_connector_office_irm #18856

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions internal/services/sentinel/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ func (r Registration) Resources() []sdk.Resource {
WatchlistResource{},
WatchlistItemResource{},
DataConnectorAwsS3Resource{},
DataConnectorOfficeIRMResource{},
}
}
2 changes: 2 additions & 0 deletions internal/services/sentinel/sentinel_data_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func assertDataConnectorKind(dc securityinsight.BasicDataConnector, expectKind s
kind = securityinsight.DataConnectorKindMicrosoftCloudAppSecurity
case securityinsight.TIDataConnector:
kind = securityinsight.DataConnectorKindThreatIntelligence
case securityinsight.OfficeIRMDataConnector:
kind = securityinsight.DataConnectorKindOfficeIRM
case securityinsight.OfficeDataConnector:
kind = securityinsight.DataConnectorKindOffice365
case securityinsight.OfficeATPDataConnector:
Expand Down
194 changes: 194 additions & 0 deletions internal/services/sentinel/sentinel_data_connector_office_irm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package sentinel

import (
"context"
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/securityinsight/mgmt/2022-01-01-preview/securityinsight"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type DataConnectorOfficeIRMResource struct{}

var _ sdk.ResourceWithCustomImporter = DataConnectorOfficeIRMResource{}

type DataConnectorOfficeIRMModel struct {
Name string `tfschema:"name"`
LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"`
TenantId string `tfschema:"tenant_id"`
}

func (r DataConnectorOfficeIRMResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: workspaces.ValidateWorkspaceID,
},

"tenant_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
}
}

func (r DataConnectorOfficeIRMResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r DataConnectorOfficeIRMResource) ResourceType() string {
return "azurerm_sentinel_data_connector_office_irm"
}

func (r DataConnectorOfficeIRMResource) ModelObject() interface{} {
return &DataConnectorOfficeIRMModel{}
}

func (r DataConnectorOfficeIRMResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.DataConnectorID
}

func (r DataConnectorOfficeIRMResource) CustomImporter() sdk.ResourceRunFunc {
return func(ctx context.Context, metadata sdk.ResourceMetaData) error {
_, err := importSentinelDataConnector(securityinsight.DataConnectorKindOfficeIRM)(ctx, metadata.ResourceData, metadata.Client)
return err
}
}

func (r DataConnectorOfficeIRMResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Sentinel.DataConnectorsClient

var plan DataConnectorOfficeIRMModel
if err := metadata.Decode(&plan); err != nil {
return fmt.Errorf("decoding %+v", err)
}

workspaceId, err := workspaces.ParseWorkspaceID(plan.LogAnalyticsWorkspaceId)
if err != nil {
return err
}

id := parse.NewDataConnectorID(workspaceId.SubscriptionId, workspaceId.ResourceGroupName, workspaceId.WorkspaceName, plan.Name)
existing, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

tenantId := plan.TenantId
if tenantId == "" {
tenantId = metadata.Client.Account.TenantId
}

params := securityinsight.OfficeIRMDataConnector{
Name: &plan.Name,
OfficeIRMDataConnectorProperties: &securityinsight.OfficeIRMDataConnectorProperties{
TenantID: &tenantId,
DataTypes: &securityinsight.AlertsDataTypeOfDataConnector{
Alerts: &securityinsight.DataConnectorDataTypeCommon{
State: securityinsight.DataTypeStateEnabled,
},
},
},
Kind: securityinsight.KindBasicDataConnectorKindOfficeIRM,
}

if _, err = client.CreateOrUpdate(ctx, id.ResourceGroup, id.WorkspaceName, id.Name, params); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r DataConnectorOfficeIRMResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,

Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Sentinel.DataConnectorsClient
id, err := parse.DataConnectorID(metadata.ResourceData.Id())
if err != nil {
return err
}

workspaceId := workspaces.NewWorkspaceID(id.SubscriptionId, id.ResourceGroup, id.WorkspaceName)

existing, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(existing.Response) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

dc, ok := existing.Value.(securityinsight.OfficeIRMDataConnector)
if !ok {
return fmt.Errorf("%s was not an Office IRM Data Connector", id)
}

var tenantId string
if props := dc.OfficeIRMDataConnectorProperties; props != nil {
if props.TenantID != nil {
tenantId = *props.TenantID
}
}

model := DataConnectorOfficeIRMModel{
Name: id.Name,
LogAnalyticsWorkspaceId: workspaceId.ID(),
TenantId: tenantId,
}

return metadata.Encode(&model)
},
}
}

func (r DataConnectorOfficeIRMResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Sentinel.DataConnectorsClient

id, err := parse.DataConnectorID(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.WorkspaceName, id.Name); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package sentinel_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type SentinelDataConnectorOfficeIRMResource struct{}

func TestAccSentinelDataConnectorOfficeIRM_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_irm", "test")
r := SentinelDataConnectorOfficeIRMResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSentinelDataConnectorOfficeIRM_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_irm", "test")
r := SentinelDataConnectorOfficeIRMResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSentinelDataConnectorOfficeIRM_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_irm", "test")
r := SentinelDataConnectorOfficeIRMResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func (r SentinelDataConnectorOfficeIRMResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
client := clients.Sentinel.DataConnectorsClient

id, err := parse.DataConnectorID(state.ID)
if err != nil {
return nil, err
}

if resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name); err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}

return utils.Bool(true), nil
}

func (r SentinelDataConnectorOfficeIRMResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_sentinel_data_connector_office_irm" "test" {
name = "accTestDC-%d"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
depends_on = [azurerm_log_analytics_solution.test]
}
`, template, data.RandomInteger)
}

func (r SentinelDataConnectorOfficeIRMResource) complete(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

data "azurerm_client_config" "test" {}

resource "azurerm_sentinel_data_connector_office_irm" "test" {
name = "accTestDC-%d"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
tenant_id = data.azurerm_client_config.test.tenant_id
depends_on = [azurerm_log_analytics_solution.test]
}
`, template, data.RandomInteger)
}

func (r SentinelDataConnectorOfficeIRMResource) requiresImport(data acceptance.TestData) string {
template := r.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_sentinel_data_connector_office_irm" "import" {
name = azurerm_sentinel_data_connector_office_irm.test.name
log_analytics_workspace_id = azurerm_sentinel_data_connector_office_irm.test.log_analytics_workspace_id
}
`, template)
}

func (r SentinelDataConnectorOfficeIRMResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-sentinel-%d"
location = "%s"
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
}

resource "azurerm_log_analytics_solution" "test" {
solution_name = "SecurityInsights"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
workspace_resource_id = azurerm_log_analytics_workspace.test.id
workspace_name = azurerm_log_analytics_workspace.test.name

plan {
publisher = "Microsoft"
product = "OMSGallery/SecurityInsights"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Loading