Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit bfdcfb4

Browse files
Vadim Chinyaevalexk53
authored andcommitted
volumes metadata support
1 parent a545026 commit bfdcfb4

14 files changed

+269
-64
lines changed

gcore/data_source_gcore_floatingip.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package gcore
33
import (
44
"context"
55
"fmt"
6-
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
76
"log"
87
"net"
98

9+
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
10+
1011
"github.com/G-Core/gcorelabscloud-go/gcore/floatingip/v1/floatingips"
1112
"github.com/hashicorp/go-cty/cty"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

gcore/data_source_gcore_loadbalancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func dataSourceLoadBalancerRead(ctx context.Context, d *schema.ResourceData, m i
102102
}
103103

104104
name := d.Get("name").(string)
105-
lbs, err := loadbalancers.ListAll(client)
105+
lbs, err := loadbalancers.ListAll(client, nil)
106106
if err != nil {
107107
return diag.FromErr(err)
108108
}

gcore/data_source_gcore_loadbalancerv2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func dataSourceLoadBalancerV2Read(ctx context.Context, d *schema.ResourceData, m
7373
}
7474

7575
name := d.Get("name").(string)
76-
lbs, err := loadbalancers.ListAll(client)
76+
lbs, err := loadbalancers.ListAll(client, nil)
7777
if err != nil {
7878
return diag.FromErr(err)
7979
}

gcore/data_source_gcore_volume.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ func dataSourceVolume() *schema.Resource {
5959
Computed: true,
6060
Description: "Available value is 'standard', 'ssd_hiiops', 'cold', 'ultra'. Defaults to standard",
6161
},
62+
"metadata_k": &schema.Schema{
63+
Type: schema.TypeString,
64+
Optional: true,
65+
},
66+
"metadata_kv": &schema.Schema{
67+
Type: schema.TypeMap,
68+
Optional: true,
69+
Elem: &schema.Schema{
70+
Type: schema.TypeString,
71+
},
72+
},
73+
"metadata_read_only": &schema.Schema{
74+
Type: schema.TypeList,
75+
Computed: true,
76+
Elem: &schema.Resource{
77+
Schema: map[string]*schema.Schema{
78+
"key": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
"value": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
"read_only": {
87+
Type: schema.TypeBool,
88+
Computed: true,
89+
},
90+
},
91+
},
92+
},
6293
},
6394
}
6495
}
@@ -75,7 +106,20 @@ func dataSourceVolumeRead(ctx context.Context, d *schema.ResourceData, m interfa
75106
}
76107

77108
name := d.Get("name").(string)
78-
vols, err := volumes.ListAll(client, volumes.ListOpts{})
109+
volumeOpts := &volumes.ListOpts{}
110+
if metadataK, ok := d.GetOk("metadata_k"); ok {
111+
volumeOpts.MetadataK = metadataK.(string)
112+
}
113+
114+
if metadataRaw, ok := d.GetOk("metadata_kv"); ok {
115+
typedMetadataKV := make(map[string]string, len(metadataRaw.(map[string]interface{})))
116+
for k, v := range metadataRaw.(map[string]interface{}) {
117+
typedMetadataKV[k] = v.(string)
118+
}
119+
volumeOpts.MetadataKV = typedMetadataKV
120+
}
121+
122+
vols, err := volumes.ListAll(client, volumeOpts)
79123
if err != nil {
80124
return diag.FromErr(err)
81125
}
@@ -101,6 +145,20 @@ func dataSourceVolumeRead(ctx context.Context, d *schema.ResourceData, m interfa
101145
d.Set("region_id", volume.RegionID)
102146
d.Set("project_id", volume.ProjectID)
103147

148+
metadataReadOnly := make([]map[string]interface{}, 0, len(volume.Metadata))
149+
if len(volume.Metadata) > 0 {
150+
for _, metadataItem := range volume.Metadata {
151+
metadataReadOnly = append(metadataReadOnly, map[string]interface{}{
152+
"key": metadataItem.Key,
153+
"value": metadataItem.Value,
154+
"read_only": metadataItem.ReadOnly,
155+
})
156+
}
157+
}
158+
159+
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
160+
return diag.FromErr(err)
161+
}
104162
log.Println("[DEBUG] Finish Volume reading")
105163
return diags
106164
}

gcore/data_source_gcore_volume_test.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import (
1515
)
1616

1717
const (
18-
volumeTestName = "test-volume"
19-
volumeTestSize = 1
18+
volumeTestName = "test-volume"
19+
volume1TestName = "test-volume-1"
20+
volume2TestName = "test-volume-2"
21+
volumeTestSize = 1
2022
)
2123

