123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package policies
- import (
- "devel.mephi.ru/iacherepanov/openstack-gophercloud"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
- )
- const policyTypeMaxLength = 255
- // ListOptsBuilder allows extensions to add additional parameters to
- // the List request
- type ListOptsBuilder interface {
- ToPolicyListQuery() (string, error)
- }
- // ListOpts provides options to filter the List results.
- type ListOpts struct {
- // Type filters the response by MIME media type
- // of the serialized policy blob.
- Type string `q:"type"`
- }
- // ToPolicyListQuery formats a ListOpts into a query string.
- func (opts ListOpts) ToPolicyListQuery() (string, error) {
- q, err := gophercloud.BuildQueryString(opts)
- return q.String(), err
- }
- // List enumerates the policies to which the current token has access.
- func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
- url := listURL(client)
- if opts != nil {
- query, err := opts.ToPolicyListQuery()
- if err != nil {
- return pagination.Pager{Err: err}
- }
- url += query
- }
- return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
- return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
- })
- }
- // CreateOptsBuilder allows extensions to add additional parameters to
- // the Create request.
- type CreateOptsBuilder interface {
- ToPolicyCreateMap() (map[string]interface{}, error)
- }
- // CreateOpts provides options used to create a policy.
- type CreateOpts struct {
- // Type is the MIME media type of the serialized policy blob.
- Type string `json:"type" required:"true"`
- // Blob is the policy rule as a serialized blob.
- Blob []byte `json:"-" required:"true"`
- // Extra is free-form extra key/value pairs to describe the policy.
- Extra map[string]interface{} `json:"-"`
- }
- // ToPolicyCreateMap formats a CreateOpts into a create request.
- func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
- if len(opts.Type) > policyTypeMaxLength {
- return nil, StringFieldLengthExceedsLimit{
- Field: "type",
- Limit: policyTypeMaxLength,
- }
- }
- b, err := gophercloud.BuildRequestBody(opts, "policy")
- if err != nil {
- return nil, err
- }
- if v, ok := b["policy"].(map[string]interface{}); ok {
- v["blob"] = string(opts.Blob)
- if opts.Extra != nil {
- for key, value := range opts.Extra {
- v[key] = value
- }
- }
- }
- return b, nil
- }
- // Create creates a new Policy.
- func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
- b, err := opts.ToPolicyCreateMap()
- if err != nil {
- r.Err = err
- return
- }
- _, r.Err = client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
- OkCodes: []int{201},
- })
- return
- }
- // Get retrieves details on a single policy, by ID.
- func Get(client *gophercloud.ServiceClient, policyID string) (r GetResult) {
- _, r.Err = client.Get(getURL(client, policyID), &r.Body, nil)
- return
- }
- // UpdateOptsBuilder allows extensions to add additional parameters to
- // the Update request.
- type UpdateOptsBuilder interface {
- ToPolicyUpdateMap() (map[string]interface{}, error)
- }
- // UpdateOpts provides options for updating a policy.
- type UpdateOpts struct {
- // Type is the MIME media type of the serialized policy blob.
- Type string `json:"type,omitempty"`
- // Blob is the policy rule as a serialized blob.
- Blob []byte `json:"-"`
- // Extra is free-form extra key/value pairs to describe the policy.
- Extra map[string]interface{} `json:"-"`
- }
- // ToPolicyUpdateMap formats a UpdateOpts into an update request.
- func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
- if len(opts.Type) > policyTypeMaxLength {
- return nil, StringFieldLengthExceedsLimit{
- Field: "type",
- Limit: policyTypeMaxLength,
- }
- }
- b, err := gophercloud.BuildRequestBody(opts, "policy")
- if err != nil {
- return nil, err
- }
- if v, ok := b["policy"].(map[string]interface{}); ok {
- if len(opts.Blob) != 0 {
- v["blob"] = string(opts.Blob)
- }
- if opts.Extra != nil {
- for key, value := range opts.Extra {
- v[key] = value
- }
- }
- }
- return b, nil
- }
- // Update updates an existing Role.
- func Update(client *gophercloud.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
- b, err := opts.ToPolicyUpdateMap()
- if err != nil {
- r.Err = err
- return
- }
- _, r.Err = client.Patch(updateURL(client, policyID), &b, &r.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200},
- })
- return
- }
- // Delete deletes a policy.
- func Delete(client *gophercloud.ServiceClient, policyID string) (r DeleteResult) {
- _, r.Err = client.Delete(deleteURL(client, policyID), nil)
- return
- }
|