Azure / aztfexport

A tool to bring existing Azure resources under Terraform's management
https://azure.github.io/aztfexport/
Mozilla Public License 2.0
1.53k stars 174 forks source link

json: cannot unmarshal object into Go struct field ServiceEndpointPolicyPropertiesFormat.contextualServiceEndpointPolicies of type string #519

Open zeusbaba opened 2 months ago

zeusbaba commented 2 months ago

getting this error on some resources.
Could you please help/advise on how to resolve this?

current versions:

aztfexport version v0.14.1(f492475)  
Terraform v1.8.2

Pasting below log snippet

2024-04-24T15:03:40.912+0200 [ERROR] aztfexport: Importing azurerm_subnet_service_endpoint_storage_policy.res-67: exit status 1

Error: retrieving Subnet Service Endpoint Storage Policy "_e41f87a2_mi_REDACTED" (Resource Group "rg-redacted-qa"): network.ServiceEndpointPoliciesClient#Get: Failure responding to request: StatusCode=200 -- Original Error: Error occurred unmarshalling JSON - Error = 'json: cannot unmarshal object into Go struct field ServiceEndpointPolicyPropertiesFormat.contextualServiceEndpointPolicies of type string' JSON = '{
  "name": "_e41f87a2_mi_REDACTED",
  "id": "/subscriptions/REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/serviceEndpointPolicies/_e41f87a2_mi_REDACTED",
  "etag": "W/\"83bfed89-30f4-redacted\"",
  "type": "Microsoft.Network/serviceEndpointPolicies",
  "location": "norwayeast",
  "properties": {
    "provisioningState": "Succeeded",
    "resourceGuid": "4f9befbd-REDACTED",
    "serviceAlias": "/services/Azure/ManagedInstance",
    "serviceEndpointPolicyDefinitions": [],
    "contextualServiceEndpointPolicies": [
      {
        "id": "/subscriptions/REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/virtualNetworks/vnet-redactedqa/subnets/managedinstance/contextualServiceEndpointPolicies/ManagedInstanceContextualPolicy"
      }
    ]
  },
  "kind": "Internal"
}' 
magodo commented 2 months ago

@zeusbaba This seems to be an upstream provider issue, you can verify this by directly import it via terraform import azurerm_subnet_service_endpoint_storage_policy.name <id>.

zeusbaba commented 2 months ago

@zeusbaba This seems to be an upstream provider issue, you can verify this by directly import it via terraform import azurerm_subnet_service_endpoint_storage_policy.name <id>.

thnx for response.
I'll try this and get back to you soon

zeusbaba commented 2 months ago

@zeusbaba This seems to be an upstream provider issue, you can verify this by directly import it via terraform import azurerm_subnet_service_endpoint_storage_policy.name <id>.

terraform import azurerm_subnet_service_endpoint_storage_policy.name id

fails, because

Error: resource address "azurerm_subnet_service_endpoint_storage_policy.name" does not exist in the configuration.

i assume that aztfexport failed to parse it in the first run. I don't see it in the generated main.tf either

magodo commented 2 months ago

@zeusbaba You'll need to first write a main.tf to which contains the following content:

resource azurerm_subnet_service_endpoint_storage_policy name {
}
zeusbaba commented 2 months ago

@magodo as you suggested, I manually added this section to the generated main.tf, and then re-run
terraform import azurerm_subnet_service_endpoint_storage_policy.name id
getting this error

Error: Error ensuring Resource Providers are registered.  
...  
 Original Error: Cannot register providers: Microsoft.AppConfiguration, Microsoft.DataFactory, Microsoft.SignalRService. Errors were: Cannot register provider Microsoft.AppConfiguration with Azure Resource Manager: unexpected status 403 (403 Forbidden) with error: AuthorizationFailed: The client '152af153-redacted' with object id '152f153-redacted' does not have authorization to perform action 'Microsoft.AppConfiguration/register/action' over scope '/subscriptions/a498c7e3-redacted' or the scope is invalid.
...

