diff --git a/azurerm/internal/services/apimanagement/api_management_api_release.go b/azurerm/internal/services/apimanagement/api_management_api_release.go new file mode 100644 index 000000000000..681a9b98bb36 --- /dev/null +++ b/azurerm/internal/services/apimanagement/api_management_api_release.go @@ -0,0 +1,145 @@ +package apimanagement + +import ( + "fmt" + "log" + "time" + + "github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2020-12-01/apimanagement" + "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/services/apimanagement/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceApiManagementApiRelease() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Create: resourceApiManagementApiReleaseCreateUpdate, + Read: resourceApiManagementApiReleaseRead, + Update: resourceApiManagementApiReleaseCreateUpdate, + Delete: resourceApiManagementApiReleaseDelete, + + 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), + }, + + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.ApiReleaseID(id) + return err + }), + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.ApiManagementChildName, + }, + + "api_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.ApiID, + }, + + "notes": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + } +} +func resourceApiManagementApiReleaseCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + client := meta.(*clients.Client).ApiManagement.ApiReleasesClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + apiId, err := parse.ApiID(d.Get("api_id").(string)) + if err != nil { + return err + } + id := parse.NewApiReleaseID(subscriptionId, apiId.ResourceGroup, apiId.ServiceName, apiId.Name, name) + ifMatch := "*" + + if d.IsNewResource() { + existing, err := client.Get(ctx, apiId.ResourceGroup, apiId.ServiceName, apiId.Name, name) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for existing %s: %+v", id, err) + } + } + if !utils.ResponseWasNotFound(existing.Response) { + return tf.ImportAsExistsError("azurerm_api_management_api_release", id.ID()) + } + ifMatch = "" + } + + parameters := apimanagement.APIReleaseContract{ + APIReleaseContractProperties: &apimanagement.APIReleaseContractProperties{ + APIID: utils.String(d.Get("api_id").(string)), + Notes: utils.String(d.Get("notes").(string)), + }, + } + + if _, err := client.CreateOrUpdate(ctx, apiId.ResourceGroup, apiId.ServiceName, apiId.Name, name, parameters, ifMatch); err != nil { + return fmt.Errorf("creating/ updating %s: %+v", id, err) + } + + d.SetId(id.ID()) + return resourceApiManagementApiReleaseRead(d, meta) +} + +func resourceApiManagementApiReleaseRead(d *pluginsdk.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + client := meta.(*clients.Client).ApiManagement.ApiReleasesClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.ApiReleaseID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.ReleaseName) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[INFO] apimanagement %s does not exist - removing from state", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("retrieving %s: %+v", id, err) + } + d.Set("name", id.ReleaseName) + if props := resp.APIReleaseContractProperties; props != nil { + d.Set("api_id", parse.NewApiID(subscriptionId, id.ResourceGroup, id.ServiceName, id.ApiName).ID()) + d.Set("notes", props.Notes) + } + return nil +} + +func resourceApiManagementApiReleaseDelete(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.ApiReleasesClient + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.ApiReleaseID(d.Id()) + if err != nil { + return err + } + + if _, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.ReleaseName, "*"); err != nil { + return fmt.Errorf("deleting %s: %+v", id, err) + } + return nil +} diff --git a/azurerm/internal/services/apimanagement/api_management_api_release_resource_test.go b/azurerm/internal/services/apimanagement/api_management_api_release_resource_test.go new file mode 100644 index 000000000000..0d34be906874 --- /dev/null +++ b/azurerm/internal/services/apimanagement/api_management_api_release_resource_test.go @@ -0,0 +1,157 @@ +package apimanagement_test + +import ( + "context" + "fmt" + "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 ApiManagementApiReleaseResource struct { +} + +func TestAccApiManagementApiRelease_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_release", "test") + r := ApiManagementApiReleaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccApiManagementApiRelease_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_release", "test") + r := ApiManagementApiReleaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func TestAccApiManagementApiRelease_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_release", "test") + r := ApiManagementApiReleaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccApiManagementApiRelease_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_release", "test") + r := ApiManagementApiReleaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.update(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func (ApiManagementApiReleaseResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := parse.ApiReleaseID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.ApiManagement.ApiReleasesClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.ReleaseName) + if err != nil { + return nil, fmt.Errorf("reading ApiManagement Api Release (%s): %+v", id, err) + } + + return utils.Bool(resp.ID != nil), nil +} + +func (r ApiManagementApiReleaseResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_release" "test" { + name = "acctest-ApiRelease-%d" + api_id = azurerm_api_management_api.test.id +} +`, ApiManagementApiResource{}.basic(data), data.RandomInteger) +} + +func (r ApiManagementApiReleaseResource) complete(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_release" "test" { + name = "acctest-ApiRelease-%d" + api_id = azurerm_api_management_api.test.id + notes = "Release 1.0" +} +`, ApiManagementApiResource{}.basic(data), data.RandomInteger) +} +func (r ApiManagementApiReleaseResource) update(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_release" "test" { + name = "acctest-ApiRelease-%d" + api_id = azurerm_api_management_api.test.id + notes = "Release 2.0" +} +`, ApiManagementApiResource{}.basic(data), data.RandomInteger) +} + +func (r ApiManagementApiReleaseResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_release" "import" { + name = azurerm_api_management_api_release.test.name + api_id = azurerm_api_management_api_release.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 1850116f96ec..44727ff5ac35 100644 --- a/azurerm/internal/services/apimanagement/client/client.go +++ b/azurerm/internal/services/apimanagement/client/client.go @@ -11,6 +11,7 @@ type Client struct { ApiPoliciesClient *apimanagement.APIPolicyClient ApiOperationsClient *apimanagement.APIOperationClient ApiOperationPoliciesClient *apimanagement.APIOperationPolicyClient + ApiReleasesClient *apimanagement.APIReleaseClient ApiSchemasClient *apimanagement.APISchemaClient ApiVersionSetClient *apimanagement.APIVersionSetClient AuthorizationServersClient *apimanagement.AuthorizationServerClient @@ -56,6 +57,9 @@ func NewClient(o *common.ClientOptions) *Client { apiOperationPoliciesClient := apimanagement.NewAPIOperationPolicyClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&apiOperationPoliciesClient.Client, o.ResourceManagerAuthorizer) + apiReleasesClient := apimanagement.NewAPIReleaseClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&apiReleasesClient.Client, o.ResourceManagerAuthorizer) + apiSchemasClient := apimanagement.NewAPISchemaClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&apiSchemasClient.Client, o.ResourceManagerAuthorizer) @@ -143,6 +147,7 @@ func NewClient(o *common.ClientOptions) *Client { ApiPoliciesClient: &apiPoliciesClient, ApiOperationsClient: &apiOperationsClient, ApiOperationPoliciesClient: &apiOperationPoliciesClient, + ApiReleasesClient: &apiReleasesClient, ApiSchemasClient: &apiSchemasClient, ApiVersionSetClient: &apiVersionSetClient, AuthorizationServersClient: &authorizationServersClient, diff --git a/azurerm/internal/services/apimanagement/parse/api_release.go b/azurerm/internal/services/apimanagement/parse/api_release.go new file mode 100644 index 000000000000..f5e980516c7e --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/api_release.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 ApiReleaseId struct { + SubscriptionId string + ResourceGroup string + ServiceName string + ApiName string + ReleaseName string +} + +func NewApiReleaseID(subscriptionId, resourceGroup, serviceName, apiName, releaseName string) ApiReleaseId { + return ApiReleaseId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ServiceName: serviceName, + ApiName: apiName, + ReleaseName: releaseName, + } +} + +func (id ApiReleaseId) String() string { + segments := []string{ + fmt.Sprintf("Release Name %q", id.ReleaseName), + fmt.Sprintf("Api Name %q", id.ApiName), + fmt.Sprintf("Service Name %q", id.ServiceName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Api Release", segmentsStr) +} + +func (id ApiReleaseId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/apis/%s/releases/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.ApiName, id.ReleaseName) +} + +// ApiReleaseID parses a ApiRelease ID into an ApiReleaseId struct +func ApiReleaseID(input string) (*ApiReleaseId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := ApiReleaseId{ + 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.ApiName, err = id.PopSegment("apis"); err != nil { + return nil, err + } + if resourceId.ReleaseName, err = id.PopSegment("releases"); 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/api_release_test.go b/azurerm/internal/services/apimanagement/parse/api_release_test.go new file mode 100644 index 000000000000..7025346cf062 --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/api_release_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 = ApiReleaseId{} + +func TestApiReleaseIDFormatter(t *testing.T) { + actual := NewApiReleaseID("12345678-1234-9876-4563-123456789012", "resGroup1", "service1", "api1", "release1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/release1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestApiReleaseID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *ApiReleaseId + }{ + + { + // 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 ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/", + Error: true, + }, + + { + // missing value for ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/", + Error: true, + }, + + { + // missing ReleaseName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/", + Error: true, + }, + + { + // missing value for ReleaseName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/release1", + Expected: &ApiReleaseId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ServiceName: "service1", + ApiName: "api1", + ReleaseName: "release1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.APIMANAGEMENT/SERVICE/SERVICE1/APIS/API1/RELEASES/RELEASE1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ApiReleaseID(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.ApiName != v.Expected.ApiName { + t.Fatalf("Expected %q but got %q for ApiName", v.Expected.ApiName, actual.ApiName) + } + if actual.ReleaseName != v.Expected.ReleaseName { + t.Fatalf("Expected %q but got %q for ReleaseName", v.Expected.ReleaseName, actual.ReleaseName) + } + } +} diff --git a/azurerm/internal/services/apimanagement/registration.go b/azurerm/internal/services/apimanagement/registration.go index 2c7cb2bfc1d1..c2372f50c6b5 100644 --- a/azurerm/internal/services/apimanagement/registration.go +++ b/azurerm/internal/services/apimanagement/registration.go @@ -41,6 +41,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_api_management_api_operation_tag": resourceApiManagementApiOperationTag(), "azurerm_api_management_api_operation_policy": resourceApiManagementApiOperationPolicy(), "azurerm_api_management_api_policy": resourceApiManagementApiPolicy(), + "azurerm_api_management_api_release": resourceApiManagementApiRelease(), "azurerm_api_management_api_schema": resourceApiManagementApiSchema(), "azurerm_api_management_api_version_set": resourceApiManagementApiVersionSet(), "azurerm_api_management_authorization_server": resourceApiManagementAuthorizationServer(), diff --git a/azurerm/internal/services/apimanagement/resourceids.go b/azurerm/internal/services/apimanagement/resourceids.go index 9a1b318cca6d..82da654f844f 100644 --- a/azurerm/internal/services/apimanagement/resourceids.go +++ b/azurerm/internal/services/apimanagement/resourceids.go @@ -31,3 +31,4 @@ package apimanagement //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Subscription -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/subscriptions/subscription1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=User -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/users/user1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=OperationTag -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/operations/operation1/tags/tag1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ApiRelease -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/release1 diff --git a/azurerm/internal/services/apimanagement/validate/api_release_id.go b/azurerm/internal/services/apimanagement/validate/api_release_id.go new file mode 100644 index 000000000000..a6e3296c3a3e --- /dev/null +++ b/azurerm/internal/services/apimanagement/validate/api_release_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 ApiReleaseID(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.ApiReleaseID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/azurerm/internal/services/apimanagement/validate/api_release_id_test.go b/azurerm/internal/services/apimanagement/validate/api_release_id_test.go new file mode 100644 index 000000000000..079789cf3597 --- /dev/null +++ b/azurerm/internal/services/apimanagement/validate/api_release_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 TestApiReleaseID(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 ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/", + Valid: false, + }, + + { + // missing value for ApiName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/", + Valid: false, + }, + + { + // missing ReleaseName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/", + Valid: false, + }, + + { + // missing value for ReleaseName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/release1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.APIMANAGEMENT/SERVICE/SERVICE1/APIS/API1/RELEASES/RELEASE1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ApiReleaseID(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_api_release.html.markdown b/website/docs/r/api_management_api_release.html.markdown new file mode 100644 index 000000000000..3b7a832c1a7e --- /dev/null +++ b/website/docs/r/api_management_api_release.html.markdown @@ -0,0 +1,88 @@ +--- +subcategory: "API Management" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_api_management_api_release" +description: |- + Manages a API Management API Release. +--- + +# azurerm_api_management_api_release + +Manages a API Management API Release. + +## Example Usage + +```hcl +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} + +resource "azurerm_api_management" "example" { + name = "example-apim" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + publisher_name = "My Company" + publisher_email = "company@terraform.io" + sku_name = "Developer_1" +} + +resource "azurerm_api_management_api" "example" { + name = "example-api" + resource_group_name = azurerm_resource_group.example.name + api_management_name = azurerm_api_management.example.name + revision = "1" + display_name = "Example API" + path = "example" + protocols = ["https"] + + import { + content_format = "swagger-link-json" + content_value = "http://conferenceapi.azurewebsites.net/?format=json" + } +} + +resource "azurerm_api_management_api_release" "example" { + name = "example-Api-Release" + api_id = azurerm_api_management_api.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name which should be used for this API Management API Release. Changing this forces a new API Management API Release to be created. + +* `api_id` - (Required) The ID of the API Management API. Changing this forces a new API Management API Release to be created. + +--- + +* `notes` - (Optional) The Release Notes. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the API Management API Release. + +## 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 API Release. +* `read` - (Defaults to 5 minutes) Used when retrieving the API Management API Release. +* `update` - (Defaults to 30 minutes) Used when updating the API Management API Release. +* `delete` - (Defaults to 30 minutes) Used when deleting the API Management API Release. + +## Import + +API Management API Releases can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_api_management_api_release.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/service1/apis/api1/releases/release1 +```