Skip to content

Commit

Permalink
Merge pull request nutanix#216 from nutanix/dr-runbook-2
Browse files Browse the repository at this point in the history
Protection rule and Recovery plan(DR Runbook) resources and datasources
  • Loading branch information
marinsalinas authored Jan 22, 2021
2 parents 275e44c + c4f4517 commit c3bc019
Show file tree
Hide file tree
Showing 27 changed files with 7,600 additions and 0 deletions.
246 changes: 246 additions & 0 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ type Service interface {
GetPermission(permissionUUID string) (*PermissionIntentResponse, error)
ListPermission(getEntitiesRequest *DSMetadata) (*PermissionListResponse, error)
ListAllPermission(filter string) (*PermissionListResponse, error)
GetProtectionRule(uuid string) (*ProtectionRuleResponse, error)
ListProtectionRules(getEntitiesRequest *DSMetadata) (*ProtectionRulesListResponse, error)
ListAllProtectionRules(filter string) (*ProtectionRulesListResponse, error)
CreateProtectionRule(request *ProtectionRuleInput) (*ProtectionRuleResponse, error)
UpdateProtectionRule(uuid string, body *ProtectionRuleInput) (*ProtectionRuleResponse, error)
DeleteProtectionRule(uuid string) (*DeleteResponse, error)
GetRecoveryPlan(uuid string) (*RecoveryPlanResponse, error)
ListRecoveryPlans(getEntitiesRequest *DSMetadata) (*RecoveryPlanListResponse, error)
ListAllRecoveryPlans(filter string) (*RecoveryPlanListResponse, error)
CreateRecoveryPlan(request *RecoveryPlanInput) (*RecoveryPlanResponse, error)
UpdateRecoveryPlan(uuid string, body *RecoveryPlanInput) (*RecoveryPlanResponse, error)
DeleteRecoveryPlan(uuid string) (*DeleteResponse, error)
}

/*CreateVM Creates a VM
Expand Down Expand Up @@ -1913,3 +1925,237 @@ func (op Operations) ListAllPermission(filter string) (*PermissionListResponse,

return resp, nil
}

//GetProtectionRule ...
func (op Operations) GetProtectionRule(uuid string) (*ProtectionRuleResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/protection_rules/%s", uuid)
protectionRule := new(ProtectionRuleResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return protectionRule, op.client.Do(ctx, req, protectionRule)
}

//ListProtectionRules ...
func (op Operations) ListProtectionRules(getEntitiesRequest *DSMetadata) (*ProtectionRulesListResponse, error) {
ctx := context.TODO()
path := "/protection_rules/list"

list := new(ProtectionRulesListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return list, op.client.Do(ctx, req, list)
}

// ListAllProtectionRules ...
func (op Operations) ListAllProtectionRules(filter string) (*ProtectionRulesListResponse, error) {
entities := make([]*ProtectionRuleResponse, 0)

resp, err := op.ListProtectionRules(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("protection_rule"),
Length: utils.Int64Ptr(itemsPerPage),
})
if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListProtectionRules(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("protection_rule"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
log.Printf("[Debug] total=%d, remaining=%d, offset=%d len(entities)=%d\n", totalEntities, remaining, offset, len(entities))
}

resp.Entities = entities
}

return resp, nil
}

//CreateProtectionRule ...
func (op Operations) CreateProtectionRule(createRequest *ProtectionRuleInput) (*ProtectionRuleResponse, error) {
ctx := context.TODO()

req, err := op.client.NewRequest(ctx, http.MethodPost, "/protection_rules", createRequest)
protectionRuleResponse := new(ProtectionRuleResponse)

if err != nil {
return nil, err
}

return protectionRuleResponse, op.client.Do(ctx, req, protectionRuleResponse)
}

//UpdateProtectionRule ...
func (op Operations) UpdateProtectionRule(uuid string, body *ProtectionRuleInput) (*ProtectionRuleResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/protection_rules/%s", uuid)
req, err := op.client.NewRequest(ctx, http.MethodPut, path, body)
protectionRuleResponse := new(ProtectionRuleResponse)

if err != nil {
return nil, err
}

return protectionRuleResponse, op.client.Do(ctx, req, protectionRuleResponse)
}

//DeleteProtectionRule ...
func (op Operations) DeleteProtectionRule(uuid string) (*DeleteResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/protection_rules/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
deleteResponse := new(DeleteResponse)

if err != nil {
return nil, err
}

return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}

//GetRecoveryPlan ...
func (op Operations) GetRecoveryPlan(uuid string) (*RecoveryPlanResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/recovery_plans/%s", uuid)
RecoveryPlan := new(RecoveryPlanResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return RecoveryPlan, op.client.Do(ctx, req, RecoveryPlan)
}

//ListRecoveryPlans ...
func (op Operations) ListRecoveryPlans(getEntitiesRequest *DSMetadata) (*RecoveryPlanListResponse, error) {
ctx := context.TODO()
path := "/recovery_plans/list"

list := new(RecoveryPlanListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return list, op.client.Do(ctx, req, list)
}

// ListAllRecoveryPlans ...
func (op Operations) ListAllRecoveryPlans(filter string) (*RecoveryPlanListResponse, error) {
entities := make([]*RecoveryPlanResponse, 0)

resp, err := op.ListRecoveryPlans(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("recovery_plan"),
Length: utils.Int64Ptr(itemsPerPage),
})
if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListRecoveryPlans(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("recovery_plan"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
log.Printf("[Debug] total=%d, remaining=%d, offset=%d len(entities)=%d\n", totalEntities, remaining, offset, len(entities))
}

resp.Entities = entities
}

return resp, nil
}

//CreateRecoveryPlan ...
func (op Operations) CreateRecoveryPlan(createRequest *RecoveryPlanInput) (*RecoveryPlanResponse, error) {
ctx := context.TODO()

req, err := op.client.NewRequest(ctx, http.MethodPost, "/recovery_plans", createRequest)
RecoveryPlanResponse := new(RecoveryPlanResponse)

if err != nil {
return nil, err
}

return RecoveryPlanResponse, op.client.Do(ctx, req, RecoveryPlanResponse)
}

//UpdateRecoveryPlan ...
func (op Operations) UpdateRecoveryPlan(uuid string, body *RecoveryPlanInput) (*RecoveryPlanResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/recovery_plans/%s", uuid)
req, err := op.client.NewRequest(ctx, http.MethodPut, path, body)
RecoveryPlanResponse := new(RecoveryPlanResponse)

if err != nil {
return nil, err
}

return RecoveryPlanResponse, op.client.Do(ctx, req, RecoveryPlanResponse)
}

//DeleteRecoveryPlan ...
func (op Operations) DeleteRecoveryPlan(uuid string) (*DeleteResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/recovery_plans/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
deleteResponse := new(DeleteResponse)

if err != nil {
return nil, err
}

return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}
Loading

0 comments on commit c3bc019

Please sign in to comment.