btw. as i see in the logs, this import is supposed to be done by aztfexport , right?

magodo commented 2 months ago

@zeusbaba Yes, the error you see here is because the AzureRM provider by default register all the resource providers that potentially be used. You can suppress it by specifying skip_provider_registration.

zeusbaba commented 2 months ago

following your suggestion, I did set ARM_SKIP_PROVIDER_REGISTRATION=true
then re-run aztfexport on the failing resource group.

but the same error still in the logs.

2024-04-26T07:23:42.666+0200 [ERROR] aztfexport: Importing azurerm_subnet_service_endpoint_storage_policy.res-69: exit status 1

Error: retrieving Subnet Service Endpoint Storage Policy "_e41f87a2_REDACTED" (Resource Group "rg-redacted-qa"): network.ServiceEndpointPoliciesClient#Get: Failure responding to request: StatusCode=200 -- Original Error: Error occurred unmarshalling JSON - Error = 'json: cannot unmarshal object into Go struct field ServiceEndpointPolicyPropertiesFormat.contextualServiceEndpointPolicies of type string' JSON = '{
  "name": "_e41f87a2_REDACTED",
  "id": "/subscriptions/a498c7e3-REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/serviceEndpointPolicies/_e41f87a2_REDACTED",
  "etag": "W/\"96ddffdc-REDACTED\"",
  "type": "Microsoft.Network/serviceEndpointPolicies",
  "location": "norwayeast",
  "properties": {
    "provisioningState": "Succeeded",
    "resourceGuid": "88e6ea86-REDACTED",
    "serviceAlias": "/services/Azure/ManagedInstance",
    "serviceEndpointPolicyDefinitions": [],
    "contextualServiceEndpointPolicies": [
      {
        "id": "/subscriptions/a498c7e3-REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/virtualNetworks/vnet-saassb1qa/subnets/managedinstance/contextualServiceEndpointPolicies/ManagedInstanceContextualPolicy"
      }
    ]
  },
  "kind": "Internal"
}'
2024-04-26T07:23:42.809+0200 [ERROR] aztfexport: Importing azurerm_subnet_service_endpoint_storage_policy.res-67: exit status 1

Error: retrieving Subnet Service Endpoint Storage Policy "_e41f87a2_REDACTED" (Resource Group "rg-redacted-qa"): network.ServiceEndpointPoliciesClient#Get: Failure responding to request: StatusCode=200 -- Original Error: Error occurred unmarshalling JSON - Error = 'json: cannot unmarshal object into Go struct field ServiceEndpointPolicyPropertiesFormat.contextualServiceEndpointPolicies of type string' JSON = '{
  "name": "_e41f87a2_REDACTED",
  "id": "/subscriptions/a498c7e3-REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/serviceEndpointPolicies/_e41f87a2_REDACTED",
  "etag": "W/\"83bfed89-REDACTED\"",
  "type": "Microsoft.Network/serviceEndpointPolicies",
  "location": "norwayeast",
  "properties": {
    "provisioningState": "Succeeded",
    "resourceGuid": "4f9befbd-REDACTED",
    "serviceAlias": "/services/Azure/ManagedInstance",
    "serviceEndpointPolicyDefinitions": [],
    "contextualServiceEndpointPolicies": [
      {
        "id": "/subscriptions/a498c7e3-REDACTED/resourceGroups/rg-redacted-qa/providers/Microsoft.Network/virtualNetworks/vnet-saassb1qa/subnets/managedinstance/contextualServiceEndpointPolicies/ManagedInstanceContextualPolicy"
      }
    ]
  },
  "kind": "Internal"
}'
magodo commented 2 months ago

Sorry, I mean this error is raised from the upstream azurerm provider, which needs to be fixed there. Could you please again try reproduce this error by terraform import command, then submit an issue there?

zeusbaba commented 2 months ago

Sorry, I mean this error is raised from the upstream azurerm provider, which needs to be fixed there. Could you please again try reproduce this error by terraform import command, then submit an issue there?

fyi. as you suggested, I've created this issue >>
we can progress based on response