Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_iot_security_solution - a state migration to work around the previously incorrect id casing #19489

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
iothubValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
Expand All @@ -35,6 +36,11 @@ func resourceIotSecuritySolution() *pluginsdk.Resource {
return err
}),

SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.SecurityCenterIotSecuritySolutionV0ToV1{},
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/set"
)

type SecurityCenterIotSecuritySolutionV0ToV1 struct{}

func (s SecurityCenterIotSecuritySolutionV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"display_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"iothub_ids": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
Set: set.HashStringIgnoreCase,
},

"additional_workspace": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"data_types": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},

"disabled_data_sources": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"log_analytics_workspace_id": {
Type: pluginsdk.TypeString,
Optional: true,
},

"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"log_unmasked_ips_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"events_to_export": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"recommendations_enabled": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"acr_authentication": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"agent_send_unutilized_msg": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"baseline": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"edge_hub_mem_optimize": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"edge_logging_option": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"inconsistent_module_settings": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"install_agent": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"ip_filter_deny_all": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"ip_filter_permissive_rule": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"open_ports": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"permissive_firewall_policy": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"permissive_input_firewall_rules": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"permissive_output_firewall_rules": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"privileged_docker_options": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"shared_credentials": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"vulnerable_tls_cipher_suite": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
},

"query_for_resources": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

"query_subscription_ids": {
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"tags": tags.Schema(),
}
}

func (s SecurityCenterIotSecuritySolutionV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.IotSecuritySolutionIDInsensitively(oldId)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)

rawState["id"] = newId.ID()
return rawState, nil
}
}
48 changes: 46 additions & 2 deletions internal/services/securitycenter/parse/iot_security_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (id IotSecuritySolutionId) String() string {
}

func (id IotSecuritySolutionId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Security/IoTSecuritySolutions/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Security/iotSecuritySolutions/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

Expand All @@ -57,7 +57,51 @@ func IotSecuritySolutionID(input string) (*IotSecuritySolutionId, error) {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.Name, err = id.PopSegment("IoTSecuritySolutions"); err != nil {
if resourceId.Name, err = id.PopSegment("iotSecuritySolutions"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}

// IotSecuritySolutionIDInsensitively parses an IotSecuritySolution ID into an IotSecuritySolutionId struct, insensitively
// This should only be used to parse an ID for rewriting, the IotSecuritySolutionID
// method should be used instead for validation etc.
//
// Whilst this may seem strange, this enables Terraform have consistent casing
// which works around issues in Core, whilst handling broken API responses.
func IotSecuritySolutionIDInsensitively(input string) (*IotSecuritySolutionId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := IotSecuritySolutionId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

// find the correct casing for the 'iotSecuritySolutions' segment
iotSecuritySolutionsKey := "iotSecuritySolutions"
for key := range id.Path {
if strings.EqualFold(key, iotSecuritySolutionsKey) {
iotSecuritySolutionsKey = key
break
}
}
if resourceId.Name, err = id.PopSegment(iotSecuritySolutionsKey); err != nil {
return nil, err
}

Expand Down
Loading