diff --git a/azurerm/internal/services/apimanagement/api_management_gateway_api_resource.go b/azurerm/internal/services/apimanagement/api_management_gateway_api_resource.go new file mode 100644 index 000000000000..5f3f49da3062 --- /dev/null +++ b/azurerm/internal/services/apimanagement/api_management_gateway_api_resource.go @@ -0,0 +1,146 @@ +package apimanagement + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2020-12-01/apimanagement" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceApiManagementGatewayApi() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Create: resourceApiManagementGatewayApiCreate, + Read: resourceApiManagementGatewayApiRead, + Delete: resourceApiManagementGatewayApiDelete, + + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.GatewayApiID(id) + return err + }), + + Timeouts: &pluginsdk.ResourceTimeout{ + Create: pluginsdk.DefaultTimeout(30 * time.Minute), + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + Update: pluginsdk.DefaultTimeout(30 * time.Minute), + Delete: pluginsdk.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*pluginsdk.Schema{ + "api_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: azure.ValidateResourceID, + }, + "gateway_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: azure.ValidateResourceID, + }, + }, + } +} + +func resourceApiManagementGatewayApiCreate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.GatewayApisClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + apiID, err := parse.ApiID(d.Get("api_id").(string)) + if err != nil { + return fmt.Errorf("parsing `api_id`: %v", err) + } + + gatewayID, err := parse.GatewayID(d.Get("gateway_id").(string)) + if err != nil { + return fmt.Errorf("parsing `gateway_id`: %v", err) + } + + exists, err := client.GetEntityTag(ctx, gatewayID.ResourceGroup, gatewayID.ServiceName, gatewayID.Name, apiID.Name) + if err != nil { + if !utils.ResponseWasStatusCode(exists, http.StatusNoContent) { + if !utils.ResponseWasNotFound(exists) { + return fmt.Errorf("checking for presence of existing %s: %+v", gatewayID, err) + } + } + } + + id := parse.NewGatewayApiID(gatewayID.SubscriptionId, gatewayID.ResourceGroup, gatewayID.ServiceName, gatewayID.Name, apiID.Name) + if !utils.ResponseWasNotFound(exists) { + return tf.ImportAsExistsError("azurerm_api_management_gateway_api", id.ID()) + } + params := &apimanagement.AssociationContract{} + _, err = client.CreateOrUpdate(ctx, gatewayID.ResourceGroup, gatewayID.ServiceName, gatewayID.Name, apiID.Name, params) + if err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + d.SetId(id.ID()) + + return resourceApiManagementGatewayApiRead(d, meta) +} + +func resourceApiManagementGatewayApiRead(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.GatewayApisClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.GatewayApiID(d.Id()) + if err != nil { + return err + } + + apiId := parse.NewApiID(id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.ApiName) + resp, err := client.GetEntityTag(ctx, id.ResourceGroup, id.ServiceName, id.GatewayName, id.ApiName) + if err != nil { + if utils.ResponseWasNotFound(resp) { + log.Printf("[DEBUG] %s does not exist - removing from state!", id) + d.SetId("") + return nil + } + if utils.ResponseWasStatusCode(resp, http.StatusNoContent) { + log.Printf("[DEBUG] %s returned with No Content status - bypassing and moving on!", id) + } else { + return fmt.Errorf("retrieving %s: %+v", id, err) + } + } + if utils.ResponseWasNotFound(resp) { + log.Printf("[DEBUG] %s was not found - removing from state!", id) + d.SetId("") + return nil + } + gateway := parse.NewGatewayID(id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.GatewayName) + + d.Set("api_id", apiId.ID()) + d.Set("gateway_id", gateway.ID()) + + return nil +} + +func resourceApiManagementGatewayApiDelete(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.GatewayApisClient + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.GatewayApiID(d.Id()) + if err != nil { + return err + } + + if resp, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.GatewayName, id.ApiName); err != nil { + if !utils.ResponseWasNotFound(resp) { + return fmt.Errorf("removing %s: %+v", id, err) + } + } + + return nil +} diff --git a/azurerm/internal/services/apimanagement/api_management_gateway_api_resource_test.go b/azurerm/internal/services/apimanagement/api_management_gateway_api_resource_test.go new file mode 100644 index 000000000000..86d9226fdcfe --- /dev/null +++ b/azurerm/internal/services/apimanagement/api_management_gateway_api_resource_test.go @@ -0,0 +1,128 @@ +package apimanagement_test + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +type ApiManagementGatewayAPIResource struct { +} + +func TestAccApiManagementGatewayApi_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_gateway_api", "test") + r := ApiManagementGatewayAPIResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccApiManagementGatewayApi_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_gateway_api", "test") + r := ApiManagementGatewayAPIResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func (ApiManagementGatewayAPIResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := parse.GatewayApiID(state.ID) + if err != nil { + return nil, err + } + if resp, err := clients.ApiManagement.GatewayApisClient.GetEntityTag(ctx, id.ResourceGroup, id.ServiceName, id.GatewayName, id.ApiName); err != nil { + if utils.ResponseWasNotFound(resp) { + return nil, fmt.Errorf("reading ApiManagement Gateway (%s): %+v", id, err) + } + + if !utils.ResponseWasStatusCode(resp, http.StatusNoContent) { + return nil, fmt.Errorf("reading ApiManagement Gateway (%s): %+v", id, err) + } + } + + return utils.Bool(true), nil +} + +func (ApiManagementGatewayAPIResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_api_management" "test" { + name = "acctestAM-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + publisher_name = "pub1" + publisher_email = "pub1@email.com" + + sku_name = "Developer_1" +} + +resource "azurerm_api_management_gateway" "test" { + name = "acctestAMGateway-%d" + api_management_id = azurerm_api_management.test.id + description = "this is a test gateway" + + location_data { + name = "old world" + city = "test city" + district = "test district" + region = "test region" + } +} + +resource "azurerm_api_management_api" "test" { + name = "acctestapi-%d" + resource_group_name = azurerm_resource_group.test.name + api_management_name = azurerm_api_management.test.name + display_name = "api1" + path = "api1" + protocols = ["https"] + revision = "1" +} + +resource "azurerm_api_management_gateway_api" "test" { + gateway_id = azurerm_api_management_gateway.test.id + api_id = azurerm_api_management_api.test.id +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) +} + +func (r ApiManagementGatewayAPIResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_gateway_api" "import" { + gateway_id = azurerm_api_management_gateway_api.test.gateway_id + api_id = azurerm_api_management_gateway_api.test.api_id +} +`, r.basic(data)) +} diff --git a/azurerm/internal/services/apimanagement/client/client.go b/azurerm/internal/services/apimanagement/client/client.go index 44727ff5ac35..035bc88b5346 100644 --- a/azurerm/internal/services/apimanagement/client/client.go +++ b/azurerm/internal/services/apimanagement/client/client.go @@ -21,6 +21,7 @@ type Client struct { DiagnosticClient *apimanagement.DiagnosticClient EmailTemplateClient *apimanagement.EmailTemplateClient GatewayClient *apimanagement.GatewayClient + GatewayApisClient *apimanagement.GatewayAPIClient GroupClient *apimanagement.GroupClient GroupUsersClient *apimanagement.GroupUserClient IdentityProviderClient *apimanagement.IdentityProviderClient @@ -87,6 +88,9 @@ func NewClient(o *common.ClientOptions) *Client { gatewayClient := apimanagement.NewGatewayClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&gatewayClient.Client, o.ResourceManagerAuthorizer) + gatewayApisClient := apimanagement.NewGatewayAPIClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&gatewayApisClient.Client, o.ResourceManagerAuthorizer) + groupClient := apimanagement.NewGroupClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&groupClient.Client, o.ResourceManagerAuthorizer) @@ -157,6 +161,7 @@ func NewClient(o *common.ClientOptions) *Client { DiagnosticClient: &diagnosticClient, EmailTemplateClient: &emailTemplateClient, GatewayClient: &gatewayClient, + GatewayApisClient: &gatewayApisClient, GroupClient: &groupClient, GroupUsersClient: &groupUsersClient, IdentityProviderClient: &identityProviderClient, diff --git a/azurerm/internal/services/apimanagement/parse/gateway_api.go b/azurerm/internal/services/apimanagement/parse/gateway_api.go new file mode 100644 index 000000000000..390291ae72a6 --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/gateway_api.go @@ -0,0 +1,81 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + "strings" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type GatewayApiId struct { + SubscriptionId string + ResourceGroup string + ServiceName string + GatewayName string + ApiName string +} + +func NewGatewayApiID(subscriptionId, resourceGroup, serviceName, gatewayName, apiName string) GatewayApiId { + return GatewayApiId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ServiceName: serviceName, + GatewayName: gatewayName, + ApiName: apiName, + } +} + +func (id GatewayApiId) String() string { + segments := []string{ + fmt.Sprintf("Api Name %q", id.ApiName), + fmt.Sprintf("Gateway Name %q", id.GatewayName), + fmt.Sprintf("Service Name %q", id.ServiceName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Gateway Api", segmentsStr) +} + +func (id GatewayApiId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/gateways/%s/apis/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.GatewayName, id.ApiName) +} + +// GatewayApiID parses a GatewayApi ID into an GatewayApiId struct +func GatewayApiID(input string) (*GatewayApiId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := GatewayApiId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + if resourceId.ServiceName, err = id.PopSegment("service"); err != nil { + return nil, err + } + if resourceId.GatewayName, err = id.PopSegment("gateways"); err != nil { + return nil, err + } + if resourceId.ApiName, err = id.PopSegment("apis"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/azurerm/internal/services/apimanagement/parse/gateway_api_test.go b/azurerm/internal/services/apimanagement/parse/gateway_api_test.go new file mode 100644 index 000000000000..80550acf6012 --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/gateway_api_test.go @@ -0,0 +1,144 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid" +) + +var _ resourceid.Formatter = GatewayApiId{} + +func TestGatewayApiIDFormatter(t *testing.T) { + actual := NewGatewayApiID("12345678-1234-9876-4563-123456789012", "resGroup1", "service1", "gateway1", "api1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/api1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestGatewayApiID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *GatewayApiId + }{ + + { + // empty + Input: "", + Error: true, + }, + + { + // missing SubscriptionId + Input: "/", + Error: true, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Error: true, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Error: true, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Error: true, + }, + + { + // missing ServiceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/", + Error: true, + }, + + { + // missing value for ServiceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/", + Error: true, + }, + + { + // missing GatewayName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/", + Error: true, + }, + + { + // missing value for GatewayName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/", + Error: true, + }, + + { + // missing ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/", + Error: true, + }, + + { + // missing value for ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/api1", + Expected: &GatewayApiId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ServiceName: "service1", + GatewayName: "gateway1", + ApiName: "api1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.APIMANAGEMENT/SERVICE/SERVICE1/GATEWAYS/GATEWAY1/APIS/API1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := GatewayApiID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %s", err) + } + if v.Error { + t.Fatal("Expect an error but didn't get one") + } + + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.ServiceName != v.Expected.ServiceName { + t.Fatalf("Expected %q but got %q for ServiceName", v.Expected.ServiceName, actual.ServiceName) + } + if actual.GatewayName != v.Expected.GatewayName { + t.Fatalf("Expected %q but got %q for GatewayName", v.Expected.GatewayName, actual.GatewayName) + } + if actual.ApiName != v.Expected.ApiName { + t.Fatalf("Expected %q but got %q for ApiName", v.Expected.ApiName, actual.ApiName) + } + } +} diff --git a/azurerm/internal/services/apimanagement/registration.go b/azurerm/internal/services/apimanagement/registration.go index c2372f50c6b5..718e5906bbf3 100644 --- a/azurerm/internal/services/apimanagement/registration.go +++ b/azurerm/internal/services/apimanagement/registration.go @@ -51,6 +51,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_api_management_diagnostic": resourceApiManagementDiagnostic(), "azurerm_api_management_email_template": resourceApiManagementEmailTemplate(), "azurerm_api_management_gateway": resourceApiManagementGateway(), + "azurerm_api_management_gateway_api": resourceApiManagementGatewayApi(), "azurerm_api_management_group": resourceApiManagementGroup(), "azurerm_api_management_group_user": resourceApiManagementGroupUser(), "azurerm_api_management_identity_provider_aad": resourceApiManagementIdentityProviderAAD(), diff --git a/azurerm/internal/services/apimanagement/resourceids.go b/azurerm/internal/services/apimanagement/resourceids.go index 82da654f844f..9c534cc468fe 100644 --- a/azurerm/internal/services/apimanagement/resourceids.go +++ b/azurerm/internal/services/apimanagement/resourceids.go @@ -16,6 +16,7 @@ package apimanagement //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Diagnostic -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/diagnostics/diagnostic1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=EmailTemplate -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/templates/template1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Gateway -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=GatewayApi -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/api1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Group -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/groups/group1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=GroupUser -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/groups/group1/users/user1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=IdentityProvider -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/identityProviders/identityProvider1 diff --git a/azurerm/internal/services/apimanagement/validate/gateway_api_id.go b/azurerm/internal/services/apimanagement/validate/gateway_api_id.go new file mode 100644 index 000000000000..8c8f25723f2b --- /dev/null +++ b/azurerm/internal/services/apimanagement/validate/gateway_api_id.go @@ -0,0 +1,23 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse" +) + +func GatewayApiID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.GatewayApiID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/azurerm/internal/services/apimanagement/validate/gateway_api_id_test.go b/azurerm/internal/services/apimanagement/validate/gateway_api_id_test.go new file mode 100644 index 000000000000..82ce16f87847 --- /dev/null +++ b/azurerm/internal/services/apimanagement/validate/gateway_api_id_test.go @@ -0,0 +1,100 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestGatewayApiID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Valid: false, + }, + + { + // missing ServiceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/", + Valid: false, + }, + + { + // missing value for ServiceName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/", + Valid: false, + }, + + { + // missing GatewayName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/", + Valid: false, + }, + + { + // missing value for GatewayName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/", + Valid: false, + }, + + { + // missing ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/", + Valid: false, + }, + + { + // missing value for ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/api1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.APIMANAGEMENT/SERVICE/SERVICE1/GATEWAYS/GATEWAY1/APIS/API1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := GatewayApiID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/website/docs/r/api_management_gateway_api.html.markdown b/website/docs/r/api_management_gateway_api.html.markdown new file mode 100644 index 000000000000..a76eedf69c9a --- /dev/null +++ b/website/docs/r/api_management_gateway_api.html.markdown @@ -0,0 +1,69 @@ +--- +subcategory: "API Management" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_api_management_gateway_api" +description: |- + Manages a API Management Gateway API. +--- + +# azurerm_api_management_gateway_api + +Manages a API Management Gateway API. + +## Example Usage + +```hcl +data "azurerm_api_management" "example" { + name = "example-api" + resource_group_name = "example-resources" +} + +data "azurerm_api_management_api" "example" { + name = "search-api" + api_management_name = data.azurerm_api_management.example.name + resource_group_name = data.azurerm_api_management.example.resource_group_name + revision = "2" +} + +data "azurerm_api_management_gateway" "example" { + gateway_id = "my-gateway" + api_management_name = data.azurerm_api_management.example.name + resource_group_name = data.azurerm_api_management.example.resource_group_name +} + +resource "azurerm_api_management_gateway_api" "example" { + gateway_id = data.azurerm_api_management_gateway.example.id + api_id = data.azurerm_api_management_api.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `gateway_id` - (Required) The Identifier for the API Management Gateway. Changing this forces a new API Management Gateway API to be created. + +* `api_id` - (Required) The Identifier of the API Management API within the API Management Service. Changing this forces a new API Management Gateway API to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the API Management Gateway API. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the API Management Gateway API. +* `read` - (Defaults to 5 minutes) Used when retrieving the API Management Gateway API. +* `update` - (Defaults to 30 minutes) Used when updating the API Management Gateway API. +* `delete` - (Defaults to 30 minutes) Used when deleting the API Management Gateway API. + +## Import + +API Management Gateway APIs can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_api_management_gateway_api.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/gateways/gateway1/apis/api1 +```