diff --git a/azurerm/internal/services/iottimeseriesinsights/client/client.go b/azurerm/internal/services/iottimeseriesinsights/client/client.go index d73ae4a2ed7b..4ba9531c5aa9 100644 --- a/azurerm/internal/services/iottimeseriesinsights/client/client.go +++ b/azurerm/internal/services/iottimeseriesinsights/client/client.go @@ -6,14 +6,19 @@ import ( ) type Client struct { - EnvironmentsClient *timeseriesinsights.EnvironmentsClient + EnvironmentsClient *timeseriesinsights.EnvironmentsClient + ReferenceDataSetsClient *timeseriesinsights.ReferenceDataSetsClient } func NewClient(o *common.ClientOptions) *Client { EnvironmentsClient := timeseriesinsights.NewEnvironmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&EnvironmentsClient.Client, o.ResourceManagerAuthorizer) + ReferenceDataSetsClient := timeseriesinsights.NewReferenceDataSetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&ReferenceDataSetsClient.Client, o.ResourceManagerAuthorizer) + return &Client{ - EnvironmentsClient: &EnvironmentsClient, + EnvironmentsClient: &EnvironmentsClient, + ReferenceDataSetsClient: &ReferenceDataSetsClient, } } diff --git a/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set.go b/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set.go new file mode 100644 index 000000000000..798f4fd016b8 --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type TimeSeriesInsightsReferenceDataSetId struct { + ResourceGroup string + EnvironmentName string + Name string +} + +func TimeSeriesInsightsReferenceDataSetID(input string) (*TimeSeriesInsightsReferenceDataSetId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("parsing Time Series Insights Reference Dataset ID %q: %+v", input, err) + } + + service := TimeSeriesInsightsReferenceDataSetId{ + ResourceGroup: id.ResourceGroup, + } + + if service.EnvironmentName, err = id.PopSegment("environments"); err != nil { + return nil, err + } + + if service.Name, err = id.PopSegment("referenceDataSets"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &service, nil +} diff --git a/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set_test.go b/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set_test.go new file mode 100644 index 000000000000..9643e7635a5c --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set_test.go @@ -0,0 +1,74 @@ +package parse + +import ( + "testing" +) + +func TestTimeSeriesInsightsReferenceDataSetId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *TimeSeriesInsightsReferenceDataSetId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Time Series Insight ReferenceDataset Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.TimeSeriesInsights/environments/Environment1/referenceDataSets/", + Expected: nil, + }, + { + Name: "Time Series Insight Environment ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.TimeSeriesInsights/environments/Environment1/referenceDataSets/DataSet1", + Expected: &TimeSeriesInsightsReferenceDataSetId{ + Name: "DataSet1", + EnvironmentName: "Environment1", + ResourceGroup: "resGroup1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.TimeSeriesInsights/Environments/Environment1/ReferenceDataSets/DataSet1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := TimeSeriesInsightsReferenceDataSetID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/iottimeseriesinsights/registration.go b/azurerm/internal/services/iottimeseriesinsights/registration.go index d6dcc783cfaa..21ae52bea3d1 100644 --- a/azurerm/internal/services/iottimeseriesinsights/registration.go +++ b/azurerm/internal/services/iottimeseriesinsights/registration.go @@ -27,5 +27,6 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource { func (r Registration) SupportedResources() map[string]*schema.Resource { return map[string]*schema.Resource{ "azurerm_iot_time_series_insights_standard_environment": resourceArmIoTTimeSeriesInsightsStandardEnvironment(), + "azurerm_iot_time_series_insights_reference_data_set": resourceArmIoTTimeSeriesInsightsReferenceDataSet(), } } diff --git a/azurerm/internal/services/iottimeseriesinsights/resource_iot_time_series_insights_reference_data_set.go b/azurerm/internal/services/iottimeseriesinsights/resource_iot_time_series_insights_reference_data_set.go new file mode 100644 index 000000000000..5f9bd56a3687 --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/resource_iot_time_series_insights_reference_data_set.go @@ -0,0 +1,243 @@ +package iottimeseriesinsights + +import ( + "fmt" + "regexp" + "strings" + "time" + + "github.com/Azure/azure-sdk-for-go/services/preview/timeseriesinsights/mgmt/2018-08-15-preview/timeseriesinsights" + "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/azure" + "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/iottimeseriesinsights/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/iottimeseriesinsights/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + 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 resourceArmIoTTimeSeriesInsightsReferenceDataSet() *schema.Resource { + return &schema.Resource{ + Create: resourceArmIoTTimeSeriesInsightsReferenceDataSetCreateUpdate, + Read: resourceArmIoTTimeSeriesInsightsReferenceDataSetRead, + Update: resourceArmIoTTimeSeriesInsightsReferenceDataSetCreateUpdate, + Delete: resourceArmIoTTimeSeriesInsightsReferenceDataSetDelete, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.TimeSeriesInsightsReferenceDataSetID(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{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile(`^[A-Za-z0-9]{3,63}`), + "IoT Time Series Insights Reference Data Set name must contain only alphanumeric characters and be between 3 and 63 characters.", + ), + }, + + "time_series_insights_environment_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.TimeSeriesInsightsEnvironmentID, + }, + + "data_string_comparison_behavior": { + Type: schema.TypeString, + Optional: true, + Default: string(timeseriesinsights.Ordinal), + ValidateFunc: validation.StringInSlice([]string{ + string(timeseriesinsights.Ordinal), + string(timeseriesinsights.OrdinalIgnoreCase), + }, false), + }, + + "key_property": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + string(timeseriesinsights.ReferenceDataKeyPropertyTypeBool), + string(timeseriesinsights.ReferenceDataKeyPropertyTypeDateTime), + string(timeseriesinsights.ReferenceDataKeyPropertyTypeDouble), + string(timeseriesinsights.ReferenceDataKeyPropertyTypeString), + }, false), + }, + }, + }, + }, + + "location": azure.SchemaLocation(), + + "tags": tags.Schema(), + }, + } +} + +func resourceArmIoTTimeSeriesInsightsReferenceDataSetCreateUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + environmentID := d.Get("time_series_insights_environment_id").(string) + id, err := parse.TimeSeriesInsightsEnvironmentID(environmentID) + if err != nil { + return err + } + location := azure.NormalizeLocation(d.Get("location").(string)) + t := d.Get("tags").(map[string]interface{}) + + if d.IsNewResource() { + existing, err := client.Get(ctx, id.ResourceGroup, id.Name, name) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for presence of existing IoT Time Series Insights Reference Data Set %q (Resource Group %q): %s", name, id.ResourceGroup, err) + } + } + + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_iot_time_series_insights_reference_data_set", *existing.ID) + } + } + + dataset := timeseriesinsights.ReferenceDataSetCreateOrUpdateParameters{ + Location: &location, + Tags: tags.Expand(t), + ReferenceDataSetCreationProperties: ×eriesinsights.ReferenceDataSetCreationProperties{ + DataStringComparisonBehavior: timeseriesinsights.DataStringComparisonBehavior(d.Get("data_string_comparison_behavior").(string)), + KeyProperties: expandIoTTimeSeriesInsightsReferenceDataSetKeyProperties(d.Get("key_property").(*schema.Set).List()), + }, + } + + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, dataset); err != nil { + return fmt.Errorf("creating/updating IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err) + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name) + if err != nil { + return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, id.ResourceGroup, err) + } + + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("cannot read IoT Time Series Insights Reference Data Set %q (Resource Group %q) ID", name, id.ResourceGroup) + } + + d.SetId(*resp.ID) + + return resourceArmIoTTimeSeriesInsightsReferenceDataSetRead(d, meta) +} + +func resourceArmIoTTimeSeriesInsightsReferenceDataSetRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.TimeSeriesInsightsReferenceDataSetID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.EnvironmentName, id.Name) + if err != nil || resp.ID == nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + + return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + + d.Set("name", resp.Name) + d.Set("time_series_insights_environment_id", strings.Split(d.Id(), "/referenceDataSets")[0]) + if location := resp.Location; location != nil { + d.Set("location", azure.NormalizeLocation(*location)) + } + + if props := resp.ReferenceDataSetResourceProperties; props != nil { + d.Set("data_string_comparison_behavior", string(props.DataStringComparisonBehavior)) + if err := d.Set("key_property", flattenIoTTimeSeriesInsightsReferenceDataSetKeyProperties(props.KeyProperties)); err != nil { + return fmt.Errorf("setting `key_property`: %+v", err) + } + } + + return tags.FlattenAndSet(d, resp.Tags) +} + +func resourceArmIoTTimeSeriesInsightsReferenceDataSetDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.TimeSeriesInsightsReferenceDataSetID(d.Id()) + if err != nil { + return err + } + + response, err := client.Delete(ctx, id.ResourceGroup, id.EnvironmentName, id.Name) + if err != nil { + if !utils.ResponseWasNotFound(response) { + return fmt.Errorf("deleting IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + } + + return nil +} + +func expandIoTTimeSeriesInsightsReferenceDataSetKeyProperties(input []interface{}) *[]timeseriesinsights.ReferenceDataSetKeyProperty { + properties := make([]timeseriesinsights.ReferenceDataSetKeyProperty, 0) + + for _, v := range input { + if v == nil { + continue + } + attr := v.(map[string]interface{}) + + properties = append(properties, timeseriesinsights.ReferenceDataSetKeyProperty{ + Type: timeseriesinsights.ReferenceDataKeyPropertyType(attr["type"].(string)), + Name: utils.String(attr["name"].(string)), + }) + } + + return &properties +} + +func flattenIoTTimeSeriesInsightsReferenceDataSetKeyProperties(input *[]timeseriesinsights.ReferenceDataSetKeyProperty) []interface{} { + if input == nil { + return []interface{}{} + } + properties := make([]interface{}, 0) + for _, property := range *input { + attr := make(map[string]interface{}) + attr["type"] = string(property.Type) + if name := property.Name; name != nil { + attr["name"] = *property.Name + } + properties = append(properties, attr) + } + + return properties +} diff --git a/azurerm/internal/services/iottimeseriesinsights/tests/resource_iot_time_series_insights_reference_data_set_test.go b/azurerm/internal/services/iottimeseriesinsights/tests/resource_iot_time_series_insights_reference_data_set_test.go new file mode 100644 index 000000000000..f48adf0cc231 --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/tests/resource_iot_time_series_insights_reference_data_set_test.go @@ -0,0 +1,190 @@ +package tests + +import ( + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/iottimeseriesinsights/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_iot_time_series_insights_reference_data_set", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_iot_time_series_insights_reference_data_set", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetExists(data.ResourceName), + ), + }, + data.ImportStep(), + { + Config: testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_update(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetExists(data.ResourceName), + ), + }, + data.ImportStep(), + { + Config: testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + +func testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + // Ensure we have enough information in state to look up in API + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + id, err := parse.TimeSeriesInsightsReferenceDataSetID(rs.Primary.ID) + if err != nil { + return err + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.EnvironmentName, id.Name) + if err != nil { + return fmt.Errorf("Bad: Get on TimeSeriesInsightsReferenceDataSetClient: %+v", err) + } + + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Time Series Insights Reference Data Set %q (resource group: %q) does not exist", id.Name, id.ResourceGroup) + } + + return nil + } +} + +func testCheckAzureRMIoTTimeSeriesInsightsReferenceDataSetDestroy(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).IoTTimeSeriesInsights.ReferenceDataSetsClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_iot_time_series_insights_reference_data_set" { + continue + } + + id, err := parse.TimeSeriesInsightsReferenceDataSetID(rs.Primary.ID) + if err != nil { + return err + } + resp, err := client.Get(ctx, id.ResourceGroup, id.EnvironmentName, id.Name) + + if err != nil { + return nil + } + + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("time Series Insights Reference Data Set still exists: %q", id.Name) + } + } + + return nil +} + +func testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} +resource "azurerm_resource_group" "test" { + name = "acctestRG-tsi-%d" + location = "%s" +} +resource "azurerm_iot_time_series_insights_standard_environment" "test" { + name = "accTEst_tsie%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku_name = "S1_1" + data_retention_time = "P30D" +} + +resource "azurerm_iot_time_series_insights_reference_data_set" "test" { + name = "accTEsttsd%d" + time_series_insights_environment_id = azurerm_iot_time_series_insights_standard_environment.test.id + location = azurerm_resource_group.test.location + + key_property { + name = "keyProperty1" + type = "String" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + +func testAccAzureRMIoTTimeSeriesInsightsReferenceDataSet_update(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} +resource "azurerm_resource_group" "test" { + name = "acctestRG-tsi-%d" + location = "%s" +} +resource "azurerm_iot_time_series_insights_standard_environment" "test" { + name = "accTEst_tsie%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku_name = "S1_1" + data_retention_time = "P30D" +} +resource "azurerm_iot_time_series_insights_reference_data_set" "test" { + name = "accTEsttsd%d" + time_series_insights_environment_id = azurerm_iot_time_series_insights_standard_environment.test.id + location = azurerm_resource_group.test.location + + key_property { + name = "keyProperty1" + type = "String" + } + + key_property { + name = "keyProperty2" + type = "Bool" + } + + tags = { + Environment = "Production" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} diff --git a/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment.go b/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment.go new file mode 100644 index 000000000000..2edbc944e6aa --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment.go @@ -0,0 +1,22 @@ +package validate + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/iottimeseriesinsights/parse" +) + +func TimeSeriesInsightsEnvironmentID(i interface{}, k string) (warnings []string, errors []error) { + v, ok := i.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) + return + } + + if _, err := parse.TimeSeriesInsightsEnvironmentID(v); err != nil { + errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err)) + return + } + + return warnings, errors +} diff --git a/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment_test.go b/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment_test.go new file mode 100644 index 000000000000..5cbf384b0b61 --- /dev/null +++ b/azurerm/internal/services/iottimeseriesinsights/validate/time_series_insights_environment_test.go @@ -0,0 +1,40 @@ +package validate + +import "testing" + +func TestTimeSeriesInsightsEnvironmentID(t *testing.T) { + cases := []struct { + ID string + Valid bool + }{ + { + ID: "", + Valid: false, + }, + { + ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/", + Valid: false, + }, + { + ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.TimeSeriesInsights/environments", + Valid: false, + }, + { + ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.TimeSeriesInsights/Environments/Environment1", + Valid: false, + }, + { + ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.TimeSeriesInsights/environments/Environment1", + Valid: true, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.ID) + _, errors := TimeSeriesInsightsEnvironmentID(tc.ID, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 25d10b1ed772..071613b8a313 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -1642,6 +1642,9 @@
  • azurerm_iot_time_series_insights_standard_environment
  • +
  • + azurerm_iot_time_series_insights_reference_data_set +
  • diff --git a/website/docs/r/iot_time_series_insights_reference_data_set.html.markdown b/website/docs/r/iot_time_series_insights_reference_data_set.html.markdown new file mode 100644 index 000000000000..010c4a605639 --- /dev/null +++ b/website/docs/r/iot_time_series_insights_reference_data_set.html.markdown @@ -0,0 +1,84 @@ +--- +subcategory: "Time Series Insights" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_iot_time_series_insights_standard_environment" +description: |- + Manages an Azure IoT Time Series Insights Reference Data Set. +--- + +# azurerm_time_series_insights_standard_environment + +Manages an Azure IoT Time Series Insights Reference Data Set. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "northeurope" +} +resource "azurerm_iot_time_series_insights_standard_environment" "example" { + name = "example" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + sku_name = "S1_1" + data_retention_time = "P30D" +} +resource "azurerm_iot_time_series_insights_reference_data_set" "example" { + name = "example" + time_series_insights_environment_id = azurerm_iot_time_series_insights_standard_environment.example.id + location = azurerm_resource_group.example.location + + key_property { + name = "keyProperty1" + type = "String" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the Azure IoT Time Series Insights Reference Data Set. Changing this forces a new resource to be created. Must be globally unique. + +* `time_series_insights_environment_id` - (Required) The resource ID of the Azure IoT Time Series Insights Environment in which to create the Azure IoT Time Series Insights Reference Data Set. Changing this forces a new resource to be created. + +* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. + +* `data_string_comparison_behavior` - (Optional) The comparison behavior that will be used to compare keys. Valid values include `Ordinal` and `OrdinalIgnoreCase`. Defaults to `Ordinal`. + +* `key_property` - (Optional) A `key_property` block as defined below. + +* `tags` - (Optional) A mapping of tags to assign to the resource. + +--- + +A `key_property` block supports the following: + +* `name`- (Required) The name of the key property. + +* `type` - (Required) The data type of the key property. Valid values include `Bool`, `DateTime`, `Double`, `String`. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the IoT Time Series Insights Reference Data Set. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the IoT Time Series Insights Reference Data Set. +* `update` - (Defaults to 30 minutes) Used when updating the IoT Time Series Insights Reference Data Set. +* `read` - (Defaults to 5 minutes) Used when retrieving the IoT Time Series Insights Reference Data Set. +* `delete` - (Defaults to 30 minutes) Used when deleting the IoT Time Series Insights Reference Data Set. + +## Import + +Azure IoT Time Series Insights Reference Data Set can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_iot_time_series_insights_reference_data_set.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.TimeSeriesInsights/environments/example/referenceDataSets/example +```