...

Source file src/github.com/LINBIT/golinstor/client/option.go

Documentation: github.com/LINBIT/golinstor/client

     1  // A REST client to interact with LINSTOR's REST API
     2  // Copyright (C) LINBIT HA-Solutions GmbH
     3  // All Rights Reserved.
     4  // Author: Roland Kammerer <roland.kammerer@linbit.com>
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     7  // not use this file except in compliance with the License. You may obtain
     8  // a copy of the License at
     9  //
    10  // http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    14  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    15  // License for the specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package client
    19  
    20  import (
    21  	"net/url"
    22  	"reflect"
    23  
    24  	"github.com/google/go-querystring/query"
    25  )
    26  
    27  // ListOpts is a struct primarily used to define parameters used for pagination. It is also used for filtering (e.g., the /view/ calls)
    28  type ListOpts struct {
    29  	// Number of items to skip. Only used if Limit is a positive value
    30  	Offset int `url:"offset"`
    31  	// Maximum number of items to retrieve
    32  	Limit int `url:"limit"`
    33  	// Some responses can be cached controller side, such as snapshot lists
    34  	Cached *bool `url:"cached,omitempty"`
    35  
    36  	StoragePool []string `url:"storage_pools"`
    37  	Resource    []string `url:"resources"`
    38  	Node        []string `url:"nodes"`
    39  	Prop        []string `url:"props"`
    40  	Snapshots   []string `url:"snapshots"`
    41  	Status      string   `url:"status,omitempty"`
    42  
    43  	// Content is used in the files API. If true, fetching files will include the content.
    44  	Content bool `url:"content,omitempty"`
    45  }
    46  
    47  func genOptions(opts ...*ListOpts) *ListOpts {
    48  	if opts == nil || len(opts) == 0 {
    49  		return nil
    50  	}
    51  
    52  	return opts[0]
    53  }
    54  
    55  func addOptions(s string, opt interface{}) (string, error) {
    56  	v := reflect.ValueOf(opt)
    57  	if v.Kind() == reflect.Ptr && v.IsNil() {
    58  		return s, nil
    59  	}
    60  
    61  	u, err := url.Parse(s)
    62  	if err != nil {
    63  		return s, err
    64  	}
    65  
    66  	vs, err := query.Values(opt)
    67  	if err != nil {
    68  		return s, err
    69  	}
    70  
    71  	u.RawQuery = vs.Encode()
    72  	return u.String(), nil
    73  }
    74  

View as plain text