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_release #12562

Merged
merged 31 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2cdcd9c
update
May 26, 2021
ba8f607
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
May 27, 2021
ad34b7b
Merge branch 'master' of https://github.com/terraform-providers/terra…
May 31, 2021
edf2cd8
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 1, 2021
420097a
revert
Jun 1, 2021
e3294fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 4, 2021
86da91a
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 7, 2021
09b9f8e
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 9, 2021
2f55300
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 10, 2021
c03c58d
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 10, 2021
57c031c
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
a6d7062
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
42b8b23
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
ef31b67
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 15, 2021
e054708
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 16, 2021
3f35b09
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
Jun 16, 2021
6dc973e
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 18, 2021
3363bb5
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 21, 2021
b9d7f9a
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 22, 2021
014c3bd
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 23, 2021
9223d60
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 25, 2021
788fecb
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 28, 2021
cf090fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 29, 2021
33ae98d
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jul 1, 2021
2a4e708
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jul 9, 2021
5a48171
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jul 9, 2021
3b9e95f
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jul 12, 2021
97e98db
update
Jul 12, 2021
fe19e7e
update
Jul 12, 2021
9ce6af0
update
Jul 13, 2021
9e6a21c
update
Jul 15, 2021
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
142 changes: 142 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,142 @@
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)

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 %q: %+v", id, err)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_api_management_api_release", id.ID())
}
}

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, ""); err != nil {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("creating/ updating %q: %+v", id, err)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
}

d.SetId(id.ID())
return resourceApiManagementApiReleaseRead(d, meta)
}

func resourceApiManagementApiReleaseRead(d *pluginsdk.ResourceData, meta interface{}) error {
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 %q does not exist - removing from state", d.Id())
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %q: %+v", id, err)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
}
d.Set("name", id.ReleaseName)
if props := resp.APIReleaseContractProperties; props != nil {
d.Set("api_id", props.APIID)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
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 %q: %+v", id, err)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
}
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