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