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

Enhance - azurerm_api_management_api_operatio - support config example, schema_id and type_name #18409

Merged
merged 1 commit into from
Sep 21, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ func resourceApiManagementApiOperationRead(d *pluginsdk.ResourceData, meta inter
return fmt.Errorf("flattening `response`: %+v", err)
}

flattenedTemplateParams := schemaz.FlattenApiManagementOperationParameterContract(props.TemplateParameters)
flattenedTemplateParams, err := schemaz.FlattenApiManagementOperationParameterContract(props.TemplateParameters)
if err != nil {
return err
}

if err := d.Set("template_parameter", flattenedTemplateParams); err != nil {
return fmt.Errorf("flattening `template_parameter`: %+v", err)
}
Expand Down Expand Up @@ -301,8 +305,18 @@ func flattenApiManagementOperationRequestContract(input *apimanagement.RequestCo
output["description"] = *input.Description
}

output["header"] = schemaz.FlattenApiManagementOperationParameterContract(input.Headers)
output["query_parameter"] = schemaz.FlattenApiManagementOperationParameterContract(input.QueryParameters)
header, err := schemaz.FlattenApiManagementOperationParameterContract(input.Headers)
if err != nil {
return nil, err
}
output["header"] = header

queryParameter, err := schemaz.FlattenApiManagementOperationParameterContract(input.QueryParameters)
if err != nil {
return nil, err
}
output["query_parameter"] = queryParameter

representation, err := schemaz.FlattenApiManagementOperationRepresentation(input.Representations)
if err != nil {
return nil, err
Expand Down Expand Up @@ -365,7 +379,11 @@ func flattenApiManagementOperationResponseContract(input *[]apimanagement.Respon
output["status_code"] = int(*v.StatusCode)
}

output["header"] = schemaz.FlattenApiManagementOperationParameterContract(v.Headers)
header, err := schemaz.FlattenApiManagementOperationParameterContract(v.Headers)
if err != nil {
return nil, err
}
output["header"] = header

representation, err := schemaz.FlattenApiManagementOperationRepresentation(v.Representations)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ resource "azurerm_api_management_api_operation" "test" {
func (r ApiManagementApiOperationResource) headers(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_api_schema" "test" {
api_name = azurerm_api_management_api.test.name
api_management_name = azurerm_api_management_api.test.api_management_name
resource_group_name = azurerm_api_management_api.test.resource_group_name
schema_id = "acctestSchema%d"
content_type = "application/json"
value = file("testdata/api_management_api_schema_swagger.json")
}

resource "azurerm_api_management_api_operation" "test" {
operation_id = "acctest-operation"
Expand Down Expand Up @@ -379,6 +387,16 @@ resource "azurerm_api_management_api_operation" "test" {
name = "X-Test-Operation"
required = true
type = "string"

type_name = "User"
schema_id = azurerm_api_management_api_schema.test.schema_id
example {
description = "This is a test description"
external_value = "https://example.com/foo/bar"
name = "test"
summary = "This is a test summary"
value = "backend-Request-Test"
}
}

representation {
Expand All @@ -401,7 +419,7 @@ SAMPLE
}
}
}
`, r.template(data))
`, r.template(data), data.RandomInteger)
}

