Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
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
6 changes: 6 additions & 0 deletions openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,9 @@ func NewDeHServiceV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts
sc, err := initClientOpts(client, eo, "deh")
return sc, err
}

// NewCSBSService creates a ServiceClient that can be used to access the Cloud Server Backup service.
func NewCSBSService(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "data-protect")
return sc, err
}
44 changes: 44 additions & 0 deletions openstack/csbs/v1/backup/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Package backup enables management and retrieval of
back up resources.

Example to List Backup
listbackup := backup.ListOpts{ID: "7b99acfd-18c3-4f26-9d39-b4ebd2ea3e12"}
allbackups, err := backup.List(client,listbackup)
if err != nil {
panic(err)
}
fmt.Println(allbackups)



Example to Create a Backup
createBackup:=backup.CreateOpts{BackupName: "c2c-backup", Description: "mybackup"}
out,err:=backup.Create(client,"fc4d5750-22e7-4798-8a46-f48f62c4c1da", "f8ddc472-cf00-4384-851e-5f2a68c33762",
createBackup).Extract()
fmt.Println(out)
fmt.Println(err)

Example to Query if resources can be backed up
createQuery:=backup.ResourceBackupCapOpts{CheckProtectable:[]backup.ResourceCapQueryParams{{ResourceId: "069e678a-f1d1-4a38-880b-459bde82fcc6",
ResourceType: "OS::Nova::Server"}}}
out,err:=backup.QueryResourceBackupCapability(client,"fc4d5750-22e7-4798-8a46-f48f62c4c1da",
createQuery).ExtractQueryResponse()
fmt.Println(out)
fmt.Println(err)


Example to Delete a Backup
out:=backup.Delete(client,"fc4d5750-22e7-4798-8a46-f48f62c4c1da")
fmt.Println(out)
if err != nil {
panic(err)
}

Example to Get Backup
result:=backup.Get(client,"7b99acfd-18c3-4f26-9d39-b4ebd2ea3e12")
out,err:=result.ExtractBackup()
fmt.Println(out)

*/
package backup
164 changes: 164 additions & 0 deletions openstack/csbs/v1/backup/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package backup

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)

type ResourceTag struct {
Key string `json:"key"`
Value string `json:"value"`
}

// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the attributes you want to see returned. Marker and Limit are used for pagination.
type ListOpts struct {
Status string `q:"status"`
Limit string `q:"limit"`
Marker string `q:"marker"`
Sort string `q:"sort"`
AllTenants string `q:"all_tenants"`
Name string `q:"name"`
ResourceId string `q:"resource_id"`
ResourceName string `q:"resource_name"`
PolicyId string `q:"policy_id"`
VmIp string `q:"ip"`
CheckpointId string `q:"checkpoint_id"`
ID string
ResourceType string `q:"resource_type"`
}

// List returns collection of
// backups. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Backup, error) {
q, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return nil, err
}
u := listURL(c) + q.String()
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
return BackupPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()

allBackups, err := ExtractBackups(pages)
if err != nil {
return nil, err
}

if opts.ID != "" {
return FilterBackupsById(allBackups, opts.ID)
}

return allBackups, nil

}

func FilterBackupsById(backups []Backup, filterId string) ([]Backup, error) {

var refinedBackups []Backup

for _, backup := range backups {

if filterId == backup.Id {
refinedBackups = append(refinedBackups, backup)
}
}

return refinedBackups, nil
}

// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToBackupCreateMap() (map[string]interface{}, error)
}

// CreateOpts contains the options for create a Backup. This object is
// passed to backup.Create().
type CreateOpts struct {
BackupName string `json:"backup_name,omitempty"`
Description string `json:"description,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
Tags []ResourceTag `json:"tags,omitempty"`
ExtraInfo interface{} `json:"extra_info,omitempty"`
}

// ToBackupCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "protect")
}

// Create will create a new backup based on the values in CreateOpts. To extract
// the checkpoint object from the response, call the Extract method on the
// CreateResult.
func Create(client *golangsdk.ServiceClient, resourceId string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToBackupCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, resourceId), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}

// ResourceBackupCapabilityOptsBuilder allows extensions to add additional parameters to the
// QueryResourceBackupCapability request.
type ResourceBackupCapabilityOptsBuilder interface {
ToQueryResourceCreateMap() (map[string]interface{}, error)
}

// ResourceBackupCapOpts contains the options for querying whether resources can be backed up. This object is
// passed to backup.QueryResourceBackupCapability().
type ResourceBackupCapOpts struct {
CheckProtectable []ResourceCapQueryParams `json:"check_protectable" required:"true"`
}

type ResourceCapQueryParams struct {
ResourceId string `json:"resource_id" required:"true"`
ResourceType string `json:"resource_type" required:"true"`
}

// ToQueryResourceCreateMap assembles a request body based on the contents of a
// ResourceBackupCapOpts.
func (opts ResourceBackupCapOpts) ToQueryResourceCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}

// QueryResourceBackupCapability will query whether resources can be backed up based on the values in ResourceBackupCapOpts. To extract
// the ResourceCap object from the response, call the ExtractQueryResponse method on the
// QueryResult.
func QueryResourceBackupCapability(client *golangsdk.ServiceClient, opts ResourceBackupCapabilityOptsBuilder) (r QueryResult) {
b, err := opts.ToQueryResourceCreateMap()
if err != nil {
r.Err = err

return
}
_, r.Err = client.Post(resourceURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}

// Get will get a single backup with specific ID. To extract the Backup object from the response,
// call the ExtractBackup method on the GetResult.
func Get(client *golangsdk.ServiceClient, backupId string) (r GetResult) {
_, r.Err = client.Get(getURL(client, backupId), &r.Body, nil)

return

}

// Delete will delete an existing backup.
func Delete(client *golangsdk.ServiceClient, checkpoint_id string) (r DeleteResult) {
_, r.Err = client.Delete(deleteURL(client, checkpoint_id), &golangsdk.RequestOpts{
OkCodes: []int{200},
JSONResponse: nil,
})
return
}
Loading