github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cce/v3/nodepools/requests.go (about)

     1  package nodepools
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/openstack/cce/v3/nodes"
     8  	"github.com/chnsz/golangsdk/openstack/common/tags"
     9  )
    10  
    11  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
    12  	MoreHeaders: map[string]string{"Content-Type": "application/json"},
    13  }
    14  
    15  // ListOpts allows the filtering of list data using given parameters.
    16  type ListOpts struct {
    17  	Name  string `json:"name"`
    18  	Uid   string `json:"uid"`
    19  	Phase string `json:"phase"`
    20  }
    21  
    22  // List returns collection of node pools.
    23  func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]NodePool, error) {
    24  	var r ListResult
    25  	_, r.Err = client.Get(rootURL(client, clusterID), &r.Body, &golangsdk.RequestOpts{
    26  		OkCodes:     []int{200},
    27  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
    28  	})
    29  
    30  	allNodePools, err := r.ExtractNodePool()
    31  
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return FilterNodePools(allNodePools, opts), nil
    37  }
    38  
    39  func FilterNodePools(nodepools []NodePool, opts ListOpts) []NodePool {
    40  
    41  	var refinedNodePools []NodePool
    42  	var matched bool
    43  
    44  	m := map[string]FilterStruct{}
    45  
    46  	if opts.Name != "" {
    47  		m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}}
    48  	}
    49  	if opts.Uid != "" {
    50  		m["Id"] = FilterStruct{Value: opts.Uid, Driller: []string{"Metadata"}}
    51  	}
    52  
    53  	if opts.Phase != "" {
    54  		m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}}
    55  	}
    56  
    57  	if len(m) > 0 && len(nodepools) > 0 {
    58  		for _, nodepool := range nodepools {
    59  			matched = true
    60  
    61  			for key, value := range m {
    62  				if sVal := GetStructNestedField(&nodepool, key, value.Driller); !(sVal == value.Value) {
    63  					matched = false
    64  				}
    65  			}
    66  			if matched {
    67  				refinedNodePools = append(refinedNodePools, nodepool)
    68  			}
    69  		}
    70  	} else {
    71  		refinedNodePools = nodepools
    72  	}
    73  	return refinedNodePools
    74  }
    75  
    76  func GetStructNestedField(v *NodePool, field string, structDriller []string) string {
    77  	r := reflect.ValueOf(v)
    78  	for _, drillField := range structDriller {
    79  		f := reflect.Indirect(r).FieldByName(drillField).Interface()
    80  		r = reflect.ValueOf(f)
    81  	}
    82  	f1 := reflect.Indirect(r).FieldByName(field)
    83  	return string(f1.String())
    84  }
    85  
    86  type FilterStruct struct {
    87  	Value   string
    88  	Driller []string
    89  }
    90  
    91  // CreateOpts allows extensions to add additional parameters to the
    92  // Create request.
    93  type CreateOpts struct {
    94  	// API type, fixed value Node
    95  	Kind string `json:"kind" required:"true"`
    96  	// API version, fixed value v3
    97  	ApiVersion string `json:"apiversion" required:"true"`
    98  	// Metadata required to create a Node Pool
    99  	Metadata CreateMetaData `json:"metadata"`
   100  	// specifications to create a Node Pool
   101  	Spec CreateSpec `json:"spec" required:"true"`
   102  }
   103  
   104  // CreateMetaData required to create a Node Pool
   105  type CreateMetaData struct {
   106  	// Name of the node pool.
   107  	Name string `json:"name" required:"true"`
   108  }
   109  
   110  // CreateSpec describes Node pools specification
   111  type CreateSpec struct {
   112  	//Node pool type
   113  	Type string `json:"type,omitempty"`
   114  	// Node template
   115  	NodeTemplate nodes.Spec `json:"nodeTemplate" required:"true"`
   116  	// Initial number of expected nodes
   117  	InitialNodeCount *int `json:"initialNodeCount" required:"true"`
   118  	// Auto scaling parameters
   119  	Autoscaling AutoscalingSpec `json:"autoscaling"`
   120  	// Node management parameters
   121  	NodeManagement NodeManagementSpec `json:"nodeManagement"`
   122  	// Pod security group configurations
   123  	PodSecurityGroups []PodSecurityGroupSpec `json:"podSecurityGroups,omitempty"`
   124  	// Node security group configurations
   125  	CustomSecurityGroups []string `json:"customSecurityGroups,omitempty"`
   126  }
   127  
   128  type PodSecurityGroupSpec struct {
   129  	Id string `json:"id,omitempty"`
   130  }
   131  
   132  // Create accepts a CreateOpts struct and uses the values to create a new
   133  // logical Node Pool. When it is created, the Node Pool does not have an internal
   134  // interface
   135  type CreateOptsBuilder interface {
   136  	ToNodePoolCreateMap() (map[string]interface{}, error)
   137  }
   138  
   139  // ToNodePoolCreateMap builds a create request body from CreateOpts.
   140  func (opts CreateOpts) ToNodePoolCreateMap() (map[string]interface{}, error) {
   141  	return golangsdk.BuildRequestBody(opts, "")
   142  }
   143  
   144  // Create accepts a CreateOpts struct and uses the values to create a new
   145  // logical node pool.
   146  func Create(c *golangsdk.ServiceClient, clusterid string, opts CreateOptsBuilder) (r CreateResult) {
   147  	b, err := opts.ToNodePoolCreateMap()
   148  	if err != nil {
   149  		r.Err = err
   150  		return
   151  	}
   152  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   153  	_, r.Err = c.Post(rootURL(c, clusterid), b, &r.Body, reqOpt)
   154  	return
   155  }
   156  
   157  // Get retrieves a particular node pool based on its unique ID and cluster ID.
   158  func Get(c *golangsdk.ServiceClient, clusterid, nodepoolid string) (r GetResult) {
   159  	_, r.Err = c.Get(resourceURL(c, clusterid, nodepoolid), &r.Body, &golangsdk.RequestOpts{
   160  		OkCodes:     []int{200},
   161  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   162  	})
   163  	return
   164  }
   165  
   166  // UpdateOptsBuilder allows extensions to add additional parameters to the
   167  // Update request.
   168  type UpdateOptsBuilder interface {
   169  	ToNodePoolUpdateMap() (map[string]interface{}, error)
   170  }
   171  
   172  // UpdateOpts contains all the values needed to update a new node pool
   173  type UpdateOpts struct {
   174  	// API type, fixed value Node
   175  	Kind string `json:"kind,omitempty"`
   176  	// API version, fixed value v3
   177  	ApiVersion string `json:"apiversion,omitempty"`
   178  	// Metadata required to update a Node Pool
   179  	Metadata UpdateMetaData `json:"metadata" required:"true"`
   180  	// specifications to update a Node Pool
   181  	Spec UpdateSpec `json:"spec,omitempty" required:"true"`
   182  }
   183  
   184  // UpdateMetaData required to update a Node Pool
   185  type UpdateMetaData struct {
   186  	// Name of the node pool.
   187  	Name string `json:"name" required:"true"`
   188  }
   189  
   190  type UpdateNodeTemplate struct {
   191  	// Node specifications
   192  	Flavor string `json:"flavor,omitempty"`
   193  	// The value of the available partition name
   194  	Az string `json:"az,omitempty"`
   195  	// The OS of the node
   196  	Os string `json:"os,omitempty"`
   197  	// ID of the dedicated host to which nodes will be scheduled
   198  	DedicatedHostID string `json:"dedicatedHostId,omitempty"`
   199  	// Node login parameters
   200  	Login *nodes.LoginSpec `json:"login,omitempty"`
   201  	// System disk parameter of the node
   202  	RootVolume *nodes.VolumeSpec `json:"rootVolume,omitempty"`
   203  	// The data disk parameter of the node must currently be a disk
   204  	DataVolumes []nodes.VolumeSpec `json:"dataVolumes,omitempty"`
   205  	// Disk initialization configuration management parameters
   206  	// If omit, disk management is performed according to the DockerLVMConfigOverride parameter in extendParam
   207  	Storage *nodes.StorageSpec `json:"storage,omitempty"`
   208  	// Elastic IP parameters of the node
   209  	PublicIP *nodes.PublicIPSpec `json:"publicIP,omitempty"`
   210  	// The billing mode of the node: the value is 0 (on demand)
   211  	BillingMode int `json:"billingMode,omitempty"`
   212  	// Number of nodes when creating in batch
   213  	Count int `json:"count,omitempty"`
   214  	// The node nic spec
   215  	NodeNicSpec *nodes.NodeNicSpec `json:"nodeNicSpec,omitempty"`
   216  	// Extended parameter
   217  	ExtendParam map[string]interface{} `json:"extendParam,omitempty"`
   218  	// UUID of an ECS group
   219  	EcsGroupID string `json:"ecsGroupId,omitempty"`
   220  	// Tag of a VM, key value pair format
   221  	UserTags []tags.ResourceTag `json:"userTags,omitempty"`
   222  	// Tag of a Kubernetes node, key value pair format
   223  	K8sTags map[string]string `json:"k8sTags,omitempty"`
   224  	// The runtime spec
   225  	RunTime *nodes.RunTimeSpec `json:"runtime,omitempty"`
   226  	// taints to created nodes to configure anti-affinity
   227  	Taints []nodes.TaintSpec `json:"taints,omitempty"`
   228  	// The name of the created partition
   229  	Partition string `json:"partition,omitempty"`
   230  	// The initialized conditions
   231  	InitializedConditions []string `json:"initializedConditions,omitempty"`
   232  }
   233  
   234  // UpdateSpec describes Node pools update specification
   235  type UpdateSpec struct {
   236  	// Node type. Currently, only VM nodes are supported.
   237  	Type string `json:"type,omitempty"`
   238  	// Node template
   239  	NodeTemplate UpdateNodeTemplate `json:"nodeTemplate"`
   240  	// Initial number of expected nodes
   241  	InitialNodeCount *int `json:"initialNodeCount" required:"true"`
   242  	// Auto scaling parameters
   243  	Autoscaling AutoscalingSpec `json:"autoscaling"`
   244  }
   245  
   246  // ToNodePoolUpdateMap builds an update body based on UpdateOpts.
   247  func (opts UpdateOpts) ToNodePoolUpdateMap() (map[string]interface{}, error) {
   248  	return golangsdk.BuildRequestBody(opts, "")
   249  }
   250  
   251  // Update allows node pools to be updated.
   252  func Update(c *golangsdk.ServiceClient, clusterid, nodepoolid string, opts UpdateOptsBuilder) (r UpdateResult) {
   253  	b, err := opts.ToNodePoolUpdateMap()
   254  	if err != nil {
   255  		r.Err = err
   256  		return
   257  	}
   258  	_, r.Err = c.Put(resourceURL(c, clusterid, nodepoolid), b, &r.Body, &golangsdk.RequestOpts{
   259  		OkCodes: []int{200},
   260  	})
   261  	return
   262  }
   263  
   264  // Delete will permanently delete a particular node pool based on its unique ID and cluster ID.
   265  func Delete(c *golangsdk.ServiceClient, clusterid, nodepoolid string) (r DeleteResult) {
   266  	_, r.Err = c.Delete(resourceURL(c, clusterid, nodepoolid), &golangsdk.RequestOpts{
   267  		OkCodes:     []int{200},
   268  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   269  	})
   270  	return
   271  }