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

New Resource: azurerm_logic_app_integration_account_map #13187

Merged
merged 2 commits into from
Sep 2, 2021
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
5 changes: 5 additions & 0 deletions internal/services/logic/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Client struct {
IntegrationAccountClient *logic.IntegrationAccountsClient
IntegrationAccountCertificateClient *logic.IntegrationAccountCertificatesClient
IntegrationAccountMapClient *logic.IntegrationAccountMapsClient
IntegrationAccountPartnerClient *logic.IntegrationAccountPartnersClient
IntegrationAccountSchemaClient *logic.IntegrationAccountSchemasClient
IntegrationAccountSessionClient *logic.IntegrationAccountSessionsClient
Expand All @@ -23,6 +24,9 @@ func NewClient(o *common.ClientOptions) *Client {
integrationAccountCertificateClient := logic.NewIntegrationAccountCertificatesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&integrationAccountCertificateClient.Client, o.ResourceManagerAuthorizer)

integrationAccountMapClient := logic.NewIntegrationAccountMapsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&integrationAccountMapClient.Client, o.ResourceManagerAuthorizer)

integrationAccountPartnerClient := logic.NewIntegrationAccountPartnersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&integrationAccountPartnerClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -44,6 +48,7 @@ func NewClient(o *common.ClientOptions) *Client {
return &Client{
IntegrationAccountClient: &integrationAccountClient,
IntegrationAccountCertificateClient: &integrationAccountCertificateClient,
IntegrationAccountMapClient: &integrationAccountMapClient,
IntegrationAccountPartnerClient: &integrationAccountPartnerClient,
IntegrationAccountSchemaClient: &integrationAccountSchemaClient,
IntegrationAccountSessionClient: &integrationAccountSessionClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package logic

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/logic/mgmt/2019-05-01/logic"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logic/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logic/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceLogicAppIntegrationAccountMap() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceLogicAppIntegrationAccountMapCreateUpdate,
Read: resourceLogicAppIntegrationAccountMapRead,
Update: resourceLogicAppIntegrationAccountMapCreateUpdate,
Delete: resourceLogicAppIntegrationAccountMapDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.IntegrationAccountMapID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.IntegrationAccountMapName(),
},

"resource_group_name": azure.SchemaResourceGroupName(),

"integration_account_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.IntegrationAccountName(),
},

"content": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"map_type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(logic.MapTypeXslt),
string(logic.MapTypeXslt20),
string(logic.MapTypeXslt30),
string(logic.MapTypeLiquid),
}, false),
},

"metadata": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
}

func resourceLogicAppIntegrationAccountMapCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Logic.IntegrationAccountMapClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewIntegrationAccountMapID(subscriptionId, d.Get("resource_group_name").(string), d.Get("integration_account_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.IntegrationAccountName, id.MapName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_logic_app_integration_account_map", id.ID())
}
}

parameters := logic.IntegrationAccountMap{
IntegrationAccountMapProperties: &logic.IntegrationAccountMapProperties{
MapType: logic.MapType(d.Get("map_type").(string)),
Content: utils.String(d.Get("content").(string)),
ContentType: utils.String("application/xml"),
},
}

if v, ok := d.GetOk("metadata"); ok {
metadata := v.(map[string]interface{})
parameters.IntegrationAccountMapProperties.Metadata = &metadata
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.IntegrationAccountName, id.MapName, parameters); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceLogicAppIntegrationAccountMapRead(d, meta)
}

func resourceLogicAppIntegrationAccountMapRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.IntegrationAccountMapClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.IntegrationAccountMapID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.IntegrationAccountName, id.MapName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] %s was not found - removing from state", *id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.MapName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("integration_account_name", id.IntegrationAccountName)

if props := resp.IntegrationAccountMapProperties; props != nil {
d.Set("map_type", props.MapType)
d.Set("content", d.Get("content").(string))

if props.Metadata != nil {
metadata := props.Metadata.(map[string]interface{})
d.Set("metadata", metadata)
}
}

return nil
}

func resourceLogicAppIntegrationAccountMapDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.IntegrationAccountMapClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.IntegrationAccountMapID(d.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.IntegrationAccountName, id.MapName); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package logic_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logic/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type LogicAppIntegrationAccountMapResource struct{}

func TestAccLogicAppIntegrationAccountMap_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_integration_account_map", "test")
r := LogicAppIntegrationAccountMapResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("content"), // not returned from the API
})
}

func TestAccLogicAppIntegrationAccountMap_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_integration_account_map", "test")
r := LogicAppIntegrationAccountMapResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func TestAccLogicAppIntegrationAccountMap_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_integration_account_map", "test")
r := LogicAppIntegrationAccountMapResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.complete(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("content"), // not returned from the API
})
}

func TestAccLogicAppIntegrationAccountMap_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_integration_account_map", "test")
r := LogicAppIntegrationAccountMapResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.complete(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("content"), // not returned from the API
{
Config: r.update(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("content"), // not returned from the API
})
}

func (r LogicAppIntegrationAccountMapResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.IntegrationAccountMapID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Logic.IntegrationAccountMapClient.Get(ctx, id.ResourceGroup, id.IntegrationAccountName, id.MapName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %q %+v", id, err)
}

return utils.Bool(resp.IntegrationAccountMapProperties != nil), nil
}

func (r LogicAppIntegrationAccountMapResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-logic-%d"
location = "%s"
}

resource "azurerm_logic_app_integration_account" "test" {
name = "acctest-ia-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (r LogicAppIntegrationAccountMapResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_logic_app_integration_account_map" "test" {
name = "acctest-iamap-%d"
resource_group_name = azurerm_resource_group.test.name
integration_account_name = azurerm_logic_app_integration_account.test.name
map_type = "Xslt"
content = file("testdata/integration_account_map_content.xsd")
}
`, r.template(data), data.RandomInteger)
}

func (r LogicAppIntegrationAccountMapResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_logic_app_integration_account_map" "import" {
name = azurerm_logic_app_integration_account_map.test.name
resource_group_name = azurerm_logic_app_integration_account_map.test.resource_group_name
integration_account_name = azurerm_logic_app_integration_account_map.test.integration_account_name
map_type = azurerm_logic_app_integration_account_map.test.map_type
content = azurerm_logic_app_integration_account_map.test.content
}
`, r.basic(data))
}

func (r LogicAppIntegrationAccountMapResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_logic_app_integration_account_map" "test" {
name = "acctest-iamap-%d"
resource_group_name = azurerm_resource_group.test.name
integration_account_name = azurerm_logic_app_integration_account.test.name
map_type = "Xslt"
content = file("testdata/integration_account_map_content.xsd")

metadata = {
foo = "bar"
}
}
`, r.template(data), data.RandomInteger)
}

func (r LogicAppIntegrationAccountMapResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_logic_app_integration_account_map" "test" {
name = "acctest-iamap-%d"
resource_group_name = azurerm_resource_group.test.name
integration_account_name = azurerm_logic_app_integration_account.test.name
map_type = "Xslt20"
content = file("testdata/integration_account_map_content2.xsd")

metadata = {
foo = "bar2"
}
}
`, r.template(data), data.RandomInteger)
}
Loading