github.com/gophercloud/gophercloud@v1.11.0/openstack/placement/v1/resourceproviders/requests.go (about) 1 package resourceproviders 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 ToResourceProviderListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering resource providers. Filtering is achieved by 15 // passing in struct field values that map to the resource provider attributes 16 // you want to see returned. 17 type ListOpts struct { 18 // Name is the name of the resource provider to filter the list 19 Name string `q:"name"` 20 21 // UUID is the uuid of the resource provider to filter the list 22 UUID string `q:"uuid"` 23 24 // MemberOf is a string representing aggregate uuids to filter or exclude from the list 25 MemberOf string `q:"member_of"` 26 27 // Resources is a comma-separated list of string indicating an amount of resource 28 // of a specified class that a provider must have the capacity and availability to serve 29 Resources string `q:"resources"` 30 31 // InTree is a string that represents a resource provider UUID. The returned resource 32 // providers will be in the same provider tree as the specified provider. 33 InTree string `q:"in_tree"` 34 35 // Required is comma-delimited list of string trait names. 36 Required string `q:"required"` 37 } 38 39 // ToResourceProviderListQuery formats a ListOpts into a query string. 40 func (opts ListOpts) ToResourceProviderListQuery() (string, error) { 41 q, err := gophercloud.BuildQueryString(opts) 42 return q.String(), err 43 } 44 45 // List makes a request against the API to list resource providers. 46 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 47 url := resourceProvidersListURL(client) 48 49 if opts != nil { 50 query, err := opts.ToResourceProviderListQuery() 51 if err != nil { 52 return pagination.Pager{Err: err} 53 } 54 url += query 55 } 56 57 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 58 return ResourceProvidersPage{pagination.SinglePageBase(r)} 59 }) 60 } 61 62 // CreateOptsBuilder allows extensions to add additional parameters to the 63 // Create request. 64 type CreateOptsBuilder interface { 65 ToResourceProviderCreateMap() (map[string]interface{}, error) 66 } 67 68 // CreateOpts represents options used to create a resource provider. 69 type CreateOpts struct { 70 Name string `json:"name"` 71 UUID string `json:"uuid,omitempty"` 72 // The UUID of the immediate parent of the resource provider. 73 // Available in version >= 1.14 74 ParentProviderUUID string `json:"parent_provider_uuid,omitempty"` 75 } 76 77 // ToResourceProviderCreateMap constructs a request body from CreateOpts. 78 func (opts CreateOpts) ToResourceProviderCreateMap() (map[string]interface{}, error) { 79 b, err := gophercloud.BuildRequestBody(opts, "") 80 if err != nil { 81 return nil, err 82 } 83 84 return b, nil 85 } 86 87 // Create makes a request against the API to create a resource provider 88 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 89 b, err := opts.ToResourceProviderCreateMap() 90 if err != nil { 91 r.Err = err 92 return 93 } 94 95 resp, err := client.Post(resourceProvidersListURL(client), b, &r.Body, &gophercloud.RequestOpts{ 96 OkCodes: []int{200}, 97 }) 98 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 99 return 100 } 101 102 // Delete accepts a unique ID and deletes the resource provider associated with it. 103 func Delete(c *gophercloud.ServiceClient, resourceProviderID string) (r DeleteResult) { 104 resp, err := c.Delete(deleteURL(c, resourceProviderID), nil) 105 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 106 return 107 } 108 109 // Get retrieves a specific resource provider based on its unique ID. 110 func Get(c *gophercloud.ServiceClient, resourceProviderID string) (r GetResult) { 111 resp, err := c.Get(getURL(c, resourceProviderID), &r.Body, nil) 112 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 113 return 114 } 115 116 // UpdateOptsBuilder allows extensions to add additional parameters to the 117 // Update request. 118 type UpdateOptsBuilder interface { 119 ToResourceProviderUpdateMap() (map[string]interface{}, error) 120 } 121 122 // UpdateOpts represents options used to update a resource provider. 123 type UpdateOpts struct { 124 Name *string `json:"name,omitempty"` 125 // Available in version >= 1.37. It can be set to any existing provider UUID 126 // except to providers that would cause a loop. Also it can be set to null 127 // to transform the provider to a new root provider. This operation needs to 128 // be used carefully. Moving providers can mean that the original rules used 129 // to create the existing resource allocations may be invalidated by that move. 130 ParentProviderUUID *string `json:"parent_provider_uuid,omitempty"` 131 } 132 133 // ToResourceProviderUpdateMap constructs a request body from UpdateOpts. 134 func (opts UpdateOpts) ToResourceProviderUpdateMap() (map[string]interface{}, error) { 135 return gophercloud.BuildRequestBody(opts, "") 136 } 137 138 // Update makes a request against the API to create a resource provider 139 func Update(client *gophercloud.ServiceClient, resourceProviderID string, opts UpdateOptsBuilder) (r UpdateResult) { 140 b, err := opts.ToResourceProviderUpdateMap() 141 if err != nil { 142 r.Err = err 143 return 144 } 145 146 resp, err := client.Put(updateURL(client, resourceProviderID), b, &r.Body, &gophercloud.RequestOpts{ 147 OkCodes: []int{200}, 148 }) 149 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 150 return 151 } 152 153 func GetUsages(client *gophercloud.ServiceClient, resourceProviderID string) (r GetUsagesResult) { 154 resp, err := client.Get(getResourceProviderUsagesURL(client, resourceProviderID), &r.Body, nil) 155 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 156 return 157 } 158 159 func GetInventories(client *gophercloud.ServiceClient, resourceProviderID string) (r GetInventoriesResult) { 160 resp, err := client.Get(getResourceProviderInventoriesURL(client, resourceProviderID), &r.Body, nil) 161 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 162 return 163 } 164 165 func GetAllocations(client *gophercloud.ServiceClient, resourceProviderID string) (r GetAllocationsResult) { 166 resp, err := client.Get(getResourceProviderAllocationsURL(client, resourceProviderID), &r.Body, nil) 167 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 168 return 169 } 170 171 func GetTraits(client *gophercloud.ServiceClient, resourceProviderID string) (r GetTraitsResult) { 172 resp, err := client.Get(getResourceProviderTraitsURL(client, resourceProviderID), &r.Body, nil) 173 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 174 return 175 }