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

     1  package nodes
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  var RequestOpts golangsdk.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 string(f1.String())
    82  }
    83  
    84  type FilterStruct struct {
    85  	Value   string
    86  	Driller []string
    87  }
    88  
    89  // CreateOptsBuilder allows extensions to add additional parameters to the
    90  // Create request.
    91  type CreateOpts struct {
    92  	// API type, fixed value Node
    93  	Kind string `json:"kind" required:"true"`
    94  	// API version, fixed value v3
    95  	ApiVersion string `json:"apiversion" required:"true"`
    96  	// Metadata required to create a Node
    97  	Metadata CreateMetaData `json:"metadata"`
    98  	// specifications to create a Node
    99  	Spec Spec `json:"spec" required:"true"`
   100  }
   101  
   102  // Metadata required to create a Node
   103  type CreateMetaData struct {
   104  	// Node name
   105  	Name string `json:"name,omitempty"`
   106  	// Node tag, key value pair format
   107  	Labels map[string]string `json:"labels,omitempty"`
   108  	// Node annotation, key value pair format
   109  	Annotations map[string]string `json:"annotations,omitempty"`
   110  }
   111  
   112  // Create accepts a CreateOpts struct and uses the values to create a new
   113  // logical Node. When it is created, the Node does not have an internal
   114  // interface
   115  type CreateOptsBuilder interface {
   116  	ToNodeCreateMap() (map[string]interface{}, error)
   117  }
   118  
   119  // ToNodeCreateMap builds a create request body from CreateOpts.
   120  func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
   121  	return golangsdk.BuildRequestBody(opts, "")
   122  }
   123  
   124  // Create accepts a CreateOpts struct and uses the values to create a new
   125  // logical node.
   126  func Create(c *golangsdk.ServiceClient, clusterid string, opts CreateOptsBuilder) (r CreateResult) {
   127  	b, err := opts.ToNodeCreateMap()
   128  	if err != nil {
   129  		r.Err = err
   130  		return
   131  	}
   132  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   133  	_, r.Err = c.Post(rootURL(c, clusterid), b, &r.Body, reqOpt)
   134  	return
   135  }
   136  
   137  type AddOpts struct {
   138  	// API type, fixed value Node
   139  	Kind string `json:"kind" required:"true"`
   140  	// API version, fixed value v3
   141  	ApiVersion string `json:"apiversion" required:"true"`
   142  	// List of nodes to add
   143  	NodeList []AddNode `json:"nodeList" required:"true"`
   144  }
   145  
   146  type AddNode struct {
   147  	ServerID string      `json:"serverID" required:"true"`
   148  	Spec     AddNodeSpec `json:"spec" required:"true"`
   149  }
   150  
   151  type AddNodeSpec struct {
   152  	VolumeConfig  *VolumeConfig          `json:"volumeConfig,omitempty"`
   153  	RuntimeConfig *RuntimeConfig         `json:"runtimeConfig,omitempty"`
   154  	K8sOptions    *K8sOptions            `json:"k8sOptions,omitempty"`
   155  	Lifecycle     *Lifecycle             `json:"lifecycle,omitempty"`
   156  	Login         LoginSpec              `json:"login" required:"true"`
   157  	Os            string                 `json:"os,omitempty"`
   158  	ExtendParam   map[string]interface{} `json:"extendParam,omitempty"`
   159  }
   160  
   161  type VolumeConfig struct {
   162  	LvmConfig string `json:"lvmConfig,omitempty"`
   163  }
   164  
   165  type RuntimeConfig struct {
   166  	DockerBaseSize int `json:"dockerBaseSize,omitempty"`
   167  }
   168  
   169  type K8sOptions struct {
   170  	MaxPods       int    `json:"maxPods,omitempty"`
   171  	NicMultiQueue string `json:"nicMultiqueue,omitempty"`
   172  	NicThreshold  string `json:"nicThreshold,omitempty"`
   173  }
   174  
   175  type Lifecycle struct {
   176  	Preinstall  string `json:"preInstall,omitempty"`
   177  	PostInstall string `json:"postInstall,omitempty"`
   178  }
   179  
   180  type AddOptsBuilder interface {
   181  	ToNodeAddMap() (map[string]interface{}, error)
   182  }
   183  
   184  func (opts AddOpts) ToNodeAddMap() (map[string]interface{}, error) {
   185  	return golangsdk.BuildRequestBody(opts, "")
   186  }
   187  
   188  func Add(c *golangsdk.ServiceClient, clusterid string, opts AddOptsBuilder) (r AddResult) {
   189  	b, err := opts.ToNodeAddMap()
   190  	if err != nil {
   191  		r.Err = err
   192  		return
   193  	}
   194  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   195  	_, r.Err = c.Post(addNodeURL(c, clusterid), b, &r.Body, reqOpt)
   196  	return
   197  }
   198  
   199  // Get retrieves a particular nodes based on its unique ID and cluster ID.
   200  func Get(c *golangsdk.ServiceClient, clusterid, nodeid string) (r GetResult) {
   201  	_, r.Err = c.Get(resourceURL(c, clusterid, nodeid), &r.Body, &golangsdk.RequestOpts{
   202  		OkCodes:     []int{200},
   203  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   204  	})
   205  	return
   206  }
   207  
   208  // UpdateOptsBuilder allows extensions to add additional parameters to the
   209  // Update request.
   210  type UpdateOptsBuilder interface {
   211  	ToNodeUpdateMap() (map[string]interface{}, error)
   212  }
   213  
   214  // UpdateOpts contains all the values needed to update a new node
   215  type UpdateOpts struct {
   216  	Metadata UpdateMetadata `json:"metadata,omitempty"`
   217  }
   218  
   219  type UpdateMetadata struct {
   220  	Name string `json:"name,omitempty"`
   221  }
   222  
   223  // ToNodeUpdateMap builds an update body based on UpdateOpts.
   224  func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
   225  	return golangsdk.BuildRequestBody(opts, "")
   226  }
   227  
   228  // Update allows nodes to be updated.
   229  func Update(c *golangsdk.ServiceClient, clusterid, nodeid string, opts UpdateOptsBuilder) (r UpdateResult) {
   230  	b, err := opts.ToNodeUpdateMap()
   231  	if err != nil {
   232  		r.Err = err
   233  		return
   234  	}
   235  	_, r.Err = c.Put(resourceURL(c, clusterid, nodeid), b, &r.Body, &golangsdk.RequestOpts{
   236  		OkCodes: []int{200},
   237  	})
   238  	return
   239  }
   240  
   241  // Delete will permanently delete a particular node based on its unique ID and cluster ID.
   242  func Delete(c *golangsdk.ServiceClient, clusterid, nodeid string) (r DeleteResult) {
   243  	_, r.Err = c.Delete(resourceURL(c, clusterid, nodeid), &golangsdk.RequestOpts{
   244  		OkCodes:     []int{200},
   245  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   246  	})
   247  	return
   248  }
   249  
   250  type RemoveOptsBuilder interface {
   251  	ToNodeRemoveMap() (map[string]interface{}, error)
   252  }
   253  
   254  type RemoveOpts struct {
   255  	//  API type, fixed value RemoveNodesTask
   256  	Kind string `json:"kind,omitempty"`
   257  	// API version, fixed value v3
   258  	Apiversion string `json:"apiVersion,omitempty"`
   259  
   260  	Spec RemoveNodeSpec `json:"spec" required:"true"`
   261  }
   262  
   263  type RemoveNodeSpec struct {
   264  	Login LoginSpec  `json:"login" required:"true"`
   265  	Nodes []NodeItem `json:"nodes,omitempty"`
   266  }
   267  
   268  type NodeItem struct {
   269  	Uid string `json:"uid,omitempty"`
   270  }
   271  
   272  func (opts RemoveOpts) ToNodeRemoveMap() (map[string]interface{}, error) {
   273  	return golangsdk.BuildRequestBody(opts, "")
   274  }
   275  
   276  func Remove(c *golangsdk.ServiceClient, clusterid string, opts RemoveOptsBuilder) (r DeleteResult) {
   277  	b, err := opts.ToNodeRemoveMap()
   278  	if err != nil {
   279  		r.Err = err
   280  		return
   281  	}
   282  
   283  	_, r.Err = c.Put(removeNodeURL(c, clusterid), b, nil, &golangsdk.RequestOpts{
   284  		OkCodes:     []int{200},
   285  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   286  	})
   287  
   288  	return
   289  }
   290  
   291  // GetJobDetails retrieves a particular job based on its unique ID
   292  func GetJobDetails(c *golangsdk.ServiceClient, jobid string) (r GetResult) {
   293  	_, r.Err = c.Get(getJobURL(c, jobid), &r.Body, &golangsdk.RequestOpts{
   294  		OkCodes:     []int{200},
   295  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   296  	})
   297  	return
   298  }