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_tag_description support config API tag description #17876

Merged
merged 13 commits into from
Sep 6, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package apimanagement

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2021-08-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceApiManagementApiTagDescription() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceApiManagementApiTagDescriptionCreateUpdate,
Read: resourceApiManagementApiTagDescriptionRead,
Update: resourceApiManagementApiTagDescriptionCreateUpdate,
Delete: resourceApiManagementApiTagDescriptionDelete,

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

"description": {
Type: pluginsdk.TypeString,
Optional: true,
},

"external_documentation_url": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},

"external_documentation_description": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
}
}

func resourceApiManagementApiTagDescriptionCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.ApiTagDescriptionClient

ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

apiTagId, err := parse.ApiTagID(d.Get("api_tag_id").(string))
if err != nil {
return fmt.Errorf("parsing `api_id`: %v", err)
}

id := parse.NewApiTagDescriptionsID(apiTagId.SubscriptionId, apiTagId.ResourceGroup, apiTagId.ServiceName, apiTagId.ApiName, apiTagId.TagName)

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

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

tagDescParameter := apimanagement.TagDescriptionCreateParameters{TagDescriptionBaseProperties: &apimanagement.TagDescriptionBaseProperties{}}
if v, ok := d.GetOk("description"); ok {
tagDescParameter.Description = utils.String(v.(string))
}

if v, ok := d.GetOk("external_documentation_url"); ok {
tagDescParameter.ExternalDocsURL = utils.String(v.(string))
}

if v, ok := d.GetOk("external_documentation_description"); ok {
tagDescParameter.ExternalDocsDescription = utils.String(v.(string))
}

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

d.SetId(id.ID())

return resourceApiManagementApiTagDescriptionRead(d, meta)
}

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

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

resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.TagDescriptionName)
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)
}

apiTagId := parse.NewApiTagID(id.SubscriptionId, id.ResourceGroup, id.ServiceName, id.ApiName, id.TagDescriptionName)

d.Set("api_tag_id", apiTagId.ID())
d.Set("description", resp.Description)
d.Set("external_documentation_url", resp.ExternalDocsURL)
d.Set("external_documentation_description", resp.ExternalDocsDescription)

return nil
}

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

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

resp, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.TagDescriptionName, "")
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package apimanagement_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type ApiManagementApiTagDescriptionResource struct{}

func TestAccApiManagementApiTagDescription_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_tag_description", "test")
r := ApiManagementApiTagDescriptionResource{}

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

func TestAccApiManagementApiTagDescription_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_tag_description", "test")
r := ApiManagementApiTagDescriptionResource{}

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

func TestAccApiManagementApiTagDescription_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_tag_description", "test")
r := ApiManagementApiTagDescriptionResource{}

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(),
})
}

func (ApiManagementApiTagDescriptionResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ApiTagDescriptionsID(state.ID)
if err != nil {
return nil, err
}

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

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

func (r ApiManagementApiTagDescriptionResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_tag" "test" {
api_management_id = azurerm_api_management.test.id
name = "acctest-Tag-%d"
}

resource "azurerm_api_management_api_tag" "test" {
api_id = azurerm_api_management_api.test.id
name = "acctest-Tag-%d"
}

resource "azurerm_api_management_api_tag_description" "test" {
api_tag_id = azurerm_api_management_api_tag.test.id
description = "tag description"
external_documentation_url = "https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs"
external_documentation_description = "external tag description"
}
`, ApiManagementApiResource{}.basic(data), data.RandomInteger, data.RandomInteger)
}

func (r ApiManagementApiTagDescriptionResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_api_tag_description" "import" {
api_tag_id = azurerm_api_management_api_tag_description.test.api_tag_id
description = azurerm_api_management_api_tag_description.test.description
external_documentation_url = azurerm_api_management_api_tag_description.test.external_documentation_url
external_documentation_description = azurerm_api_management_api_tag_description.test.external_documentation_description
}
`, r.basic(data))
}

func (r ApiManagementApiTagDescriptionResource) update(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_tag" "test" {
api_management_id = azurerm_api_management.test.id
name = "acctest-Tag-%d"
}

resource "azurerm_api_management_api_tag" "test" {
api_id = azurerm_api_management_api.test.id
name = "acctest-Tag-%d"
}

resource "azurerm_api_management_api_tag_description" "test" {
api_tag_id = azurerm_api_management_api_tag.test.id
description = "tag description update"
external_documentation_url = "https://registry.terraform.io/providers/hashicorp/azurerm"
external_documentation_description = "external tag description update"
}
`, ApiManagementApiResource{}.basic(data), data.RandomInteger, data.RandomInteger)
}
5 changes: 5 additions & 0 deletions internal/services/apimanagement/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Client struct {
ApiReleasesClient *apimanagement.APIReleaseClient
ApiSchemasClient *apimanagement.APISchemaClient
ApiVersionSetClient *apimanagement.APIVersionSetClient
ApiTagDescriptionClient *apimanagement.APITagDescriptionClient
AuthorizationServersClient *apimanagement.AuthorizationServerClient
BackendClient *apimanagement.BackendClient
CacheClient *apimanagement.CacheClient
Expand Down Expand Up @@ -72,6 +73,9 @@ func NewClient(o *common.ClientOptions) *Client {
apiVersionSetClient := apimanagement.NewAPIVersionSetClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&apiVersionSetClient.Client, o.ResourceManagerAuthorizer)

apiTagDescriptionClient := apimanagement.NewAPITagDescriptionClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&apiTagDescriptionClient.Client, o.ResourceManagerAuthorizer)

authorizationServersClient := apimanagement.NewAuthorizationServerClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&authorizationServersClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -174,6 +178,7 @@ func NewClient(o *common.ClientOptions) *Client {
ApiReleasesClient: &apiReleasesClient,
ApiSchemasClient: &apiSchemasClient,
ApiVersionSetClient: &apiVersionSetClient,
ApiTagDescriptionClient: &apiTagDescriptionClient,
AuthorizationServersClient: &authorizationServersClient,
BackendClient: &backendClient,
CacheClient: &cacheClient,
Expand Down
Loading