github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/tenants/requests.go (about) 1 package tenants 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ListOpts filters the Tenants that are returned by the List call. 9 type ListOpts struct { 10 // Marker is the ID of the last Tenant on the previous page. 11 Marker string `q:"marker"` 12 13 // Limit specifies the page size. 14 Limit int `q:"limit"` 15 } 16 17 // List enumerates the Tenants to which the current token has access. 18 func List(client *golangsdk.ServiceClient, opts *ListOpts) pagination.Pager { 19 url := listURL(client) 20 if opts != nil { 21 q, err := golangsdk.BuildQueryString(opts) 22 if err != nil { 23 return pagination.Pager{Err: err} 24 } 25 url += q.String() 26 } 27 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 28 return TenantPage{pagination.LinkedPageBase{PageResult: r}} 29 }) 30 } 31 32 // CreateOpts represents the options needed when creating new tenant. 33 type CreateOpts struct { 34 // Name is the name of the tenant. 35 Name string `json:"name" required:"true"` 36 37 // Description is the description of the tenant. 38 Description string `json:"description,omitempty"` 39 40 // Enabled sets the tenant status to enabled or disabled. 41 Enabled *bool `json:"enabled,omitempty"` 42 } 43 44 // CreateOptsBuilder enables extensions to add additional parameters to the 45 // Create request. 46 type CreateOptsBuilder interface { 47 ToTenantCreateMap() (map[string]interface{}, error) 48 } 49 50 // ToTenantCreateMap assembles a request body based on the contents of 51 // a CreateOpts. 52 func (opts CreateOpts) ToTenantCreateMap() (map[string]interface{}, error) { 53 return golangsdk.BuildRequestBody(opts, "tenant") 54 } 55 56 // Create is the operation responsible for creating new tenant. 57 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 58 b, err := opts.ToTenantCreateMap() 59 if err != nil { 60 r.Err = err 61 return 62 } 63 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 64 OkCodes: []int{200, 201}, 65 }) 66 return 67 } 68 69 // Get requests details on a single tenant by ID. 70 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 71 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 72 return 73 } 74 75 // UpdateOptsBuilder allows extensions to add additional parameters to the 76 // Update request. 77 type UpdateOptsBuilder interface { 78 ToTenantUpdateMap() (map[string]interface{}, error) 79 } 80 81 // UpdateOpts specifies the base attributes that may be updated on an existing 82 // tenant. 83 type UpdateOpts struct { 84 // Name is the name of the tenant. 85 Name string `json:"name,omitempty"` 86 87 // Description is the description of the tenant. 88 Description string `json:"description,omitempty"` 89 90 // Enabled sets the tenant status to enabled or disabled. 91 Enabled *bool `json:"enabled,omitempty"` 92 } 93 94 // ToTenantUpdateMap formats an UpdateOpts structure into a request body. 95 func (opts UpdateOpts) ToTenantUpdateMap() (map[string]interface{}, error) { 96 return golangsdk.BuildRequestBody(opts, "tenant") 97 } 98 99 // Update is the operation responsible for updating exist tenants by their TenantID. 100 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 101 b, err := opts.ToTenantUpdateMap() 102 if err != nil { 103 r.Err = err 104 return 105 } 106 _, r.Err = client.Put(updateURL(client, id), &b, &r.Body, &golangsdk.RequestOpts{ 107 OkCodes: []int{200}, 108 }) 109 return 110 } 111 112 // Delete is the operation responsible for permanently deleting a tenant. 113 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 114 _, r.Err = client.Delete(deleteURL(client, id), nil) 115 return 116 }