Skip to content

Commit cade1c7

Browse files
[DCS] tags support (#629)
[DCS] tags support What this PR does / why we need it Which issue this PR fixes Special notes for your reviewer Tags Reviewed-by: Aloento Reviewed-by: Artem Lifshits
1 parent fbea97a commit cade1c7

File tree

5 files changed

+138
-3
lines changed

5 files changed

+138
-3
lines changed

acceptance/openstack/dcs/v1/configs_test.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package v1
22

33
import (
4+
"fmt"
45
"testing"
56

7+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
68
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
79
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
10+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
811
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/configs"
12+
dcsTags "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/tags"
913
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/whitelists"
1014
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
1115
)
@@ -53,8 +57,66 @@ func TestDcsConfigLifeCycle(t *testing.T) {
5357
th.AssertDeepEquals(t, updateOpts.RedisConfigs[0].ParamName, configList.RedisConfigs[0].ParamName)
5458

5559
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()
5786
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
60122
}

acceptance/openstack/dcs/v1/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1
33
import (
44
"testing"
55

6+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
67
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/others"
78

89
"github.com/opentelekomcloud/gophertelekomcloud"
@@ -69,6 +70,16 @@ func createDCSInstance(t *testing.T, client *golangsdk.ServiceClient) *lifecycle
6970
SpecCode: specCode,
7071
SecurityGroupID: openstack.DefaultSecurityGroup(t),
7172
InstanceBackupPolicy: &plan,
73+
Tags: []tags.ResourceTag{
74+
{
75+
Key: "muh",
76+
Value: "kuh",
77+
},
78+
{
79+
Key: "muh2",
80+
Value: "kuh2",
81+
},
82+
},
7283
})
7384
th.AssertNoErr(t, err)
7485
t.Cleanup(func() {

openstack/dcs/v1/lifecycle/Create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/opentelekomcloud/gophertelekomcloud"
55
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
66
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
7+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
78
)
89

910
type CreateOps struct {
@@ -114,6 +115,8 @@ type CreateOps struct {
114115
// true: indicates that a DCS instance can be accessed without a password.
115116
// false: indicates that a DCS instance can be accessed only after password authentication.
116117
NoPasswordAccess string `json:"no_password_access"`
118+
// Tags
119+
Tags []tags.ResourceTag `json:"tags,omitempty"`
117120
}
118121

119122
type InstanceBackupPolicy struct {

openstack/dcs/v2/tags/Create.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tags
2+
3+
import (
4+
"strings"
5+
6+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
7+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
8+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
9+
)
10+
11+
type tagsActionOpts struct {
12+
Action string `json:"action" required:"true"`
13+
Tags []tags.ResourceTag `json:"tags,omitempty"`
14+
}
15+
16+
// Create an instance tags with given parameters.
17+
func Create(client *golangsdk.ServiceClient, instanceID string, tags []tags.ResourceTag) error {
18+
opts := tagsActionOpts{
19+
Action: "create",
20+
Tags: tags,
21+
}
22+
b, err := build.RequestBody(opts, "")
23+
if err != nil {
24+
return err
25+
}
26+
url := client.ServiceURL("dcs", instanceID, "tags", "action")
27+
_, err = client.Post(strings.Replace(url, "v1.0", "v2", 1), b, nil, &golangsdk.RequestOpts{
28+
OkCodes: []int{204},
29+
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
30+
})
31+
return err
32+
}

openstack/dcs/v2/tags/Delete.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tags
2+
3+
import (
4+
"strings"
5+
6+
"github.com/opentelekomcloud/gophertelekomcloud"
7+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
8+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
9+
)
10+
11+
// Delete an instance tags
12+
func Delete(client *golangsdk.ServiceClient, instanceID string, tags []tags.ResourceTag) error {
13+
opts := tagsActionOpts{
14+
Action: "delete",
15+
Tags: tags,
16+
}
17+
b, err := build.RequestBody(opts, "")
18+
if err != nil {
19+
return err
20+
}
21+
url := client.ServiceURL("dcs", instanceID, "tags", "action")
22+
_, err = client.Post(strings.Replace(url, "v1.0", "v2", 1), b, nil, &golangsdk.RequestOpts{
23+
OkCodes: []int{204},
24+
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
25+
})
26+
return err
27+
}

0 commit comments

Comments
 (0)