Skip to content

Commit

Permalink
New resource azurerm_api_management_api_release (hashicorp#12562)
Browse files Browse the repository at this point in the history
  • Loading branch information
yupwei68 authored and yupwei68 committed Jul 26, 2021
1 parent 1bfe465 commit c8748e9
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 0 deletions.
145 changes: 145 additions & 0 deletions azurerm/internal/services/apimanagement/api_management_api_release.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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))
}
5 changes: 5 additions & 0 deletions azurerm/internal/services/apimanagement/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -143,6 +147,7 @@ func NewClient(o *common.ClientOptions) *Client {
ApiPoliciesClient: &apiPoliciesClient,
ApiOperationsClient: &apiOperationsClient,
ApiOperationPoliciesClient: &apiOperationPoliciesClient,
ApiReleasesClient: &apiReleasesClient,
ApiSchemasClient: &apiSchemasClient,
ApiVersionSetClient: &apiVersionSetClient,
AuthorizationServersClient: &authorizationServersClient,
Expand Down
Loading

0 comments on commit c8748e9

Please sign in to comment.