-
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.
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
- Loading branch information
Showing
6 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
} |
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
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,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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.