github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/policies/requests.go (about)

     1  package policies
     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 the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToPolicyListQuery() (string, error)
    12  }
    13  
    14  // ListOpts represents options used to list policies.
    15  type ListOpts struct {
    16  	// Limit limits the number of Policies to return.
    17  	Limit int `q:"limit"`
    18  
    19  	// Marker and Limit control paging. Marker instructs List where to start
    20  	// listing from.
    21  	Marker string `q:"marker"`
    22  
    23  	// Sorts the response by one or more attribute and optional sort direction
    24  	// combinations.
    25  	Sort string `q:"sort"`
    26  
    27  	// GlobalProject indicates whether to include resources for all projects or
    28  	// resources for the current project.
    29  	GlobalProject *bool `q:"global_project"`
    30  
    31  	// Name to filter the response by the specified name property of the object.
    32  	Name string `q:"name"`
    33  
    34  	// Filter the response by the specified type property of the object.
    35  	Type string `q:"type"`
    36  }
    37  
    38  // ToPolicyListQuery formats a ListOpts into a query string.
    39  func (opts ListOpts) ToPolicyListQuery() (string, error) {
    40  	q, err := gophercloud.BuildQueryString(opts)
    41  	return q.String(), err
    42  }
    43  
    44  // List instructs OpenStack to retrieve a list of policies.
    45  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    46  	url := policyListURL(client)
    47  	if opts != nil {
    48  		query, err := opts.ToPolicyListQuery()
    49  		if err != nil {
    50  			return pagination.Pager{Err: err}
    51  		}
    52  		url += query
    53  	}
    54  
    55  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    56  		p := PolicyPage{pagination.MarkerPageBase{PageResult: r}}
    57  		p.MarkerPageBase.Owner = p
    58  		return p
    59  	})
    60  }
    61  
    62  // CreateOptsBuilder allows extensions to add additional parameters to the
    63  // Create request.
    64  type CreateOptsBuilder interface {
    65  	ToPolicyCreateMap() (map[string]interface{}, error)
    66  }
    67  
    68  // CreateOpts represents options used to create a policy.
    69  type CreateOpts struct {
    70  	Name string `json:"name"`
    71  	Spec Spec   `json:"spec"`
    72  }
    73  
    74  // ToPolicyCreateMap constructs a request body from CreateOpts.
    75  func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
    76  	b, err := gophercloud.BuildRequestBody(opts, "")
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	return map[string]interface{}{"policy": b}, nil
    82  }
    83  
    84  // Create makes a request against the API to create a policy
    85  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    86  	b, err := opts.ToPolicyCreateMap()
    87  	if err != nil {
    88  		r.Err = err
    89  		return
    90  	}
    91  	resp, err := client.Post(policyCreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
    92  		OkCodes: []int{201},
    93  	})
    94  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    95  	return
    96  }
    97  
    98  // Delete makes a request against the API to delete a policy.
    99  func Delete(client *gophercloud.ServiceClient, policyID string) (r DeleteResult) {
   100  	resp, err := client.Delete(policyDeleteURL(client, policyID), &gophercloud.RequestOpts{
   101  		OkCodes: []int{204},
   102  	})
   103  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   104  	return
   105  }
   106  
   107  // UpdateOptsBuilder allows extensions to add additional parameters to the
   108  // Update request.
   109  type UpdateOptsBuilder interface {
   110  	ToPolicyUpdateMap() (map[string]interface{}, error)
   111  }
   112  
   113  // UpdateOpts represents options to update a policy.
   114  type UpdateOpts struct {
   115  	Name string `json:"name,omitempty"`
   116  }
   117  
   118  // ToPolicyUpdateMap constructs a request body from UpdateOpts.
   119  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
   120  	return gophercloud.BuildRequestBody(opts, "policy")
   121  }
   122  
   123  // Update updates a specified policy.
   124  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   125  	b, err := opts.ToPolicyUpdateMap()
   126  	if err != nil {
   127  		r.Err = err
   128  		return
   129  	}
   130  	resp, err := client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   131  		OkCodes: []int{200},
   132  	})
   133  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   134  	return
   135  }
   136  
   137  // ValidateOptsBuilder allows extensions to add additional parameters to the
   138  // Validate request.
   139  type ValidateOptsBuilder interface {
   140  	ToPolicyValidateMap() (map[string]interface{}, error)
   141  }
   142  
   143  // ValidateOpts represents options used to validate a policy.
   144  type ValidateOpts struct {
   145  	Spec Spec `json:"spec"`
   146  }
   147  
   148  // ToPolicyValidateMap formats a CreateOpts into a body map.
   149  func (opts ValidateOpts) ToPolicyValidateMap() (map[string]interface{}, error) {
   150  	return gophercloud.BuildRequestBody(opts, "policy")
   151  }
   152  
   153  // Validate policy will validate a specified policy.
   154  func Validate(client *gophercloud.ServiceClient, opts ValidateOptsBuilder) (r ValidateResult) {
   155  	b, err := opts.ToPolicyValidateMap()
   156  	if err != nil {
   157  		r.Err = err
   158  		return
   159  	}
   160  	resp, err := client.Post(validateURL(client), b, &r.Body, &gophercloud.RequestOpts{
   161  		OkCodes: []int{200, 201},
   162  	})
   163  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   164  	return
   165  }
   166  
   167  // Get makes a request against the API to get details for a policy.
   168  func Get(client *gophercloud.ServiceClient, policyTypeName string) (r GetResult) {
   169  	url := policyGetURL(client, policyTypeName)
   170  	resp, err := client.Get(url, &r.Body, &gophercloud.RequestOpts{
   171  		OkCodes: []int{200},
   172  	})
   173  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   174  	return
   175  }