Skip to content

Commit e3d8702

Browse files
authored
[CBR] Clean up redundant code. (#374)
[CBR] Clean up redundant code. What this PR does / why we need it Following up the #371 Which issue this PR fixes fixes #370 Clean up redundant code. Special notes for your reviewer Reviewed-by: Anton Sidelnikov <None> Reviewed-by: Aloento <None> Reviewed-by: Artem Lifshits <None>
1 parent d2a6ffc commit e3d8702

7 files changed

Lines changed: 63 additions & 186 deletions

File tree

acceptance/openstack/cbr/v3/tags_test.go

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@ import (
55

66
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
77
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
8-
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/tags"
8+
cbrtags "github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/tags"
99
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/vaults"
10+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
1011
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
1112
)
1213

1314
func TestTags(t *testing.T) {
1415
client, err := clients.NewCbrV3Client()
1516
th.AssertNoErr(t, err)
1617

17-
monoTag := vaults.Tag{
18+
firstTag := tags.ResourceTag{
1819
Key: "TestKey",
1920
Value: "TestValue",
2021
}
2122

23+
secondTag := tags.ResourceTag{
24+
Key: "TestKey2",
25+
Value: "TestValue2",
26+
}
27+
28+
combineTag := []tags.ResourceTag{
29+
firstTag,
30+
secondTag,
31+
}
32+
2233
opts := vaults.CreateOpts{
2334
Billing: &vaults.BillingCreate{
2435
ConsistentLevel: "crash_consistent",
@@ -29,9 +40,7 @@ func TestTags(t *testing.T) {
2940
Description: "gophertelemocloud testing vault",
3041
Name: tools.RandomString("cbr-test-", 5),
3142
Resources: []vaults.ResourceCreate{},
32-
Tags: []vaults.Tag{
33-
monoTag,
34-
},
43+
Tags: combineTag,
3544
}
3645
vault, err := vaults.Create(client, opts).Extract()
3746
th.AssertNoErr(t, err)
@@ -40,52 +49,31 @@ func TestTags(t *testing.T) {
4049
th.AssertNoErr(t, vaults.Delete(client, vault.ID).ExtractErr())
4150
}()
4251

43-
projectTags, err := tags.ShowVaultProjectTag(client).Extract()
52+
projectTags, err := cbrtags.ShowVaultProjectTag(client).Extract()
4453
th.AssertNoErr(t, err)
45-
th.AssertEquals(t, len(projectTags) > 0, true)
54+
th.AssertEquals(t, len(projectTags), 2)
4655

47-
instances, err := tags.ShowVaultResourceInstances(client, tags.ResourceInstancesRequest{
48-
Tags: []tags.Tag{{
49-
Key: monoTag.Key,
50-
Values: []string{monoTag.Value},
56+
instances, err := cbrtags.ShowVaultResourceInstances(client, cbrtags.ResourceInstancesRequest{
57+
Tags: []tags.ListedTag{{
58+
Key: firstTag.Key,
59+
Values: []string{firstTag.Value},
5160
}},
52-
Action: tags.Filter,
61+
Action: cbrtags.Filter,
5362
}).Extract()
5463
th.AssertNoErr(t, err)
5564

5665
resourceID := instances.Resources[0].ResourceID
5766
th.AssertEquals(t, resourceID, vault.ID)
5867

59-
vaultTags, err := tags.ShowVaultTag(client, resourceID).Extract()
68+
vaultTags, err := cbrtags.ShowVaultTag(client, resourceID).Extract()
6069
th.AssertNoErr(t, err)
61-
th.AssertEquals(t, vaultTags.Tags[0].Key, monoTag.Key)
62-
63-
th.AssertNoErr(t, tags.DeleteVaultTag(client, resourceID, monoTag.Key).ExtractErr())
64-
vaultTags, _ = tags.ShowVaultTag(client, resourceID).Extract()
65-
for i := range vaultTags.Tags {
66-
if vaultTags.Tags[i].Key == monoTag.Key {
67-
panic("Tag should be deleted")
68-
}
69-
}
70-
71-
th.AssertNoErr(t, tags.CreateVaultTags(client, resourceID, monoTag).ExtractErr())
72-
vaultTags, _ = tags.ShowVaultTag(client, resourceID).Extract()
73-
isExist := false
74-
for i := range vaultTags.Tags {
75-
if vaultTags.Tags[i].Key == monoTag.Key {
76-
th.AssertEquals(t, vaultTags.Tags[i].Value, monoTag.Value)
77-
isExist = true
78-
}
79-
}
80-
th.AssertEquals(t, isExist, true)
70+
th.AssertEquals(t, vaultTags[0].Key == firstTag.Key || vaultTags[0].Key == secondTag.Key, true)
8171

82-
th.AssertNoErr(t, tags.BatchCreateAndDeleteVaultTags(client, resourceID, tags.BulkCreateAndDeleteVaultTagsRequest{
83-
Tags: []vaults.Tag{monoTag},
84-
Action: tags.Delete,
85-
}).ExtractErr())
72+
th.AssertNoErr(t, cbrtags.DeleteVaultTag(client, resourceID, combineTag).ExtractErr())
73+
vaultTags, _ = cbrtags.ShowVaultTag(client, resourceID).Extract()
74+
th.AssertEquals(t, len(vaultTags), 0)
8675

87-
th.AssertNoErr(t, tags.BatchCreateAndDeleteVaultTags(client, resourceID, tags.BulkCreateAndDeleteVaultTagsRequest{
88-
Tags: []vaults.Tag{monoTag},
89-
Action: tags.Create,
90-
}).ExtractErr())
76+
th.AssertNoErr(t, cbrtags.CreateVaultTags(client, resourceID, []tags.ResourceTag{firstTag}).ExtractErr())
77+
vaultTags, _ = cbrtags.ShowVaultTag(client, resourceID).Extract()
78+
th.AssertEquals(t, len(vaultTags), 1)
9179
}

openstack/cbr/v3/tags/requests.go

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"fmt"
55

66
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
7-
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/vaults"
7+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
88
)
99

10-
func ShowVaultProjectTag(client *golangsdk.ServiceClient) (r TagResult) {
11-
_, r.Err = client.Get(showVaultProjectTagURL(client), &r.Body, nil)
12-
return
10+
func ShowVaultProjectTag(client *golangsdk.ServiceClient) (r tags.ListResult) {
11+
return tags.List(client, baseURL)
1312
}
1413

1514
// ----------------------------------------------------------------------------
@@ -39,23 +38,23 @@ type ResourceInstancesRequest struct {
3938
// If this parameter is set to true, all resources without tags are queried.
4039
WithoutAnyTag bool `json:"without_any_tag,omitempty"`
4140
// Returns the full amount of data when there are no filter conditions
42-
Tags []Tag `json:"tags,omitempty"`
41+
Tags []tags.ListedTag `json:"tags,omitempty"`
4342
// Backups with any tags in this list will be filtered.
44-
TagsAny []Tag `json:"tags_any,omitempty"`
43+
TagsAny []tags.ListedTag `json:"tags_any,omitempty"`
4544
// Backups without these tags will be filtered.
46-
NotTags []Tag `json:"not_tags,omitempty"`
45+
NotTags []tags.ListedTag `json:"not_tags,omitempty"`
4746
// Backups without any tags in this list will be filtered.
48-
NotTagsAny []Tag `json:"not_tags_any,omitempty"`
47+
NotTagsAny []tags.ListedTag `json:"not_tags_any,omitempty"`
4948
// Number of search records, default is 1000, the minimum value of limit is 1, the maximum value of limit is 1000
5049
Limit string `json:"limit,omitempty"`
5150
// Index position (no this parameter when action is count)
5251
Offset string `json:"offset,omitempty"`
5352
// filter is a paginated query. count simply returns the total number of items according to the criteria
5453
Action ActionType `json:"action"`
5554
// Query conditions supported by the resource itself
56-
Matches []vaults.Tag `json:"matches,omitempty"`
57-
CloudType CloudType `json:"cloud_type,omitempty"`
58-
ObjectType ObjectType `json:"object_type,omitempty"`
55+
Matches []tags.ResourceTag `json:"matches,omitempty"`
56+
CloudType CloudType `json:"cloud_type,omitempty"`
57+
ObjectType ObjectType `json:"object_type,omitempty"`
5958
}
6059

6160
func ShowVaultResourceInstances(client *golangsdk.ServiceClient, req ResourceInstancesRequest) (r InstancesResult) {
@@ -73,59 +72,18 @@ func ShowVaultResourceInstances(client *golangsdk.ServiceClient, req ResourceIns
7372

7473
// ----------------------------------------------------------------------------
7574

76-
func ShowVaultTag(client *golangsdk.ServiceClient, id string) (r ShowVaultTagResult) {
77-
_, r.Err = client.Get(vaultTagsURL(client, id), &r.Body, nil)
78-
return
75+
func ShowVaultTag(client *golangsdk.ServiceClient, id string) (r tags.GetResult) {
76+
return tags.Get(client, baseURL, id)
7977
}
8078

8179
// ----------------------------------------------------------------------------
8280

83-
func CreateVaultTags(client *golangsdk.ServiceClient, id string, req vaults.Tag) (r golangsdk.ErrWithResult) {
84-
reqBody, err := golangsdk.BuildRequestBody(req, "tag")
85-
if err != nil {
86-
r.Err = fmt.Errorf("failed to create vault map: %s", err)
87-
return
88-
}
89-
_, err = client.Post(vaultTagsURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
90-
OkCodes: []int{204},
91-
})
92-
r.Err = err
93-
return
94-
}
95-
96-
// ----------------------------------------------------------------------------
97-
98-
func DeleteVaultTag(client *golangsdk.ServiceClient, id string, key string) (r golangsdk.ErrResult) {
99-
_, err := client.Delete(deleteVaultTagURL(client, id, key), &golangsdk.RequestOpts{
100-
OkCodes: []int{204},
101-
})
102-
r.Err = err
103-
return
81+
func CreateVaultTags(client *golangsdk.ServiceClient, id string, req []tags.ResourceTag) (r tags.ActionResult) {
82+
return tags.Create(client, baseURL, id, req)
10483
}
10584

10685
// ----------------------------------------------------------------------------
10786

108-
type BulkCreateAndDeleteVaultTagsRequest struct {
109-
Tags []vaults.Tag `json:"tags,omitempty"`
110-
Action BulkActionType `json:"action"`
111-
}
112-
113-
type BulkActionType string
114-
115-
const (
116-
Create = "create"
117-
Delete = "delete"
118-
)
119-
120-
func BatchCreateAndDeleteVaultTags(client *golangsdk.ServiceClient, id string, req BulkCreateAndDeleteVaultTagsRequest) (r golangsdk.ErrWithResult) {
121-
reqBody, err := golangsdk.BuildRequestBody(req, "")
122-
if err != nil {
123-
r.Err = fmt.Errorf("failed to create vault map: %s", err)
124-
return
125-
}
126-
_, err = client.Post(batchCreateAndDeleteVaultTagsURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
127-
OkCodes: []int{204},
128-
})
129-
r.Err = err
130-
return
87+
func DeleteVaultTag(client *golangsdk.ServiceClient, id string, req []tags.ResourceTag) (r tags.ActionResult) {
88+
return tags.Delete(client, baseURL, id, req)
13189
}

openstack/cbr/v3/tags/results.go

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,19 @@ import (
55

66
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
77
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/vaults"
8+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
89
)
910

10-
type Tag struct {
11-
Key string `json:"key"`
12-
Values []string `json:"values"`
13-
}
14-
15-
type TagResult struct {
16-
golangsdk.Result
17-
}
18-
19-
func (r TagResult) Extract() ([]Tag, error) {
20-
var s struct {
21-
Tags []Tag `json:"tags"`
22-
}
23-
if r.Err != nil {
24-
return nil, r.Err
25-
}
26-
err := r.ExtractInto(&s)
27-
if err != nil {
28-
return nil, fmt.Errorf("failed to extract Vault Project Tags")
29-
}
30-
return s.Tags, nil
31-
}
32-
33-
// ----------------------------------------------------------------------------
34-
3511
type InstancesResponse struct {
3612
Resources []TagResource `json:"resources"`
3713
TotalCount int `json:"total_count"`
3814
}
3915

4016
type TagResource struct {
41-
ResourceID string `json:"resource_id"`
42-
ResourceDetail BoxedVault `json:"resource_detail"`
43-
Tags []vaults.Tag `json:"tags"`
44-
ResourceName string `json:"resource_name"`
17+
ResourceID string `json:"resource_id"`
18+
ResourceDetail BoxedVault `json:"resource_detail"`
19+
Tags []tags.ResourceTag `json:"tags"`
20+
ResourceName string `json:"resource_name"`
4521
}
4622

4723
type BoxedVault struct {
@@ -63,25 +39,3 @@ func (r InstancesResult) Extract() (*InstancesResponse, error) {
6339
}
6440
return &s, nil
6541
}
66-
67-
// ----------------------------------------------------------------------------
68-
69-
type ShowVaultTagResult struct {
70-
golangsdk.Result
71-
}
72-
73-
type ShowVaultTagResponse struct {
74-
Tags []vaults.Tag `json:"tags"`
75-
}
76-
77-
func (r ShowVaultTagResult) Extract() (*ShowVaultTagResponse, error) {
78-
var s = ShowVaultTagResponse{}
79-
if r.Err != nil {
80-
return nil, r.Err
81-
}
82-
err := r.ExtractInto(&s)
83-
if err != nil {
84-
return nil, fmt.Errorf("failed to extract Vault Tag")
85-
}
86-
return &s, nil
87-
}

openstack/cbr/v3/tags/urls.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,6 @@ package tags
33
import golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
44

55
const baseURL = "vault"
6-
const tags = "tags"
7-
8-
// POST /v3/{project_id}/vault/{vault_id}/tags/action
9-
func batchCreateAndDeleteVaultTagsURL(client *golangsdk.ServiceClient, id string) string {
10-
return client.ServiceURL(baseURL, id, tags, "action")
11-
}
12-
13-
// GET/POST /v3/{project_id}/vault/{vault_id}/tags
14-
func vaultTagsURL(client *golangsdk.ServiceClient, id string) string {
15-
return client.ServiceURL(baseURL, id, tags)
16-
}
17-
18-
// DELETE /v3/{project_id}/vault/{vault_id}/tags/{key}
19-
func deleteVaultTagURL(client *golangsdk.ServiceClient, id string, key string) string {
20-
return client.ServiceURL(baseURL, id, tags, key)
21-
}
22-
23-
// GET /v3/{project_id}/vault/tags
24-
func showVaultProjectTagURL(client *golangsdk.ServiceClient) string {
25-
return client.ServiceURL(baseURL, tags)
26-
}
276

287
// POST /v3/{project_id}/vault/resource_instances/action
298
func showVaultResourceInstancesURL(client *golangsdk.ServiceClient) string {

openstack/cbr/v3/vaults/requests.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/opentelekomcloud/gophertelekomcloud"
7+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
78
)
89

910
type CreateOptsBuilder interface {
@@ -80,13 +81,9 @@ type ResourceCreate struct {
8081
ExtraInfo *ResourceExtraInfo `json:"extra_info,omitempty"`
8182
}
8283

83-
type Tag struct {
84-
Key string `json:"key"`
85-
Value string `json:"value,omitempty"`
86-
}
8784
type VaultBindRules struct {
8885
// Filters automatically associated resources by tag.
89-
Tags []Tag `json:"tags,omitempty"`
86+
Tags []tags.ResourceTag `json:"tags,omitempty"`
9087
}
9188

9289
type CreateOpts struct {
@@ -105,7 +102,7 @@ type CreateOpts struct {
105102
// This list cannot be an empty list.
106103
// The list can contain up to 10 keys.
107104
// Keys in this list must be unique.
108-
Tags []Tag `json:"tags,omitempty"`
105+
Tags []tags.ResourceTag `json:"tags,omitempty"`
109106
// Enterprise project ID. The default value is 0.
110107
EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
111108
// Whether automatic association is supported

openstack/cbr/v3/vaults/results.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/opentelekomcloud/gophertelekomcloud"
7+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
78
)
89

910
type vaultResult struct {
@@ -47,14 +48,14 @@ type ResourceResp struct {
4748
}
4849

4950
type Vault struct {
50-
ID string `json:"id"`
51-
Name string `json:"name"`
52-
Billing Billing `json:"billing"`
53-
Description string `json:"description"`
54-
ProjectID string `json:"project_id"`
55-
ProviderID string `json:"provider_id"`
56-
Resources []ResourceResp `json:"resources"`
57-
Tags []Tag `json:"tags"`
51+
ID string `json:"id"`
52+
Name string `json:"name"`
53+
Billing Billing `json:"billing"`
54+
Description string `json:"description"`
55+
ProjectID string `json:"project_id"`
56+
ProviderID string `json:"provider_id"`
57+
Resources []ResourceResp `json:"resources"`
58+
Tags []tags.ResourceTag `json:"tags"`
5859

5960
EnterpriseProjectID string `json:"enterprise_project_id"`
6061

0 commit comments

Comments
 (0)