github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/softwareconfig/requests.go (about)

     1  package softwareconfig
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  // ListOpts allows the filtering and sorting of paginated collections through
    11  // the API. Filtering is achieved by passing in struct field values that map to
    12  // the software config attributes you want to see returned. Marker and Limit are used for pagination.
    13  type ListOpts struct {
    14  	Id     string
    15  	Name   string
    16  	Marker string `q:"marker"`
    17  	Limit  int    `q:"limit"`
    18  }
    19  
    20  // List returns collection of
    21  // Software Config. It accepts a ListOpts struct, which allows you to filter and sort
    22  // the returned collection for greater efficiency.
    23  //
    24  // Default policy settings return only those Software Config that are owned by the
    25  // tenant who submits the request, unless an admin user submits the request.
    26  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]SoftwareConfig, error) {
    27  	q, err := golangsdk.BuildQueryString(&opts)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	u := rootURL(c) + q.String()
    32  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    33  		return SoftwareConfigPage{pagination.LinkedPageBase{PageResult: r}}
    34  	}).AllPages()
    35  
    36  	allConfigs, err := ExtractSoftwareConfigs(pages)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return FilterSoftwareConfig(allConfigs, opts)
    42  }
    43  
    44  func FilterSoftwareConfig(config []SoftwareConfig, opts ListOpts) ([]SoftwareConfig, error) {
    45  
    46  	var refinedSoftwareConfig []SoftwareConfig
    47  	var matched bool
    48  	m := map[string]interface{}{}
    49  
    50  	if opts.Id != "" {
    51  		m["Id"] = opts.Id
    52  	}
    53  	if opts.Name != "" {
    54  		m["Name"] = opts.Name
    55  	}
    56  
    57  	if len(m) > 0 && len(config) > 0 {
    58  		for _, config := range config {
    59  			matched = true
    60  
    61  			for key, value := range m {
    62  				if sVal := getStructField(&config, key); !(sVal == value) {
    63  					matched = false
    64  				}
    65  			}
    66  
    67  			if matched {
    68  				refinedSoftwareConfig = append(refinedSoftwareConfig, config)
    69  			}
    70  		}
    71  	} else {
    72  		refinedSoftwareConfig = config
    73  	}
    74  	return refinedSoftwareConfig, nil
    75  }
    76  
    77  func getStructField(v *SoftwareConfig, field string) string {
    78  	r := reflect.ValueOf(v)
    79  	f := reflect.Indirect(r).FieldByName(field)
    80  	return string(f.String())
    81  }
    82  
    83  // CreateOptsBuilder allows extensions to add additional parameters to the
    84  // Create request.
    85  type CreateOptsBuilder interface {
    86  	ToSoftwareConfigCreateMap() (map[string]interface{}, error)
    87  }
    88  
    89  // CreateOpts contains all the values needed to create a new Software Config. There are
    90  // no required values.
    91  type CreateOpts struct {
    92  	// Specifies the script used for defining the configuration.
    93  	Config string `json:"config,omitempty"`
    94  	//Specifies the name of the software configuration group.
    95  	Group string `json:"group,omitempty"`
    96  	//Specifies the name of the software configuration.
    97  	Name string `json:"name" required:"true"`
    98  	//Specifies the software configuration input.
    99  	Inputs []map[string]interface{} `json:"inputs,omitempty"`
   100  	//Specifies the software configuration output.
   101  	Outputs []map[string]interface{} `json:"outputs,omitempty"`
   102  	//Specifies options used by a software configuration management tool.
   103  	Options map[string]interface{} `json:"options,omitempty"`
   104  }
   105  
   106  // ToSoftwareConfigCreateMap builds a create request body from CreateOpts.
   107  func (opts CreateOpts) ToSoftwareConfigCreateMap() (map[string]interface{}, error) {
   108  	return golangsdk.BuildRequestBody(opts, "")
   109  }
   110  
   111  // Create accepts a CreateOpts struct and uses the values to create a new Software config
   112  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   113  	b, err := opts.ToSoftwareConfigCreateMap()
   114  	if err != nil {
   115  		r.Err = err
   116  		return
   117  	}
   118  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   119  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   120  	return
   121  }
   122  
   123  // Get retrieves a particular software config based on its unique ID.
   124  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   125  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   126  	return
   127  }
   128  
   129  // Delete will permanently delete a particular Software Config based on its unique ID.
   130  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   131  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   132  	return
   133  }