github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/sdrs/v1/protectedinstances/requests.go (about) 1 package protectedinstances 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 ) 6 7 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 8 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 9 } 10 11 // CreateOptsBuilder allows extensions to add additional parameters to the 12 // Create request. 13 type CreateOptsBuilder interface { 14 ToInstanceCreateMap() (map[string]interface{}, error) 15 } 16 17 // CreateOpts contains all the values needed to create a new instance. 18 type CreateOpts struct { 19 //Group ID 20 GroupID string `json:"server_group_id" required:"true"` 21 //Server ID 22 ServerID string `json:"server_id" required:"true"` 23 //Instance Name 24 Name string `json:"name" required:"true"` 25 //Instance Description 26 Description string `json:"description,omitempty"` 27 //Cluster ID 28 ClusterID string `json:"cluster_id,omitempty"` 29 //Subnet ID 30 SubnetID string `json:"primary_subnet_id,omitempty"` 31 //IP Address 32 IpAddress string `json:"primary_ip_address,omitempty"` 33 } 34 35 // ToInstanceCreateMap builds a create request body from CreateOpts. 36 func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { 37 return golangsdk.BuildRequestBody(opts, "protected_instance") 38 } 39 40 // Create will create a new Instance based on the values in CreateOpts. 41 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 42 b, err := opts.ToInstanceCreateMap() 43 if err != nil { 44 r.Err = err 45 return 46 } 47 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 48 _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) 49 return 50 } 51 52 // UpdateOptsBuilder allows extensions to add additional parameters to the 53 // Update request. 54 type UpdateOptsBuilder interface { 55 ToInstanceUpdateMap() (map[string]interface{}, error) 56 } 57 58 // UpdateOpts contains all the values needed to update an Instance. 59 type UpdateOpts struct { 60 //Instance name 61 Name string `json:"name" required:"true"` 62 } 63 64 // ToInstanceUpdateMap builds a update request body from UpdateOpts. 65 func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) { 66 return golangsdk.BuildRequestBody(opts, "protected_instance") 67 } 68 69 // Update accepts a UpdateOpts struct and uses the values to update an Instance.The response code from api is 200 70 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 71 b, err := opts.ToInstanceUpdateMap() 72 if err != nil { 73 r.Err = err 74 return 75 } 76 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 77 _, r.Err = c.Put(resourceURL(c, id), b, nil, reqOpt) 78 return 79 } 80 81 // Get retrieves a particular Instance based on its unique ID. 82 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 83 _, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{ 84 MoreHeaders: RequestOpts.MoreHeaders, 85 }) 86 return 87 } 88 89 // DeleteOptsBuilder allows extensions to add additional parameters to the 90 // Delete request. 91 type DeleteOptsBuilder interface { 92 ToInstanceDeleteMap() (map[string]interface{}, error) 93 } 94 95 // DeleteOpts contains all the values needed to delete an Instance. 96 type DeleteOpts struct { 97 //Delete Target Server 98 DeleteTargetServer bool `json:"delete_target_server,omitempty"` 99 //Delete Target Eip 100 DeleteTargetEip bool `json:"delete_target_eip,omitempty"` 101 } 102 103 // ToInstanceDeleteMap builds a update request body from DeleteOpts. 104 func (opts DeleteOpts) ToInstanceDeleteMap() (map[string]interface{}, error) { 105 return golangsdk.BuildRequestBody(opts, "") 106 } 107 108 // Delete will permanently delete a particular Instance based on its unique ID. 109 func Delete(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r JobResult) { 110 b, err := opts.ToInstanceDeleteMap() 111 if err != nil { 112 r.Err = err 113 return 114 } 115 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 116 _, r.Err = c.DeleteWithBodyResp(resourceURL(c, id), b, &r.Body, reqOpt) 117 return 118 }