Skip to content

Commit

Permalink
Add new datasource: openstack_networking_quota_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nikParasyr committed Nov 29, 2021
1 parent 3a235ee commit 7ed6021
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
111 changes: 111 additions & 0 deletions openstack/data_source_openstack_networking_quota_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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/networking/v2/extensions/quotas"
)

func dataSourceNetworkingQuotaV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNetworkingQuotaV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,

Computed: true,
ForceNew: true,
},

"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"floatingip": {
Type: schema.TypeInt,
Computed: true,
},

"network": {
Type: schema.TypeInt,
Computed: true,
},

"port": {
Type: schema.TypeInt,
Computed: true,
},

"rbac_policy": {
Type: schema.TypeInt,
Computed: true,
},

"router": {
Type: schema.TypeInt,
Computed: true,
},

"security_group": {
Type: schema.TypeInt,
Computed: true,
},

"security_group_rule": {
Type: schema.TypeInt,
Computed: true,
},

"subnet": {
Type: schema.TypeInt,
Computed: true,
},

"subnetpool": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceNetworkingQuotaV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
region := GetRegion(d, config)
networkingClient, err := config.NetworkingV2Client(region)
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}

projectID := d.Get("project_id").(string)

q, err := quotas.Get(networkingClient, projectID).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_networking_quota_v2"))
}

log.Printf("[DEBUG] Retrieved openstack_networking_quota_v2 %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("floatingip", q.FloatingIP)
d.Set("network", q.Network)
d.Set("port", q.Port)
d.Set("rbac_policy", q.RBACPolicy)
d.Set("router", q.Router)
d.Set("security_group", q.SecurityGroup)
d.Set("security_group_rule", q.SecurityGroupRule)
d.Set("subnet", q.Subnet)
d.Set("subnetpool", q.SubnetPool)

return nil
}
70 changes: 70 additions & 0 deletions openstack/data_source_openstack_networking_quota_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package openstack

import (
"fmt"
"testing"

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

func TestAccNetworkingV2QuotaDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkingV2QuotaDataSourceBasic,
},
{
Config: testAccNetworkingV2QuotaDataSourceSource(),
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingQuotaV2DataSourceID("data.openstack_networking_quota_v2.source"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "floatingip"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "network"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "port"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "rbac_policy"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "router"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "security_group"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "security_group_rule"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "subnet"),
resource.TestCheckResourceAttrSet("data.openstack_networking_quota_v2.source", "subnetpool"),
),
},
},
})
}

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

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

return nil
}
}

const testAccNetworkingV2QuotaDataSourceBasic = `
resource "openstack_identity_project_v3" "project" {
name = "test-quota-datasource"
}
`

func testAccNetworkingV2QuotaDataSourceSource() string {
return fmt.Sprintf(`
%s
data "openstack_networking_quota_v2" "source" {
project_id = "${openstack_identity_project_v3.project.id}"
}
`, testAccNetworkingV2QuotaDataSourceBasic)
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func Provider() *schema.Provider {
"openstack_networking_qos_dscp_marking_rule_v2": dataSourceNetworkingQoSDSCPMarkingRuleV2(),
"openstack_networking_qos_minimum_bandwidth_rule_v2": dataSourceNetworkingQoSMinimumBandwidthRuleV2(),
"openstack_networking_qos_policy_v2": dataSourceNetworkingQoSPolicyV2(),
"openstack_networking_quota_v2": dataSourceNetworkingQuotaV2(),
"openstack_networking_subnet_v2": dataSourceNetworkingSubnetV2(),
"openstack_networking_subnet_ids_v2": dataSourceNetworkingSubnetIDsV2(),
"openstack_networking_secgroup_v2": dataSourceNetworkingSecGroupV2(),
Expand Down
43 changes: 43 additions & 0 deletions website/docs/d/networking_quota_v2.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: "openstack"
page_title: "OpenStack: openstack_networking_quota_v2"
sidebar_current: "docs-openstack-datasource-networking-quota-v2"
description: |-
Get information on a NEtworking Quota of a project.
---

# openstack\_networking\_quota\_v2

Use this data source to get the networking quota of an OpenStack project.

## Example Usage

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

## Argument Reference

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

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


## Attributes Reference

The following attributes are exported:

* `region` - See Argument Reference above.
* `project_id` - See Argument Reference above.
* `floatingip` - The number of allowed floating ips.
* `network` - The number of allowed networks.
* `port` - The number of allowed ports.
* `rbac_policy` - The number of allowed rbac policies.
* `router` - The amount of allowed routers.
* `security_group` - The number of allowed security groups.
* `security_group_rule` - The number of allowed security group rules.
* `subnet` - The number of allowed subnets.
* `subnetpool-` - The number of allowed subnet pools.
3 changes: 3 additions & 0 deletions website/openstack.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<li<%= sidebar_current("docs-openstack-datasource-networking-qos-policy-v2") %>>
<a href="/docs/providers/openstack/d/networking_qos_policy_v2.html">openstack_networking_qos_policy_v2</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-quota-v2") %>>
<a href="/docs/providers/openstack/d/networking_quota_v2.html">openstack_networking_quota_v2</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-networking-router-v2") %>>
<a href="/docs/providers/openstack/d/networking_router_v2.html">openstack_networking_router_v2</a>
</li>
Expand Down

0 comments on commit 7ed6021

Please sign in to comment.