aztfexport icon indicating copy to clipboard operation
aztfexport copied to clipboard

Tool can't parse the Service level scope defined in Azure API Management

Open mirzos opened this issue 1 year ago • 8 comments

I am trying this tool to import configuration for a vanilla Azure API Management instance. When I try to import it into Terraform code it throws an error. This error is related to the built-in subscription for Service scope called "Built-in all-access subscription". Seems like the tool is not able to interpret this scope.

Failed to import /subscriptions/0000-0000-0000-0000-0000/resourceGroups/resource-group-name/providers/Microsoft.ApiManagement/service/apim-instance-name/subscriptions/master as azurerm_api_management_subscription.res-33: exit status 1

Error: parsing scope into product/ api id "/subscriptions/0000-0000-0000-0000-0000/resourceGroups/resource-group-name/providers/Microsoft.ApiManagement/service/apim-instance-name/": ID was missing the apis element

mirzos avatar Aug 15 '23 20:08 mirzos

@mirzos The error seems derive from the provider: https://github.com/hashicorp/terraform-provider-azurerm/blob/8f356e27d0daad612a75308e801c5ba9374d525e/internal/services/apimanagement/api_management_subscription_resource.go#L247-L260

			// check if the subscription is for all apis or a specific product/ api
			if props.Scope != "" && !strings.HasSuffix(props.Scope, "/apis") {
				// the scope is either a product or api id
				parseId, err := product.ParseProductIDInsensitively(props.Scope)
				if err == nil {
					productId = parseId.ID()
				} else {
					parsedApiId, err := api.ParseApiIDInsensitively(props.Scope)
					if err != nil {
						return fmt.Errorf("parsing scope into product/ api id %q: %+v", props.Scope, err)
					}
					apiId = parsedApiId.ID()
				}
			}

Would you mind to manually run terraform import on this resource. If the error remains, then you'd want to create an upstream issue in https://github.com/hashicorp/terraform-provider-azurerm and link it here.

magodo avatar Aug 16 '23 00:08 magodo

@magodo Thanks for your reply and I tried with Terraform directly to import the configs and I was able to do so.

mirzos avatar Aug 17 '23 15:08 mirzos

Hi @mirzos, just to be sure you are importing the exact same resource: /subscriptions/0000-0000-0000-0000-0000/resourceGroups/resource-group-name/providers/Microsoft.ApiManagement/service/apim-instance-name/subscriptions/master?

magodo avatar Aug 18 '23 01:08 magodo

Hi @magodo yes, this is the exact resource I am trying to import

mirzos avatar Aug 21 '23 15:08 mirzos

Hi @magodo, could you find the issue?

mirzos avatar Aug 24 '23 21:08 mirzos

I've tried to run terraform import against the built-in master subscription on my side, failed with the same error, while the other subscriptions (named by uuid) are successfully imported. The reason is the scope returned is not of the form that the provider expected. This is mostly like a provider bug, or either an API bug.

Since there is nothing we can do here, I prefer to skip this resource for now.

magodo avatar Aug 25 '23 01:08 magodo

Hi @magodo thanks for your answer. I will skip this resource for now (y)

mirzos avatar Aug 25 '23 13:08 mirzos

@magodo do you have any updates to this issue?

stemaMSFT avatar Jul 23 '24 22:07 stemaMSFT