From 4dc9881c69eedcef4e19022c94fe13db53e492e1 Mon Sep 17 00:00:00 2001 From: shenda1 Date: Thu, 26 Dec 2024 14:55:13 +0530 Subject: [PATCH 1/2] added function for getting all the replication rules Signed-off-by: shenda1 --- client.go | 1 + mocks/Client.go | 30 ++++++++++++++++++++++++++++++ replication.go | 26 ++++++++++++++++++++++++++ replication_test.go | 13 +++++++++++++ replication_types.go | 5 ++++- 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 2784cdf..4a59746 100644 --- a/client.go +++ b/client.go @@ -119,6 +119,7 @@ type Client interface { ModifyReplicationRule(ctx context.Context, modifyParams *ReplicationRuleModify, id string) (EmptyResponse, error) GetReplicationRule(ctx context.Context, id string) (resp ReplicationRule, err error) GetReplicationRuleByName(ctx context.Context, name string) (ReplicationRule, error) + GetReplicationRules(ctx context.Context) ([]ReplicationRule, error) CreateProtectionPolicy(ctx context.Context, createParams *ProtectionPolicyCreate) (CreateResponse, error) ModifyVolumeGroup(ctx context.Context, modifyParams *VolumeGroupModify, id string) (resp EmptyResponse, err error) GetProtectionPolicy(ctx context.Context, id string) (ProtectionPolicy, error) diff --git a/mocks/Client.go b/mocks/Client.go index affb0a9..18354b3 100644 --- a/mocks/Client.go +++ b/mocks/Client.go @@ -4392,6 +4392,36 @@ func (_m *Client) ModifyReplicationRule(ctx context.Context, modifyParams *gopow return r0, r1 } +// GetReplicationRules provides a mock function with given fields: ctx +func (_m *Client) GetReplicationRules(ctx context.Context) ([]gopowerstore.ReplicationRule, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for GetReplicationRules") + } + + var r0 []gopowerstore.ReplicationRule + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) ([]gopowerstore.ReplicationRule, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) []gopowerstore.ReplicationRule); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]gopowerstore.ReplicationRule) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // NewClient creates a new instance of Client. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewClient(t interface { diff --git a/replication.go b/replication.go index 8243c18..f027723 100644 --- a/replication.go +++ b/replication.go @@ -293,3 +293,29 @@ func (c *ClientIMPL) ModifyReplicationRule(ctx context.Context, modifyParams *Re &resp) return resp, WrapErr(err) } + +// GetReplicationRules returns a list of replication rules +func (c *ClientIMPL) GetReplicationRules(ctx context.Context) ([]ReplicationRule, error) { + var result []ReplicationRule + err := c.readPaginatedData(func(offset int) (api.RespMeta, error) { + var page []ReplicationRule + policy := ReplicationRule{} + qp := c.APIClient().QueryParamsWithFields(&policy) + qp.Order("name") + qp.Offset(offset).Limit(paginationDefaultPageSize) + meta, err := c.APIClient().Query( + ctx, + RequestConfig{ + Method: "GET", + Endpoint: replicationRuleURL, + QueryParams: qp, + }, + &page) + err = WrapErr(err) + if err == nil { + result = append(result, page...) + } + return meta, err + }) + return result, err +} diff --git a/replication_test.go b/replication_test.go index 2104980..ec20be1 100644 --- a/replication_test.go +++ b/replication_test.go @@ -35,6 +35,7 @@ var ( protectionPolicyID = "15c03067-c4f2-428b-b637-18b0266979f0" protectionPolicyID2 = "3224ff5a-2e83-4a7f-a0c4-009df20e36db" replicationRuleID = "6b930711-46bc-4a4b-9d6a-22c77a7838c4" + replicationRuleID2 = "2d0780e3-2ce7-4d8b-b2ec-349c5e9e26a9" ) func TestClientIMPL_CreateProtectionPolicy(t *testing.T) { @@ -244,3 +245,15 @@ func TestClientIMPL_GetProtectionPolicies(t *testing.T) { assert.Len(t, policies, 2) assert.Equal(t, protectionPolicyID, policies[0].ID) } + +func TestClientIMPL_GetReplicationRules(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`[{"id": "%s"}, {"id": "%s"}]`, replicationRuleID, replicationRuleID2) + httpmock.RegisterResponder("GET", replicationRuleMockURL, + httpmock.NewStringResponder(200, respData)) + rules, err := C.GetReplicationRules(context.Background()) + assert.Nil(t, err) + assert.Len(t, rules, 2) + assert.Equal(t, replicationRuleID, rules[0].ID) +} diff --git a/replication_types.go b/replication_types.go index 5056cd2..28eff10 100644 --- a/replication_types.go +++ b/replication_types.go @@ -84,10 +84,13 @@ type ReplicationRule struct { ProtectionPolicies []ProtectionPolicy `json:"policies"` AlertThreshold int `json:"alert_threshold"` IsReadOnly bool `json:"is_read_only,omitempty"` + IsReplica bool `json:"is_replica,omitempty"` + ManagedBy string `json:"managed_by,omitempty"` + ManagedByID string `json:"managed_by_id,omitempty"` } func (rule *ReplicationRule) Fields() []string { - return []string{"id", "name", "rpo", "remote_system_id", "alert_threshold", "is_read_only"} + return []string{"id", "name", "rpo", "remote_system_id", "alert_threshold", "is_read_only", "is_replica", "managed_by", "managed_by_id", "policies(id,name)"} } // VirtualMachines - Details of virtual machine From af402d9d636e7236861173ef3e8962e866762195 Mon Sep 17 00:00:00 2001 From: shenda1 Date: Fri, 27 Dec 2024 10:32:17 +0530 Subject: [PATCH 2/2] added fields to get more details about replication rule Signed-off-by: shenda1 --- replication_types.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/replication_types.go b/replication_types.go index 28eff10..04b2013 100644 --- a/replication_types.go +++ b/replication_types.go @@ -80,17 +80,19 @@ type ReplicationRule struct { // If RPO is Zero, it indicates the replication_type is 'sync'. Rpo RPOEnum `json:"rpo"` // RemoteSystemID - unique identifier of the remote system to which this rule will replicate the associated resources. - RemoteSystemID string `json:"remote_system_id"` - ProtectionPolicies []ProtectionPolicy `json:"policies"` - AlertThreshold int `json:"alert_threshold"` - IsReadOnly bool `json:"is_read_only,omitempty"` - IsReplica bool `json:"is_replica,omitempty"` - ManagedBy string `json:"managed_by,omitempty"` - ManagedByID string `json:"managed_by_id,omitempty"` + RemoteSystemID string `json:"remote_system_id"` + ProtectionPolicies []ProtectionPolicy `json:"policies"` + AlertThreshold int `json:"alert_threshold"` + IsReadOnly bool `json:"is_read_only,omitempty"` + IsReplica bool `json:"is_replica,omitempty"` + ManagedBy string `json:"managed_by,omitempty"` + ManagedByID string `json:"managed_by_id,omitempty"` + RemoteSystem RemoteSystem `json:"remote_system,omitempty"` + ReplicationSession []ReplicationSession `json:"replication_sessions,omitempty"` } func (rule *ReplicationRule) Fields() []string { - return []string{"id", "name", "rpo", "remote_system_id", "alert_threshold", "is_read_only", "is_replica", "managed_by", "managed_by_id", "policies(id,name)"} + return []string{"id", "name", "rpo", "remote_system_id", "alert_threshold", "is_read_only", "is_replica", "managed_by", "managed_by_id", "policies(id,name)", "remote_system(id,name)", "replication_sessions(id,state)"} } // VirtualMachines - Details of virtual machine