github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cce/v3/addons/requests.go (about)

     1  package addons
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  // ListOpts allows the filtering of list data using given parameters.
    10  type ListOpts struct {
    11  	AddonTemplateName string `json:"addonTemplateName"`
    12  	Uid               string `json:"uid"`
    13  	Version           string `json:"version"`
    14  	Status            string `json:"status"`
    15  }
    16  
    17  // List returns collection of addons.
    18  func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]Addon, error) {
    19  	var r ListResult
    20  	_, r.Err = client.Get(resourceListURL(client, clusterID), &r.Body, nil)
    21  
    22  	allAddons, err := r.ExtractAddon()
    23  
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	return FilterAddons(allAddons, opts), nil
    29  }
    30  
    31  func FilterAddons(addons []Addon, opts ListOpts) []Addon {
    32  
    33  	var refinedAddons []Addon
    34  	var matched bool
    35  
    36  	m := map[string]FilterStruct{}
    37  
    38  	if opts.AddonTemplateName != "" {
    39  		m["AddonTemplateName"] = FilterStruct{Value: opts.AddonTemplateName, Driller: []string{"Spec"}}
    40  	}
    41  	if opts.Version != "" {
    42  		m["Version"] = FilterStruct{Value: opts.Version, Driller: []string{"Spec"}}
    43  	}
    44  	if opts.Uid != "" {
    45  		m["Id"] = FilterStruct{Value: opts.Uid, Driller: []string{"Metadata"}}
    46  	}
    47  	if opts.Status != "" {
    48  		m["Status"] = FilterStruct{Value: opts.Status, Driller: []string{"Status"}}
    49  	}
    50  
    51  	if len(m) > 0 && len(addons) > 0 {
    52  		for _, addon := range addons {
    53  			matched = true
    54  
    55  			for key, value := range m {
    56  				if sVal := GetStructNestedField(&addon, key, value.Driller); sVal != value.Value {
    57  					matched = false
    58  					break
    59  				}
    60  			}
    61  			if matched {
    62  				refinedAddons = append(refinedAddons, addon)
    63  			}
    64  		}
    65  	}
    66  
    67  	return refinedAddons
    68  }
    69  
    70  func GetStructNestedField(v *Addon, field string, structDriller []string) string {
    71  	r := reflect.ValueOf(v)
    72  	for _, drillField := range structDriller {
    73  		f := reflect.Indirect(r).FieldByName(drillField).Interface()
    74  		r = reflect.ValueOf(f)
    75  	}
    76  	f1 := reflect.Indirect(r).FieldByName(field)
    77  	return string(f1.String())
    78  }
    79  
    80  type FilterStruct struct {
    81  	Value   string
    82  	Driller []string
    83  }
    84  
    85  // CreateOptsBuilder allows extensions to add additional parameters to the
    86  // Create request.
    87  type CreateOptsBuilder interface {
    88  	ToAddonCreateMap() (map[string]interface{}, error)
    89  }
    90  
    91  // CreateOpts contains all the values needed to create a new addon
    92  type CreateOpts struct {
    93  	// API type, fixed value Addon
    94  	Kind string `json:"kind" required:"true"`
    95  	// API version, fixed value v3
    96  	ApiVersion string `json:"apiVersion" required:"true"`
    97  	// Metadata required to create an addon
    98  	Metadata CreateMetadata `json:"metadata" required:"true"`
    99  	// specifications to create an addon
   100  	Spec RequestSpec `json:"spec" required:"true"`
   101  }
   102  
   103  type CreateMetadata struct {
   104  	Anno Annotations `json:"annotations" required:"true"`
   105  }
   106  
   107  type Annotations struct {
   108  	AddonInstallType string `json:"addon.install/type" required:"true"`
   109  }
   110  
   111  //Specifications to create an addon
   112  type RequestSpec struct {
   113  	// For the addon version.
   114  	Version string `json:"version" required:"true"`
   115  	// Cluster ID.
   116  	ClusterID string `json:"clusterID" required:"true"`
   117  	// Addon Template Name.
   118  	AddonTemplateName string `json:"addonTemplateName" required:"true"`
   119  	// Addon Parameters
   120  	Values Values `json:"values" required:"true"`
   121  }
   122  
   123  type Values struct {
   124  	Basic  map[string]interface{} `json:"basic" required:"true"`
   125  	Custom map[string]interface{} `json:"custom,omitempty"`
   126  	Flavor map[string]interface{} `json:"flavor,omitempty"`
   127  }
   128  
   129  // ToAddonCreateMap builds a create request body from CreateOpts.
   130  func (opts CreateOpts) ToAddonCreateMap() (map[string]interface{}, error) {
   131  	return golangsdk.BuildRequestBody(opts, "")
   132  }
   133  
   134  // Create accepts a CreateOpts struct and uses the values to create a new
   135  // addon.
   136  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder, cluster_id string) (r CreateResult) {
   137  	b, err := opts.ToAddonCreateMap()
   138  	if err != nil {
   139  		r.Err = err
   140  		return
   141  	}
   142  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   143  	_, r.Err = c.Post(rootURL(c, cluster_id), b, &r.Body, reqOpt)
   144  	return
   145  }
   146  
   147  // Get retrieves a particular addon based on its unique ID.
   148  func Get(c *golangsdk.ServiceClient, id, cluster_id string) (r GetResult) {
   149  	_, r.Err = c.Get(resourceURL(c, id, cluster_id), &r.Body, nil)
   150  	return
   151  }
   152  
   153  // Delete will permanently delete a particular addon based on its unique ID.
   154  func Delete(c *golangsdk.ServiceClient, id, cluster_id string) (r DeleteResult) {
   155  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   156  	_, r.Err = c.Delete(resourceURL(c, id, cluster_id), reqOpt)
   157  	return
   158  }