Skip to content

Commit 65e2f8e

Browse files
author
Uddipaan Hazarika
committed
added tags update fix
1 parent 691215e commit 65e2f8e

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

docs/resources/vdb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Environment variable to be set when the engine creates a VDB. See the Engine doc
228228

229229
* `bookmark_id` - (Optional) The ID or name of the bookmark from which to execute the operation. The bookmark must contain only one VDB.
230230

231-
* `tags` - (Optional) The tags to be created for VDB. This is a map of 2 parameters:
231+
* `tags` - (Optional) [Updatable] The tags to be created for VDB. This is a map of 2 parameters:
232232
* `key` - (Required) Key of the tag
233233
* `value` - (Required) Value of the tag
234234

internal/provider/commons.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var updatableVdbKeys = map[string]bool{
4949
"vdb_restart": true,
5050
"new_dbid": true,
5151
"mount_point": true,
52+
"tags": true,
5253
}
5354

5455
var isDestructiveVdbUpdate = map[string]bool{
@@ -84,4 +85,5 @@ var isDestructiveVdbUpdate = map[string]bool{
8485
"vdb_restart": false,
8586
"new_dbid": false,
8687
"mount_point": true,
88+
"tags": false,
8789
}

internal/provider/resource_vdb.go

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,8 @@ func resourceVdbRead(ctx context.Context, d *schema.ResourceData, meta interface
15931593
d.Set("pre_rollback", flattenHooks(result.GetHooks().PreRollback))
15941594
d.Set("post_rollback", flattenHooks(result.GetHooks().PostRollback))
15951595
d.Set("database_name", result.GetDatabaseName())
1596+
d.Set("tags", flattenTags(result.GetTags()))
1597+
15961598
_, is_provision := d.GetOk("provision_type")
15971599
if !is_provision {
15981600
// its an import, set to default value
@@ -1665,13 +1667,10 @@ func resourceVdbUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
16651667
k = "listener_ids"
16661668
}
16671669
if d.HasChange(k) {
1668-
tflog.Info(ctx, ">>>>>@@@VDB<<<<<<"+k)
1670+
tflog.Debug(ctx, "changed keys"+k)
16691671
changedKeys = append(changedKeys, k)
16701672
}
16711673
}
1672-
for _, ck := range changedKeys {
1673-
tflog.Info(ctx, "!!!!!!!VDB!!!!!!!!"+ck)
1674-
}
16751674

16761675
var updateFailure, destructiveUpdate bool = false, false
16771676
var nonUpdatableField []string
@@ -1681,75 +1680,32 @@ func resourceVdbUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
16811680

16821681
// if changedKeys contains non updatable field set a flag
16831682
for _, key := range changedKeys {
1684-
tflog.Info(ctx, "!!!!!!!!VDB!!!!!!!"+key)
16851683
if !updatableVdbKeys[key] {
16861684
updateFailure = true
1687-
tflog.Info(ctx, ">>>>>!!!VDB<<<<<<"+key)
1685+
tflog.Debug(ctx, "non updatable field: "+key)
16881686
nonUpdatableField = append(nonUpdatableField, key)
16891687
}
16901688
}
16911689

16921690
if updateFailure {
1693-
tflog.Info(ctx, "######updateVDBfailure")
16941691
revertChanges(d, changedKeys)
16951692
return diag.Errorf("cannot update options %v. Please refer to provider documentation for updatable params.", nonUpdatableField)
16961693
}
16971694

16981695
// find if destructive update
16991696
for _, key := range changedKeys {
17001697
if isDestructiveVdbUpdate[key] {
1701-
tflog.Info(ctx, "######isDestructiveVDBUpdate"+key)
1698+
tflog.Debug(ctx, "destructive updates for: "+key)
17021699
destructiveUpdate = true
17031700
}
17041701
}
17051702
if destructiveUpdate {
17061703
if diags := disableVDB(ctx, client, vdbId); diags != nil {
17071704
tflog.Error(ctx, "failure in disabling vdbs")
1708-
//disableVdbFailure = true
17091705
revertChanges(d, changedKeys)
17101706
return diags
17111707
}
17121708
}
1713-
// if d.HasChanges(
1714-
// "auto_select_repository",
1715-
// "source_data_id",
1716-
// "id",
1717-
// "database_type",
1718-
// "database_version",
1719-
// "status",
1720-
// "ip_address",
1721-
// "fqdn",
1722-
// "parent_id",
1723-
// "group_name",
1724-
// "creation_date",
1725-
// "target_group_id",
1726-
// "database_name",
1727-
// "truncate_log_on_checkpoint",
1728-
// "repository_id",
1729-
// "file_mapping_rules",
1730-
// "instance_name",
1731-
// "unique_name",
1732-
// "open_reset_logs",
1733-
// "snapshot_policy_id",
1734-
// "retention_policy_id",
1735-
// "recovery_model",
1736-
// "online_log_groups",
1737-
// "online_log_size",
1738-
// "os_username",
1739-
// "os_password",
1740-
// "archive_log",
1741-
// "timestamp",
1742-
// "timestamp_in_database_timezone",
1743-
// "snapshot_id") {
1744-
1745-
// // revert and set the old value to the changed keys
1746-
// for _, key := range changedKeys {
1747-
// old, _ := d.GetChange(key)
1748-
// d.Set(key, old)
1749-
// }
1750-
1751-
// return diag.Errorf("cannot update one (or more) of the options changed. Please refer to provider documentation for updatable params.")
1752-
// }
17531709

17541710
nvdh := dctapi.NewVirtualDatasetHooks()
17551711

@@ -1953,8 +1909,39 @@ func resourceVdbUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
19531909
return diag.Errorf("[NOT OK] VDB-Update %s. JobId: %s / Error: %s", job_status, *res.Job.Id, job_err)
19541910
}
19551911

