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_365_project #18858

Merged
Show file tree
Hide file tree
Changes from 2 commits
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{},
DataConnectorOffice365ProjectResource{},
}
}
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.Office365ProjectDataConnector:
kind = securityinsight.DataConnectorKindOffice365Project
case securityinsight.OfficeDataConnector:
kind = securityinsight.DataConnectorKindOffice365
case securityinsight.OfficeATPDataConnector:
Expand Down
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 DataConnectorOffice365ProjectResource struct{}

var _ sdk.ResourceWithCustomImporter = DataConnectorOffice365ProjectResource{}

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

func (r DataConnectorOffice365ProjectResource) 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 DataConnectorOffice365ProjectResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r DataConnectorOffice365ProjectResource) ResourceType() string {
return "azurerm_sentinel_data_connector_office_365_project"
}

func (r DataConnectorOffice365ProjectResource) ModelObject() interface{} {
return &DataConnectorOffice365ProjectModel{}
}

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

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

func (r DataConnectorOffice365ProjectResource) 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 DataConnectorOffice365ProjectModel
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.Office365ProjectDataConnector{
Name: &plan.Name,
Office365ProjectDataConnectorProperties: &securityinsight.Office365ProjectDataConnectorProperties{
TenantID: &tenantId,
DataTypes: &securityinsight.Office365ProjectConnectorDataTypes{
Logs: &securityinsight.Office365ProjectConnectorDataTypesLogs{
State: securityinsight.DataTypeStateEnabled,
},
},
},
Kind: securityinsight.KindBasicDataConnectorKindOffice365Project,
}

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 DataConnectorOffice365ProjectResource) 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.Office365ProjectDataConnector)
if !ok {
return fmt.Errorf("%s was not an Office 365 Project Data Connector", id)
}

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

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

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

func (r DataConnectorOffice365ProjectResource) 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 SentinelDataConnectorOffice365ProjectResource struct{}

func TestAccSentinelDataConnectorOffice365Project_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_365_project", "test")
r := SentinelDataConnectorOffice365ProjectResource{}

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

func TestAccSentinelDataConnectorOffice365Project_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_365_project", "test")
r := SentinelDataConnectorOffice365ProjectResource{}

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

func TestAccSentinelDataConnectorOffice365Project_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_office_365_project", "test")
r := SentinelDataConnectorOffice365ProjectResource{}

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 SentinelDataConnectorOffice365ProjectResource) 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 SentinelDataConnectorOffice365ProjectResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_sentinel_data_connector_office_365_project" "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 SentinelDataConnectorOffice365ProjectResource) complete(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

data "azurerm_client_config" "test" {}

resource "azurerm_sentinel_data_connector_office_365_project" "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 SentinelDataConnectorOffice365ProjectResource) requiresImport(data acceptance.TestData) string {
template := r.basic(data)
return fmt.Sprintf(`
%s

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

func (r SentinelDataConnectorOffice365ProjectResource) 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