File: requests.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (160 lines) | stat: -rw-r--r-- 5,350 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package registeredlimits

import (
	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/pagination"
)

// ListOptsBuilder allows extensions to add additional parameters to
// the List request
type ListOptsBuilder interface {
	ToRegisteredLimitListQuery() (string, error)
}

// ListOpts provides options to filter the List results.
type ListOpts struct {
	// Filters the response by a region ID.
	RegionID string `q:"region_id"`

	// Filters the response by a service ID.
	ServiceID string `q:"service_id"`

	// Filters the response by a resource name.
	ResourceName string `q:"resource_name"`
}

// ToRegisteredLimitListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToRegisteredLimitListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	return q.String(), err
}

// List enumerates the registered limits.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
	url := rootURL(client)
	if opts != nil {
		query, err := opts.ToRegisteredLimitListQuery()
		if err != nil {
			return pagination.Pager{Err: err}
		}
		url += query
	}
	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
		return RegisteredLimitPage{pagination.LinkedPageBase{PageResult: r}}
	})
}

// BatchCreateOptsBuilder allows extensions to add additional parameters to
// the Create request.
type BatchCreateOptsBuilder interface {
	ToRegisteredLimitsCreateMap() (map[string]interface{}, error)
}

type CreateOpts struct {
	// RegionID is the ID of the region where the limit is applied.
	RegionID string `json:"region_id,omitempty"`

	// ServiceID is the ID of the service where the limit is applied.
	ServiceID string `json:"service_id" required:"true"`

	// Description of the limit.
	Description string `json:"description,omitempty"`

	// ResourceName is the name of the resource that the limit is applied to.
	ResourceName string `json:"resource_name" required:"true"`

	// DefaultLimit is the default limit.
	DefaultLimit int `json:"default_limit" required:"true"`
}

// BatchCreateOpts provides options used to create limits.
type BatchCreateOpts []CreateOpts

// ToRegisteredLimitsCreateMap formats a BatchCreateOpts into a create request.
func (opts BatchCreateOpts) ToRegisteredLimitsCreateMap() (map[string]interface{}, error) {
	registered_limits := make([]map[string]interface{}, len(opts))
	for i, registered_limit := range opts {
		registeredLimitMap, err := registered_limit.ToMap()
		if err != nil {
			return nil, err
		}
		registered_limits[i] = registeredLimitMap
	}
	return map[string]interface{}{"registered_limits": registered_limits}, nil
}

func (opts CreateOpts) ToMap() (map[string]interface{}, error) {
	return gophercloud.BuildRequestBody(opts, "")
}

// BatchCreate creates new Limits.
func BatchCreate(client *gophercloud.ServiceClient, opts BatchCreateOptsBuilder) (r CreateResult) {
	b, err := opts.ToRegisteredLimitsCreateMap()
	if err != nil {
		r.Err = err
		return
	}
	resp, err := client.Post(rootURL(client), &b, &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{201},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// Get retrieves details on a single registered_limit, by ID.
func Get(client *gophercloud.ServiceClient, registeredLimitID string) (r GetResult) {
	resp, err := client.Get(resourceURL(client, registeredLimitID), &r.Body, nil)
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// UpdateOptsBuilder allows extensions to add additional parameters to
// the Update request.
type UpdateOptsBuilder interface {
	ToRegisteredLimitUpdateMap() (map[string]interface{}, error)
}

// UpdateOpts represents parameters to update a domain.
type UpdateOpts struct {
	// Description of the registered_limit.
	Description *string `json:"description,omitempty"`

	// DefaultLimit is the override limit.
	DefaultLimit *int `json:"default_limit,omitempty"`

	// RegionID is the ID of the region where the limit is applied.
	RegionID string `json:"region_id,omitempty"`

	// ServiceID is the ID of the service where the limit is applied.
	ServiceID string `json:"service_id,omitempty"`

	// ResourceName is the name of the resource that the limit is applied to.
	ResourceName string `json:"resource_name,omitempty"`
	//Either service_id, resource_name, or region_id must be different than existing value otherwise it will raise 409.
}

// ToRegisteredLimitUpdateMap formats UpdateOpts into an update request.
func (opts UpdateOpts) ToRegisteredLimitUpdateMap() (map[string]interface{}, error) {
	return gophercloud.BuildRequestBody(opts, "registered_limit")
}

// Update modifies the attributes of a registered limit.
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
	b, err := opts.ToRegisteredLimitUpdateMap()
	if err != nil {
		r.Err = err
		return
	}
	resp, err := client.Patch(resourceURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// Delete deletes a registered_limit.
func Delete(client *gophercloud.ServiceClient, registeredLimitID string) (r DeleteResult) {
	resp, err := client.Delete(resourceURL(client, registeredLimitID), nil)
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}