diff --git a/internal/services/sentinel/registration.go b/internal/services/sentinel/registration.go index 21fa0aaada6d..c57a0e159798 100644 --- a/internal/services/sentinel/registration.go +++ b/internal/services/sentinel/registration.go @@ -63,6 +63,7 @@ func (r Registration) Resources() []sdk.Resource { WatchlistResource{}, WatchlistItemResource{}, DataConnectorAwsS3Resource{}, + DataConnectorIOTResource{}, DataConnectorDynamics365Resource{}, DataConnectorOffice365ProjectResource{}, DataConnectorOfficePowerBIResource{}, diff --git a/internal/services/sentinel/sentinel_data_connector.go b/internal/services/sentinel/sentinel_data_connector.go index 5b82d21e1693..1c80fc5a70d0 100644 --- a/internal/services/sentinel/sentinel_data_connector.go +++ b/internal/services/sentinel/sentinel_data_connector.go @@ -43,6 +43,8 @@ func assertDataConnectorKind(dc securityinsight.BasicDataConnector, expectKind s kind = securityinsight.DataConnectorKindMicrosoftCloudAppSecurity case securityinsight.TIDataConnector: kind = securityinsight.DataConnectorKindThreatIntelligence + case securityinsight.IoTDataConnector: + kind = securityinsight.DataConnectorKindIOT case securityinsight.Dynamics365DataConnector: kind = securityinsight.DataConnectorKindDynamics365 case securityinsight.Office365ProjectDataConnector: diff --git a/internal/services/sentinel/sentinel_data_connector_iot.go b/internal/services/sentinel/sentinel_data_connector_iot.go new file mode 100644 index 000000000000..9b3102ea1808 --- /dev/null +++ b/internal/services/sentinel/sentinel_data_connector_iot.go @@ -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 DataConnectorIOTResource struct{} + +var _ sdk.ResourceWithCustomImporter = DataConnectorIOTResource{} + +type DataConnectorIOTModel struct { + Name string `tfschema:"name"` + LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"` + SubscriptionId string `tfschema:"subscription_id"` +} + +func (r DataConnectorIOTResource) 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, + }, + + "subscription_id": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validation.IsUUID, + }, + } +} + +func (r DataConnectorIOTResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{} +} + +func (r DataConnectorIOTResource) ResourceType() string { + return "azurerm_sentinel_data_connector_iot" +} + +func (r DataConnectorIOTResource) ModelObject() interface{} { + return &DataConnectorIOTModel{} +} + +func (r DataConnectorIOTResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return validate.DataConnectorID +} + +func (r DataConnectorIOTResource) CustomImporter() sdk.ResourceRunFunc { + return func(ctx context.Context, metadata sdk.ResourceMetaData) error { + _, err := importSentinelDataConnector(securityinsight.DataConnectorKindIOT)(ctx, metadata.ResourceData, metadata.Client) + return err + } +} + +func (r DataConnectorIOTResource) 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 DataConnectorIOTModel + 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) + } + + subscriptionId := plan.SubscriptionId + if subscriptionId == "" { + subscriptionId = metadata.Client.Account.SubscriptionId + } + + params := securityinsight.IoTDataConnector{ + Name: &plan.Name, + IoTDataConnectorProperties: &securityinsight.IoTDataConnectorProperties{ + SubscriptionID: &subscriptionId, + DataTypes: &securityinsight.AlertsDataTypeOfDataConnector{ + Alerts: &securityinsight.DataConnectorDataTypeCommon{ + State: securityinsight.DataTypeStateEnabled, + }, + }, + }, + Kind: securityinsight.KindBasicDataConnectorKindIOT, + } + + 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 DataConnectorIOTResource) 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.IoTDataConnector) + if !ok { + return fmt.Errorf("%s was not an IoT Data Connector", id) + } + + var subscriptionId string + if props := dc.IoTDataConnectorProperties; props != nil { + if props.SubscriptionID != nil { + subscriptionId = *props.SubscriptionID + } + } + + model := DataConnectorIOTModel{ + Name: id.Name, + LogAnalyticsWorkspaceId: workspaceId.ID(), + SubscriptionId: subscriptionId, + } + + return metadata.Encode(&model) + }, + } +} + +func (r DataConnectorIOTResource) 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 + }, + } +} diff --git a/internal/services/sentinel/sentinel_data_connector_iot_test.go b/internal/services/sentinel/sentinel_data_connector_iot_test.go new file mode 100644 index 000000000000..4da3163711b6 --- /dev/null +++ b/internal/services/sentinel/sentinel_data_connector_iot_test.go @@ -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 SentinelDataConnectorIOTResource struct{} + +func TestAccSentinelDataConnectorIOT_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_iot", "test") + r := SentinelDataConnectorIOTResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccSentinelDataConnectorIOT_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_iot", "test") + r := SentinelDataConnectorIOTResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccSentinelDataConnectorIOT_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_sentinel_data_connector_iot", "test") + r := SentinelDataConnectorIOTResource{} + + 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 SentinelDataConnectorIOTResource) 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 SentinelDataConnectorIOTResource) basic(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +resource "azurerm_sentinel_data_connector_iot" "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 SentinelDataConnectorIOTResource) complete(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +data "azurerm_client_config" "test" {} + +resource "azurerm_sentinel_data_connector_iot" "test" { + name = "accTestDC-%d" + log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id + subscription_id = data.azurerm_client_config.test.subscription_id + depends_on = [azurerm_log_analytics_solution.test] +} +`, template, data.RandomInteger) +} + +func (r SentinelDataConnectorIOTResource) requiresImport(data acceptance.TestData) string { + template := r.basic(data) + return fmt.Sprintf(` +%s + +resource "azurerm_sentinel_data_connector_iot" "import" { + name = azurerm_sentinel_data_connector_iot.test.name + log_analytics_workspace_id = azurerm_sentinel_data_connector_iot.test.log_analytics_workspace_id +} +`, template) +} + +func (r SentinelDataConnectorIOTResource) 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) +} diff --git a/website/docs/r/sentinel_data_connector_iot.html.markdown b/website/docs/r/sentinel_data_connector_iot.html.markdown new file mode 100644 index 000000000000..5868b737f451 --- /dev/null +++ b/website/docs/r/sentinel_data_connector_iot.html.markdown @@ -0,0 +1,79 @@ +--- +subcategory: "Sentinel" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_sentinel_data_connector_iot" +description: |- + Manages an Iot Data Connector. +--- + +# azurerm_sentinel_data_connector_iot + +Manages an Iot Data Connector. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-rg" + location = "West Europe" +} + +resource "azurerm_log_analytics_workspace" "example" { + name = "example-workspace" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + sku = "PerGB2018" +} + +resource "azurerm_log_analytics_solution" "example" { + solution_name = "SecurityInsights" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + workspace_resource_id = azurerm_log_analytics_workspace.example.id + workspace_name = azurerm_log_analytics_workspace.example.name + + plan { + publisher = "Microsoft" + product = "OMSGallery/SecurityInsights" + } +} + +resource "azurerm_sentinel_data_connector_iot" "example" { + name = "example" + log_analytics_workspace_id = azurerm_log_analytics_solution.example.workspace_resource_id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `log_analytics_workspace_id` - (Required) The ID of the Log Analytics Workspace that this Iot Data Connector resides in. Changing this forces a new Iot Data Connector to be created. + +* `name` - (Required) The name which should be used for this Iot Data Connector. Changing this forces a new Iot Data Connector to be created. + +--- + +* `subscription_id` - (Optional) The ID of the subscription that this Iot Data Connector connects to. Changing this forces a new Iot Data Connector to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Iot Data Connector. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the Iot Data Connector. +* `read` - (Defaults to 5 minutes) Used when retrieving the Iot Data Connector. +* `delete` - (Defaults to 30 minutes) Used when deleting the Iot Data Connector. + +## Import + +Iot Data Connectors can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_sentinel_data_connector_iot.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.OperationalInsights/workspaces/workspace1/providers/Microsoft.SecurityInsights/dataConnectors/dc1 +```