Skip to content

Commit

Permalink
Merge pull request #3010 from terraform-providers/f/api-management-api
Browse files Browse the repository at this point in the history
New Resource: `azurerm_api_management_api`
  • Loading branch information
tombuildsstuff authored Mar 15, 2019
2 parents 34e2b99 + bee4eca commit 7565458
Show file tree
Hide file tree
Showing 13 changed files with 1,755 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

// API Management
apiManagementApiClient apimanagement.APIClient
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
Expand Down Expand Up @@ -494,6 +495,10 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool, partn
}

func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
apisClient := apimanagement.NewAPIClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&apisClient.Client, auth)
c.apiManagementApiClient = apisClient

groupsClient := apimanagement.NewGroupClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&groupsClient.Client, auth)
c.apiManagementGroupClient = groupsClient
Expand Down
184 changes: 184 additions & 0 deletions azurerm/data_source_api_management_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceApiManagementApi() *schema.Resource {
return &schema.Resource{
Read: dataSourceApiManagementApiRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementApiName,
},

"api_management_name": azure.SchemaApiManagementDataSourceName(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"revision": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"description": {
Type: schema.TypeString,
Computed: true,
},

"display_name": {
Type: schema.TypeString,
Computed: true,
},

"is_current": {
Type: schema.TypeBool,
Computed: true,
},

"is_online": {
Type: schema.TypeBool,
Computed: true,
},

"path": {
Type: schema.TypeString,
Computed: true,
},

"protocols": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"service_url": {
Type: schema.TypeString,
Computed: true,
},

"soap_pass_through": {
Type: schema.TypeBool,
Computed: true,
},

"subscription_key_parameter_names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"header": {
Type: schema.TypeString,
Computed: true,
},
"query": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"version": {
Type: schema.TypeString,
Computed: true,
},

"version_set_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceApiManagementApiRead(d *schema.ResourceData, meta interface{}) error {
ctx := meta.(*ArmClient).StopContext
client := meta.(*ArmClient).apiManagementApiClient

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)
revision := d.Get("revision").(string)

apiId := fmt.Sprintf("%s;rev=%s", name, revision)
resp, err := client.Get(ctx, resourceGroup, serviceName, apiId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("API %q Revision %q (API Management Service %q / Resource Group %q) does not exist!", name, revision, serviceName, resourceGroup)
}

return fmt.Errorf("Error retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("api_management_name", serviceName)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)

if props := resp.APIContractProperties; props != nil {
d.Set("description", props.Description)
d.Set("display_name", props.DisplayName)
d.Set("is_current", props.IsCurrent)
d.Set("is_online", props.IsOnline)
d.Set("path", props.Path)
d.Set("revision", props.APIRevision)
d.Set("service_url", props.ServiceURL)
d.Set("soap_pass_through", string(props.APIType) == string(apimanagement.SoapPassThrough))
d.Set("version", props.APIVersion)
d.Set("version_set_id", props.APIVersionSetID)

if err := d.Set("protocols", flattenApiManagementApiDataSourceProtocols(props.Protocols)); err != nil {
return fmt.Errorf("Error setting `protocols`: %s", err)
}

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

return nil
}

func flattenApiManagementApiDataSourceProtocols(input *[]apimanagement.Protocol) []string {
if input == nil {
return []string{}
}

results := make([]string, 0)
for _, v := range *input {
results = append(results, string(v))
}

return results
}
func flattenApiManagementApiDataSourceSubscriptionKeyParamNames(paramNames *apimanagement.SubscriptionKeyParameterNamesContract) []interface{} {
if paramNames == nil {
return make([]interface{}, 0)
}

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

if paramNames.Header != nil {
result["header"] = *paramNames.Header
}

if paramNames.Query != nil {
result["query"] = *paramNames.Query
}

return []interface{}{result}
}
90 changes: 90 additions & 0 deletions azurerm/data_source_api_management_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMApiManagementApi_basic(t *testing.T) {
dataSourceName := "data.azurerm_api_management_api.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApiManagementApi_basic(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "display_name", "api1"),
resource.TestCheckResourceAttr(dataSourceName, "path", "api1"),
resource.TestCheckResourceAttr(dataSourceName, "protocols.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "protocols.0", "https"),
resource.TestCheckResourceAttr(dataSourceName, "soap_pass_through", "false"),
resource.TestCheckResourceAttr(dataSourceName, "is_current", "true"),
resource.TestCheckResourceAttr(dataSourceName, "is_online", "false"),
),
},
},
})
}

func TestAccDataSourceAzureRMApiManagementApi_complete(t *testing.T) {
dataSourceName := "data.azurerm_api_management_api.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApiManagementApi_complete(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "display_name", "Butter Parser"),
resource.TestCheckResourceAttr(dataSourceName, "path", "butter-parser"),
resource.TestCheckResourceAttr(dataSourceName, "protocols.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "description", "What is my purpose? You parse butter."),
resource.TestCheckResourceAttr(dataSourceName, "service_url", "https://example.com/foo/bar"),
resource.TestCheckResourceAttr(dataSourceName, "soap_pass_through", "false"),
resource.TestCheckResourceAttr(dataSourceName, "subscription_key_parameter_names.0.header", "X-Butter-Robot-API-Key"),
resource.TestCheckResourceAttr(dataSourceName, "subscription_key_parameter_names.0.query", "location"),
resource.TestCheckResourceAttr(dataSourceName, "is_current", "true"),
resource.TestCheckResourceAttr(dataSourceName, "is_online", "false"),
),
},
},
})
}

func testAccDataSourceApiManagementApi_basic(rInt int, location string) string {
template := testAccAzureRMApiManagementApi_basic(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_api_management_api" "test" {
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}"
revision = "${azurerm_api_management_api.test.revision}"
}
`, template)
}

func testAccDataSourceApiManagementApi_complete(rInt int, location string) string {
template := testAccAzureRMApiManagementApi_complete(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_api_management_api" "test" {
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}"
revision = "${azurerm_api_management_api.test.revision}"
}
`, template)
}
18 changes: 18 additions & 0 deletions azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ func ApiManagementServicePublisherEmail(v interface{}, k string) (warnings []str

return warnings, errors
}

func ApiManagementApiName(v interface{}, k string) (ws []string, es []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[^*#&+]{1,256}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only be up to 256 characters in length and not include the characters `*`, `#`, `&` or `+`", k))
}
return ws, es
}

func ApiManagementApiPath(v interface{}, k string) (ws []string, es []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[\w][\w-/.]+[\w-]$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only be up to 256 characters in length, not start or end with `/` and only contain valid url characters", k))
}
return ws, es
}
Loading

0 comments on commit 7565458

Please sign in to comment.