-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2cdcd9c
update
ba8f607
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
ad34b7b
Merge branch 'master' of https://github.com/terraform-providers/terra…
edf2cd8
Merge branch 'master' of https://github.com/terraform-providers/terra…
420097a
revert
e3294fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
86da91a
Merge branch 'master' of https://github.com/terraform-providers/terra…
09b9f8e
Merge branch 'master' of https://github.com/terraform-providers/terra…
2f55300
Merge branch 'master' of https://github.com/terraform-providers/terra…
c03c58d
Merge branch 'master' of https://github.com/terraform-providers/terra…
57c031c
Merge branch 'master' of https://github.com/terraform-providers/terra…
a6d7062
Merge branch 'master' of https://github.com/terraform-providers/terra…
42b8b23
Merge branch 'master' of https://github.com/terraform-providers/terra…
ef31b67
Merge branch 'master' of https://github.com/terraform-providers/terra…
e054708
Merge branch 'master' of https://github.com/terraform-providers/terra…
3f35b09
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
6dc973e
Merge branch 'master' of https://github.com/terraform-providers/terra…
3363bb5
Merge branch 'master' of https://github.com/terraform-providers/terra…
b9d7f9a
Merge branch 'master' of https://github.com/terraform-providers/terra…
014c3bd
Merge branch 'master' of https://github.com/terraform-providers/terra…
9223d60
Merge branch 'master' of https://github.com/terraform-providers/terra…
788fecb
Merge branch 'master' of https://github.com/terraform-providers/terra…
cf090fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
33ae98d
Merge branch 'master' of https://github.com/terraform-providers/terra…
2a4e708
Merge branch 'master' of https://github.com/terraform-providers/terra…
5a48171
Merge branch 'master' of https://github.com/terraform-providers/terra…
1686b95
update
d145003
update
5e038c3
update
0258cb4
update
7f5ba89
update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
azurerm/internal/services/apimanagement/api_management_tag_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,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) | ||
|
||
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 | ||
} |
124 changes: 124 additions & 0 deletions
124
azurerm/internal/services/apimanagement/api_management_tag_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,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) | ||
} |
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,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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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)