requests.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package domains
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  5. )
  6. // ListOptsBuilder allows extensions to add additional parameters to
  7. // the List request
  8. type ListOptsBuilder interface {
  9. ToDomainListQuery() (string, error)
  10. }
  11. // ListOpts provides options to filter the List results.
  12. type ListOpts struct {
  13. // Enabled filters the response by enabled domains.
  14. Enabled *bool `q:"enabled"`
  15. // Name filters the response by domain name.
  16. Name string `q:"name"`
  17. }
  18. // ToDomainListQuery formats a ListOpts into a query string.
  19. func (opts ListOpts) ToDomainListQuery() (string, error) {
  20. q, err := gophercloud.BuildQueryString(opts)
  21. return q.String(), err
  22. }
  23. // List enumerates the domains to which the current token has access.
  24. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  25. url := listURL(client)
  26. if opts != nil {
  27. query, err := opts.ToDomainListQuery()
  28. if err != nil {
  29. return pagination.Pager{Err: err}
  30. }
  31. url += query
  32. }
  33. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  34. return DomainPage{pagination.LinkedPageBase{PageResult: r}}
  35. })
  36. }
  37. // Get retrieves details on a single domain, by ID.
  38. func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
  39. _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
  40. return
  41. }
  42. // CreateOptsBuilder allows extensions to add additional parameters to
  43. // the Create request.
  44. type CreateOptsBuilder interface {
  45. ToDomainCreateMap() (map[string]interface{}, error)
  46. }
  47. // CreateOpts provides options used to create a domain.
  48. type CreateOpts struct {
  49. // Name is the name of the new domain.
  50. Name string `json:"name" required:"true"`
  51. // Description is a description of the domain.
  52. Description string `json:"description,omitempty"`
  53. // Enabled sets the domain status to enabled or disabled.
  54. Enabled *bool `json:"enabled,omitempty"`
  55. }
  56. // ToDomainCreateMap formats a CreateOpts into a create request.
  57. func (opts CreateOpts) ToDomainCreateMap() (map[string]interface{}, error) {
  58. return gophercloud.BuildRequestBody(opts, "domain")
  59. }
  60. // Create creates a new Domain.
  61. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  62. b, err := opts.ToDomainCreateMap()
  63. if err != nil {
  64. r.Err = err
  65. return
  66. }
  67. _, r.Err = client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
  68. OkCodes: []int{201},
  69. })
  70. return
  71. }
  72. // Delete deletes a domain.
  73. func Delete(client *gophercloud.ServiceClient, domainID string) (r DeleteResult) {
  74. _, r.Err = client.Delete(deleteURL(client, domainID), nil)
  75. return
  76. }
  77. // UpdateOptsBuilder allows extensions to add additional parameters to
  78. // the Update request.
  79. type UpdateOptsBuilder interface {
  80. ToDomainUpdateMap() (map[string]interface{}, error)
  81. }
  82. // UpdateOpts represents parameters to update a domain.
  83. type UpdateOpts struct {
  84. // Name is the name of the domain.
  85. Name string `json:"name,omitempty"`
  86. // Description is the description of the domain.
  87. Description string `json:"description,omitempty"`
  88. // Enabled sets the domain status to enabled or disabled.
  89. Enabled *bool `json:"enabled,omitempty"`
  90. }
  91. // ToUpdateCreateMap formats a UpdateOpts into an update request.
  92. func (opts UpdateOpts) ToDomainUpdateMap() (map[string]interface{}, error) {
  93. return gophercloud.BuildRequestBody(opts, "domain")
  94. }
  95. // Update modifies the attributes of a domain.
  96. func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
  97. b, err := opts.ToDomainUpdateMap()
  98. if err != nil {
  99. r.Err = err
  100. return
  101. }
  102. _, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
  103. OkCodes: []int{200},
  104. })
  105. return
  106. }