diff --git a/openstack/sdrs/v1/replications/requests.go b/openstack/sdrs/v1/replications/requests.go new file mode 100644 index 000000000..4933c4957 --- /dev/null +++ b/openstack/sdrs/v1/replications/requests.go @@ -0,0 +1,110 @@ +package replications + +import ( + "github.com/huaweicloud/golangsdk" +) + +var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ + MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, +} + +// CreateOptsBuilder allows extensions to add additional parameters to the +// Create request. +type CreateOptsBuilder interface { + ToReplicationCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new replication. +type CreateOpts struct { + //Protection Group ID + GroupID string `json:"server_group_id" required:"true"` + //Volume ID + VolumeID string `json:"volume_id" required:"true"` + //Replication Name + Name string `json:"name" required:"true"` + //Replication Description + Description string `json:"description,omitempty"` +} + +// ToReplicationCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToReplicationCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "replication") +} + +// Create will create a new Replication based on the values in CreateOpts. +func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { + b, err := opts.ToReplicationCreateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) + return +} + +// UpdateOptsBuilder allows extensions to add additional parameters to the +// Update request. +type UpdateOptsBuilder interface { + ToReplicationUpdateMap() (map[string]interface{}, error) +} + +// UpdateOpts contains all the values needed to update a Replication. +type UpdateOpts struct { + //Replication name + Name string `json:"name" required:"true"` +} + +// ToReplicationUpdateMap builds a update request body from UpdateOpts. +func (opts UpdateOpts) ToReplicationUpdateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "replication") +} + +// Update accepts a UpdateOpts struct and uses the values to update a Replication.The response code from api is 200 +func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { + b, err := opts.ToReplicationUpdateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.Put(resourceURL(c, id), b, nil, reqOpt) + return +} + +// Get retrieves a particular Replication based on its unique ID. +func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = c.Get(resourceURL(c, id), &r.Body, &RequestOpts) + return +} + +// DeleteOptsBuilder allows extensions to add additional parameters to the +// Delete request. +type DeleteOptsBuilder interface { + ToReplicationDeleteMap() (map[string]interface{}, error) +} + +// DeleteOpts contains all the values needed to delete a Replication. +type DeleteOpts struct { + //Group ID + GroupID string `json:"server_group_id,omitempty"` + //Delete Target Volume + DeleteVolume bool `json:"delete_target_volume,omitempty"` +} + +// ToReplicationDeleteMap builds a update request body from DeleteOpts. +func (opts DeleteOpts) ToReplicationDeleteMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "replication") +} + +// Delete will permanently delete a particular Replication based on its unique ID. +func Delete(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r JobResult) { + b, err := opts.ToReplicationUpdateMap() + if err != nil { + r.Err = err + return + } + reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} + _, r.Err = c.DeleteWithBodyResp(resourceURL(c, id), b, &r.Body, reqOpt) + return +} diff --git a/openstack/sdrs/v1/replications/results.go b/openstack/sdrs/v1/replications/results.go new file mode 100644 index 000000000..0413c1749 --- /dev/null +++ b/openstack/sdrs/v1/replications/results.go @@ -0,0 +1,75 @@ +package replications + +import ( + "github.com/huaweicloud/golangsdk" +) + +type Replication struct { + //Replication ID + Id string `json:"id"` + //Replication Name + Name string `json:"name"` + //Replication Description + Description string `json:"description"` + //Replication Model + ReplicaModel string `json:"replication_model"` + //Replication Status + Status string `json:"status"` + //Replication Attachment + Attachment []Attachment `json:"attachment"` + //Replication Group ID + GroupID string `json:"server_group_id"` + //Replication Volume IDs + VolumeIDs string `json:"volume_ids"` + //Replication Priority Station + PriorityStation string `json:"priority_station"` + //Replication Fault Level + FaultLevel string `json:"fault_level"` + //Replication Record Metadata + RecordMetadata RecordMetadata `json:"record_metadata"` +} + +type Attachment struct { + //Device Name + Device string `json:"device"` + //Protected Instance ID + ProtectedInstance string `json:"protected_instance"` +} + +type RecordMetadata struct { + //Whether Multiattach + Multiattach bool `json:"multiattach"` + //Whether Bootable + Bootable bool `json:"bootable"` + //Volume Size + VolumeSize int `json:"volume_size"` + //Volume Type + VolumeType int `json:"volume_type"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a replication. +func (r commonResult) Extract() (*Replication, error) { + var response Replication + err := r.ExtractInto(&response) + return &response, err +} + +func (r commonResult) ExtractInto(v interface{}) error { + return r.Result.ExtractIntoStructPtr(v, "replication") +} + +// UpdateResult represents the result of a update operation. Call its Extract +// method to interpret it as a Replication. +type UpdateResult struct { + commonResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a Replication. +type GetResult struct { + commonResult +} diff --git a/openstack/sdrs/v1/replications/results_job.go b/openstack/sdrs/v1/replications/results_job.go new file mode 100644 index 000000000..0c4dfbd14 --- /dev/null +++ b/openstack/sdrs/v1/replications/results_job.go @@ -0,0 +1,80 @@ +package replications + +import ( + "fmt" + + "github.com/huaweicloud/golangsdk" +) + +type JobResponse struct { + JobID string `json:"job_id"` +} + +type JobStatus struct { + Status string `json:"status"` + Entities map[string]string `json:"entities"` + JobID string `json:"job_id"` + JobType string `json:"job_type"` + BeginTime string `json:"begin_time"` + EndTime string `json:"end_time"` + ErrorCode string `json:"error_code"` + FailReason string `json:"fail_reason"` +} + +type JobResult struct { + golangsdk.Result +} + +func (r JobResult) ExtractJobResponse() (*JobResponse, error) { + job := new(JobResponse) + err := r.ExtractInto(job) + return job, err +} + +func (r JobResult) ExtractJobStatus() (*JobStatus, error) { + job := new(JobStatus) + err := r.ExtractInto(job) + return job, err +} + +func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error { + + jobClient := *client + jobClient.ResourceBase = jobClient.Endpoint + return golangsdk.WaitFor(secs, func() (bool, error) { + job := new(JobStatus) + _, err := jobClient.Get(jobClient.ServiceURL("jobs", jobID), &job, nil) + if err != nil { + return false, err + } + + if job.Status == "SUCCESS" { + return true, nil + } + if job.Status == "FAIL" { + err = fmt.Errorf("Job failed with code %s: %s.\n", job.ErrorCode, job.FailReason) + return false, err + } + + return false, nil + }) +} + +func GetJobEntity(client *golangsdk.ServiceClient, jobId string, label string) (interface{}, error) { + + jobClient := *client + jobClient.ResourceBase = jobClient.Endpoint + job := new(JobStatus) + _, err := jobClient.Get(jobClient.ServiceURL("jobs", jobId), &job, nil) + if err != nil { + return nil, err + } + + if job.Status == "SUCCESS" { + if e := job.Entities[label]; e != "" { + return e, nil + } + } + + return nil, fmt.Errorf("Unexpected conversion error in GetJobEntity.") +} diff --git a/openstack/sdrs/v1/replications/urls.go b/openstack/sdrs/v1/replications/urls.go new file mode 100644 index 000000000..a4a204cb5 --- /dev/null +++ b/openstack/sdrs/v1/replications/urls.go @@ -0,0 +1,11 @@ +package replications + +import "github.com/huaweicloud/golangsdk" + +func rootURL(c *golangsdk.ServiceClient) string { + return c.ServiceURL("replications") +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL("replications", id) +} diff --git a/service_client.go b/service_client.go index f030bd79e..9d18096d2 100644 --- a/service_client.go +++ b/service_client.go @@ -126,6 +126,15 @@ func (client *ServiceClient) DeleteWithResponse(url string, JSONResponse interfa return client.Request("DELETE", url, opts) } +// DeleteWithBodyResp calls `Request` with the "DELETE" HTTP verb. +func (client *ServiceClient) DeleteWithBodyResp(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { + if opts == nil { + opts = new(RequestOpts) + } + client.initReqOpts(url, JSONBody, JSONResponse, opts) + return client.Request("DELETE", url, opts) +} + func (client *ServiceClient) setMicroversionHeader(opts *RequestOpts) { switch client.Type { case "compute":