-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_api_management_api_tag_description
support co…
…nfig API tag description (#17876) Co-authored-by: xuzhang3 <Zhangxu894765>
- Loading branch information
Showing
10 changed files
with
757 additions
and
0 deletions.
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
internal/services/apimanagement/api_management_api_tag_description_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
138 changes: 138 additions & 0 deletions
138
internal/services/apimanagement/api_management_api_tag_description_resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.