-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_iot_time_series_insights_reference_data_set
#7112
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ernal/services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
74 changes: 74 additions & 0 deletions
74
.../services/iottimeseriesinsights/parse/iot_time_series_insights_reference_data_set_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 245 additions & 0 deletions
245
...al/services/iottimeseriesinsights/resource_iot_time_series_insights_reference_data_set.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,245 @@ | ||||||||
package iottimeseriesinsights | ||||||||
|
||||||||
import ( | ||||||||
"fmt" | ||||||||
"regexp" | ||||||||
"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/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.", | ||||||||
), | ||||||||
}, | ||||||||
|
||||||||
"environment_name": { | ||||||||
Type: schema.TypeString, | ||||||||
Required: true, | ||||||||
ForceNew: true, | ||||||||
ValidateFunc: validation.StringMatch( | ||||||||
regexp.MustCompile(`^[-\w\._\(\)]+$`), | ||||||||
"IoT Time Series Insights Standard Environment name must contain only word characters, periods, underscores, and parentheses.", | ||||||||
), | ||||||||
}, | ||||||||
|
||||||||
"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), | ||||||||
}, | ||||||||
}, | ||||||||
}, | ||||||||
}, | ||||||||
|
||||||||
"resource_group_name": azure.SchemaResourceGroupName(), | ||||||||
|
||||||||
"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) | ||||||||
environmentName := d.Get("environment_name").(string) | ||||||||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||||||||
resourceGroup := d.Get("resource_group_name").(string) | ||||||||
t := d.Get("tags").(map[string]interface{}) | ||||||||
|
||||||||
if d.IsNewResource() { | ||||||||
existing, err := client.Get(ctx, resourceGroup, environmentName, 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, 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()), | ||||||||
}, | ||||||||
} | ||||||||
|
||||||||
_, err := client.CreateOrUpdate(ctx, resourceGroup, environmentName, name, dataset) | ||||||||
if err != nil { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. qwe could comebine these
Suggested change
|
||||||||
return fmt.Errorf("creating/updating IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, resourceGroup, err) | ||||||||
} | ||||||||
|
||||||||
resp, err := client.Get(ctx, resourceGroup, environmentName, name) | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("retrieving IoT Time Series Insights Reference Data Set %q (Resource Group %q): %+v", name, 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, 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("environment_name", id.EnvironmentName) | ||||||||
d.Set("resource_group_name", id.ResourceGroup) | ||||||||
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 | ||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh also would this be more clear as