Skip to content

Commit

Permalink
New resource azurerm_api_management_api_operation_tag (hashicorp#12384
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yupwei68 authored and yupwei68 committed Jul 26, 2021
1 parent fa1d995 commit 036c7d7
Show file tree
Hide file tree
Showing 11 changed files with 749 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ resource "azurerm_api_management" "test" {
resource_group_name = azurerm_resource_group.test.name
publisher_name = "pub1"
publisher_email = "[email protected]"
sku_name = "Developer_1"
sku_name = "Consumption_0"
}
resource "azurerm_api_management_api" "test" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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 resourceApiManagementApiOperationTag() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceApiManagementApiOperationTagCreateUpdate,
Read: resourceApiManagementApiOperationTagRead,
Update: resourceApiManagementApiOperationTagCreateUpdate,
Delete: resourceApiManagementApiOperationTagDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.OperationTagID(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_operation_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiOperationID,
},

"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiManagementChildName,
},

"display_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}

func resourceApiManagementApiOperationTagCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).ApiManagement.TagClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

apiOperationId, err := parse.ApiOperationID(d.Get("api_operation_id").(string))
if err != nil {
return err
}
name := d.Get("name").(string)

id := parse.NewOperationTagID(subscriptionId, apiOperationId.ResourceGroup, apiOperationId.ServiceName, apiOperationId.ApiName, apiOperationId.OperationName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, apiOperationId.ResourceGroup, apiOperationId.ServiceName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Tag %q: %s", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_api_management_api_operation_tag", id.ID())
}
}

parameters := apimanagement.TagCreateUpdateParameters{
TagContractProperties: &apimanagement.TagContractProperties{
DisplayName: utils.String(d.Get("display_name").(string)),
},
}

if _, err := client.CreateOrUpdate(ctx, apiOperationId.ResourceGroup, apiOperationId.ServiceName, name, parameters, ""); err != nil {
return fmt.Errorf("creating/updating %q: %+v", id, err)
}

if _, err := client.AssignToOperation(ctx, apiOperationId.ResourceGroup, apiOperationId.ServiceName, apiOperationId.ApiName, apiOperationId.OperationName, name); err != nil {
return fmt.Errorf("assigning to operation %q: %+v", id, err)
}

d.SetId(id.ID())

return resourceApiManagementApiOperationTagRead(d, meta)
}

func resourceApiManagementApiOperationTagRead(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).ApiManagement.TagClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.OperationTagID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.TagName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] %q was not found - removing from state!", id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %q: %+v", id, err)
}

d.Set("api_operation_id", parse.NewApiOperationID(subscriptionId, id.ResourceGroup, id.ServiceName, id.ApiName, id.OperationName).ID())
d.Set("name", id.TagName)

if props := resp.TagContractProperties; props != nil {
d.Set("display_name", props.DisplayName)
}

return nil
}

func resourceApiManagementApiOperationTagDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.TagClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.OperationTagID(d.Id())
if err != nil {
return err
}

if _, err = client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.TagName, ""); err != nil {
return fmt.Errorf("deleting %q: %+v", id, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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 ApiManagementApiOperationTagResource struct {
}

func TestAccApiManagementApiOperationTag_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_operation_tag", "test")
r := ApiManagementApiOperationTagResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccApiManagementApiOperationTag_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_operation_tag", "test")
r := ApiManagementApiOperationTagResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func TestAccApiManagementApiOperationTag_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_operation_tag", "test")
r := ApiManagementApiOperationTagResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(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 (ApiManagementApiOperationTagResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.OperationTagID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.ApiManagement.TagClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.TagName)
if err != nil {
return nil, fmt.Errorf("reading %q: %+v", id, err)
}

return utils.Bool(resp.ID != nil), nil
}

func (r ApiManagementApiOperationTagResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_api_operation_tag" "test" {
api_operation_id = azurerm_api_management_api_operation.test.id
name = "acctest-Op-Tag-%d"
display_name = "Display-Op-Tag"
}
`, ApiManagementApiOperationResource{}.basic(data), data.RandomInteger)
}

func (r ApiManagementApiOperationTagResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_api_operation_tag" "import" {
api_operation_id = azurerm_api_management_api_operation_tag.test.api_operation_id
name = azurerm_api_management_api_operation_tag.test.name
display_name = azurerm_api_management_api_operation_tag.test.display_name
}
`, r.basic(data))
}

func (r ApiManagementApiOperationTagResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_api_operation_tag" "test" {
api_operation_id = azurerm_api_management_api_operation.test.id
name = "acctest-Op-Tag-%d"
display_name = "Display-Op-Tag Updated"
}
`, ApiManagementApiOperationResource{}.basic(data), data.RandomInteger)
}
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 @@ -34,6 +34,7 @@ type Client struct {
SignInClient *apimanagement.SignInSettingsClient
SignUpClient *apimanagement.SignUpSettingsClient
SubscriptionsClient *apimanagement.SubscriptionClient
TagClient *apimanagement.TagClient
TenantAccessClient *apimanagement.TenantAccessClient
UsersClient *apimanagement.UserClient
}
Expand Down Expand Up @@ -123,6 +124,9 @@ func NewClient(o *common.ClientOptions) *Client {
subscriptionsClient := apimanagement.NewSubscriptionClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&subscriptionsClient.Client, o.ResourceManagerAuthorizer)

tagClient := apimanagement.NewTagClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&tagClient.Client, o.ResourceManagerAuthorizer)

tenantAccessClient := apimanagement.NewTenantAccessClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&tenantAccessClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -158,6 +162,7 @@ func NewClient(o *common.ClientOptions) *Client {
SignInClient: &signInClient,
SignUpClient: &signUpClient,
SubscriptionsClient: &subscriptionsClient,
TagClient: &tagClient,
TenantAccessClient: &tenantAccessClient,
UsersClient: &usersClient,
}
Expand Down
Loading

0 comments on commit 036c7d7

Please sign in to comment.