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_operation_tag #12384

Merged
merged 23 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
546fb0a
update
Jun 23, 2021
6bfbb57
update
Jun 24, 2021
f3394d5
update
Jun 28, 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
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