From 6c96aa7f5703b48e251d5eacd4f8aca53c74ba96 Mon Sep 17 00:00:00 2001 From: shenda1 Date: Fri, 3 Jan 2025 10:57:11 +0530 Subject: [PATCH] adding replication rule datasource Signed-off-by: shenda1 --- Makefile | 2 +- docs/data-sources/replication_rule.md | 87 ++++++ docs/resources/replication_rule.md | 2 +- .../data-source.tf | 53 ++++ .../powerstore_replication_rule/provider.tf | 33 +++ .../powerstore_replication_rule/variables.tf | 36 +++ .../powerstore_replication_rule/resource.tf | 2 +- go.mod | 2 +- go.sum | 2 + models/replication_rule.go | 41 +++ powerstore/datasource_replication_rule.go | 268 ++++++++++++++++++ .../datasource_replication_rule_test.go | 85 ++++++ powerstore/provider.go | 1 + 13 files changed, 610 insertions(+), 4 deletions(-) create mode 100644 docs/data-sources/replication_rule.md create mode 100644 examples/data-sources/powerstore_replication_rule/data-source.tf create mode 100644 examples/data-sources/powerstore_replication_rule/provider.tf create mode 100644 examples/data-sources/powerstore_replication_rule/variables.tf create mode 100644 powerstore/datasource_replication_rule.go create mode 100644 powerstore/datasource_replication_rule_test.go diff --git a/Makefile b/Makefile index 04a2749f..f6688293 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io NAMESPACE=dell NAME=powerstore BINARY=terraform-provider-${NAME} -VERSION=1.1.0 +VERSION=1.2.0 OS_ARCH=linux_amd64 default: install diff --git a/docs/data-sources/replication_rule.md b/docs/data-sources/replication_rule.md new file mode 100644 index 00000000..ab19d155 --- /dev/null +++ b/docs/data-sources/replication_rule.md @@ -0,0 +1,87 @@ +--- +# Copyright (c) 2024 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: "powerstore_replication_rule data source" +linkTitle: "powerstore_replication_rule" +page_title: "powerstore_replication_rule Data Source - powerstore" +subcategory: "" +description: |- + This datasource is used to query the existing replication rule from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block. +--- + +# powerstore_replication_rule (Data Source) + +This datasource is used to query the existing replication rule from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block. + +## Example Usage + +{{tffile "/root/akash/terraform-provider-powerstore/examples/data-sources/powerstore_replication_rule/data-source.tf"}} + + +## Schema + +### Optional + +- `id` (String) Unique identifier of the replication rule. Conflicts with `name`. +- `name` (String) Name of the replication rule. Conflicts with `id`. + +### Read-Only + +- `replication_rules` (Attributes List) List of replication rules. (see [below for nested schema](#nestedatt--replication_rules)) + + +### Nested Schema for `replication_rules` + +Read-Only: + +- `alert_threshold` (Number) The alert threshold for the replication rule. +- `id` (String) Unique identifier of the replication rule. Conflicts with `name`. +- `is_read_only` (Boolean) Indicates whether the replication rule is read-only. +- `is_replica` (Boolean) Indicates whether the replication rule is a replica. +- `managed_by` (String) The entity that manages the replication rule. +- `managed_by_id` (String) The ID of the managing entity. +- `name` (String) Name of the replication rule. Conflicts with `id`. +- `policies` (Attributes List) The protection policies associated with the replication rule. (see [below for nested schema](#nestedatt--replication_rules--policies)) +- `remote_system` (Attributes) The remote system associated with the replication rule. (see [below for nested schema](#nestedatt--replication_rules--remote_system)) +- `remote_system_id` (String) The ID of the remote system associated with the replication rule. +- `replication_session` (Attributes List) The replication session associated with the replication rule. (see [below for nested schema](#nestedatt--replication_rules--replication_session)) +- `rpo` (String) The RPO (Recovery Point Objective) of the replication rule. + + +### Nested Schema for `replication_rules.policies` + +Read-Only: + +- `id` (String) The ID of the protection policy. +- `name` (String) The name of the protection policy. + + + +### Nested Schema for `replication_rules.remote_system` + +Read-Only: + +- `id` (String) The ID of the remote system. +- `name` (String) The name of the remote system. + + + +### Nested Schema for `replication_rules.replication_session` + +Read-Only: + +- `id` (String) The ID of the replication session. +- `state` (String) The state of the replication session. \ No newline at end of file diff --git a/docs/resources/replication_rule.md b/docs/resources/replication_rule.md index cc7e71dc..31ab468b 100644 --- a/docs/resources/replication_rule.md +++ b/docs/resources/replication_rule.md @@ -61,7 +61,7 @@ resource "powerstore_replication_rule" "test" { } //Below example is for import operation -/*resource "powerstore_snapshotrule" "terraform-provider-test-import" { +/*resource "powerstore_replication_rule" "terraform-provider-test-import" { }*/ ``` diff --git a/examples/data-sources/powerstore_replication_rule/data-source.tf b/examples/data-sources/powerstore_replication_rule/data-source.tf new file mode 100644 index 00000000..3aac8b66 --- /dev/null +++ b/examples/data-sources/powerstore_replication_rule/data-source.tf @@ -0,0 +1,53 @@ +/* +Copyright (c) 2024 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. +*/ + +# commands to run this tf file : terraform init && terraform apply --auto-approve +# This datasource reads replication rules either by id or name where user can provide a value to any one of them +# If it is a empty datsource block , then it will read all the replication rules +# If id or name is provided then it reads a particular replication rule with that id or name +# Only one of the attribute can be provided among id and name + +# Get replication rule details using name +resource "powerstore_replication_rule" "test" { + name = "terraform_replication_rule" + rpo = "One_Hour" + remote_system_id = "db11abb3-789e-47f9-96b5-84b5374cbcd2" + alert_threshold = 1000 + is_read_only = false +} + +data "powerstore_replication_rule" "test" { + depends_on = [powerstore_replication_rule.test] + name = "terraform_replication_rule" +} + +# Get replication rule details using ID +resource "powerstore_replication_rule" "test" { + name = "terraform_replication_rule" + rpo = "One_Hour" + remote_system_id = "db11abb3-789e-47f9-96b5-84b5374cbcd2" + alert_threshold = 1000 + is_read_only = false +} + +data "powerstore_replication_rule" "test" { + id = powerstore_replication_rule.test.id +} + +output "replicationRule" { + value = data.powerstore_replication_rule.test.replication_rules +} diff --git a/examples/data-sources/powerstore_replication_rule/provider.tf b/examples/data-sources/powerstore_replication_rule/provider.tf new file mode 100644 index 00000000..dde25fa9 --- /dev/null +++ b/examples/data-sources/powerstore_replication_rule/provider.tf @@ -0,0 +1,33 @@ +/* +Copyright (c) 2024 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 { + powerstore = { + version = "1.2.0" + source = "registry.terraform.io/dell/powerstore" + } + } +} + +provider "powerstore" { + username = var.username + password = var.password + endpoint = var.endpoint + insecure = true + timeout = var.timeout +} \ No newline at end of file diff --git a/examples/data-sources/powerstore_replication_rule/variables.tf b/examples/data-sources/powerstore_replication_rule/variables.tf new file mode 100644 index 00000000..b079f08a --- /dev/null +++ b/examples/data-sources/powerstore_replication_rule/variables.tf @@ -0,0 +1,36 @@ +/* +Copyright (c) 2024 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. +*/ + +variable "username" { + type = string + description = "Stores the username of PowerStore host." +} + +variable "password" { + type = string + description = "Stores the password of PowerStore host." +} + +variable "timeout" { + type = string + description = "Stores the timeout of PowerStore host." +} + +variable "endpoint" { + type = string + description = "Stores the endpoint of PowerStore host. eg: https://10.1.1.1/api/rest" +} \ No newline at end of file diff --git a/examples/resources/powerstore_replication_rule/resource.tf b/examples/resources/powerstore_replication_rule/resource.tf index 6eb68e79..a44acd35 100644 --- a/examples/resources/powerstore_replication_rule/resource.tf +++ b/examples/resources/powerstore_replication_rule/resource.tf @@ -30,5 +30,5 @@ resource "powerstore_replication_rule" "test" { } //Below example is for import operation -/*resource "powerstore_snapshotrule" "terraform-provider-test-import" { +/*resource "powerstore_replication_rule" "terraform-provider-test-import" { }*/ \ No newline at end of file diff --git a/go.mod b/go.mod index e5764f1b..b98e1c6f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23 toolchain go1.23.2 require ( - github.com/dell/gopowerstore v1.16.1-0.20241227083619-36c4ea66275f + github.com/dell/gopowerstore v1.16.1-0.20250103044727-b55f5083bb46 github.com/hashicorp/terraform-plugin-docs v0.20.1 github.com/hashicorp/terraform-plugin-framework v1.13.0 github.com/hashicorp/terraform-plugin-framework-validators v0.15.0 diff --git a/go.sum b/go.sum index 37ba96ad..ee5b4f79 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/dell/gopowerstore v1.16.1-0.20241227064011-3c3808a39067 h1:FcRAYtZkdr github.com/dell/gopowerstore v1.16.1-0.20241227064011-3c3808a39067/go.mod h1:RYodZ8GgJG5p85AviydL43Mt8ldcTqr5a+Cv+vqWacE= github.com/dell/gopowerstore v1.16.1-0.20241227083619-36c4ea66275f h1:wr07YuRpeBb5sEgBahBGPWMZKomPC2TfbpBjMWMqwVE= github.com/dell/gopowerstore v1.16.1-0.20241227083619-36c4ea66275f/go.mod h1:RYodZ8GgJG5p85AviydL43Mt8ldcTqr5a+Cv+vqWacE= +github.com/dell/gopowerstore v1.16.1-0.20250103044727-b55f5083bb46 h1:EdQbU3wBHUE1Jh/ucKbVkldi+l//jBg9qB0razB0G1A= +github.com/dell/gopowerstore v1.16.1-0.20250103044727-b55f5083bb46/go.mod h1:S3Gxw34FJbKNorL/OPe/DF+0pUSkBm6E6b4ka7m6cuo= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= diff --git a/models/replication_rule.go b/models/replication_rule.go index 4ae1b490..88d41707 100644 --- a/models/replication_rule.go +++ b/models/replication_rule.go @@ -28,3 +28,44 @@ type ReplicationRule struct { AlertThreshold types.Int64 `tfsdk:"alert_threshold"` IsReadOnly types.Bool `tfsdk:"is_read_only"` } + +// ReplicationRuleDataSourceModel defines the model for replication rule data source +type ReplicationRuleDataSourceModel struct { + ReplicationRules []ReplicationRuleDataSource `tfsdk:"replication_rules"` + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` +} + +// ReplicationRuleDataSource defines the model for replication rule details +type ReplicationRuleDataSource struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + RPO types.String `tfsdk:"rpo"` + RemoteSystemID types.String `tfsdk:"remote_system_id"` + AlertThreshold types.Int64 `tfsdk:"alert_threshold"` + IsReadOnly types.Bool `tfsdk:"is_read_only"` + IsReplica types.Bool `tfsdk:"is_replica"` + ManagedBy types.String `tfsdk:"managed_by"` + ManagedByID types.String `tfsdk:"managed_by_id"` + Policies []Policy `tfsdk:"policies"` + RemoteSystem RemoteSystem `tfsdk:"remote_system"` + ReplicationSession []ReplicationSession `tfsdk:"replication_session"` +} + +// Policy defines the model for policy +type Policy struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` +} + +// RemoteSystem defines the model for remote system +type RemoteSystem struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` +} + +// ReplicationSession defines the model for replication session +type ReplicationSession struct { + ID types.String `tfsdk:"id"` + State types.String `tfsdk:"state"` +} diff --git a/powerstore/datasource_replication_rule.go b/powerstore/datasource_replication_rule.go new file mode 100644 index 00000000..44c1cb0b --- /dev/null +++ b/powerstore/datasource_replication_rule.go @@ -0,0 +1,268 @@ +package powerstore + +import ( + "context" + "fmt" + "terraform-provider-powerstore/client" + "terraform-provider-powerstore/models" + + "github.com/dell/gopowerstore" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +var ( + _ datasource.DataSource = &replicationRuleDataSource{} + _ datasource.DataSourceWithConfigure = &replicationRuleDataSource{} +) + +// newReplicationRuleDataSource returns the replication rule data source object +func newReplicationRuleDataSource() datasource.DataSource { + return &replicationRuleDataSource{} +} + +type replicationRuleDataSource struct { + client *client.Client +} + +func (d *replicationRuleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_replication_rule" +} + +func (d *replicationRuleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "This datasource is used to query the existing replication rule from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block.", + MarkdownDescription: "This datasource is used to query the existing replication rule from PowerStore array. The information fetched from this datasource can be used for getting the details for further processing in resource block.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "Unique identifier of the replication rule. Conflicts with `name`.", + MarkdownDescription: "Unique identifier of the replication rule. Conflicts with `name`.", + Optional: true, + Validators: []validator.String{ + stringvalidator.ConflictsWith(path.MatchRoot("name")), + stringvalidator.LengthAtLeast(1), + }, + }, + "name": schema.StringAttribute{ + Description: "Name of the replication rule. Conflicts with `id`.", + MarkdownDescription: "Name of the replication rule. Conflicts with `id`.", + Optional: true, + Validators: []validator.String{ + stringvalidator.LengthAtLeast(1), + }, + }, + "replication_rules": schema.ListNestedAttribute{ + Description: "List of replication rules.", + MarkdownDescription: "List of replication rules.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "Unique identifier of the replication rule. Conflicts with `name`.", + MarkdownDescription: "Unique identifier of the replication rule. Conflicts with `name`.", + Computed: true, + }, + "name": schema.StringAttribute{ + Description: "Name of the replication rule. Conflicts with `id`.", + MarkdownDescription: "Name of the replication rule. Conflicts with `id`.", + Computed: true, + }, + "rpo": schema.StringAttribute{ + Description: "The RPO (Recovery Point Objective) of the replication rule.", + MarkdownDescription: "The RPO (Recovery Point Objective) of the replication rule.", + Computed: true, + }, + "remote_system_id": schema.StringAttribute{ + Description: "The ID of the remote system associated with the replication rule.", + MarkdownDescription: "The ID of the remote system associated with the replication rule.", + Computed: true, + }, + "alert_threshold": schema.Int64Attribute{ + Description: "The alert threshold for the replication rule.", + MarkdownDescription: "The alert threshold for the replication rule.", + Computed: true, + }, + "is_read_only": schema.BoolAttribute{ + Description: "Indicates whether the replication rule is read-only.", + MarkdownDescription: "Indicates whether the replication rule is read-only.", + Computed: true, + }, + "is_replica": schema.BoolAttribute{ + Description: "Indicates whether the replication rule is a replica.", + MarkdownDescription: "Indicates whether the replication rule is a replica.", + Computed: true, + }, + "managed_by": schema.StringAttribute{ + Description: "The entity that manages the replication rule.", + MarkdownDescription: "The entity that manages the replication rule.", + Computed: true, + }, + "managed_by_id": schema.StringAttribute{ + Description: "The ID of the managing entity.", + MarkdownDescription: "The ID of the managing entity.", + Computed: true, + }, + "policies": schema.ListNestedAttribute{ + Description: "The protection policies associated with the replication rule.", + MarkdownDescription: "The protection policies associated with the replication rule.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "The ID of the protection policy.", + MarkdownDescription: "The ID of the protection policy.", + Computed: true, + }, + "name": schema.StringAttribute{ + Description: "The name of the protection policy.", + MarkdownDescription: "The name of the protection policy.", + Computed: true, + }, + }, + }, + }, + "remote_system": schema.SingleNestedAttribute{ + Description: "The remote system associated with the replication rule.", + MarkdownDescription: "The remote system associated with the replication rule.", + Computed: true, + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "The ID of the remote system.", + MarkdownDescription: "The ID of the remote system.", + Computed: true, + }, + "name": schema.StringAttribute{ + Description: "The name of the remote system.", + MarkdownDescription: "The name of the remote system.", + Computed: true, + }, + }, + }, + "replication_session": schema.ListNestedAttribute{ + Description: "The replication session associated with the replication rule.", + MarkdownDescription: "The replication session associated with the replication rule.", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Description: "The ID of the replication session.", + MarkdownDescription: "The ID of the replication session.", + Computed: true, + }, + "state": schema.StringAttribute{ + Description: "The state of the replication session.", + MarkdownDescription: "The state of the replication session.", + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func (d *replicationRuleDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + d.client = req.ProviderData.(*client.Client) +} + +func (d *replicationRuleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var ( + state models.ReplicationRuleDataSourceModel + replicationRules []gopowerstore.ReplicationRule + replicationRule gopowerstore.ReplicationRule + err error + ) + + diags := req.Config.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + //Read the replication rules based on replication rule id/name and if nothing is mentioned, then it returns all the replication rules + if state.Name.ValueString() != "" { + tflog.Info(ctx, fmt.Sprintf("Read replication rule with name: %s", state.Name.ValueString())) + replicationRule, err = d.client.PStoreClient.GetReplicationRuleByName(context.Background(), state.Name.ValueString()) + replicationRules = append(replicationRules, replicationRule) + tflog.Info(ctx, fmt.Sprintf("Content: %v", replicationRule)) + } else if state.ID.ValueString() != "" { + replicationRule, err = d.client.PStoreClient.GetReplicationRule(context.Background(), state.ID.ValueString()) + replicationRules = append(replicationRules, replicationRule) + } else { + replicationRules, err = d.client.PStoreClient.GetReplicationRules(context.Background()) + } + + //check if there is any error while getting the replication rules details + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read PowerStore Replication Rules", + err.Error(), + ) + return + } + + state.ReplicationRules = updateReplicationRuleState(replicationRules) + if state.ID.IsNull() { + state.ID = types.StringValue("replication_rule_data_source") + } + diags = resp.State.Set(ctx, state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} + +// updateReplicationRuleState iterates over the replication rules list and update the state +func updateReplicationRuleState(ReplicationRules []gopowerstore.ReplicationRule) (response []models.ReplicationRuleDataSource) { + for _, ReplicationRuleValue := range ReplicationRules { + policiesList := []models.Policy{} + for _, policy := range ReplicationRuleValue.ProtectionPolicies { + policiesList = append(policiesList, models.Policy{ + ID: types.StringValue(policy.ID), + Name: types.StringValue(policy.Name), + }) + } + + remoteSystem := models.RemoteSystem{ + ID: types.StringValue(ReplicationRuleValue.RemoteSystem.ID), + Name: types.StringValue(ReplicationRuleValue.RemoteSystem.Name), + } + + sessionList := []models.ReplicationSession{} + for _, session := range ReplicationRuleValue.ReplicationSession { + sessionList = append(sessionList, models.ReplicationSession{ + ID: types.StringValue(session.ID), + State: types.StringValue(string(session.State)), + }) + } + + var replicationRuleState = models.ReplicationRuleDataSource{ + ID: types.StringValue(ReplicationRuleValue.ID), + Name: types.StringValue(ReplicationRuleValue.Name), + RPO: types.StringValue(string(ReplicationRuleValue.Rpo)), + RemoteSystemID: types.StringValue(ReplicationRuleValue.RemoteSystemID), + IsReadOnly: types.BoolValue(ReplicationRuleValue.IsReadOnly), + AlertThreshold: types.Int64Value(int64(ReplicationRuleValue.AlertThreshold)), + IsReplica: types.BoolValue(ReplicationRuleValue.IsReplica), + ManagedBy: types.StringValue(string(ReplicationRuleValue.ManagedBy)), + ManagedByID: types.StringValue(string(ReplicationRuleValue.ManagedByID)), + } + replicationRuleState.Policies = policiesList + replicationRuleState.RemoteSystem = remoteSystem + replicationRuleState.ReplicationSession = sessionList + + response = append(response, replicationRuleState) + } + return response +} diff --git a/powerstore/datasource_replication_rule_test.go b/powerstore/datasource_replication_rule_test.go new file mode 100644 index 00000000..c134e43c --- /dev/null +++ b/powerstore/datasource_replication_rule_test.go @@ -0,0 +1,85 @@ +/* +Copyright (c) 2024 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. +*/ + +package powerstore + +import ( + "log" + "os" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccReplicationRuleDataSource(t *testing.T) { + if os.Getenv("TF_ACC") == "" { + t.Skip("Dont run with units tests because it will try to create the context") + } + + log.Println("Running TestAccReplicationRuleDataSource") + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testProviderFactory, + Steps: []resource.TestStep{ + { + Config: ProviderConfigForTesting + ReplicationRuleDataSourceName, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.powerstore_replication_rule.test1", "replication_rules.#", "1"), + resource.TestCheckResourceAttrPair("data.powerstore_replication_rule.test1", "replication_rules.0.name", "powerstore_replication_rule.test", "name"), + ), + }, + { + Config: ProviderConfigForTesting + ReplicationRuleDataSourceID, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.powerstore_replication_rule.test1", "replication_rules.#", "1"), + resource.TestCheckResourceAttrPair("data.powerstore_replication_rule.test1", "replication_rules.0.id", "powerstore_replication_rule.test", "id"), + ), + }, + { + Config: ProviderConfigForTesting + ReplicationRuleDataSourceparamsAll, + }, + { + Config: ProviderConfigForTesting + ReplicationRuleDataSourceparamsNameNegative, + ExpectError: regexp.MustCompile("Unable to Read PowerStore Replication Rules"), + }, + }, + }) +} + +var ReplicationRuleDataSourceName = ReplicationRuleParamsCreate + ` +data "powerstore_replication_rule" "test1" { + name = powerstore_replication_rule.test.name +} +` + +var ReplicationRuleDataSourceID = ReplicationRuleParamsCreate + ` +data "powerstore_replication_rule" "test1" { + id = powerstore_replication_rule.test.id +} +` + +var ReplicationRuleDataSourceparamsAll = ReplicationRuleParamsCreate + ` +data "powerstore_replication_rule" "test1" { +} +` + +var ReplicationRuleDataSourceparamsNameNegative = ` +data "powerstore_replication_rule" "test1" { + name = "invalid" +} +` diff --git a/powerstore/provider.go b/powerstore/provider.go index d53b6299..19fd1b35 100644 --- a/powerstore/provider.go +++ b/powerstore/provider.go @@ -164,6 +164,7 @@ func (p *Pstoreprovider) DataSources(ctx context.Context) []func() datasource.Da newHostGroupDataSource, newVolumeGroupSnapshotDataSource, newVolumeSnapshotDataSource, + newReplicationRuleDataSource, } }