func (r ApiManagementApiOperationResource) representation(data acceptance.TestData) string {
Expand Down
51 changes: 47 additions & 4 deletions internal/services/apimanagement/schemaz/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ func FlattenApiManagementOperationRepresentation(input *[]apimanagement.Represen
output["content_type"] = *v.ContentType
}

output["form_parameter"] = FlattenApiManagementOperationParameterContract(v.FormParameters)
formParameter, err := FlattenApiManagementOperationParameterContract(v.FormParameters)
if err != nil {
return nil, err
}
output["form_parameter"] = formParameter

if v.Examples != nil {
example, err := FlattenApiManagementOperationParameterExampleContract(v.Examples)
Expand Down Expand Up @@ -236,6 +240,18 @@ func SchemaApiManagementOperationParameterContract() *pluginsdk.Schema {
},
Set: pluginsdk.HashString,
},

"example": SchemaApiManagementOperationParameterExampleContract(),

"schema_id": {
Type: pluginsdk.TypeString,
Optional: true,
},

"type_name": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
}
Expand All @@ -257,13 +273,24 @@ func ExpandApiManagementOperationParameterContract(d *pluginsdk.ResourceData, sc
required := vs["required"].(bool)
valuesRaw := vs["values"].(*pluginsdk.Set).List()

schemaId := vs["schema_id"].(string)
typeName := vs["type_name"].(string)
examples := make(map[string]*apimanagement.ParameterExampleContract)
if vs["example"] != nil {
examplesRaw := vs["example"].([]interface{})
examples = ExpandApiManagementOperationParameterExampleContract(examplesRaw)
}

output := apimanagement.ParameterContract{
Name: utils.String(name),
Description: utils.String(description),
Type: utils.String(paramType),
Required: utils.Bool(required),
DefaultValue: nil,
Values: utils.ExpandStringSlice(valuesRaw),
SchemaID: utils.String(schemaId),
TypeName: utils.String(typeName),
Examples: examples,
}

// DefaultValue must be included in Values, else it returns error
Expand All @@ -278,9 +305,9 @@ func ExpandApiManagementOperationParameterContract(d *pluginsdk.ResourceData, sc
return &outputs
}

func FlattenApiManagementOperationParameterContract(input *[]apimanagement.ParameterContract) []interface{} {
func FlattenApiManagementOperationParameterContract(input *[]apimanagement.ParameterContract) ([]interface{}, error) {
if input == nil {
return []interface{}{}
return []interface{}{}, nil
}

outputs := make([]interface{}, 0)
Expand Down Expand Up @@ -309,10 +336,26 @@ func FlattenApiManagementOperationParameterContract(input *[]apimanagement.Param

output["values"] = pluginsdk.NewSet(pluginsdk.HashString, utils.FlattenStringSlice(v.Values))

if v.Examples != nil {
example, err := FlattenApiManagementOperationParameterExampleContract(v.Examples)
if err != nil {
return nil, err
}
output["example"] = example
}

if v.SchemaID != nil {
output["schema_id"] = *v.SchemaID
}

if v.TypeName != nil {
output["type_name"] = *v.TypeName
}

outputs = append(outputs, output)
}

return outputs
return outputs, nil
}

func SchemaApiManagementOperationParameterExampleContract() *pluginsdk.Schema {
Expand Down
23 changes: 23 additions & 0 deletions website/docs/r/api_management_api_operation.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ A `form_parameter` block supports the following:
* `default_value` - (Optional) The default value for this Form Parameter.

* `values` - (Optional) One or more acceptable values for this Form Parameter.

* `example` - (Optional) (Optional) One or more `example` blocks as defined above.

* `schema_id` - (Optional) The name of the Schema.

* `type_name` - (Optional) The type name defined by the Schema.

---

Expand All @@ -111,6 +117,11 @@ A `header` block supports the following:

* `values` - (Optional) One or more acceptable values for this Header.

* `example` - (Optional) (Optional) One or more `example` blocks as defined above.

* `schema_id` - (Optional) The name of the Schema.

* `type_name` - (Optional) The type name defined by the Schema.
---

A `query_parameter` block supports the following:
Expand All @@ -127,6 +138,12 @@ A `query_parameter` block supports the following:

* `values` - (Optional) One or more acceptable values for this Query Parameter.

* `example` - (Optional) (Optional) One or more `example` blocks as defined above.

* `schema_id` - (Optional) The name of the Schema.

* `type_name` - (Optional) The type name defined by the Schema.

---

A `request` block supports the following:
Expand Down Expand Up @@ -187,6 +204,12 @@ A `template_parameter` block supports the following:

* `values` - (Optional) One or more acceptable values for this Template Parameter.

* `example` - (Optional) (Optional) One or more `example` blocks as defined above.

* `schema_id` - (Optional) The name of the Schema.

* `type_name` - (Optional) The type name defined by the Schema.

---

## Attributes Reference
Expand Down