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

Support for [API Connection] #7200

Closed
wiliambuzatto opened this issue Jun 3, 2020 · 4 comments
Closed

Support for [API Connection] #7200

wiliambuzatto opened this issue Jun 3, 2020 · 4 comments

Comments

@wiliambuzatto
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Would be nice to create an Api Connection using terraform.

References

https://docs.microsoft.com/en-us/azure/templates/microsoft.web/2016-06-01/connections

@JamieH-risual
Copy link

Is there any updates on this? I'm currently using the Logic App Standard (Single Tenant) resource for an integration platform which uses multiple managed connectors. The parameter values for each connector type are mostly different and are not well documented (Microsoft Docs recommends creating the connection in the Azure Portal and recording a network trace in your browser to capture the parameters...) so I appreciate this may take a while to collate all the required information. I've got a work around using the ARM template deployment but it's not the most efficient approach as I need to update my module every time I need to use a new data source.

ARM Template:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connectionName": {
            "type": "String",
            "metadata": {
                "description": "The name for the connection."
            }
        },
        "connectionType": {
            "type": "String",
            "metadata": {
                "description": "The type of the connection."
            }
        },
        "parameterValues": {
            "type": "Object"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Web/connections",
            "apiVersion": "2018-07-01-preview",
            "location": "[resourceGroup().location]",
            "kind": "V2",
            "name": "[parameters('connectionName')]",
            "properties": {
                "api": {
                    "id": "[subscriptionResourceId('Microsoft.Web/locations/managedApis', resourceGroup().location, parameters('connectionType'))]"
                },
                "displayName": "[parameters('connectionName')]",
                "parameterValues": "[parameters('parameterValues')]"
            }
        }
    ],
    "outputs": {
      "connectionRuntime": {
        "type": "String",
        "value": "[reference(resourceId('Microsoft.Web/connections', parameters('connectionName')),'2016-06-01', 'full').properties.connectionRuntimeUrl]"
      }
    }
}

Terraform Module:

locals {
  params = {
    parameterValues = {
      value = merge(local.event_grid_publish, local.azure_file, local.key_vault, local.log_analytics_workspace, local.service_bus, local.event_grid)
    }
  }
  azure_file              = var.template_deployment_params.connectionType.value == "azurefile" ? tomap({ "accountName" = data.azurerm_storage_account.main[0].name, "accesskey" = data.azurerm_storage_account.main[0].primary_access_key }) : {}
  event_grid              = var.template_deployment_params.connectionType.value == "azureeventgrid" ? var.template_deployment_param_values.parameterValues.value : {}
  event_grid_publish      = var.template_deployment_params.connectionType.value == "azureeventgridpublish" ? tomap({ api_key = data.azurerm_eventgrid_topic.main[0].primary_access_key, "endpoint" = data.azurerm_eventgrid_topic.main[0].endpoint }) : {}
  key_vault               = var.template_deployment_params.connectionType.value == "keyvault" ? var.template_deployment_param_values.parameterValues.value : {}
  log_analytics_workspace = var.template_deployment_params.connectionType.value == "azureloganalyticsdatacollector" ? var.template_deployment_param_values.parameterValues.value : {}
  service_bus             = var.template_deployment_params.connectionType.value == "servicebus" ? tomap({ "connectionString" = data.azurerm_servicebus_namespace.main[0].default_primary_connection_string }) : {}
}

resource "azurerm_resource_group_template_deployment" "main" {
  name                = var.template_deployment_name
  resource_group_name = var.template_deployment_resourcegroup
  template_content    = var.template_deployment_body

  parameters_content = jsonencode(merge(var.template_deployment_params, local.params))

  deployment_mode = "Incremental"
}

# ARM output not working due to bug: GitHub issue #12828
# resource "azurerm_key_vault_secret" "main" {
#   depends_on = [
#     azurerm_resource_group_template_deployment.main
#   ]
#   name         = "${var.template_deployment_params.connectionName.value}-connection-runtimeurl"
#   value        = jsondecode(azurerm_resource_group_template_deployment.main.output_content).connectionRuntime.value
#   key_vault_id = data.azurerm_key_vault.main.id
# }

# data "azurerm_key_vault" "main" {
#   name                = var.template_deployment_kv
#   resource_group_name = var.template_deployment_kv_rg
# }

data "azurerm_storage_account" "main" {
  count               = var.template_deployment_params.connectionType.value == "azurefile" ? 1 : 0
  name                = var.template_deployment_param_values.parameterValues.value.sa_name
  resource_group_name = var.template_deployment_param_values.parameterValues.value.sa_resourcegroup
}

data "azurerm_eventgrid_topic" "main" {
  count               = var.template_deployment_params.connectionType.value == "azureeventgridpublish" ? 1 : 0
  name                = var.template_deployment_param_values.parameterValues.value.eg_topic
  resource_group_name = var.template_deployment_param_values.parameterValues.value.eg_resourcegroup
}

# data "azurerm_log_analytics_workspace" "main" {
#   count               = var.template_deployment_params.connectionType.value == "azureloganalyticsdatacollector" ? 1 : 0
#   name                = var.template_deployment_param_values.parameterValues.value.law_name
#   resource_group_name = var.template_deployment_param_values.parameterValues.value.law_resourcegroup
# }

data "azurerm_servicebus_namespace" "main" {
  count               = var.template_deployment_params.connectionType.value == "servicebus" ? 1 : 0
  name                = var.template_deployment_param_values.parameterValues.value.sb_name
  resource_group_name = var.template_deployment_param_values.parameterValues.value.sb_resourcegroup
}

There is also a bug when using an ARM template. The connection runtime URL's are required in the connections.json file for each Logic App so it would be useful to expose these from a data source as they will be dynamic as I push through each environment or if I need to redeploy. They are exposed in the output of the ARM template however ARM template outputs to Terraform currently produce a null value. #12828

Another resource which is required and also missing any documentation is the API Connection Access Policies used to allow access from the Logic App to the required resource type. Again this is missing documentation, this is the ARM template I'm using to create the resource:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connectionName": {
            "type": "String",
            "metadata": {
                "description": "The name for the connection."
            }
        },
        "logicAppSPObjectId": {
            "type": "String",
            "metadata": {
                "description": "The storage account access key"
            }
        },
        "tenantId": {
            "type": "String",
            "metadata": {
                "description": "The tenant ID"
            }
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Web/connections/accessPolicies",
            "apiVersion": "2016-06-01",
            "name": "[concat(parameters('connectionName'),'/',parameters('logicAppSPObjectId'))]",
            "location": "[resourceGroup().location]",
            "dependsOn": [],
            "properties": {
                "principal": {
                    "type": "ActiveDirectory",
                    "identity": {
                        "tenantId": "[parameters('tenantId')]",
                        "objectId": "[parameters('logicAppSPObjectId')]"
                    }
                }
            }
        }
    ]
}

@oWretch
Copy link
Contributor

oWretch commented Dec 13, 2021

Duplicate of #1691

@tombuildsstuff
Copy link
Contributor

Thanks @oWretch - duplicate of #1691

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants