-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3010 from terraform-providers/f/api-management-api
New Resource: `azurerm_api_management_api`
- Loading branch information
Showing
13 changed files
with
1,755 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.