Skip to content

Commit

Permalink
Feat: add support for azapi (#208)
Browse files Browse the repository at this point in the history
* Feat: add support for azapi

* update test

* more use-cases

* more use-cases
  • Loading branch information
TomerHeber authored Nov 24, 2024
1 parent 1574f88 commit 1992269
Show file tree
Hide file tree
Showing 7 changed files with 602 additions and 10 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ linters:
- errname
- errorlint
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- goconst
- gocritic
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ TERRATAG_DEFAULT_TO_TERRAFORM
- `google`
- `azurerm`
- `azurestack`
- `azapi`

## Develop

Expand Down
23 changes: 14 additions & 9 deletions internal/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const AZURE = "azure"

var resourcesToSkip = []string{"azurerm_api_management_named_value"}

var prefixes = map[string]Provider{
"aws_": AWS,
"google_": GCP,
"azurerm_": AZURE,
"azurestack_": AZURE,
"azapi_": AZURE,
}

func getProviderByResource(resourceType string) Provider {
switch {
case strings.HasPrefix(resourceType, "aws_"):
return AWS
case strings.HasPrefix(resourceType, "google_"):
return GCP
case strings.HasPrefix(resourceType, "azurerm_") || strings.HasPrefix(resourceType, "azurestack_"):
return AZURE
default:
return ""
for prefix, provider := range prefixes {
if strings.HasPrefix(resourceType, prefix) {
return provider
}
}

return ""
}

func IsTaggableByAttribute(resourceType string, attribute string) bool {
Expand Down
1 change: 1 addition & 0 deletions test/fixture/terraform_latest/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ suites:
- google_vs_google_beta
- aws_instance_volume_tags
- provider_function
- azapi
202 changes: 202 additions & 0 deletions test/tests/azapi/expected/main.terratag.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
}
}
}

provider "azapi" {
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "example-rg"
location = "west europe"
tags = local.terratag_added_main
}

resource "azurerm_user_assigned_identity" "example" {
name = "example"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
tags = local.terratag_added_main
}

resource "azapi_resource" "example" {
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
name = "registry1"
parent_id = azurerm_resource_group.example.id

location = azurerm_resource_group.example.location
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.example.id]
}

body = {
sku = {
name = "Standard"
}
properties = {
adminUserEnabled = true
}
}

tags = merge({
"Key" = "Value"
}, local.terratag_added_main)

response_export_values = ["properties.loginServer", "properties.policies.quarantinePolicy.status"]
}

resource "azapi_resource" "example2" {
type = "Microsoft.ContainerRegistry/registries@2020-11-01-preview"
name = "registry2"
parent_id = azurerm_resource_group.example.id

location = azurerm_resource_group.example.location
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.example.id]
}

body = {
sku = {
name = "Standard"
}
properties = {
adminUserEnabled = true
}
}

response_export_values = ["properties.loginServer", "properties.policies.quarantinePolicy.status"]
tags = local.terratag_added_main
}

data "azurerm_synapse_workspace" "example" {
name = "example-workspace"
resource_group_name = azurerm_resource_group.example.name
}

resource "azapi_data_plane_resource" "dataset" {
type = "Microsoft.Synapse/workspaces/datasets@2020-12-01"
parent_id = trimprefix(data.azurerm_synapse_workspace.example.connectivity_endpoints.dev, "https://")
name = "example-dataset"
body = {
properties = {
type = "AzureBlob",
typeProperties = {
folderPath = {
value = "@dataset().MyFolderPath"
type = "Expression"
}
fileName = {
value = "@dataset().MyFileName"
type = "Expression"
}
format = {
type = "TextFormat"
}
}
parameters = {
MyFolderPath = {
type = "String"
}
MyFileName = {
type = "String"
}
}
}
}
}

variable "enabled" {
type = bool
default = false
description = "whether start the spring service"
}

resource "azurerm_spring_cloud_service" "test" {
name = "example-spring"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku_name = "S0"
tags = local.terratag_added_main
}

resource "azapi_resource_action" "start" {
type = "Microsoft.AppPlatform/Spring@2022-05-01-preview"
resource_id = azurerm_spring_cloud_service.test.id
action = "start"
response_export_values = ["*"]

count = var.enabled ? 1 : 0
}

resource "azapi_resource_action" "stop" {
type = "Microsoft.AppPlatform/Spring@2022-05-01-preview"
resource_id = azurerm_spring_cloud_service.test.id
action = "stop"
response_export_values = ["*"]

count = var.enabled ? 0 : 1
}

resource "azurerm_public_ip" "example" {
name = "example-ip"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
allocation_method = "Static"
tags = local.terratag_added_main
}

resource "azurerm_lb" "example" {
name = "example-lb"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name

frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.example.id
}
tags = local.terratag_added_main
}

resource "azurerm_lb_nat_rule" "example" {
resource_group_name = azurerm_resource_group.example.name
loadbalancer_id = azurerm_lb.example.id
name = "RDPAccess"
protocol = "Tcp"
frontend_port = 3389
backend_port = 3389
frontend_ip_configuration_name = "PublicIPAddress"
}

resource "azapi_update_resource" "example" {
type = "Microsoft.Network/loadBalancers@2021-03-01"
resource_id = azurerm_lb.example.id

body = {
properties = {
inboundNatRules = [
{
properties = {
idleTimeoutInMinutes = 15
}
}
]
}
}

depends_on = [
azurerm_lb_nat_rule.example,
]
}
locals {
terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"}
}

Loading

0 comments on commit 1992269

Please sign in to comment.