diff --git a/openstack/sdrs/v1/protectedinstances/requests.go b/openstack/sdrs/v1/protectedinstances/requests.go new file mode 100644 index 000000000..d45a5ed05 --- /dev/null +++ b/openstack/sdrs/v1/protectedinstances/requests.go @@ -0,0 +1,116 @@ +package protectedinstances + +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 { + ToInstanceCreateMap() (map[string]interface{}, error) +} + +// CreateOpts contains all the values needed to create a new instance. +type CreateOpts struct { + //Group ID + GroupID string `json:"server_group_id" required:"true"` + //Server ID + ServerID string `json:"server_id" required:"true"` + //Instance Name + Name string `json:"name" required:"true"` + //Instance Description + Description string `json:"description,omitempty"` + //Subnet ID + SubnetID string `json:"primary_subnet_id,omitempty"` + //IP Address + IpAddress string `json:"primary_ip_address,omitempty"` + //Flavor ID + FlavorRef string `json:"flavorRef,omitempty"` +} + +// ToInstanceCreateMap builds a create request body from CreateOpts. +func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "protected_instance") +} + +// Create will create a new Instance based on the values in CreateOpts. +func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { + b, err := opts.ToInstanceCreateMap() + 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 { + ToInstanceUpdateMap() (map[string]interface{}, error) +} + +// UpdateOpts contains all the values needed to update an Instance. +type UpdateOpts struct { + //Instance name + Name string `json:"name" required:"true"` +} + +// ToInstanceUpdateMap builds a update request body from UpdateOpts. +func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "protected_instance") +} + +// Update accepts a UpdateOpts struct and uses the values to update an Instance.The response code from api is 200 +func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { + b, err := opts.ToInstanceUpdateMap() + 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 Instance 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 { + ToInstanceDeleteMap() (map[string]interface{}, error) +} + +// DeleteOpts contains all the values needed to delete an Instance. +type DeleteOpts struct { + //Delete Target Server + DeleteTargetServer bool `json:"delete_target_server,omitempty"` + //Delete Target Eip + DeleteTargetEip bool `json:"delete_target_eip,omitempty"` +} + +// ToInstanceDeleteMap builds a update request body from DeleteOpts. +func (opts DeleteOpts) ToInstanceDeleteMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +// Delete will permanently delete a particular Instance based on its unique ID. +func Delete(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r JobResult) { + b, err := opts.ToInstanceDeleteMap() + 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/protectedinstances/results.go b/openstack/sdrs/v1/protectedinstances/results.go new file mode 100644 index 000000000..88f757762 --- /dev/null +++ b/openstack/sdrs/v1/protectedinstances/results.go @@ -0,0 +1,60 @@ +package protectedinstances + +import ( + "github.com/huaweicloud/golangsdk" +) + +type Instance struct { + //Instance ID + Id string `json:"id"` + //Instance Name + Name string `json:"name"` + //Instance Description + Description string `json:"description"` + //Protection Group ID + GroupID string `json:"server_group_id"` + //Instance Status + Status string `json:"status"` + //Source Server + SourceServer string `json:"source_server"` + //Target Server + TargetServer string `json:"target_server"` + //Priority Station + PriorityStation string `json:"priority_station"` + //Attachment + Attachment []Attachment `json:"attachment"` +} + +type Attachment struct { + //Replication ID + Replication string `json:"replication"` + //Device Name + Device string `json:"device"` +} + +type commonResult struct { + golangsdk.Result +} + +// Extract is a function that accepts a result and extracts a instance. +func (r commonResult) Extract() (*Instance, error) { + var response Instance + err := r.ExtractInto(&response) + return &response, err +} + +func (r commonResult) ExtractInto(v interface{}) error { + return r.Result.ExtractIntoStructPtr(v, "protected_instance") +} + +// UpdateResult represents the result of a update operation. Call its Extract +// method to interpret it as a Instance. +type UpdateResult struct { + commonResult +} + +// GetResult represents the result of a get operation. Call its Extract +// method to interpret it as a Instance. +type GetResult struct { + commonResult +} diff --git a/openstack/sdrs/v1/protectedinstances/results_job.go b/openstack/sdrs/v1/protectedinstances/results_job.go new file mode 100644 index 000000000..77e5a7301 --- /dev/null +++ b/openstack/sdrs/v1/protectedinstances/results_job.go @@ -0,0 +1,80 @@ +package protectedinstances + +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/protectedinstances/urls.go b/openstack/sdrs/v1/protectedinstances/urls.go new file mode 100644 index 000000000..3bbc46fd6 --- /dev/null +++ b/openstack/sdrs/v1/protectedinstances/urls.go @@ -0,0 +1,11 @@ +package protectedinstances + +import "github.com/huaweicloud/golangsdk" + +func rootURL(c *golangsdk.ServiceClient) string { + return c.ServiceURL("protected-instances") +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL("protected-instances", id) +}