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 non-string type for parameters of azurerm_managed_application #8632

Merged
merged 8 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package managedapplications

import (
"bytes"
"encoding/json"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/managedapplications"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -71,13 +74,25 @@ func resourceManagedApplication() *schema.Resource {
},

"parameters": {
Type: schema.TypeMap,
Optional: true,
Type: schema.TypeMap,
Optional: true,
Computed: true,
Deprecated: "Deprecated in favour of `parameter_values`",
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
ConflictsWith: []string{"parameter_values"},
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"parameter_values": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: structure.SuppressJsonDiff,
ConflictsWith: []string{"parameters"},
},

"plan": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -158,26 +173,20 @@ func resourceManagedApplicationCreateUpdate(d *schema.ResourceData, meta interfa
ManagedResourceGroupID: utils.String(targetResourceGroupId),
}
}

if v, ok := d.GetOk("application_definition_id"); ok {
parameters.ApplicationDefinitionID = utils.String(v.(string))
}

if v, ok := d.GetOk("plan"); ok {
parameters.Plan = expandManagedApplicationPlan(v.([]interface{}))
}
if v, ok := d.GetOk("parameters"); ok {
params := v.(map[string]interface{})

newParams := make(map[string]interface{}, len(params))
for key, val := range params {
newParams[key] = struct {
Value interface{} `json:"value"`
}{
Value: val,
}
}

parameters.Parameters = &newParams
params, err := expandManagedApplicationParameters(d)
if err != nil {
return fmt.Errorf("Error expanding `parameters` or `parameter_values`: %+v", err)
}
parameters.Parameters = params

future, err := client.CreateOrUpdate(ctx, resourceGroupName, name, parameters)
if err != nil {
Expand Down Expand Up @@ -235,6 +244,12 @@ func resourceManagedApplicationRead(d *schema.ResourceData, meta interface{}) er
d.Set("managed_resource_group_name", id.ResourceGroup)
d.Set("application_definition_id", props.ApplicationDefinitionID)

parameterValues, err := flattenManagedApplicationParameterValuesValueToString(props.Parameters)
if err != nil {
return fmt.Errorf("serializing JSON from `parameter_values`: %+v", err)
}
d.Set("parameter_values", parameterValues)

if err = d.Set("parameters", flattenManagedApplicationParametersOrOutputs(props.Parameters)); err != nil {
return err
}
Expand Down Expand Up @@ -284,6 +299,30 @@ func expandManagedApplicationPlan(input []interface{}) *managedapplications.Plan
}
}

func expandManagedApplicationParameters(d *schema.ResourceData) (*map[string]interface{}, error) {
newParams := make(map[string]interface{})

if v, ok := d.GetOk("parameter_values"); ok {
if err := json.Unmarshal([]byte(v.(string)), &newParams); err != nil {
return nil, fmt.Errorf("unmarshalling `parameter_values`: %+v", err)
}
}

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

for key, val := range params {
newParams[key] = struct {
Value interface{} `json:"value"`
}{
Value: val,
}
}
}

return &newParams, nil
}

func flattenManagedApplicationPlan(input *managedapplications.Plan) []interface{} {
results := make([]interface{}, 0)
if input == nil {
Expand Down Expand Up @@ -336,3 +375,27 @@ func flattenManagedApplicationParametersOrOutputs(input interface{}) map[string]

return results
}

func flattenManagedApplicationParameterValuesValueToString(input interface{}) (string, error) {
if input == nil {
return "", nil
}

for k, v := range input.(map[string]interface{}) {
if v != nil {
delete(input.(map[string]interface{})[k].(map[string]interface{}), "type")
}
}

result, err := json.Marshal(input)
if err != nil {
return "", err
}

compactJson := bytes.Buffer{}
if err := json.Compact(&compactJson, result); err != nil {
return "", err
}

return compactJson.String(), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ func TestAccManagedApplication_update(t *testing.T) {
})
}

func TestAccManagedApplication_parameterValues(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a test that goes string -> non-string -> string and checking to make sure the correct property is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tc

data := acceptance.BuildTestData(t, "azurerm_managed_application", "test")
r := ManagedApplicationResource{}

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

func (ManagedApplicationResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.ApplicationID(state.ID)
if err != nil {
Expand Down Expand Up @@ -188,6 +203,29 @@ resource "azurerm_managed_application" "test" {
`, r.template(data), data.RandomInteger, data.RandomInteger)
}

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

resource "azurerm_managed_application" "test" {
name = "acctestManagedApp%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "ServiceCatalog"
managed_resource_group_name = "infraGroup%d"
application_definition_id = azurerm_managed_application_definition.test.id

parameter_values = <<VALUES
{
"location": {"value": "${azurerm_resource_group.test.location}"},
"storageAccountNamePrefix": {"value": "store%s"},
"storageAccountType": {"value": "Standard_LRS"}
}
VALUES
}
`, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomString)
}

func (ManagedApplicationResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/managed_application.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ The following arguments are supported:

* `parameters` - (Optional) A mapping of name and value pairs to pass to the managed application as parameters.

* `parameter_values` - (Optional) The parameter values to pass to the Managed Application. This field is a json object that allows you to assign parameters to this Managed Application.

* `plan` - (Optional) One `plan` block as defined below.

* `tags` - (Optional) A mapping of tags to assign to the resource.
Expand Down