Skip to content

Commit

Permalink
New data source: openstack_blockstorage_quotaset_v3
Browse files Browse the repository at this point in the history
  • Loading branch information
nikParasyr committed Nov 25, 2021
1 parent 3a235ee commit c2d4f94
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
108 changes: 108 additions & 0 deletions openstack/data_source_openstack_blockstorage_quotaset_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package openstack

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
}
69 changes: 69 additions & 0 deletions openstack/data_source_openstack_blockstorage_quotaset_v3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package openstack

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccBlockStorageV3QuotasetDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBlockStorageV3QuotasetDataSourceBasic,
},
{
Config: testAccBlockStorageV3QuotasetDataSourceSource(),
Check: resource.ComposeTestCheckFunc(
testAccCheckBlockStorageQuotasetV3DataSourceID("data.openstack_blockstorage_quotaset_v3.source"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "volumes"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "snapshots"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "gigabytes"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "per_volume_gigabytes"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "backups"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "backup_gigabytes"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "groups"),
resource.TestCheckResourceAttrSet("data.openstack_blockstorage_quotaset_v3.source", "volume_type_quota"),
),
},
},
})
}

func testAccCheckBlockStorageQuotasetV3DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find blockstorage quotaset data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Blockstorage quotaset data source ID not set")
}

return nil
}
}

const testAccBlockStorageV3QuotasetDataSourceBasic = `
resource "openstack_identity_project_v3" "project" {
name = "test-quotaset-datasource"
}
`

func testAccBlockStorageV3QuotasetDataSourceSource() string {
return fmt.Sprintf(`
%s
data "openstack_blockstorage_quotaset_v3" "source" {
project_id = "${openstack_identity_project_v3.project.id}"
}
`, testAccBlockStorageV3QuotasetDataSourceBasic)
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func Provider() *schema.Provider {
"openstack_blockstorage_snapshot_v3": dataSourceBlockStorageSnapshotV3(),
"openstack_blockstorage_volume_v2": dataSourceBlockStorageVolumeV2(),
"openstack_blockstorage_volume_v3": dataSourceBlockStorageVolumeV3(),
"openstack_blockstorage_quotaset_v3": dataSourceBlockStorageQuotasetV3(),
"openstack_compute_aggregate_v2": dataSourceComputeAggregateV2(),
"openstack_compute_availability_zones_v2": dataSourceComputeAvailabilityZonesV2(),
"openstack_compute_instance_v2": dataSourceComputeInstanceV2(),
Expand Down
42 changes: 42 additions & 0 deletions website/docs/d/blockstorage_quotaset_v3.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: "openstack"
page_title: "OpenStack: openstack_blockstorage_quotaset_v3"
sidebar_current: "docs-openstack-datasource-blockstorage-quotaset-v3"
description: |-
Get information on a BlockStorage Quotaset v3 of a project.
---

# openstack\_blockstorage\_quotaset\_v3

Use this data source to get the blockstorage quotaset v3 of an OpenStack project.

## Example Usage

```hcl
data "openstack_blockstorage_quotaset_v3" "quota" {
project_id = "2e367a3d29f94fd988e6ec54e305ec9d"
}
```

## Argument Reference

* `region` - (Optional) The region in which to obtain the V3 Blockstorage client.
If omitted, the `region` argument of the provider is used.

* `project_id` - (Required) The id of the project to retrieve the quotaset.


## Attributes Reference

The following attributes are exported:

* `region` - See Argument Reference above.
* `project_id` - See Argument Reference above.
* `volumes` - The number of volumes that are allowed.
* `snapshots` - The number of snapshots that are allowed.
* `gigabytes` - The size (GB) of volumes and snapshots that are allowed.
* `per_volume_gigabytes` - The size (GB) of volumes that are allowed for each volume.
* `backups` - The number of backups that are allowed.
* `backup_gigabytes` - The size (GB) of backups that are allowed.
* `groups` - The number of groups that are allowed.
* `volume_type_quota` - Map with gigabytes_{volume_type}, snapshots_{volume_type}, volumes_{volume_type} for each volume type.
3 changes: 3 additions & 0 deletions website/openstack.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<li<%= sidebar_current("docs-openstack-datasource-blockstorage-volume-v3") %>>
<a href="/docs/providers/openstack/d/blockstorage_volume_v3.html">openstack_blockstorage_volume_v3</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-blockstorage-quotaset-v3") %>>
<a href="/docs/providers/openstack/d/blockstorage_quotaset_v3.html">openstack_blockstorage_quotaset_v3</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-compute-availability-zones-v2") %>>
<a href="/docs/providers/openstack/d/compute_availability_zones_v2.html">openstack_compute_availability_zones_v2</a>
</li>
Expand Down

0 comments on commit c2d4f94

Please sign in to comment.