Skip to content

Commit

Permalink
Add ability to turn off subscription key in API Management API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Batzner committed Nov 14, 2019
1 parent 2e9e342 commit 154aad9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
50 changes: 50 additions & 0 deletions azurerm/resource_arm_api_management_api.go
Expand Up @@ -159,6 +159,21 @@ func resourceArmApiManagementApi() *schema.Resource {
},
},

"authentication_settings": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subscription_key_required": {
Type: schema.TypeBool,
Required: true,
},
},
},
},

"soap_pass_through": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -266,6 +281,9 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf
subscriptionKeyParameterNamesRaw := d.Get("subscription_key_parameter_names").([]interface{})
subscriptionKeyParameterNames := expandApiManagementApiSubscriptionKeyParamNames(subscriptionKeyParameterNamesRaw)

authenticationSettingsRaw := d.Get("auhtentication_settings").([]interface{})
authenticationSettings := expandApiManagementAuthenticationSettings(authenticationSettingsRaw)

var apiType apimanagement.APIType
soapPassThrough := d.Get("soap_pass_through").(bool)
if soapPassThrough {
Expand All @@ -284,6 +302,7 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf
ServiceURL: utils.String(serviceUrl),
SubscriptionKeyParameterNames: subscriptionKeyParameterNames,
APIVersion: utils.String(version),
AuthenticationSettings: authenticationSettings,
},
}

Expand Down Expand Up @@ -363,6 +382,10 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("subscription_key_parameter_names", flattenApiManagementApiSubscriptionKeyParamNames(props.SubscriptionKeyParameterNames)); err != nil {
return fmt.Errorf("Error setting `subscription_key_parameter_names`: %+v", err)
}

if err := d.Set("authentication_settings", flattenApiManagementAuthenticationSettings(props.AuthenticationSettings)); err != nil {
return fmt.Errorf("Error setting `authentication_settings`: %+v", err)
}
}

return nil
Expand Down Expand Up @@ -454,3 +477,30 @@ func flattenApiManagementApiSubscriptionKeyParamNames(paramNames *apimanagement.

return []interface{}{result}
}

func expandApiManagementAuthenticationSettings(input []interface{}) *apimanagement.AuthenticationSettingsContract {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})
subscriptionKeyRequired := v["subscriptionKeyRequired"].(bool)
contract := apimanagement.AuthenticationSettingsContract{
SubscriptionKeyRequired: utils.Bool(subscriptionKeyRequired),
}
return &contract
}

func flattenApiManagementAuthenticationSettings(paramNames *apimanagement.AuthenticationSettingsContract) []interface{} {
if paramNames == nil {
return make([]interface{}, 0)
}

result := make(map[string]interface{})

if paramNames.SubscriptionKeyRequired != nil {
result["subscription_key_required"] = *paramNames.SubscriptionKeyRequired
}

return []interface{}{result}
}
4 changes: 4 additions & 0 deletions azurerm/resource_arm_api_management_api_test.go
Expand Up @@ -517,6 +517,10 @@ resource "azurerm_api_management_api" "test" {
header = "X-Butter-Robot-API-Key"
query = "location"
}
authentication_settings {
subscription_key_required = false
}
}
`, template, rInt)
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/d/api_management_api.html.markdown
Expand Up @@ -58,12 +58,20 @@ output "api_management_api_id" {

* `subscription_key_parameter_names` - A `subscription_key_parameter_names` block as documented below.

* `authentication_settings` - An `authentication_settings` block as documented below.

* `version` - The Version number of this API, if this API is versioned.

* `version_set_id` - The ID of the Version Set which this API is associated with.

---

An `authentication_settings` block exports the following:

* `subscription_key_required` - Should this API require a subscription key?

---

A `subscription_key_parameter_names` block exports the following:

* `header` - The name of the HTTP Header which should be used for the Subscription Key.
Expand Down

0 comments on commit 154aad9

Please sign in to comment.