github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/cce/v3/nodes/requests.go (about)

     1  package nodes
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  )
     8  
     9  var RequestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json"},
    11  }
    12  
    13  // ListOpts allows the filtering of list data using given parameters.
    14  type ListOpts struct {
    15  	Name  string `json:"name"`
    16  	Uid   string `json:"uid"`
    17  	Phase string `json:"phase"`
    18  }
    19  
    20  // List returns collection of nodes.
    21  func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]Nodes, error) {
    22  	var r ListResult
    23  	_, r.Err = client.Get(rootURL(client, clusterID), &r.Body, &golangsdk.RequestOpts{
    24  		OkCodes:     []int{200},
    25  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
    26  	})
    27  
    28  	allNodes, err := r.ExtractNode()
    29  
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return FilterNodes(allNodes, opts), nil
    35  }
    36  
    37  func FilterNodes(nodes []Nodes, opts ListOpts) []Nodes {
    38  
    39  	var refinedNodes []Nodes
    40  	var matched bool
    41  
    42  	m := map[string]FilterStruct{}
    43  
    44  	if opts.Name != "" {
    45  		m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}}
    46  	}
    47  	if opts.Uid != "" {
    48  		m["Id"] = FilterStruct{Value: opts.Uid, Driller: []string{"Metadata"}}
    49  	}
    50  
    51  	if opts.Phase != "" {
    52  		m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}}
    53  	}
    54  
    55  	if len(m) > 0 && len(nodes) > 0 {
    56  		for _, nodes := range nodes {
    57  			matched = true
    58  
    59  			for key, value := range m {
    60  				if sVal := GetStructNestedField(&nodes, key, value.Driller); !(sVal == value.Value) {
    61  					matched = false
    62  				}
    63  			}
    64  			if matched {
    65  				refinedNodes = append(refinedNodes, nodes)
    66  			}
    67  		}
    68  	} else {
    69  		refinedNodes = nodes
    70  	}
    71  	return refinedNodes
    72  }
    73  
    74  func GetStructNestedField(v *Nodes, field string, structDriller []string) string {
    75  	r := reflect.ValueOf(v)
    76  	for _, drillField := range structDriller {
    77  		f := reflect.Indirect(r).FieldByName(drillField).Interface()
    78  		r = reflect.ValueOf(f)
    79  	}
    80  	f1 := reflect.Indirect(r).FieldByName(field)
    81  	return f1.String()
    82  }
    83  
    84  type FilterStruct struct {
    85  	Value   string
    86  	Driller []string
    87  }
    88  
    89  // CreateOpts is a struct contains the parameters of creating Node
    90  type CreateOpts struct {
    91  	// API type, fixed value Node
    92  	Kind string `json:"kind" required:"true"`
    93  	// API version, fixed value v3
    94  	ApiVersion string `json:"apiVersion" required:"true"`
    95  	// Metadata required to create a Node
    96  	Metadata CreateMetaData `json:"metadata"`
    97  	// specifications to create a Node
    98  	Spec Spec `json:"spec" required:"true"`
    99  }
   100  
   101  // CreateMetaData required to create a Node
   102  type CreateMetaData struct {
   103  	// Node name
   104  	Name string `json:"name,omitempty"`
   105  	// Node tag, key value pair format
   106  	Labels map[string]string `json:"labels,omitempty"`
   107  	// Node annotation, key value pair format
   108  	Annotations map[string]string `json:"annotations,omitempty"`
   109  }
   110  
   111  // CreateOptsBuilder allows extensions to add additional parameters to the
   112  // Create request.
   113  type CreateOptsBuilder interface {
   114  	ToNodeCreateMap() (map[string]interface{}, error)
   115  }
   116  
   117  // ToNodeCreateMap builds a create request body from CreateOpts.
   118  func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
   119  	return golangsdk.BuildRequestBody(opts, "")
   120  }
   121  
   122  // Create accepts a CreateOpts struct and uses the values to create a new
   123  // logical node.
   124  func Create(c *golangsdk.ServiceClient, clusterID string, opts CreateOptsBuilder) (r CreateResult) {
   125  	b, err := opts.ToNodeCreateMap()
   126  	if err != nil {
   127  		r.Err = err
   128  		return
   129  	}
   130  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   131  	_, r.Err = c.Post(rootURL(c, clusterID), b, &r.Body, reqOpt)
   132  	return
   133  }
   134  
   135  // Get retrieves a particular nodes based on its unique ID and cluster ID.
   136  func Get(c *golangsdk.ServiceClient, clusterID, nodeID string) (r GetResult) {
   137  	_, r.Err = c.Get(resourceURL(c, clusterID, nodeID), &r.Body, &golangsdk.RequestOpts{
   138  		OkCodes:     []int{200},
   139  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   140  	})
   141  	return
   142  }
   143  
   144  // UpdateOptsBuilder allows extensions to add additional parameters to the
   145  // Update request.
   146  type UpdateOptsBuilder interface {
   147  	ToNodeUpdateMap() (map[string]interface{}, error)
   148  }
   149  
   150  // UpdateOpts contains all the values needed to update a new node
   151  type UpdateOpts struct {
   152  	Metadata UpdateMetadata `json:"metadata,omitempty"`
   153  }
   154  
   155  type UpdateMetadata struct {
   156  	Name string `json:"name,omitempty"`
   157  }
   158  
   159  // ToNodeUpdateMap builds an update body based on UpdateOpts.
   160  func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
   161  	return golangsdk.BuildRequestBody(opts, "")
   162  }
   163  
   164  // Update allows nodes to be updated.
   165  func Update(c *golangsdk.ServiceClient, clusterID, nodeID string, opts UpdateOptsBuilder) (r UpdateResult) {
   166  	b, err := opts.ToNodeUpdateMap()
   167  	if err != nil {
   168  		r.Err = err
   169  		return
   170  	}
   171  	_, r.Err = c.Put(resourceURL(c, clusterID, nodeID), b, &r.Body, &golangsdk.RequestOpts{
   172  		OkCodes: []int{200},
   173  	})
   174  	return
   175  }
   176  
   177  // Delete will permanently delete a particular node based on its unique ID and cluster ID.
   178  func Delete(c *golangsdk.ServiceClient, clusterID, nodeID string) (r DeleteResult) {
   179  	_, r.Err = c.Delete(resourceURL(c, clusterID, nodeID), &golangsdk.RequestOpts{
   180  		OkCodes:     []int{200},
   181  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   182  	})
   183  	return
   184  }
   185  
   186  // GetJobDetails retrieves a particular job based on its unique ID
   187  func GetJobDetails(c *golangsdk.ServiceClient, jobID string) (r GetResult) {
   188  	_, r.Err = c.Get(getJobURL(c, jobID), &r.Body, &golangsdk.RequestOpts{
   189  		OkCodes:     []int{200},
   190  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   191  	})
   192  	return
   193  }