-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new datasource: openstack_networking_quota_v2
- Loading branch information
1 parent
3a235ee
commit 7ed6021
Showing
5 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
openstack/data_source_openstack_networking_quota_v2_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters