Skip to content

Commit

Permalink
new resource: azurerm_resource_tag
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Dec 3, 2022
1 parent e104dea commit b3b5e90
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 0 deletions.
72 changes: 72 additions & 0 deletions internal/services/resource/parse/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package parse

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = TagId{}

type TagId struct {
ResourceId string
Key string
}

func NewTagID(resourceId string, tagKey string) TagId {
return TagId{
ResourceId: resourceId,
Key: tagKey,
}
}

func (id TagId) String() string {
return fmt.Sprintf("Tag %s at scope %s", id.Key, id.ResourceId)
}

func (id TagId) ID() string {
fmtString := "%s|%s"
return fmt.Sprintf(fmtString, id.ResourceId, id.Key)
}

// TagID parses a Tag ID into an TagID struct
func TagID(input string) (*TagId, error) {
segments := strings.Split(input, "|")

id, err := resourceids.ParseAzureResourceID(segments[0])
if err != nil {
return nil, err
}

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

tagId := TagId{
ResourceId: segments[0],
Key: segments[1],
}

if tagId.ResourceId == "" {
return nil, fmt.Errorf("ID was missing the 'resourceId' element")
}

if tagId.Key == "" {
return nil, fmt.Errorf("ID was missing the 'key' element")
}

return &tagId, nil
}

func ValidateResourceTagID(input interface{}, _ string) (warnings []string, errors []error) {
_, err := TagID(input.(string))

errors = []error{}

if err != nil {
errors = append(errors, err)
}

return nil, errors
}
1 change: 1 addition & 0 deletions internal/services/resource/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ResourceProviderRegistrationResource{},
ResourceTagResource{},
}
}
191 changes: 191 additions & 0 deletions internal/services/resource/resource_tag_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package resource

import (
"context"
"fmt"
"time"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type ResourceTagModel struct {
ResourceId string `tfschema:"resource_id"`
Key string `tfschema:"key"`
Value string `tfschema:"value"`
}

type ResourceTagResource struct{}

var _ sdk.ResourceWithUpdate = ResourceTagResource{}

func (r ResourceTagResource) ResourceType() string {
return "azurerm_resource_tag"
}

func (r ResourceTagResource) ModelObject() interface{} {
return &ResourceTagModel{}
}

func (r ResourceTagResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return parse.ValidateResourceTagID
}

func (r ResourceTagResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"resource_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"key": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.TagKey,
},
"value": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.TagValue,
},
}
}

func (r ResourceTagResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r ResourceTagResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ResourceTagModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Resource.TagsClient
id := parse.NewTagID(model.ResourceId, model.Key)
existing, err := client.GetAtScope(ctx, model.ResourceId)

if err != nil {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}

if _, ok := existing.Properties.Tags[model.Key]; ok {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

existing.Properties.Tags[model.Key] = &model.Value

if _, err := client.CreateOrUpdateAtScope(ctx, model.ResourceId, existing); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r ResourceTagResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.TagsClient

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

var model ResourceTagModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

resp, err := client.GetAtScope(ctx, id.ResourceId)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if metadata.ResourceData.HasChange("value") {
resp.Properties.Tags[model.Key] = &model.Value
}

if _, err := client.CreateOrUpdateAtScope(ctx, id.ResourceId, resp); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return nil
},
}
}

func (r ResourceTagResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.TagsClient

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

resp, err := client.GetAtScope(ctx, id.ResourceId)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if _, ok := resp.Properties.Tags[id.Key]; !ok {
return metadata.MarkAsGone(id)
}

state := ResourceTagModel{
ResourceId: id.ResourceId,
Key: id.Key,
Value: *resp.Properties.Tags[id.Key],
}

return metadata.Encode(&state)
},
}
}

func (r ResourceTagResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.TagsClient

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

var model ResourceTagModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

resp, err := client.GetAtScope(ctx, id.ResourceId)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

delete(resp.Properties.Tags, model.Key)

if _, err := client.CreateOrUpdateAtScope(ctx, id.ResourceId, resp); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return nil
},
}
}
Loading

0 comments on commit b3b5e90

Please sign in to comment.