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_tag #12535

Merged
merged 31 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1686b95
update
Jul 9, 2021
d145003
update
Jul 9, 2021
5e038c3
update
Jul 9, 2021
0258cb4
update
Jul 30, 2021
7f5ba89
update
Jul 30, 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
155 changes: 155 additions & 0 deletions azurerm/internal/services/apimanagement/api_management_tag_resource.go
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 resourceApiManagementTag() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceApiManagementTagCreateUpdate,
Read: resourceApiManagementTagRead,
Update: resourceApiManagementTagCreateUpdate,
Delete: resourceApiManagementTagDelete,

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

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

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

func resourceApiManagementTagCreateUpdate(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()

apiManagementId, err := parse.ApiManagementID(d.Get("api_management_id").(string))
if err != nil {
return err
}

id := parse.NewTagID(subscriptionId, apiManagementId.ResourceGroup, apiManagementId.ServiceName, d.Get("name").(string))

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

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_api_management_tag", id.ID())
}
}
displayName := d.Get("name").(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
displayName := d.Get("name").(string)
displayName := name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed name := d.get("name").(string), so I keep displayName := d.Get("name").(string)


if v, ok := d.GetOk("display_name"); ok {
displayName = v.(string)
}

parameters := apimanagement.TagCreateUpdateParameters{
TagContractProperties: &apimanagement.TagContractProperties{
DisplayName: &displayName,
},
}

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

d.SetId(id.ID())

return resourceApiManagementTagRead(d, meta)
}

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

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

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

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

d.Set("api_management_id", parse.NewApiManagementID(id.SubscriptionId, id.ResourceGroup, id.ServiceName).ID())
d.Set("name", id.Name)

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

return nil
}

func resourceApiManagementTagDelete(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.TagID(d.Id())
if err != nil {
return err
}

if _, err = client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.Name, ""); 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,124 @@
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 ApiManagementTagResource struct {
}

func TestAccApiManagementTag_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_tag", "test")
r := ApiManagementTagResource{}

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

func TestAccApiManagementTag_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_tag", "test")
r := ApiManagementTagResource{}

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

func TestAccApiManagementTag_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_tag", "test")
r := ApiManagementTagResource{}

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 (ApiManagementTagResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.TagID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.ApiManagement.TagClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
return nil, fmt.Errorf("reading %q: %+v", id, err)
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
}

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

func (r ApiManagementTagResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_api_management_tag" "test" {
api_management_id = azurerm_api_management.test.id
name = "acctest-Op-Tag-%[2]d"
}
`, ApiManagementResource{}.consumption(data), data.RandomInteger)
}

func (r ApiManagementTagResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_api_management_tag" "import" {
api_management_id = azurerm_api_management_tag.test.api_management_id
name = azurerm_api_management_tag.test.name
}
`, r.basic(data))
}

func (r ApiManagementTagResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_api_management_tag" "test" {
api_management_id = azurerm_api_management.test.id
name = "acctest-Op-Tag-%[2]d"
display_name = "Display-Op-Tag Updated"
}
`, ApiManagementResource{}.consumption(data), data.RandomInteger)
}
75 changes: 75 additions & 0 deletions azurerm/internal/services/apimanagement/parse/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 TagId struct {
SubscriptionId string
ResourceGroup string
ServiceName string
Name string
}

func NewTagID(subscriptionId, resourceGroup, serviceName, name string) TagId {
return TagId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
ServiceName: serviceName,
Name: name,
}
}

func (id TagId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Service Name %q", id.ServiceName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Tag", segmentsStr)
}

func (id TagId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/tags/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.Name)
}

// TagID parses a Tag ID into an TagId struct
func TagID(input string) (*TagId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := TagId{
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.Name, err = id.PopSegment("tags"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
Loading