1956-
if diags := enableVDB(ctx, client, d.Get("id").(string)); diags != nil {
1957-
return diags //if failure should we enable
1912+
if d.HasChanges(
1913+
"tags",
1914+
) { // tags update
1915+
tflog.Debug(ctx, "updating tags")
1916+
if d.HasChange("tags") {
1917+
// delete old tag
1918+
tflog.Debug(ctx, "deleting old tags")
1919+
oldTag, newTag := d.GetChange("tags")
1920+
if len(toTagArray(oldTag)) != 0 {
1921+
tflog.Debug(ctx, "tag to be deleted: "+toTagArray(oldTag)[0].GetKey()+" "+toTagArray(oldTag)[0].GetValue())
1922+
deleteTag := *dctapi.NewDeleteTag()
1923+
tagDelResp, tagDelErr := client.VDBsAPI.DeleteVdbTags(ctx, vdbId).DeleteTag(deleteTag).Execute()
1924+
tflog.Debug(ctx, "tag delete response: "+tagDelResp.Status)
1925+
if diags := apiErrorResponseHelper(ctx, nil, tagDelResp, tagDelErr); diags != nil {
1926+
revertChanges(d, changedKeys)
1927+
updateFailure = true
1928+
}
1929+
}
1930+
// create tag
1931+
if len(toTagArray(newTag)) != 0 {
1932+
tflog.Info(ctx, "creating new tags")
1933+
_, httpResp, tagCrtErr := client.VDBsAPI.CreateVdbTags(ctx, vdbId).TagsRequest(*dctapi.NewTagsRequest(toTagArray(newTag))).Execute()
1934+
if diags := apiErrorResponseHelper(ctx, nil, httpResp, tagCrtErr); diags != nil {
1935+
revertChanges(d, changedKeys)
1936+
return diags
1937+
}
1938+
}
1939+
}
1940+
}
1941+
if destructiveUpdate {
1942+
if diags := enableVDB(ctx, client, vdbId); diags != nil {
1943+
return diags //if failure should we enable
1944+
}
19581945
}
19591946

19601947
return diags

internal/provider/utility.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ func flattenHooks(hooks []dctapi.Hook) []interface{} {
169169
return make([]interface{}, 0)
170170
}
171171

172+
func flattenTags(tags []dctapi.Tag) []interface{} {
173+
if tags != nil {
174+
returnedTags := make([]interface{}, len(tags))
175+
for i, tag := range tags {
176+
returnedTag := make(map[string]interface{})
177+
returnedTag["key"] = tag.GetKey()
178+
returnedTag["value"] = tag.GetValue()
179+
returnedTags[i] = returnedTag
180+
}
181+
return returnedTags
182+
}
183+
return make([]interface{}, 0)
184+
}
185+
172186
func apiErrorResponseHelper(ctx context.Context, res interface{}, httpRes *http.Response, err error) diag.Diagnostics {
173187
// Helper function to return Diagnostics object if there is
174188
// a failure during API call.

0 commit comments

Comments
 (0)