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_api_management_api #3010

Merged
merged 38 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
86f7705
Added basic support for API Management API
torresdal Oct 9, 2018
e97cb7d
Cleaned up unused schema elements of API Management API
torresdal Oct 11, 2018
14eba35
Added docs for api_management_api resource
torresdal Oct 12, 2018
8ab94d1
Added missing fields to schema
torresdal Oct 12, 2018
308f7d3
Cleaned up, tested and added data to schema fields
torresdal Oct 12, 2018
5e0b3bc
Cleanup:
torresdal Oct 13, 2018
7e60e9e
Updated docs for API Management API resource and data to reflect rece…
torresdal Oct 13, 2018
8ccd016
Fixed sidebar issues in doc
torresdal Oct 13, 2018
4b2f6bf
Removed display_name option - name works for both
torresdal Oct 13, 2018
e9de296
Add simple test for data source
torresdal Oct 13, 2018
3c8b334
Add simple test for API Management API
torresdal Oct 13, 2018
046e7f3
Testing:
torresdal Oct 13, 2018
93cd651
Fixed error found in integration tests:
torresdal Oct 13, 2018
b3857dc
Check for generated name in test. Use local swagger file instead of url.
torresdal Oct 13, 2018
faba325
Output APIM Service name on error
tombuildsstuff Oct 23, 2018
2f4992a
Output APIM Service name on error
tombuildsstuff Oct 23, 2018
7f0d5f2
Added debug line when APIM API does not exist
tombuildsstuff Oct 23, 2018
d1c50e9
Grammar improvement
tombuildsstuff Oct 23, 2018
2ca63a5
Grammar improvement
tombuildsstuff Oct 23, 2018
530929a
Grammar improvement
tombuildsstuff Oct 23, 2018
4134a35
Fixed and made changes according to review
torresdal Oct 25, 2018
28daf66
Fixed failing test by removing Computed on revision for api-managemen…
torresdal Oct 25, 2018
f83994a
Replaced ip's with int.test url in example wsdl using for int.test
torresdal Oct 25, 2018
13119ac
Fixed issues after running int.tests.
torresdal Oct 26, 2018
0c75f2f
Don't mix name and display_name, seperate them as in azure api
torresdal Nov 22, 2018
4d8f6f3
Simplyfied create/update with/without import api
torresdal Nov 22, 2018
2fad08d
Documented display name. Removed defaults for header and query
torresdal Nov 22, 2018
2460dda
Fixed empty return statements
torresdal Nov 22, 2018
d4a2981
Removed unused props var
torresdal Nov 22, 2018
af6606c
Fixed issue where display_name is a mandatory value for the Azure API
torresdal Nov 23, 2018
ef0d918
Added missing display_name to int. tests
torresdal Nov 23, 2018
425d7dd
fixing the build
tombuildsstuff Mar 1, 2019
710b3f8
refactoring
tombuildsstuff Mar 1, 2019
3d86e68
fixing a bad merge conflict
tombuildsstuff Mar 4, 2019
90931f8
Refactoring `azurerm_api_management_api`
tombuildsstuff Mar 6, 2019
2c00119
Adding back in the resources missing from a bad merge conflict with m…
tombuildsstuff Mar 6, 2019
d4f836d
Fixing the failing test
tombuildsstuff Mar 6, 2019
bee4eca
r/api_management_api: additional validation / making revision a string
tombuildsstuff Mar 15, 2019
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 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 {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
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