diff --git a/openstack/data_source_openstack_networking_quota_v2.go b/openstack/data_source_openstack_networking_quota_v2.go new file mode 100644 index 000000000..636dc7ff9 --- /dev/null +++ b/openstack/data_source_openstack_networking_quota_v2.go @@ -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 +} diff --git a/openstack/data_source_openstack_networking_quota_v2_test.go b/openstack/data_source_openstack_networking_quota_v2_test.go new file mode 100644 index 000000000..60473354a --- /dev/null +++ b/openstack/data_source_openstack_networking_quota_v2_test.go @@ -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.source"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "floatingip"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "network"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "port"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "rbac_policy"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "router"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "security_group"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "security_group_rule"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.source", "subnet"), + resource.TestCheckResourceAttrSet("data.openstack_networking_quota.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" "source" { + project_id = "${openstack_identity_project_v3.project.id}" +} +`, testAccNetworkingV2QuotaDataSourceBasic) +} diff --git a/openstack/provider.go b/openstack/provider.go index caa979573..9d589d50c 100644 --- a/openstack/provider.go +++ b/openstack/provider.go @@ -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(), diff --git a/website/docs/d/networking_quota_v2.html.markdown b/website/docs/d/networking_quota_v2.html.markdown new file mode 100644 index 000000000..d768f653b --- /dev/null +++ b/website/docs/d/networking_quota_v2.html.markdown @@ -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. diff --git a/website/openstack.erb b/website/openstack.erb index 53cd176ea..4e608eaa6 100644 --- a/website/openstack.erb +++ b/website/openstack.erb @@ -100,6 +100,9 @@ > openstack_networking_qos_policy_v2 + > + openstack_networking_quota_v2 + > openstack_networking_router_v2