/
data_source_openstack_blockstorage_quotaset_v3.go
108 lines (89 loc) · 2.51 KB
/
data_source_openstack_blockstorage_quotaset_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package nhncloud
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
)
func dataSourceBlockStorageQuotasetV3() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBlockStorageQuotasetV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"volumes": {
Type: schema.TypeInt,
Computed: true,
},
"snapshots": {
Type: schema.TypeInt,
Computed: true,
},
"gigabytes": {
Type: schema.TypeInt,
Computed: true,
},
"per_volume_gigabytes": {
Type: schema.TypeInt,
Computed: true,
},
"backups": {
Type: schema.TypeInt,
Computed: true,
},
"backup_gigabytes": {
Type: schema.TypeInt,
Computed: true,
},
"groups": {
Type: schema.TypeInt,
Computed: true,
},
"volume_type_quota": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceBlockStorageQuotasetV3Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
region := GetRegion(d, config)
blockStorageClient, err := config.BlockStorageV3Client(region)
if err != nil {
return diag.Errorf("Error creating OpenStack block storage client: %s", err)
}
projectID := d.Get("project_id").(string)
q, err := quotasets.Get(blockStorageClient, projectID).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_blockstorage_quotaset_v3"))
}
log.Printf("[DEBUG] Retrieved openstack_blockstorage_quotaset_v3 %s: %#v", d.Id(), q)
id := fmt.Sprintf("%s/%s", projectID, region)
d.SetId(id)
d.Set("project_id", projectID)
d.Set("region", region)
d.Set("volumes", q.Volumes)
d.Set("snapshots", q.Snapshots)
d.Set("gigabytes", q.Gigabytes)
d.Set("per_volume_gigabytes", q.PerVolumeGigabytes)
d.Set("backups", q.Backups)
d.Set("backup_gigabytes", q.BackupGigabytes)
d.Set("groups", q.Groups)
volumeTypeQuota, _ := blockStorageQuotasetVolTypeQuotaToStr(q.Extra)
if err := d.Set("volume_type_quota", volumeTypeQuota); err != nil {
log.Printf("[WARN] Unable to set openstack_blockstorage_quotaset_v3 %s volume_type_quotas: %s", d.Id(), err)
}
return nil
}