From c9889aa075ea16e105cdb431a438095a97f603bb Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 21 Oct 2021 08:49:30 +0200 Subject: [PATCH 1/8] new resource stream analytics reference input mssql --- .../services/streamanalytics/registration.go | 1 + .../stream_analytics_reference_input.go | 43 +++ ...nalytics_reference_input_mssql_resource.go | 244 ++++++++++++++++++ ...ics_reference_input_mssql_resource_test.go | 205 +++++++++++++++ 4 files changed, 493 insertions(+) create mode 100644 internal/services/streamanalytics/stream_analytics_reference_input.go create mode 100644 internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go create mode 100644 internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go diff --git a/internal/services/streamanalytics/registration.go b/internal/services/streamanalytics/registration.go index aba63b03e856..78e2ea45b312 100644 --- a/internal/services/streamanalytics/registration.go +++ b/internal/services/streamanalytics/registration.go @@ -36,6 +36,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_stream_analytics_output_servicebus_queue": resourceStreamAnalyticsOutputServiceBusQueue(), "azurerm_stream_analytics_output_servicebus_topic": resourceStreamAnalyticsOutputServiceBusTopic(), "azurerm_stream_analytics_reference_input_blob": resourceStreamAnalyticsReferenceInputBlob(), + "azurerm_stream_analytics_reference_input_mssql": resourceStreamAnalyticsReferenceMsSql(), "azurerm_stream_analytics_stream_input_blob": resourceStreamAnalyticsStreamInputBlob(), "azurerm_stream_analytics_stream_input_eventhub": resourceStreamAnalyticsStreamInputEventHub(), "azurerm_stream_analytics_stream_input_iothub": resourceStreamAnalyticsStreamInputIoTHub(), diff --git a/internal/services/streamanalytics/stream_analytics_reference_input.go b/internal/services/streamanalytics/stream_analytics_reference_input.go new file mode 100644 index 000000000000..8b6d65556ac5 --- /dev/null +++ b/internal/services/streamanalytics/stream_analytics_reference_input.go @@ -0,0 +1,43 @@ +package streamanalytics + +import ( + "context" + "fmt" + + "github.com/Azure/azure-sdk-for-go/services/preview/streamanalytics/mgmt/2020-03-01-preview/streamanalytics" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +func importStreamAnalyticsReferenceInput(expectType streamanalytics.TypeBasicReferenceInputDataSource) pluginsdk.ImporterFunc { + return func(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) (data []*pluginsdk.ResourceData, err error) { + id, err := parse.StreamInputID(d.Id()) + if err != nil { + return nil, err + } + + client := meta.(*clients.Client).StreamAnalytics.InputsClient + resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) + if err != nil { + return nil, fmt.Errorf("retrieving %s: %+v", *id, err) + } + + if props := resp.Properties; props != nil { + v, ok := props.AsReferenceInputProperties() + if !ok { + return nil, fmt.Errorf("converting Reference Input MsSql to a Reference Input: %+v", err) + } + + inputDataSource, ok := v.Datasource.AsAzureSQLReferenceInputDataSource() + if !ok { + return nil, fmt.Errorf("converting Reference Input MsSql to a MsSql Stream Input: %+v", err) + } + + if actualType := inputDataSource.Type; actualType != expectType { + return nil, fmt.Errorf("stream analytics reference input has mismatched type, expected: %q, got %q", expectType, actualType) + } + } + return []*pluginsdk.ResourceData{d}, nil + } +} diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go new file mode 100644 index 000000000000..3fc8b98e12c6 --- /dev/null +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go @@ -0,0 +1,244 @@ +package streamanalytics + +import ( + "fmt" + + "log" + "time" + + "github.com/Azure/azure-sdk-for-go/services/preview/streamanalytics/mgmt/2020-03-01-preview/streamanalytics" + "github.com/hashicorp/go-azure-helpers/response" + "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/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/internal/timeouts" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +func resourceStreamAnalyticsReferenceMsSql() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Create: resourceStreamAnalyticsReferenceInputMsSqlCreateUpdate, + Read: resourceStreamAnalyticsReferenceInputMsSqlRead, + Update: resourceStreamAnalyticsReferenceInputMsSqlCreateUpdate, + Delete: resourceStreamAnalyticsReferenceInputMsSqlDelete, + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { + _, err := parse.StreamInputID(id) + return err + }, importStreamAnalyticsReferenceInput(streamanalytics.TypeBasicReferenceInputDataSourceTypeMicrosoftSQLServerDatabase)), + + Timeouts: &pluginsdk.ResourceTimeout{ + Create: pluginsdk.DefaultTimeout(30 * time.Minute), + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + Update: pluginsdk.DefaultTimeout(30 * time.Minute), + Delete: pluginsdk.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "stream_analytics_job_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "server": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "database": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "username": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "password": { + Type: pluginsdk.TypeString, + Required: true, + Sensitive: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "refresh_type": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + "Static", + "RefreshPeriodicallyWithFull", + "RefreshPeriodicallyWithDelta", + }, false), + }, + + "refresh_rate": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validate.BatchMaxWaitTime, + }, + + "full_snapshot_query": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "delta_snapshot_query": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + } +} + +func resourceStreamAnalyticsReferenceInputMsSqlCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).StreamAnalytics.InputsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + log.Printf("[INFO] preparing arguments for Azure Stream Analytics Reference Input MsSql creation.") + id := parse.NewStreamInputID(subscriptionId, d.Get("resource_group_name").(string), d.Get("stream_analytics_job_name").(string), d.Get("name").(string)) + if d.IsNewResource() { + existing, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) + } + } + + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_stream_analytics_reference_input_mssql", id.ID()) + } + } + + refreshType := d.Get("refresh_type").(string) + + if _, ok := d.GetOk("refresh_rate"); refreshType != "Static" && !ok { + return fmt.Errorf("refresh_rate must be set if refresh_type is RefreshPeriodicallyWithFull or RefreshPeriodicallyWithDelta") + } else if _, ok = d.GetOk("delta_snapshot_query"); refreshType == "Static" && ok { + return fmt.Errorf("delta_snapshot_query cannot be set if refresh_type is Static") + } + + properties := &streamanalytics.AzureSQLReferenceInputDataSourceProperties{ + Server: utils.String(d.Get("server").(string)), + Database: utils.String(d.Get("database").(string)), + User: utils.String(d.Get("username").(string)), + Password: utils.String(d.Get("password").(string)), + RefreshType: utils.String(refreshType), + } + + if v, ok := d.GetOk("refresh_rate"); ok { + properties.RefreshRate = utils.String(v.(string)) + } + + if v, ok := d.GetOk("full_snapshot_query"); ok { + properties.FullSnapshotQuery = utils.String(v.(string)) + } + + if v, ok := d.GetOk("delta_snapshot_query"); ok { + properties.DeltaSnapshotQuery = utils.String(v.(string)) + } + + props := streamanalytics.Input{ + Name: utils.String(id.InputName), + Properties: &streamanalytics.ReferenceInputProperties{ + Type: streamanalytics.TypeReference, + Datasource: &streamanalytics.AzureSQLReferenceInputDataSource{ + Type: streamanalytics.TypeBasicReferenceInputDataSourceTypeMicrosoftSQLServerDatabase, + Properties: properties, + }, + }, + } + + if _, err := client.CreateOrReplace(ctx, props, id.ResourceGroup, id.StreamingjobName, id.InputName, "", ""); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + d.SetId(id.ID()) + return resourceStreamAnalyticsReferenceInputMsSqlRead(d, meta) +} + +func resourceStreamAnalyticsReferenceInputMsSqlRead(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).StreamAnalytics.InputsClient + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.StreamInputID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[DEBUG] %s was not found - removing from state!", *id) + d.SetId("") + return nil + } + + return fmt.Errorf("retrieving %s: %+v", *id, err) + } + + d.Set("name", id.InputName) + d.Set("stream_analytics_job_name", id.StreamingjobName) + d.Set("resource_group_name", id.ResourceGroup) + + if props := resp.Properties; props != nil { + v, ok := props.AsReferenceInputProperties() + if !ok { + return fmt.Errorf("converting Reference Input MsSql to a Reference Input: %+v", err) + } + + inputDataSource, ok := v.Datasource.AsAzureSQLReferenceInputDataSource() + if !ok { + return fmt.Errorf("converting Reference Input MsSql to a MsSql Stream Input: %+v", err) + } + + d.Set("server", inputDataSource.Properties.Server) + d.Set("database", inputDataSource.Properties.Database) + d.Set("username", inputDataSource.Properties.User) + d.Set("refresh_type", inputDataSource.Properties.RefreshType) + d.Set("refresh_rate", inputDataSource.Properties.RefreshRate) + d.Set("full_snapshot_query", inputDataSource.Properties.FullSnapshotQuery) + d.Set("delta_snapshot_query", inputDataSource.Properties.DeltaSnapshotQuery) + } + return nil +} + +func resourceStreamAnalyticsReferenceInputMsSqlDelete(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).StreamAnalytics.InputsClient + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.StreamInputID(d.Id()) + if err != nil { + return err + } + + if resp, err := client.Delete(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName); err != nil { + if !response.WasNotFound(resp.Response) { + return fmt.Errorf("deleting %s: %+v", *id, err) + } + } + return nil +} diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go new file mode 100644 index 000000000000..d043a6f36427 --- /dev/null +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go @@ -0,0 +1,205 @@ +package streamanalytics_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/streamanalytics/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type StreamAnalyticsReferenceInputMsSqlResource struct{} + +func TestAccStreamAnalyticsReferenceInputMsSql_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_stream_analytics_reference_input_mssql", "test") + r := StreamAnalyticsReferenceInputMsSqlResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("password"), + }) +} + +func TestAccStreamAnalyticsReferenceInputMsSql_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_stream_analytics_reference_input_mssql", "test") + r := StreamAnalyticsReferenceInputMsSqlResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + { + Config: r.updated(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("password"), + }) +} + +func TestAccStreamAnalyticsReferenceInputMsSql_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_stream_analytics_reference_input_mssql", "test") + r := StreamAnalyticsReferenceInputMsSqlResource{} + + 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 StreamAnalyticsReferenceInputMsSqlResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := parse.StreamInputID(state.ID) + if err != nil { + return nil, err + } + + resp, err := client.StreamAnalytics.InputsClient.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return utils.Bool(false), nil + } + return nil, fmt.Errorf("reading (%s): %+v", *id, err) + } + return utils.Bool(true), nil +} + +func (r StreamAnalyticsReferenceInputMsSqlResource) basic(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +resource "azurerm_stream_analytics_reference_input_mssql" "test" { + name = "acctestinput-%d" + stream_analytics_job_name = azurerm_stream_analytics_job.test.name + resource_group_name = azurerm_stream_analytics_job.test.resource_group_name + server = azurerm_mssql_server.test.fully_qualified_domain_name + database = azurerm_mssql_database.test.name + username = "maurice" + password = "ludicrousdisplay" + refresh_type = "RefreshPeriodicallyWithFull" + refresh_rate = "00:10:00" + full_snapshot_query = < Date: Thu, 21 Oct 2021 09:23:34 +0200 Subject: [PATCH 2/8] add docs --- ...nalytics_reference_input_mssql_resource.go | 5 +- ...alytics_reference_input_blob.html.markdown | 2 +- ...lytics_reference_input_mssql.html.markdown | 105 ++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 website/docs/r/stream_analytics_reference_input_mssql.html.markdown diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go index 3fc8b98e12c6..5a2f4bbec6a0 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go @@ -199,6 +199,7 @@ func resourceStreamAnalyticsReferenceInputMsSqlRead(d *pluginsdk.ResourceData, m return fmt.Errorf("retrieving %s: %+v", *id, err) } + d.SetId(id.ID()) d.Set("name", id.InputName) d.Set("stream_analytics_job_name", id.StreamingjobName) d.Set("resource_group_name", id.ResourceGroup) @@ -206,12 +207,12 @@ func resourceStreamAnalyticsReferenceInputMsSqlRead(d *pluginsdk.ResourceData, m if props := resp.Properties; props != nil { v, ok := props.AsReferenceInputProperties() if !ok { - return fmt.Errorf("converting Reference Input MsSql to a Reference Input: %+v", err) + return fmt.Errorf("converting Reference Input MS SQL to a Reference Input: %+v", err) } inputDataSource, ok := v.Datasource.AsAzureSQLReferenceInputDataSource() if !ok { - return fmt.Errorf("converting Reference Input MsSql to a MsSql Stream Input: %+v", err) + return fmt.Errorf("converting Reference Input MS SQL to a MS SQL Stream Input: %+v", err) } d.Set("server", inputDataSource.Properties.Server) diff --git a/website/docs/r/stream_analytics_reference_input_blob.html.markdown b/website/docs/r/stream_analytics_reference_input_blob.html.markdown index 7df1c2e28ef6..7c7e0b7f96ac 100644 --- a/website/docs/r/stream_analytics_reference_input_blob.html.markdown +++ b/website/docs/r/stream_analytics_reference_input_blob.html.markdown @@ -68,7 +68,7 @@ The following arguments are supported: * `path_pattern` - (Required) The blob path pattern. Not a regular expression. It represents a pattern against which blob names will be matched to determine whether or not they should be included as input or output to the job. -* `storage_account_name` - (Required) The name of the Storage Account that has the blob container with reference data. +* `storage_account_name` - (Required) The name of the Storage Account hat has the blob container with reference data. * `storage_account_key` - (Required) The Access Key which should be used to connect to this Storage Account. diff --git a/website/docs/r/stream_analytics_reference_input_mssql.html.markdown b/website/docs/r/stream_analytics_reference_input_mssql.html.markdown new file mode 100644 index 000000000000..777319aa975b --- /dev/null +++ b/website/docs/r/stream_analytics_reference_input_mssql.html.markdown @@ -0,0 +1,105 @@ +--- +subcategory: "Stream Analytics" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_stream_analytics_reference_input_mssql" +description: |- + Manages a Stream Analytics Reference Input from MS SQL. +--- + +# azurerm_stream_analytics_reference_input_mssql + +Manages a Stream Analytics Reference Input from MS SQL. Reference data (also known as a lookup table) is a finite data set that is static or slowly changing in nature, used to perform a lookup or to correlate with your data stream. Learn more [here](https://docs.microsoft.com/en-us/azure/stream-analytics/stream-analytics-use-reference-data#azure-sql-database). + +## Example Usage + +```hcl +data "azurerm_resource_group" "example" { + name = "example-resources" +} + +data "azurerm_stream_analytics_job" "example" { + name = "example-job" + resource_group_name = azurerm_resource_group.example.name +} + +resource "azurerm_mssql_server" "example" { + name = "example-sqlserver" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + version = "12.0" + administrator_login = "admin" + administrator_login_password = "password" +} + +resource "azurerm_mssql_database" "example" { + name = "example-db" + server_id = azurerm_mssql_server.example.id +} + +resource "azurerm_stream_analytics_reference_input_mssql" "example" { + name = "example-reference-input" + resource_group_name = azurerm_stream_analytics_job.example.resource_group_name + stream_analytics_job_name = azurerm_stream_analytics_job.example.name + server = azurerm_mssql_server.example.fully_qualified_domain_name + database = azurerm_mssql_database.example.name + username = "exampleuser" + password = "examplepassword" + refresh_type = "RefreshPeriodicallyWithFull" + refresh_rate = "00:20:00" + full_snapshot_query = < Date: Thu, 21 Oct 2021 09:28:04 +0200 Subject: [PATCH 3/8] add custom importer for reference blob and fix printing of *id --- ...analytics_reference_input_blob_resource.go | 25 ++++++++++--------- ...tics_reference_input_blob_resource_test.go | 12 +++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource.go b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource.go index 9adf698761f1..3d1a685b0031 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource.go @@ -23,10 +23,11 @@ func resourceStreamAnalyticsReferenceInputBlob() *pluginsdk.Resource { Read: resourceStreamAnalyticsReferenceInputBlobRead, Update: resourceStreamAnalyticsReferenceInputBlobUpdate, Delete: resourceStreamAnalyticsReferenceInputBlobDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.StreamInputID(id) return err - }), + }, importStreamAnalyticsReferenceInput(streamanalytics.TypeBasicReferenceInputDataSourceTypeMicrosoftStorageBlob)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -101,17 +102,17 @@ func resourceStreamAnalyticsReferenceInputBlobCreate(d *pluginsdk.ResourceData, defer cancel() log.Printf("[INFO] preparing arguments for Azure Stream Analytics Reference Input Blob creation.") - resourceId := parse.NewStreamInputID(subscriptionId, d.Get("resource_group_name").(string), d.Get("stream_analytics_job_name").(string), d.Get("name").(string)) + id := parse.NewStreamInputID(subscriptionId, d.Get("resource_group_name").(string), d.Get("stream_analytics_job_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.StreamingjobName, resourceId.InputName) + existing, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing %s: %+v", resourceId, err) + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) } } if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_stream_analytics_reference_input_blob", resourceId.ID()) + return tf.ImportAsExistsError("azurerm_stream_analytics_reference_input_blob", id.ID()) } } @@ -120,11 +121,11 @@ func resourceStreamAnalyticsReferenceInputBlobCreate(d *pluginsdk.ResourceData, return fmt.Errorf("creating the input props for resource creation: %v", err) } - if _, err := client.CreateOrReplace(ctx, props, resourceId.ResourceGroup, resourceId.StreamingjobName, resourceId.InputName, "", ""); err != nil { - return fmt.Errorf("creating %s: %+v", resourceId, err) + if _, err := client.CreateOrReplace(ctx, props, id.ResourceGroup, id.StreamingjobName, id.InputName, "", ""); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) } - d.SetId(resourceId.ID()) + d.SetId(id.ID()) return resourceStreamAnalyticsReferenceInputBlobRead(d, meta) } @@ -145,7 +146,7 @@ func resourceStreamAnalyticsReferenceInputBlobUpdate(d *pluginsdk.ResourceData, } if _, err := client.Update(ctx, props, id.ResourceGroup, id.StreamingjobName, id.InputName, ""); err != nil { - return fmt.Errorf("updating %s: %+v", id, err) + return fmt.Errorf("updating %s: %+v", *id, err) } return resourceStreamAnalyticsReferenceInputBlobRead(d, meta) @@ -164,7 +165,7 @@ func resourceStreamAnalyticsReferenceInputBlobRead(d *pluginsdk.ResourceData, me resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[DEBUG] %s was not found - removing from state!", id) + log.Printf("[DEBUG] %s was not found - removing from state!", *id) d.SetId("") return nil } @@ -217,7 +218,7 @@ func resourceStreamAnalyticsReferenceInputBlobDelete(d *pluginsdk.ResourceData, if resp, err := client.Delete(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName); err != nil { if !response.WasNotFound(resp.Response) { - return fmt.Errorf("deleting %s: %+v", id, err) + return fmt.Errorf("deleting %s: %+v", *id, err) } } diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go index 8b6d2959ae25..50910ba04a56 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go @@ -3,6 +3,7 @@ package streamanalytics_test import ( "context" "fmt" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/parse" "testing" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" @@ -96,16 +97,17 @@ func TestAccStreamAnalyticsReferenceInputBlob_requiresImport(t *testing.T) { } func (r StreamAnalyticsReferenceInputBlobResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - name := state.Attributes["name"] - jobName := state.Attributes["stream_analytics_job_name"] - resourceGroup := state.Attributes["resource_group_name"] + id, err := parse.StreamInputID(state.ID) + if err != nil { + return nil, err + } - resp, err := client.StreamAnalytics.InputsClient.Get(ctx, resourceGroup, jobName, name) + resp, err := client.StreamAnalytics.InputsClient.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.InputName) if err != nil { if utils.ResponseWasNotFound(resp.Response) { return utils.Bool(false), nil } - return nil, fmt.Errorf("retrieving Stream Output %q (Stream Analytics Job %q / Resource Group %q): %+v", name, jobName, resourceGroup, err) + return nil, fmt.Errorf("retrieving (%s): %+v", *id, err) } return utils.Bool(true), nil } From a823962fa47b32685355dd1c7c02fbcc6b4f7270 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 21 Oct 2021 10:09:26 +0200 Subject: [PATCH 4/8] fix typo --- .../docs/r/stream_analytics_reference_input_blob.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/stream_analytics_reference_input_blob.html.markdown b/website/docs/r/stream_analytics_reference_input_blob.html.markdown index 7c7e0b7f96ac..7df1c2e28ef6 100644 --- a/website/docs/r/stream_analytics_reference_input_blob.html.markdown +++ b/website/docs/r/stream_analytics_reference_input_blob.html.markdown @@ -68,7 +68,7 @@ The following arguments are supported: * `path_pattern` - (Required) The blob path pattern. Not a regular expression. It represents a pattern against which blob names will be matched to determine whether or not they should be included as input or output to the job. -* `storage_account_name` - (Required) The name of the Storage Account hat has the blob container with reference data. +* `storage_account_name` - (Required) The name of the Storage Account that has the blob container with reference data. * `storage_account_key` - (Required) The Access Key which should be used to connect to this Storage Account. From 4b80898edc94bffc8c028dbe4513bd29596e1756 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 21 Oct 2021 10:17:14 +0200 Subject: [PATCH 5/8] remove whitespace --- .../stream_analytics_reference_input_mssql_resource.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go index 5a2f4bbec6a0..0cf72bbdba8e 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go @@ -2,7 +2,6 @@ package streamanalytics import ( "fmt" - "log" "time" From a97991223faa0c2fa0f100d1ab5b3282938504b8 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 21 Oct 2021 10:18:55 +0200 Subject: [PATCH 6/8] goimports --- .../stream_analytics_reference_input_blob_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go index 50910ba04a56..0f1fa37229a7 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_blob_resource_test.go @@ -3,12 +3,12 @@ package streamanalytics_test import ( "context" "fmt" - "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/parse" "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/streamanalytics/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) From 9e1b31ec42d49ae1680f67016965e367a90bf08f Mon Sep 17 00:00:00 2001 From: Steph Date: Fri, 22 Oct 2021 09:00:19 +0200 Subject: [PATCH 7/8] rename refresh_rate to refresh_interval_duration --- .../stream_analytics_reference_input_mssql_resource.go | 10 +++++----- ...am_analytics_reference_input_mssql_resource_test.go | 4 ++-- ...tream_analytics_reference_input_mssql.html.markdown | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go index 0cf72bbdba8e..56c343ba6e4c 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource.go @@ -88,7 +88,7 @@ func resourceStreamAnalyticsReferenceMsSql() *pluginsdk.Resource { }, false), }, - "refresh_rate": { + "refresh_interval_duration": { Type: pluginsdk.TypeString, Optional: true, ValidateFunc: validate.BatchMaxWaitTime, @@ -132,8 +132,8 @@ func resourceStreamAnalyticsReferenceInputMsSqlCreateUpdate(d *pluginsdk.Resourc refreshType := d.Get("refresh_type").(string) - if _, ok := d.GetOk("refresh_rate"); refreshType != "Static" && !ok { - return fmt.Errorf("refresh_rate must be set if refresh_type is RefreshPeriodicallyWithFull or RefreshPeriodicallyWithDelta") + if _, ok := d.GetOk("refresh_interval_duration"); refreshType != "Static" && !ok { + return fmt.Errorf("refresh_interval_duration must be set if refresh_type is RefreshPeriodicallyWithFull or RefreshPeriodicallyWithDelta") } else if _, ok = d.GetOk("delta_snapshot_query"); refreshType == "Static" && ok { return fmt.Errorf("delta_snapshot_query cannot be set if refresh_type is Static") } @@ -146,7 +146,7 @@ func resourceStreamAnalyticsReferenceInputMsSqlCreateUpdate(d *pluginsdk.Resourc RefreshType: utils.String(refreshType), } - if v, ok := d.GetOk("refresh_rate"); ok { + if v, ok := d.GetOk("refresh_interval_duration"); ok { properties.RefreshRate = utils.String(v.(string)) } @@ -218,7 +218,7 @@ func resourceStreamAnalyticsReferenceInputMsSqlRead(d *pluginsdk.ResourceData, m d.Set("database", inputDataSource.Properties.Database) d.Set("username", inputDataSource.Properties.User) d.Set("refresh_type", inputDataSource.Properties.RefreshType) - d.Set("refresh_rate", inputDataSource.Properties.RefreshRate) + d.Set("refresh_interval_duration", inputDataSource.Properties.RefreshRate) d.Set("full_snapshot_query", inputDataSource.Properties.FullSnapshotQuery) d.Set("delta_snapshot_query", inputDataSource.Properties.DeltaSnapshotQuery) } diff --git a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go index d043a6f36427..8857e082e03c 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input_mssql_resource_test.go @@ -96,7 +96,7 @@ resource "azurerm_stream_analytics_reference_input_mssql" "test" { username = "maurice" password = "ludicrousdisplay" refresh_type = "RefreshPeriodicallyWithFull" - refresh_rate = "00:10:00" + refresh_interval_duration = "00:10:00" full_snapshot_query = < Date: Fri, 22 Oct 2021 09:52:49 +0200 Subject: [PATCH 8/8] check for blob type input data source in custom importer --- .../stream_analytics_reference_input.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/services/streamanalytics/stream_analytics_reference_input.go b/internal/services/streamanalytics/stream_analytics_reference_input.go index 8b6d65556ac5..a45f72dbf748 100644 --- a/internal/services/streamanalytics/stream_analytics_reference_input.go +++ b/internal/services/streamanalytics/stream_analytics_reference_input.go @@ -26,15 +26,20 @@ func importStreamAnalyticsReferenceInput(expectType streamanalytics.TypeBasicRef if props := resp.Properties; props != nil { v, ok := props.AsReferenceInputProperties() if !ok { - return nil, fmt.Errorf("converting Reference Input MsSql to a Reference Input: %+v", err) + return nil, fmt.Errorf("converting properties to a Reference Input: %+v", err) } - inputDataSource, ok := v.Datasource.AsAzureSQLReferenceInputDataSource() - if !ok { - return nil, fmt.Errorf("converting Reference Input MsSql to a MsSql Stream Input: %+v", err) + var actualType streamanalytics.TypeBasicReferenceInputDataSource + + if inputMsSql, ok := v.Datasource.AsAzureSQLReferenceInputDataSource(); ok { + actualType = inputMsSql.Type + } else if inputBlob, ok := v.Datasource.AsBlobReferenceInputDataSource(); ok { + actualType = inputBlob.Type + } else { + return nil, fmt.Errorf("unable to convert input data source: %+v", v) } - if actualType := inputDataSource.Type; actualType != expectType { + if actualType != expectType { return nil, fmt.Errorf("stream analytics reference input has mismatched type, expected: %q, got %q", expectType, actualType) } }