2224
func TestAccVolumeDataSource(t *testing.T) {
@@ -30,42 +32,72 @@ func TestAccVolumeDataSource(t *testing.T) {
3032
t.Fatal(err)
3133
}
3234

33-
opts := volumes.CreateOpts{
34-
Name: volumeTestName,
35+
opts1 := volumes.CreateOpts{
36+
Name: volume1TestName,
3537
Size: volumeTestSize,
3638
Source: volumes.NewVolume,
3739
TypeName: volumes.Standard,
40+
Metadata: map[string]string{"key1": "val1", "key2": "val2"},
3841
}
3942

40-
volumeID, err := createTestVolume(client, opts)
43+
volume1ID, err := createTestVolume(client, opts1)
4144
if err != nil {
4245
t.Fatal(err)
4346
}
4447

45-
defer volumes.Delete(client, volumeID, volumes.DeleteOpts{})
48+
opts2 := volumes.CreateOpts{
49+
Name: volume2TestName,
50+
Size: volumeTestSize,
51+
Source: volumes.NewVolume,
52+
TypeName: volumes.Standard,
53+
Metadata: map[string]string{"key1": "val1", "key3": "val3"},
54+
}
55+
56+
volume2ID, err := createTestVolume(client, opts2)
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
61+
defer volumes.Delete(client, volume1ID, volumes.DeleteOpts{})
62+
defer volumes.Delete(client, volume2ID, volumes.DeleteOpts{})
4663

4764
fullName := "data.gcore_volume.acctest"
48-
tpl := func(name string) string {
65+
tpl := func(name string, metaQuery string) string {
4966
return fmt.Sprintf(`
5067
data "gcore_volume" "acctest" {
5168
%s
5269
%s
5370
name = "%s"
71+
%s
5472
}
55-
`, projectInfo(), regionInfo(), name)
73+
`, projectInfo(), regionInfo(), name, metaQuery)
5674
}
5775

5876
resource.Test(t, resource.TestCase{
5977
PreCheck: func() { testAccPreCheck(t) },
6078
ProviderFactories: testAccProviders,
6179
Steps: []resource.TestStep{
6280
{
63-
Config: tpl(opts.Name),
81+
Config: tpl(opts1.Name, `metadata_k="key1"`),
82+
Check: resource.ComposeTestCheckFunc(
83+
testAccCheckResourceExists(fullName),
84+
resource.TestCheckResourceAttr(fullName, "name", opts1.Name),
85+
resource.TestCheckResourceAttr(fullName, "id", volume1ID),
86+
resource.TestCheckResourceAttr(fullName, "size", strconv.Itoa(opts1.Size)),
87+
testAccCheckMetadata(fullName, true, map[string]string{
88+
"key1": "val1", "key2": "val2"}),
89+
),
90+
},
91+
{
92+
Config: tpl(opts2.Name, `metadata_kv={key3 = "val3"}`),
6493
Check: resource.ComposeTestCheckFunc(
6594
testAccCheckResourceExists(fullName),
66-
resource.TestCheckResourceAttr(fullName, "name", opts.Name),
67-
resource.TestCheckResourceAttr(fullName, "id", volumeID),
68-
resource.TestCheckResourceAttr(fullName, "size", strconv.Itoa(opts.Size)),
95+
resource.TestCheckResourceAttr(fullName, "name", opts2.Name),
96+
resource.TestCheckResourceAttr(fullName, "id", volume2ID),
97+
resource.TestCheckResourceAttr(fullName, "size", strconv.Itoa(opts2.Size)),
98+
testAccCheckMetadata(fullName, true, map[string]string{
99+
"key3": "val3",
100+
}),
69101
),
70102
},
71103
},

gcore/resource_gcore_floatingip.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package gcore
33
import (
44
"context"
55
"fmt"
6-
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
7-
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
86
"log"
97
"net"
108
"time"
119

10+
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
11+
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
12+
1213
gcorecloud "github.com/G-Core/gcorelabscloud-go"
1314
"github.com/G-Core/gcorelabscloud-go/gcore/floatingip/v1/floatingips"
1415
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"

gcore/resource_gcore_network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package gcore
33
import (
44
"context"
55
"fmt"
6-
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
7-
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
86
"log"
97
"time"
108

119
gcorecloud "github.com/G-Core/gcorelabscloud-go"
1210
"github.com/G-Core/gcorelabscloud-go/gcore/network/v1/networks"
1311
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
12+
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
13+
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1616
)

gcore/resource_gcore_subnet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package gcore
33
import (
44
"context"
55
"fmt"
6-
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
7-
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
86
"log"
97
"net"
108
"regexp"
@@ -13,6 +11,8 @@ import (
1311
gcorecloud "github.com/G-Core/gcorelabscloud-go"
1412
"github.com/G-Core/gcorelabscloud-go/gcore/subnet/v1/subnets"
1513
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
14+
"github.com/G-Core/gcorelabscloud-go/gcore/utils"
15+
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata"
1616
"github.com/hashicorp/go-cty/cty"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

gcore/resource_gcore_subnet_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ func TestAccSubnet(t *testing.T) {
211211
Check: resource.ComposeTestCheckFunc(
212212
testAccCheckResourceExists(fullName),
213213
checkSubnetAttrs(fullName, &updateFixt),
214+
testAccCheckMetadata(fullName, true, map[string]string{
215+
"key3": "val3",
216+
}),
217+
testAccCheckMetadata(fullName, false, map[string]string{
218+
"key1": "val1",
219+
}),
220+
testAccCheckMetadata(fullName, false, map[string]string{
221+
"key2": "val2",
222+
}),
214223
),
215224
},
216225
},

0 commit comments

Comments
 (0)