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