|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + golangsdk "github.com/opentelekomcloud/gophertelekomcloud" |
6 | 8 | "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" |
7 | 9 | "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto" |
| 10 | + "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags" |
8 | 11 | "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/configs" |
| 12 | + dcsTags "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/tags" |
9 | 13 | "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/whitelists" |
10 | 14 | th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" |
11 | 15 | ) |
@@ -53,8 +57,66 @@ func TestDcsConfigLifeCycle(t *testing.T) { |
53 | 57 | th.AssertDeepEquals(t, updateOpts.RedisConfigs[0].ParamName, configList.RedisConfigs[0].ParamName) |
54 | 58 |
|
55 | 59 | t.Logf("Retrieving whitelist configuration") |
56 | | - whitelistResp, err := whitelists.Get(client, dcsInstance.InstanceID) |
| 60 | + err = WaitForAWhitelistToBeRetrieved(client, dcsInstance.InstanceID, 180) |
| 61 | + if err == nil { |
| 62 | + whitelistResp, err := whitelists.Get(client, dcsInstance.InstanceID) |
| 63 | + th.AssertNoErr(t, err) |
| 64 | + th.AssertEquals(t, whitelistResp.InstanceID, dcsInstance.InstanceID) |
| 65 | + th.AssertEquals(t, whitelistResp.Groups[0].GroupName, "test-group-1") |
| 66 | + } |
| 67 | + |
| 68 | + t.Logf("Retrieving instance tags") |
| 69 | + instanceTags, err := tags.Get(client, "instances", dcsInstance.InstanceID).Extract() |
| 70 | + th.AssertNoErr(t, err) |
| 71 | + th.AssertEquals(t, len(instanceTags), 2) |
| 72 | + th.AssertEquals(t, instanceTags[0].Key, "muh") |
| 73 | + th.AssertEquals(t, instanceTags[0].Value, "kuh") |
| 74 | + |
| 75 | + t.Logf("Updating instance tags") |
| 76 | + err = updateDcsTags(client, dcsInstance.InstanceID, instanceTags, |
| 77 | + []tags.ResourceTag{ |
| 78 | + { |
| 79 | + Key: "muhUpdated", |
| 80 | + Value: "kuhUpdated", |
| 81 | + }, |
| 82 | + }) |
| 83 | + th.AssertNoErr(t, err) |
| 84 | + t.Logf("Retrieving updated instance tags") |
| 85 | + instanceTagsUpdated, err := tags.Get(client, "instances", dcsInstance.InstanceID).Extract() |
57 | 86 | th.AssertNoErr(t, err) |
58 | | - th.AssertEquals(t, whitelistResp.InstanceID, dcsInstance.InstanceID) |
59 | | - th.AssertEquals(t, whitelistResp.Groups[0].GroupName, "test-group-1") |
| 87 | + th.AssertEquals(t, len(instanceTagsUpdated), 1) |
| 88 | + th.AssertEquals(t, instanceTagsUpdated[0].Key, "muhUpdated") |
| 89 | + th.AssertEquals(t, instanceTagsUpdated[0].Value, "kuhUpdated") |
| 90 | +} |
| 91 | + |
| 92 | +// WaitForAWhitelistToBeRetrieved - wait until whitelist is retrieved |
| 93 | +func WaitForAWhitelistToBeRetrieved(client *golangsdk.ServiceClient, id string, timeoutSeconds int) error { |
| 94 | + return golangsdk.WaitFor(timeoutSeconds, func() (bool, error) { |
| 95 | + wl, err := whitelists.Get(client, id) |
| 96 | + if err != nil { |
| 97 | + return false, fmt.Errorf("error retriving whitelist: %w", err) |
| 98 | + } |
| 99 | + if wl.InstanceID != "" { |
| 100 | + return true, nil |
| 101 | + } |
| 102 | + return false, nil |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func updateDcsTags(client *golangsdk.ServiceClient, id string, old, new []tags.ResourceTag) error { |
| 107 | + // remove old tags |
| 108 | + if len(old) > 0 { |
| 109 | + err := dcsTags.Delete(client, id, old) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + } |
| 114 | + // add new tags |
| 115 | + if len(new) > 0 { |
| 116 | + err := dcsTags.Create(client, id, new) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
60 | 122 | } |
0 commit comments