Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Network Rule Datasource #112

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions docs/data-sources/network_rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
# Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Mozilla Public License Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://mozilla.org/MPL/2.0/
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

title: "powerscale_network_rule data source"
linkTitle: "powerscale_network_rule"
page_title: "powerscale_network_rule Data Source - terraform-provider-powerscale"
subcategory: ""
description: |-
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.
---

# powerscale_network_rule (Data Source)

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.

## Example Usage

```terraform
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.

Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://mozilla.org/MPL/2.0/


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

# This Terraform DataSource is used to query the details of existing network rules from PowerScale array.

# Returns a list of PowerScale network rules based on names and query parameters specified in the filter block.
data "powerscale_network_rule" "test" {
filter {
# Optional query parameters
# Note: the following filters will be applied with AND logic
names = ["rule0"]

# Optional
groupnet = "groupnet0"
subnet = "subnet0"
pool = "pool0"
}
}

# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_network_rule.test
output "powerscale_network_rule" {
value = data.powerscale_network_rule.test
}

# Returns all PowerScale network rules on PowerScale array
data "powerscale_network_rule" "all" {
}

# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_network_rule.all
output "powerscale_network_rule_data_all" {
value = data.powerscale_network_rule.all
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter` (Block, Optional) (see [below for nested schema](#nestedblock--filter))

### Read-Only

- `id` (String) Unique identifier of the network rule.
- `network_rules` (Attributes List) List of Network Rules. (see [below for nested schema](#nestedatt--network_rules))

<a id="nestedblock--filter"></a>
### Nested Schema for `filter`

Optional:

- `groupnet` (String) If specified, only rules for this groupnet will be returned.
- `names` (Set of String) Filter network rules by names.
- `pool` (String) If specified, only rules for this pool will be returned.
- `subnet` (String) If specified, only rules for this subnet will be returned.


<a id="nestedatt--network_rules"></a>
### Nested Schema for `network_rules`

Read-Only:

- `description` (String) Description for the provisioning rule.
- `groupnet` (String) Name of the groupnet this rule belongs to
- `id` (String) Unique rule ID.
- `iface` (String) Interface name the provisioning rule applies to.
- `name` (String) Name of the provisioning rule.
- `node_type` (String) Node type the provisioning rule applies to.
- `pool` (String) Name of the pool this rule belongs to.
- `subnet` (String) Name of the subnet this rule belongs to.
48 changes: 48 additions & 0 deletions examples/data-sources/powerscale_network_rule/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.

Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://mozilla.org/MPL/2.0/


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

# This Terraform DataSource is used to query the details of existing network rules from PowerScale array.

# Returns a list of PowerScale network rules based on names and query parameters specified in the filter block.
data "powerscale_network_rule" "test" {
filter {
# Optional query parameters
# Note: the following filters will be applied with AND logic
names = ["rule0"]

# Optional
groupnet = "groupnet0"
subnet = "subnet0"
pool = "pool0"
}
}

# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_network_rule.test
output "powerscale_network_rule" {
value = data.powerscale_network_rule.test
}

# Returns all PowerScale network rules on PowerScale array
data "powerscale_network_rule" "all" {
}

# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_network_rule.all
output "powerscale_network_rule_data_all" {
value = data.powerscale_network_rule.all
}
30 changes: 30 additions & 0 deletions examples/data-sources/powerscale_network_rule/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.

Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://mozilla.org/MPL/2.0/


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
terraform {
required_providers {
powerscale = {
source = "registry.terraform.io/dell/powerscale"
}
}
}

provider "powerscale" {
username = var.username
password = var.password
endpoint = var.endpoint
insecure = var.insecure
}
3 changes: 3 additions & 0 deletions powerscale/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ const (
// UpdateSmartPoolSettingsErrorMsg specifies error details occurred while updating smartpool settings.
UpdateSmartPoolSettingsErrorMsg = "Could not update SmartPool settings "

// ListRuleErrorMsg specifies error details occurred while listing rules.
ListRuleErrorMsg = "Could not list rules "

// CreateRuleErrorMsg specifies error details occurred while creating rule.
CreateRuleErrorMsg = "Could not create rule "

Expand Down
38 changes: 38 additions & 0 deletions powerscale/helper/network_rule_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,46 @@ import (
powerscale "dell/powerscale-go-client"
"fmt"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/models"
)

// ListNetworkRules list network rules.
func ListNetworkRules(ctx context.Context, client *client.Client, filter *models.NetworkRuleFilterType) ([]powerscale.V3PoolsPoolRulesRule, error) {
networkRuleParams := client.PscaleOpenAPIClient.NetworkApi.GetNetworkv3NetworkRules(ctx)

if filter != nil {
if groupnet := filter.Groupnet.ValueString(); groupnet != "" {
networkRuleParams = networkRuleParams.Groupnet(groupnet)
}
if subnet := filter.Subnet.ValueString(); subnet != "" {
networkRuleParams = networkRuleParams.Subnet(subnet)
}
if pool := filter.Pool.ValueString(); pool != "" {
networkRuleParams = networkRuleParams.Pool(pool)
}
}

ruleList, _, err := networkRuleParams.Execute()
if err != nil {
return nil, err
}
rules := ruleList.GetRules()

// filter rules by filter.Names
if filter != nil && len(filter.Names) > 0 {
var filteredRules []powerscale.V3PoolsPoolRulesRule
for _, rule := range rules {
for _, name := range filter.Names {
if name.ValueString() == rule.GetName() {
filteredRules = append(filteredRules, rule)
}
}
}
return filteredRules, nil
}
return rules, nil
}

// CreateNetworkRule create.
func CreateNetworkRule(ctx context.Context, client *client.Client, groupnet string, subnet string, pool string, ruleToCreate powerscale.V3PoolsPoolRule) (*powerscale.CreateResponse, error) {
networkRuleID, _, err := client.PscaleOpenAPIClient.NetworkGroupnetsSubnetsApi.CreateNetworkGroupnetsSubnetsv3PoolsPoolRule(ctx, groupnet, subnet, pool).V3PoolsPoolRule(ruleToCreate).Execute()
Expand Down
15 changes: 15 additions & 0 deletions powerscale/models/network_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ package models

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

// NetworkRuleDataSourceModel describes the data source model.
type NetworkRuleDataSourceModel struct {
ID types.String `tfsdk:"id"`
NetworkRules []V3PoolsPoolRulesRule `tfsdk:"network_rules"`
NetworkRuleFilter *NetworkRuleFilterType `tfsdk:"filter"`
}

// V3PoolsPoolRulesRule struct for V3PoolsPoolRulesRule.
type V3PoolsPoolRulesRule struct {
// Description for the provisioning rule.
Expand All @@ -37,3 +44,11 @@ type V3PoolsPoolRulesRule struct {
// Name of the subnet this rule belongs to.
Subnet types.String `tfsdk:"subnet"`
}

// NetworkRuleFilterType describes the filter data model.
type NetworkRuleFilterType struct {
Names []types.String `tfsdk:"names"`
Groupnet types.String `tfsdk:"groupnet"`
Subnet types.String `tfsdk:"subnet"`
Pool types.String `tfsdk:"pool"`
}
Loading