Skip to content

Commit 7c67b39

Browse files
committed
Implement Network Rule Datasource
1 parent 7102fd5 commit 7c67b39

File tree

9 files changed

+565
-0
lines changed

9 files changed

+565
-0
lines changed

docs/data-sources/network_rule.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
# Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
3+
#
4+
# Licensed under the Mozilla Public License Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://mozilla.org/MPL/2.0/
9+
#
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
title: "powerscale_network_rule data source"
18+
linkTitle: "powerscale_network_rule"
19+
page_title: "powerscale_network_rule Data Source - terraform-provider-powerscale"
20+
subcategory: ""
21+
description: |-
22+
This datasource is used to query the existing network rules from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block.
23+
---
24+
25+
# powerscale_network_rule (Data Source)
26+
27+
This datasource is used to query the existing network rules from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block.
28+
29+
## Example Usage
30+
31+
```terraform
32+
/*
33+
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
34+
35+
Licensed under the Mozilla Public License Version 2.0 (the "License");
36+
you may not use this file except in compliance with the License.
37+
You may obtain a copy of the License at
38+
39+
http://mozilla.org/MPL/2.0/
40+
41+
42+
Unless required by applicable law or agreed to in writing, software
43+
distributed under the License is distributed on an "AS IS" BASIS,
44+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45+
See the License for the specific language governing permissions and
46+
limitations under the License.
47+
*/
48+
49+
# This Terraform DataSource is used to query the details of existing network rules from PowerScale array.
50+
51+
# Returns a list of PowerScale network rules based on names and query parameters specified in the filter block.
52+
data "powerscale_network_rule" "test" {
53+
filter {
54+
# Optional query parameters
55+
# Note: the following filters will be applied with AND logic
56+
names = ["rule0"]
57+
58+
# Optional
59+
groupnet = "groupnet0"
60+
subnet = "subnet0"
61+
pool = "pool0"
62+
}
63+
}
64+
65+
# Output value of above block by executing 'terraform output' command
66+
# You can use the the fetched information by the variable data.powerscale_network_rule.test
67+
output "powerscale_network_rule" {
68+
value = data.powerscale_network_rule.test
69+
}
70+
71+
# Returns all PowerScale network rules on PowerScale array
72+
data "powerscale_network_rule" "all" {
73+
}
74+
75+
# Output value of above block by executing 'terraform output' command
76+
# You can use the the fetched information by the variable data.powerscale_network_rule.all
77+
output "powerscale_network_rule_data_all" {
78+
value = data.powerscale_network_rule.all
79+
}
80+
```
81+
82+
<!-- schema generated by tfplugindocs -->
83+
## Schema
84+
85+
### Optional
86+
87+
- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))
88+
89+
### Read-Only
90+
91+
- `id` (String) Unique identifier of the network rule.
92+
- `network_rules` (Attributes List) List of Network Rules. (see [below for nested schema](#nestedatt--network_rules))
93+
94+
<a id="nestedblock--filter"></a>
95+
### Nested Schema for `filter`
96+
97+
Optional:
98+
99+
- `groupnet` (String) If specified, only rules for this groupnet will be returned.
100+
- `names` (Set of String) Filter network rules by names.
101+
- `pool` (String) If specified, only rules for this pool will be returned.
102+
- `subnet` (String) If specified, only rules for this subnet will be returned.
103+
104+
105+
<a id="nestedatt--network_rules"></a>
106+
### Nested Schema for `network_rules`
107+
108+
Read-Only:
109+
110+
- `description` (String) Description for the provisioning rule.
111+
- `groupnet` (String) Name of the groupnet this rule belongs to
112+
- `id` (String) Unique rule ID.
113+
- `iface` (String) Interface name the provisioning rule applies to.
114+
- `name` (String) Name of the provisioning rule.
115+
- `node_type` (String) Node type the provisioning rule applies to.
116+
- `pool` (String) Name of the pool this rule belongs to.
117+
- `subnet` (String) Name of the subnet this rule belongs to.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
3+
4+
Licensed under the Mozilla Public License Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://mozilla.org/MPL/2.0/
9+
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
# This Terraform DataSource is used to query the details of existing network rules from PowerScale array.
19+
20+
# Returns a list of PowerScale network rules based on names and query parameters specified in the filter block.
21+
data "powerscale_network_rule" "test" {
22+
filter {
23+
# Optional query parameters
24+
# Note: the following filters will be applied with AND logic
25+
names = ["rule0"]
26+
27+
# Optional
28+
groupnet = "groupnet0"
29+
subnet = "subnet0"
30+
pool = "pool0"
31+
}
32+
}
33+
34+
# Output value of above block by executing 'terraform output' command
35+
# You can use the the fetched information by the variable data.powerscale_network_rule.test
36+
output "powerscale_network_rule" {
37+
value = data.powerscale_network_rule.test
38+
}
39+
40+
# Returns all PowerScale network rules on PowerScale array
41+
data "powerscale_network_rule" "all" {
42+
}
43+
44+
# Output value of above block by executing 'terraform output' command
45+
# You can use the the fetched information by the variable data.powerscale_network_rule.all
46+
output "powerscale_network_rule_data_all" {
47+
value = data.powerscale_network_rule.all
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
3+
4+
Licensed under the Mozilla Public License Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://mozilla.org/MPL/2.0/
9+
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
terraform {
18+
required_providers {
19+
powerscale = {
20+
source = "registry.terraform.io/dell/powerscale"
21+
}
22+
}
23+
}
24+
25+
provider "powerscale" {
26+
username = var.username
27+
password = var.password
28+
endpoint = var.endpoint
29+
insecure = var.insecure
30+
}

powerscale/constants/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ const (
238238
// UpdateSmartPoolSettingsErrorMsg specifies error details occurred while updating smartpool settings.
239239
UpdateSmartPoolSettingsErrorMsg = "Could not update SmartPool settings "
240240

241+
// ListRuleErrorMsg specifies error details occurred while listing rules.
242+
ListRuleErrorMsg = "Could not list rules "
243+
241244
// CreateRuleErrorMsg specifies error details occurred while creating rule.
242245
CreateRuleErrorMsg = "Could not create rule "
243246

powerscale/helper/network_rule_helper.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,46 @@ import (
2121
powerscale "dell/powerscale-go-client"
2222
"fmt"
2323
"terraform-provider-powerscale/client"
24+
"terraform-provider-powerscale/powerscale/models"
2425
)
2526

27+
// ListNetworkRules list network rules.
28+
func ListNetworkRules(ctx context.Context, client *client.Client, filter *models.NetworkRuleFilterType) ([]powerscale.V3PoolsPoolRulesRule, error) {
29+
networkRuleParams := client.PscaleOpenAPIClient.NetworkApi.GetNetworkv3NetworkRules(ctx)
30+
31+
if filter != nil {
32+
if groupnet := filter.Groupnet.ValueString(); groupnet != "" {
33+
networkRuleParams = networkRuleParams.Groupnet(groupnet)
34+
}
35+
if subnet := filter.Subnet.ValueString(); subnet != "" {
36+
networkRuleParams = networkRuleParams.Subnet(subnet)
37+
}
38+
if pool := filter.Pool.ValueString(); pool != "" {
39+
networkRuleParams = networkRuleParams.Pool(pool)
40+
}
41+
}
42+
43+
ruleList, _, err := networkRuleParams.Execute()
44+
if err != nil {
45+
return nil, err
46+
}
47+
rules := ruleList.GetRules()
48+
49+
// filter rules by filter.Names
50+
if filter != nil && len(filter.Names) > 0 {
51+
var filteredRules []powerscale.V3PoolsPoolRulesRule
52+
for _, rule := range rules {
53+
for _, name := range filter.Names {
54+
if name.ValueString() == rule.GetName() {
55+
filteredRules = append(filteredRules, rule)
56+
}
57+
}
58+
}
59+
return filteredRules, nil
60+
}
61+
return rules, nil
62+
}
63+
2664
// CreateNetworkRule create.
2765
func CreateNetworkRule(ctx context.Context, client *client.Client, groupnet string, subnet string, pool string, ruleToCreate powerscale.V3PoolsPoolRule) (*powerscale.CreateResponse, error) {
2866
networkRuleID, _, err := client.PscaleOpenAPIClient.NetworkGroupnetsSubnetsApi.CreateNetworkGroupnetsSubnetsv3PoolsPoolRule(ctx, groupnet, subnet, pool).V3PoolsPoolRule(ruleToCreate).Execute()

powerscale/models/network_rule.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ package models
1818

1919
import "github.com/hashicorp/terraform-plugin-framework/types"
2020

21+
// NetworkRuleDataSourceModel describes the data source model.
22+
type NetworkRuleDataSourceModel struct {
23+
ID types.String `tfsdk:"id"`
24+
NetworkRules []V3PoolsPoolRulesRule `tfsdk:"network_rules"`
25+
NetworkRuleFilter *NetworkRuleFilterType `tfsdk:"filter"`
26+
}
27+
2128
// V3PoolsPoolRulesRule struct for V3PoolsPoolRulesRule.
2229
type V3PoolsPoolRulesRule struct {
2330
// Description for the provisioning rule.
@@ -37,3 +44,11 @@ type V3PoolsPoolRulesRule struct {
3744
// Name of the subnet this rule belongs to.
3845
Subnet types.String `tfsdk:"subnet"`
3946
}
47+
48+
// NetworkRuleFilterType describes the filter data model.
49+
type NetworkRuleFilterType struct {
50+
Names []types.String `tfsdk:"names"`
51+
Groupnet types.String `tfsdk:"groupnet"`
52+
Subnet types.String `tfsdk:"subnet"`
53+
Pool types.String `tfsdk:"pool"`
54+
}

0 commit comments

Comments
 (0)