github.com/openshift-online/ocm-sdk-go@v0.1.473/accountsmgmt/v1/organizations_client.go (about)

     1  /*
     2  Copyright (c) 2020 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all
    18  // your changes will be lost when the file is generated again.
    19  
    20  package v1 // github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1
    21  
    22  import (
    23  	"bufio"
    24  	"bytes"
    25  	"context"
    26  	"io"
    27  	"net/http"
    28  	"net/url"
    29  	"path"
    30  
    31  	"github.com/openshift-online/ocm-sdk-go/errors"
    32  	"github.com/openshift-online/ocm-sdk-go/helpers"
    33  )
    34  
    35  // OrganizationsClient is the client of the 'organizations' resource.
    36  //
    37  // Manages the collection of organizations.
    38  type OrganizationsClient struct {
    39  	transport http.RoundTripper
    40  	path      string
    41  }
    42  
    43  // NewOrganizationsClient creates a new client for the 'organizations'
    44  // resource using the given transport to send the requests and receive the
    45  // responses.
    46  func NewOrganizationsClient(transport http.RoundTripper, path string) *OrganizationsClient {
    47  	return &OrganizationsClient{
    48  		transport: transport,
    49  		path:      path,
    50  	}
    51  }
    52  
    53  // Add creates a request for the 'add' method.
    54  //
    55  // Creates a new organization.
    56  func (c *OrganizationsClient) Add() *OrganizationsAddRequest {
    57  	return &OrganizationsAddRequest{
    58  		transport: c.transport,
    59  		path:      c.path,
    60  	}
    61  }
    62  
    63  // List creates a request for the 'list' method.
    64  //
    65  // Retrieves a list of organizations.
    66  func (c *OrganizationsClient) List() *OrganizationsListRequest {
    67  	return &OrganizationsListRequest{
    68  		transport: c.transport,
    69  		path:      c.path,
    70  	}
    71  }
    72  
    73  // Organization returns the target 'organization' resource for the given identifier.
    74  //
    75  // Reference to the service that manages a specific organization.
    76  func (c *OrganizationsClient) Organization(id string) *OrganizationClient {
    77  	return NewOrganizationClient(
    78  		c.transport,
    79  		path.Join(c.path, id),
    80  	)
    81  }
    82  
    83  // OrganizationsAddRequest is the request for the 'add' method.
    84  type OrganizationsAddRequest struct {
    85  	transport http.RoundTripper
    86  	path      string
    87  	query     url.Values
    88  	header    http.Header
    89  	body      *Organization
    90  }
    91  
    92  // Parameter adds a query parameter.
    93  func (r *OrganizationsAddRequest) Parameter(name string, value interface{}) *OrganizationsAddRequest {
    94  	helpers.AddValue(&r.query, name, value)
    95  	return r
    96  }
    97  
    98  // Header adds a request header.
    99  func (r *OrganizationsAddRequest) Header(name string, value interface{}) *OrganizationsAddRequest {
   100  	helpers.AddHeader(&r.header, name, value)
   101  	return r
   102  }
   103  
   104  // Impersonate wraps requests on behalf of another user.
   105  // Note: Services that do not support this feature may silently ignore this call.
   106  func (r *OrganizationsAddRequest) Impersonate(user string) *OrganizationsAddRequest {
   107  	helpers.AddImpersonationHeader(&r.header, user)
   108  	return r
   109  }
   110  
   111  // Body sets the value of the 'body' parameter.
   112  //
   113  // Organization data.
   114  func (r *OrganizationsAddRequest) Body(value *Organization) *OrganizationsAddRequest {
   115  	r.body = value
   116  	return r
   117  }
   118  
   119  // Send sends this request, waits for the response, and returns it.
   120  //
   121  // This is a potentially lengthy operation, as it requires network communication.
   122  // Consider using a context and the SendContext method.
   123  func (r *OrganizationsAddRequest) Send() (result *OrganizationsAddResponse, err error) {
   124  	return r.SendContext(context.Background())
   125  }
   126  
   127  // SendContext sends this request, waits for the response, and returns it.
   128  func (r *OrganizationsAddRequest) SendContext(ctx context.Context) (result *OrganizationsAddResponse, err error) {
   129  	query := helpers.CopyQuery(r.query)
   130  	header := helpers.CopyHeader(r.header)
   131  	buffer := &bytes.Buffer{}
   132  	err = writeOrganizationsAddRequest(r, buffer)
   133  	if err != nil {
   134  		return
   135  	}
   136  	uri := &url.URL{
   137  		Path:     r.path,
   138  		RawQuery: query.Encode(),
   139  	}
   140  	request := &http.Request{
   141  		Method: "POST",
   142  		URL:    uri,
   143  		Header: header,
   144  		Body:   io.NopCloser(buffer),
   145  	}
   146  	if ctx != nil {
   147  		request = request.WithContext(ctx)
   148  	}
   149  	response, err := r.transport.RoundTrip(request)
   150  	if err != nil {
   151  		return
   152  	}
   153  	defer response.Body.Close()
   154  	result = &OrganizationsAddResponse{}
   155  	result.status = response.StatusCode
   156  	result.header = response.Header
   157  	reader := bufio.NewReader(response.Body)
   158  	_, err = reader.Peek(1)
   159  	if err == io.EOF {
   160  		err = nil
   161  		return
   162  	}
   163  	if result.status >= 400 {
   164  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   165  		if err != nil {
   166  			return
   167  		}
   168  		err = result.err
   169  		return
   170  	}
   171  	err = readOrganizationsAddResponse(result, reader)
   172  	if err != nil {
   173  		return
   174  	}
   175  	return
   176  }
   177  
   178  // OrganizationsAddResponse is the response for the 'add' method.
   179  type OrganizationsAddResponse struct {
   180  	status int
   181  	header http.Header
   182  	err    *errors.Error
   183  	body   *Organization
   184  }
   185  
   186  // Status returns the response status code.
   187  func (r *OrganizationsAddResponse) Status() int {
   188  	if r == nil {
   189  		return 0
   190  	}
   191  	return r.status
   192  }
   193  
   194  // Header returns header of the response.
   195  func (r *OrganizationsAddResponse) Header() http.Header {
   196  	if r == nil {
   197  		return nil
   198  	}
   199  	return r.header
   200  }
   201  
   202  // Error returns the response error.
   203  func (r *OrganizationsAddResponse) Error() *errors.Error {
   204  	if r == nil {
   205  		return nil
   206  	}
   207  	return r.err
   208  }
   209  
   210  // Body returns the value of the 'body' parameter.
   211  //
   212  // Organization data.
   213  func (r *OrganizationsAddResponse) Body() *Organization {
   214  	if r == nil {
   215  		return nil
   216  	}
   217  	return r.body
   218  }
   219  
   220  // GetBody returns the value of the 'body' parameter and
   221  // a flag indicating if the parameter has a value.
   222  //
   223  // Organization data.
   224  func (r *OrganizationsAddResponse) GetBody() (value *Organization, ok bool) {
   225  	ok = r != nil && r.body != nil
   226  	if ok {
   227  		value = r.body
   228  	}
   229  	return
   230  }
   231  
   232  // OrganizationsListRequest is the request for the 'list' method.
   233  type OrganizationsListRequest struct {
   234  	transport   http.RoundTripper
   235  	path        string
   236  	query       url.Values
   237  	header      http.Header
   238  	fetchLabels *bool
   239  	fields      *string
   240  	page        *int
   241  	search      *string
   242  	size        *int
   243  }
   244  
   245  // Parameter adds a query parameter.
   246  func (r *OrganizationsListRequest) Parameter(name string, value interface{}) *OrganizationsListRequest {
   247  	helpers.AddValue(&r.query, name, value)
   248  	return r
   249  }
   250  
   251  // Header adds a request header.
   252  func (r *OrganizationsListRequest) Header(name string, value interface{}) *OrganizationsListRequest {
   253  	helpers.AddHeader(&r.header, name, value)
   254  	return r
   255  }
   256  
   257  // Impersonate wraps requests on behalf of another user.
   258  // Note: Services that do not support this feature may silently ignore this call.
   259  func (r *OrganizationsListRequest) Impersonate(user string) *OrganizationsListRequest {
   260  	helpers.AddImpersonationHeader(&r.header, user)
   261  	return r
   262  }
   263  
   264  // FetchLabels sets the value of the 'fetch_labels' parameter.
   265  //
   266  // If true, includes the labels on an organization in the output. Could slow request response time.
   267  func (r *OrganizationsListRequest) FetchLabels(value bool) *OrganizationsListRequest {
   268  	r.fetchLabels = &value
   269  	return r
   270  }
   271  
   272  // Fields sets the value of the 'fields' parameter.
   273  //
   274  // Projection
   275  // This field contains a comma-separated list of fields you'd like to get in
   276  // a result. No new fields can be added, only existing ones can be filtered.
   277  // To specify a field 'id' of a structure 'plan' use 'plan.id'.
   278  // To specify all fields of a structure 'labels' use 'labels.*'.
   279  func (r *OrganizationsListRequest) Fields(value string) *OrganizationsListRequest {
   280  	r.fields = &value
   281  	return r
   282  }
   283  
   284  // Page sets the value of the 'page' parameter.
   285  //
   286  // Index of the requested page, where one corresponds to the first page.
   287  func (r *OrganizationsListRequest) Page(value int) *OrganizationsListRequest {
   288  	r.page = &value
   289  	return r
   290  }
   291  
   292  // Search sets the value of the 'search' parameter.
   293  //
   294  // Search criteria.
   295  //
   296  // The syntax of this parameter is similar to the syntax of the _where_ clause
   297  // of an SQL statement, but using the names of the attributes of the organization
   298  // instead of the names of the columns of a table. For example, in order to
   299  // retrieve organizations with name starting with my:
   300  //
   301  // ```sql
   302  // name like 'my%'
   303  // ```
   304  //
   305  // If the parameter isn't provided, or if the value is empty, then all the
   306  // items that the user has permission to see will be returned.
   307  func (r *OrganizationsListRequest) Search(value string) *OrganizationsListRequest {
   308  	r.search = &value
   309  	return r
   310  }
   311  
   312  // Size sets the value of the 'size' parameter.
   313  //
   314  // Maximum number of items that will be contained in the returned page.
   315  func (r *OrganizationsListRequest) Size(value int) *OrganizationsListRequest {
   316  	r.size = &value
   317  	return r
   318  }
   319  
   320  // Send sends this request, waits for the response, and returns it.
   321  //
   322  // This is a potentially lengthy operation, as it requires network communication.
   323  // Consider using a context and the SendContext method.
   324  func (r *OrganizationsListRequest) Send() (result *OrganizationsListResponse, err error) {
   325  	return r.SendContext(context.Background())
   326  }
   327  
   328  // SendContext sends this request, waits for the response, and returns it.
   329  func (r *OrganizationsListRequest) SendContext(ctx context.Context) (result *OrganizationsListResponse, err error) {
   330  	query := helpers.CopyQuery(r.query)
   331  	if r.fetchLabels != nil {
   332  		helpers.AddValue(&query, "fetchLabels", *r.fetchLabels)
   333  	}
   334  	if r.fields != nil {
   335  		helpers.AddValue(&query, "fields", *r.fields)
   336  	}
   337  	if r.page != nil {
   338  		helpers.AddValue(&query, "page", *r.page)
   339  	}
   340  	if r.search != nil {
   341  		helpers.AddValue(&query, "search", *r.search)
   342  	}
   343  	if r.size != nil {
   344  		helpers.AddValue(&query, "size", *r.size)
   345  	}
   346  	header := helpers.CopyHeader(r.header)
   347  	uri := &url.URL{
   348  		Path:     r.path,
   349  		RawQuery: query.Encode(),
   350  	}
   351  	request := &http.Request{
   352  		Method: "GET",
   353  		URL:    uri,
   354  		Header: header,
   355  	}
   356  	if ctx != nil {
   357  		request = request.WithContext(ctx)
   358  	}
   359  	response, err := r.transport.RoundTrip(request)
   360  	if err != nil {
   361  		return
   362  	}
   363  	defer response.Body.Close()
   364  	result = &OrganizationsListResponse{}
   365  	result.status = response.StatusCode
   366  	result.header = response.Header
   367  	reader := bufio.NewReader(response.Body)
   368  	_, err = reader.Peek(1)
   369  	if err == io.EOF {
   370  		err = nil
   371  		return
   372  	}
   373  	if result.status >= 400 {
   374  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   375  		if err != nil {
   376  			return
   377  		}
   378  		err = result.err
   379  		return
   380  	}
   381  	err = readOrganizationsListResponse(result, reader)
   382  	if err != nil {
   383  		return
   384  	}
   385  	return
   386  }
   387  
   388  // OrganizationsListResponse is the response for the 'list' method.
   389  type OrganizationsListResponse struct {
   390  	status int
   391  	header http.Header
   392  	err    *errors.Error
   393  	items  *OrganizationList
   394  	page   *int
   395  	size   *int
   396  	total  *int
   397  }
   398  
   399  // Status returns the response status code.
   400  func (r *OrganizationsListResponse) Status() int {
   401  	if r == nil {
   402  		return 0
   403  	}
   404  	return r.status
   405  }
   406  
   407  // Header returns header of the response.
   408  func (r *OrganizationsListResponse) Header() http.Header {
   409  	if r == nil {
   410  		return nil
   411  	}
   412  	return r.header
   413  }
   414  
   415  // Error returns the response error.
   416  func (r *OrganizationsListResponse) Error() *errors.Error {
   417  	if r == nil {
   418  		return nil
   419  	}
   420  	return r.err
   421  }
   422  
   423  // Items returns the value of the 'items' parameter.
   424  //
   425  // Retrieved list of organizations.
   426  func (r *OrganizationsListResponse) Items() *OrganizationList {
   427  	if r == nil {
   428  		return nil
   429  	}
   430  	return r.items
   431  }
   432  
   433  // GetItems returns the value of the 'items' parameter and
   434  // a flag indicating if the parameter has a value.
   435  //
   436  // Retrieved list of organizations.
   437  func (r *OrganizationsListResponse) GetItems() (value *OrganizationList, ok bool) {
   438  	ok = r != nil && r.items != nil
   439  	if ok {
   440  		value = r.items
   441  	}
   442  	return
   443  }
   444  
   445  // Page returns the value of the 'page' parameter.
   446  //
   447  // Index of the requested page, where one corresponds to the first page.
   448  func (r *OrganizationsListResponse) Page() int {
   449  	if r != nil && r.page != nil {
   450  		return *r.page
   451  	}
   452  	return 0
   453  }
   454  
   455  // GetPage returns the value of the 'page' parameter and
   456  // a flag indicating if the parameter has a value.
   457  //
   458  // Index of the requested page, where one corresponds to the first page.
   459  func (r *OrganizationsListResponse) GetPage() (value int, ok bool) {
   460  	ok = r != nil && r.page != nil
   461  	if ok {
   462  		value = *r.page
   463  	}
   464  	return
   465  }
   466  
   467  // Size returns the value of the 'size' parameter.
   468  //
   469  // Maximum number of items that will be contained in the returned page.
   470  func (r *OrganizationsListResponse) Size() int {
   471  	if r != nil && r.size != nil {
   472  		return *r.size
   473  	}
   474  	return 0
   475  }
   476  
   477  // GetSize returns the value of the 'size' parameter and
   478  // a flag indicating if the parameter has a value.
   479  //
   480  // Maximum number of items that will be contained in the returned page.
   481  func (r *OrganizationsListResponse) GetSize() (value int, ok bool) {
   482  	ok = r != nil && r.size != nil
   483  	if ok {
   484  		value = *r.size
   485  	}
   486  	return
   487  }
   488  
   489  // Total returns the value of the 'total' parameter.
   490  //
   491  // Total number of items of the collection that match the search criteria,
   492  // regardless of the size of the page.
   493  func (r *OrganizationsListResponse) Total() int {
   494  	if r != nil && r.total != nil {
   495  		return *r.total
   496  	}
   497  	return 0
   498  }
   499  
   500  // GetTotal returns the value of the 'total' parameter and
   501  // a flag indicating if the parameter has a value.
   502  //
   503  // Total number of items of the collection that match the search criteria,
   504  // regardless of the size of the page.
   505  func (r *OrganizationsListResponse) GetTotal() (value int, ok bool) {
   506  	ok = r != nil && r.total != nil
   507  	if ok {
   508  		value = *r.total
   509  	}
   510  	return
   511  }