github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/rts/v1/softwareconfig/requests.go (about) 1 package softwareconfig 2 3 import ( 4 "reflect" 5 6 "github.com/opentelekomcloud/gophertelekomcloud" 7 "github.com/opentelekomcloud/gophertelekomcloud/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 if err != nil { 37 return nil, err 38 } 39 40 allConfigs, err := ExtractSoftwareConfigs(pages) 41 if err != nil { 42 return nil, err 43 } 44 45 return FilterSoftwareConfig(allConfigs, opts) 46 } 47 48 func FilterSoftwareConfig(config []SoftwareConfig, opts ListOpts) ([]SoftwareConfig, error) { 49 50 var refinedSoftwareConfig []SoftwareConfig 51 var matched bool 52 m := map[string]interface{}{} 53 54 if opts.Id != "" { 55 m["Id"] = opts.Id 56 } 57 if opts.Name != "" { 58 m["Name"] = opts.Name 59 } 60 61 if len(m) > 0 && len(config) > 0 { 62 for _, config := range config { 63 matched = true 64 65 for key, value := range m { 66 if sVal := getStructField(&config, key); !(sVal == value) { 67 matched = false 68 } 69 } 70 71 if matched { 72 refinedSoftwareConfig = append(refinedSoftwareConfig, config) 73 } 74 } 75 } else { 76 refinedSoftwareConfig = config 77 } 78 return refinedSoftwareConfig, nil 79 } 80 81 func getStructField(v *SoftwareConfig, field string) string { 82 r := reflect.ValueOf(v) 83 f := reflect.Indirect(r).FieldByName(field) 84 return f.String() 85 } 86 87 // CreateOptsBuilder allows extensions to add additional parameters to the 88 // Create request. 89 type CreateOptsBuilder interface { 90 ToSoftwareConfigCreateMap() (map[string]interface{}, error) 91 } 92 93 // CreateOpts contains all the values needed to create a new Software Config. There are 94 // no required values. 95 type CreateOpts struct { 96 // Specifies the script used for defining the configuration. 97 Config string `json:"config,omitempty"` 98 // Specifies the name of the software configuration group. 99 Group string `json:"group,omitempty"` 100 // Specifies the name of the software configuration. 101 Name string `json:"name" required:"true"` 102 // Specifies the software configuration input. 103 Inputs []map[string]interface{} `json:"inputs,omitempty"` 104 // Specifies the software configuration output. 105 Outputs []map[string]interface{} `json:"outputs,omitempty"` 106 // Specifies options used by a software configuration management tool. 107 Options map[string]interface{} `json:"options,omitempty"` 108 } 109 110 // ToSoftwareConfigCreateMap builds a create request body from CreateOpts. 111 func (opts CreateOpts) ToSoftwareConfigCreateMap() (map[string]interface{}, error) { 112 return golangsdk.BuildRequestBody(opts, "") 113 } 114 115 // Create accepts a CreateOpts struct and uses the values to create a new Software config 116 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 117 b, err := opts.ToSoftwareConfigCreateMap() 118 if err != nil { 119 r.Err = err 120 return 121 } 122 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 123 _, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt) 124 return 125 } 126 127 // Get retrieves a particular software config based on its unique ID. 128 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 129 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 130 return 131 } 132 133 // Delete will permanently delete a particular Software Config based on its unique ID. 134 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 135 _, r.Err = c.Delete(resourceURL(c, id), nil) 136 return 137 }