github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/workspace/v2/terminals/requests.go (about) 1 package terminals 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 // CreateOpts is the structure required by the Create method to batch bind the terminals and desktop. 9 type CreateOpts struct { 10 // The list of MAC binding VM policy information that needs to be added. 11 BindList []TerminalBindingInfo `json:"bind_list,omitempty"` 12 } 13 14 // TerminalBindingInfo is the structure that represents the configuation of terminal binding. 15 type TerminalBindingInfo struct { 16 // Line number, used for batch import. 17 Line int `json:"line,omitempty"` 18 // Terminal MAC address. 19 Mac string `json:"mac,omitempty"` 20 // Desktop name, used for batch import. 21 DesktopName string `json:"desktop_name,omitempty"` 22 // Description. 23 Description string `json:"description,omitempty"` 24 } 25 26 var requestOpts = golangsdk.RequestOpts{ 27 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 28 } 29 30 // Create is a method to batch create terminal binding using given parameters. 31 func Create(c *golangsdk.ServiceClient, opts CreateOpts) error { 32 b, err := golangsdk.BuildRequestBody(opts, "") 33 if err != nil { 34 return err 35 } 36 37 _, err = c.Post(rootURL(c), b, nil, &golangsdk.RequestOpts{ 38 MoreHeaders: requestOpts.MoreHeaders, 39 }) 40 return err 41 } 42 43 // ListOpts is the structure that used to query terminal binding list. 44 type ListOpts struct { 45 // Computer name. 46 ComputerName string `q:"computer_name"` 47 // MAC address. 48 Mac string `q:"mac"` 49 // The offset number. 50 // Valid range is 0-8000. 51 Offset *int `q:"offset" required:"true"` 52 // Number of records to be queried. 53 // Valid range is 0-8000. 54 Limit int `q:"limit" required:"true"` 55 // Whether query total count. 56 CountOnly bool `q:"count_only"` 57 } 58 59 // List is the method that used to query terminal binding list using given parameters. 60 func List(c *golangsdk.ServiceClient, opts ListOpts) ([]TerminalBindingResp, error) { 61 url := rootURL(c) 62 query, err := golangsdk.BuildQueryString(opts) 63 if err != nil { 64 return nil, err 65 } 66 url += query.String() 67 68 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 69 p := TerminalBindingPage{pagination.OffsetPageBase{PageResult: r}} 70 return p 71 }) 72 pager.Headers = requestOpts.MoreHeaders 73 pages, err := pager.AllPages() 74 75 if err != nil { 76 return nil, err 77 } 78 return ExtractTerminalBindings(pages) 79 } 80 81 // UpdateOpts is the structure that used to change the specified binding configuration. 82 type UpdateOpts struct { 83 // Bind ID. 84 ID string `json:"id" required:"true"` 85 // MAC address. 86 MAC *string `json:"mac" required:"true"` 87 // Desktop name. 88 DesktopName string `json:"desktop_name" required:"true"` 89 // Description. 90 Description string `json:"description,omitempty"` 91 } 92 93 // Update is a method used to change the specified binding configuration using givin parameters. 94 func Update(c *golangsdk.ServiceClient, userId string, opts UpdateOpts) error { 95 b, err := golangsdk.BuildRequestBody(opts, "") 96 if err != nil { 97 return err 98 } 99 100 _, err = c.Put(rootURL(c), b, nil, &golangsdk.RequestOpts{ 101 MoreHeaders: requestOpts.MoreHeaders, 102 }) 103 return err 104 } 105 106 // DeleteOpts is the structure required by the Update method to delete some binding configuration. 107 type DeleteOpts struct { 108 // ID list of bind configuration. 109 IDs []string `json:"id_list,omitempty"` 110 } 111 112 // Delete is a method to remove an existing user using given parameters. 113 func Delete(c *golangsdk.ServiceClient, opts DeleteOpts) ([]DeleteResult, error) { 114 b, err := golangsdk.BuildRequestBody(opts, "") 115 if err != nil { 116 return nil, err 117 } 118 119 var r deleteResp 120 _, err = c.Post(deleteURL(c), b, nil, &golangsdk.RequestOpts{ 121 MoreHeaders: requestOpts.MoreHeaders, 122 }) 123 return r.ResultList, err 124 } 125 126 // UpdateConfigOpts is the structure that represents the bind switch status. 127 type UpdateConfigOpts struct { 128 // Disable of enable for the bind switch. 129 TcBindSwitch string `json:"tc_bind_switch" required:"true"` 130 } 131 132 // UpdateConfig is the method that used to update the bind switch status. 133 func UpdateConfig(c *golangsdk.ServiceClient, opts UpdateConfigOpts) error { 134 b, err := golangsdk.BuildRequestBody(opts, "") 135 if err != nil { 136 return err 137 } 138 _, err = c.Post(configURL(c), b, nil, &golangsdk.RequestOpts{ 139 MoreHeaders: requestOpts.MoreHeaders, 140 }) 141 return err 142 } 143 144 // GetConfig is the method that used to query the current bind switch status. 145 func GetConfig(c *golangsdk.ServiceClient) (string, error) { 146 var r struct { 147 TcBindSwitch string `json:"tc_bind_switch"` 148 } 149 _, err := c.Get(configURL(c), &r, &golangsdk.RequestOpts{ 150 MoreHeaders: requestOpts.MoreHeaders, 151 }) 152 return r.TcBindSwitch, err 153 }