-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add VDB group update and tag management with acceptance tests #126
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package provider | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
|
||
|
@@ -18,6 +19,9 @@ func resourceVdbGroup() *schema.Resource { | |
ReadContext: resourceVdbGroupRead, | ||
UpdateContext: resourceVdbGroupUpdate, | ||
DeleteContext: resourceVdbGroupDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
|
@@ -35,18 +39,39 @@ func resourceVdbGroup() *schema.Resource { | |
Type: schema.TypeString, | ||
}, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"key": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceVdbGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
|
||
var diags diag.Diagnostics | ||
|
||
client := meta.(*apiClient).client | ||
|
||
vdbGroupCreateReq := *dctapi.NewCreateVDBGroupRequest(d.Get("name").(string)) | ||
vdbGroupCreateReq.SetVdbIds(toStringArray(d.Get("vdb_ids"))) | ||
|
||
if v, has_v := d.GetOk("tags"); has_v { | ||
vdbGroupCreateReq.SetTags(toTagArray(v)) | ||
} | ||
|
||
apiRes, httpRes, err := client.VDBGroupsAPI.CreateVdbGroup(ctx).CreateVDBGroupRequest(vdbGroupCreateReq).Execute() | ||
|
||
if diags := apiErrorResponseHelper(ctx, apiRes, httpRes, err); diags != nil { | ||
|
@@ -64,7 +89,6 @@ func resourceVdbGroupCreate(ctx context.Context, d *schema.ResourceData, meta in | |
} | ||
|
||
func resourceVdbGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
|
||
client := meta.(*apiClient).client | ||
|
||
var diags diag.Diagnostics | ||
|
@@ -80,12 +104,151 @@ func resourceVdbGroupRead(ctx context.Context, d *schema.ResourceData, meta inte | |
|
||
d.Set("name", apiRes.GetName()) | ||
d.Set("vdb_ids", apiRes.GetVdbIds()) | ||
d.Set("tags", flattenTags(apiRes.GetTags())) | ||
return diags | ||
} | ||
|
||
func resourceVdbGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*apiClient).client | ||
vdbGroupId := d.Id() | ||
|
||
// Existing update logic | ||
updateVdbGroupReq := *dctapi.NewUpdateVDBGroupParameters() | ||
if d.HasChange("name") { | ||
updateVdbGroupReq.SetName(d.Get("name").(string)) | ||
} | ||
if d.HasChange("vdb_ids") { | ||
updateVdbGroupReq.SetVdbIds(toStringArray(d.Get("vdb_ids"))) | ||
} | ||
|
||
_, httpRes, err := client.VDBGroupsAPI.UpdateVdbGroupById(ctx, vdbGroupId).UpdateVDBGroupParameters(updateVdbGroupReq).Execute() | ||
if diags := apiErrorResponseHelper(ctx, nil, httpRes, err); diags != nil { | ||
return diags | ||
} | ||
|
||
return diag.Errorf("not implemented") | ||
// Polling logic for name/vdb_ids update | ||
maxAttempts := 10 | ||
for attempt := 1; attempt <= maxAttempts; attempt++ { | ||
status, err := PollVdbGroupStatus(ctx, client, vdbGroupId) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if status == "RUNNING" { | ||
break | ||
} | ||
if status == "FAILED" || status == "CANCELED" { | ||
return diag.Errorf("VDB group update failed with status: %s", status) | ||
} | ||
time.Sleep(time.Duration(STATUS_POLL_SLEEP_TIME) * time.Second) | ||
} | ||
|
||
// Tag update logic | ||
if d.HasChange("tags") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This update logic looks too complex and I am not sure if we really want a lookup logic around. As of now there are no tag update api. we explicitly need to delete the old one and create a new one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since tags are used for managing access, there could be potential race conditions when tags that should not be deleted get deleted as part of the update if we take the approach of deleting and recreating all tags? |
||
oldTags, newTags := d.GetChange("tags") | ||
oldTagList := oldTags.([]interface{}) | ||
newTagList := newTags.([]interface{}) | ||
|
||
// Create a map of old tags for easy lookup | ||
oldTagMap := make(map[string]map[string]bool) | ||
for _, tag := range oldTagList { | ||
tagMap := tag.(map[string]interface{}) | ||
key := tagMap["key"].(string) | ||
value := tagMap["value"].(string) | ||
if _, exists := oldTagMap[key]; !exists { | ||
oldTagMap[key] = make(map[string]bool) | ||
} | ||
oldTagMap[key][value] = true | ||
} | ||
|
||
// Create a map of new tags for easy lookup | ||
newTagMap := make(map[string]map[string]bool) | ||
for _, tag := range newTagList { | ||
tagMap := tag.(map[string]interface{}) | ||
key := tagMap["key"].(string) | ||
value := tagMap["value"].(string) | ||
if _, exists := newTagMap[key]; !exists { | ||
newTagMap[key] = make(map[string]bool) | ||
} | ||
newTagMap[key][value] = true | ||
} | ||
|
||
// If newTagList is empty, delete all existing tags | ||
if len(newTagList) == 0 { | ||
for key := range oldTagMap { | ||
deleteTag := *dctapi.NewDeleteTag() | ||
deleteTag.SetKey(key) | ||
httpRes, err := client.VDBGroupsAPI.DeleteVdbGroupTags(ctx, vdbGroupId).DeleteTag(deleteTag).Execute() | ||
if diags := apiErrorResponseHelper(ctx, nil, httpRes, err); diags != nil { | ||
return diags | ||
} | ||
} | ||
} else { | ||
// Delete removed tags | ||
for key, oldValues := range oldTagMap { | ||
newValues, exists := newTagMap[key] | ||
if !exists { | ||
// Key doesn't exist in new tags, delete all values for this key | ||
deleteTag := *dctapi.NewDeleteTag() | ||
deleteTag.SetKey(key) | ||
httpRes, err := client.VDBGroupsAPI.DeleteVdbGroupTags(ctx, vdbGroupId).DeleteTag(deleteTag).Execute() | ||
if diags := apiErrorResponseHelper(ctx, nil, httpRes, err); diags != nil { | ||
return diags | ||
} | ||
} else { | ||
// Key exists, delete only values that are not in new tags | ||
for oldValue := range oldValues { | ||
if !newValues[oldValue] { | ||
deleteTag := *dctapi.NewDeleteTag() | ||
deleteTag.SetKey(key) | ||
deleteTag.SetValue(oldValue) | ||
httpRes, err := client.VDBGroupsAPI.DeleteVdbGroupTags(ctx, vdbGroupId).DeleteTag(deleteTag).Execute() | ||
if diags := apiErrorResponseHelper(ctx, nil, httpRes, err); diags != nil { | ||
return diags | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Create new tags | ||
var tags []dctapi.Tag | ||
for key, newValues := range newTagMap { | ||
oldValues, exists := oldTagMap[key] | ||
if !exists { | ||
// Key doesn't exist in old tags, create all values | ||
for value := range newValues { | ||
tag := *dctapi.NewTag(key, value) | ||
tags = append(tags, tag) | ||
} | ||
} else { | ||
// Key exists, create only new values | ||
for value := range newValues { | ||
if !oldValues[value] { | ||
tag := *dctapi.NewTag(key, value) | ||
tags = append(tags, tag) | ||
} | ||
} | ||
} | ||
} | ||
if len(tags) > 0 { | ||
tagsRequest := *dctapi.NewTagsRequest(tags) | ||
_, httpRes, err := client.VDBGroupsAPI.CreateVdbGroupsTags(ctx, vdbGroupId).TagsRequest(tagsRequest).Execute() | ||
if diags := apiErrorResponseHelper(ctx, nil, httpRes, err); diags != nil { | ||
return diags | ||
} | ||
} | ||
} | ||
} | ||
|
||
return resourceVdbGroupRead(ctx, d, meta) | ||
} | ||
|
||
func PollVdbGroupStatus(ctx context.Context, client *dctapi.APIClient, vdbGroupId string) (string, error) { | ||
res, _, err := client.VDBGroupsAPI.GetVdbGroup(ctx, vdbGroupId).Execute() | ||
if err != nil { | ||
return "", err | ||
} | ||
return res.GetStatus(), nil | ||
} | ||
|
||
func resourceVdbGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
|
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.
We will need computed:true here, as without that, it will detect UIPs in case of auto-tagging.
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.
Done