github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/containerinfra/v1/clusters/requests.go (about)

     1  package clusters
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder Builder.
    11  type CreateOptsBuilder interface {
    12  	ToClusterCreateMap() (map[string]any, error)
    13  }
    14  
    15  // CreateOpts params
    16  type CreateOpts struct {
    17  	ClusterTemplateID string            `json:"cluster_template_id" required:"true"`
    18  	CreateTimeout     *int              `json:"create_timeout"`
    19  	DiscoveryURL      string            `json:"discovery_url,omitempty"`
    20  	DockerVolumeSize  *int              `json:"docker_volume_size,omitempty"`
    21  	FlavorID          string            `json:"flavor_id,omitempty"`
    22  	Keypair           string            `json:"keypair,omitempty"`
    23  	Labels            map[string]string `json:"labels,omitempty"`
    24  	MasterCount       *int              `json:"master_count,omitempty"`
    25  	MasterFlavorID    string            `json:"master_flavor_id,omitempty"`
    26  	Name              string            `json:"name"`
    27  	NodeCount         *int              `json:"node_count,omitempty"`
    28  	FloatingIPEnabled *bool             `json:"floating_ip_enabled,omitempty"`
    29  	MasterLBEnabled   *bool             `json:"master_lb_enabled,omitempty"`
    30  	FixedNetwork      string            `json:"fixed_network,omitempty"`
    31  	FixedSubnet       string            `json:"fixed_subnet,omitempty"`
    32  	MergeLabels       *bool             `json:"merge_labels,omitempty"`
    33  }
    34  
    35  // ToClusterCreateMap constructs a request body from CreateOpts.
    36  func (opts CreateOpts) ToClusterCreateMap() (map[string]any, error) {
    37  	return gophercloud.BuildRequestBody(opts, "")
    38  }
    39  
    40  // Create requests the creation of a new cluster.
    41  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    42  	b, err := opts.ToClusterCreateMap()
    43  	if err != nil {
    44  		r.Err = err
    45  		return
    46  	}
    47  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    48  		OkCodes: []int{202},
    49  	})
    50  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    51  	return
    52  }
    53  
    54  // Get retrieves a specific clusters based on its unique ID.
    55  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    56  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
    57  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    58  	return
    59  }
    60  
    61  // Delete deletes the specified cluster ID.
    62  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    63  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
    64  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    65  	return
    66  }
    67  
    68  // ListOptsBuilder allows extensions to add additional parameters to the
    69  // List request.
    70  type ListOptsBuilder interface {
    71  	ToClustersListQuery() (string, error)
    72  }
    73  
    74  // ListOpts allows the sorting of paginated collections through
    75  // the API. SortKey allows you to sort by a particular cluster attribute.
    76  // SortDir sets the direction, and is either `asc' or `desc'.
    77  // Marker and Limit are used for pagination.
    78  type ListOpts struct {
    79  	Marker  string `q:"marker"`
    80  	Limit   int    `q:"limit"`
    81  	SortKey string `q:"sort_key"`
    82  	SortDir string `q:"sort_dir"`
    83  }
    84  
    85  // ToClustersListQuery formats a ListOpts into a query string.
    86  func (opts ListOpts) ToClustersListQuery() (string, error) {
    87  	q, err := gophercloud.BuildQueryString(opts)
    88  	return q.String(), err
    89  }
    90  
    91  // List returns a Pager which allows you to iterate over a collection of
    92  // clusters. It accepts a ListOptsBuilder, which allows you to sort
    93  // the returned collection for greater efficiency.
    94  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    95  	url := listURL(c)
    96  	if opts != nil {
    97  		query, err := opts.ToClustersListQuery()
    98  		if err != nil {
    99  			return pagination.Pager{Err: err}
   100  		}
   101  		url += query
   102  	}
   103  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   104  		return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
   105  	})
   106  }
   107  
   108  // ListDetail returns a Pager which allows you to iterate over a collection of
   109  // clusters with detailed information.
   110  // It accepts a ListOptsBuilder, which allows you to sort the returned
   111  // collection for greater efficiency.
   112  func ListDetail(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   113  	url := listDetailURL(c)
   114  	if opts != nil {
   115  		query, err := opts.ToClustersListQuery()
   116  		if err != nil {
   117  			return pagination.Pager{Err: err}
   118  		}
   119  		url += query
   120  	}
   121  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   122  		return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
   123  	})
   124  }
   125  
   126  type UpdateOp string
   127  
   128  const (
   129  	AddOp     UpdateOp = "add"
   130  	RemoveOp  UpdateOp = "remove"
   131  	ReplaceOp UpdateOp = "replace"
   132  )
   133  
   134  type UpdateOpts struct {
   135  	Op    UpdateOp `json:"op" required:"true"`
   136  	Path  string   `json:"path" required:"true"`
   137  	Value any      `json:"value,omitempty"`
   138  }
   139  
   140  // UpdateOptsBuilder allows extensions to add additional parameters to the
   141  // Update request.
   142  type UpdateOptsBuilder interface {
   143  	ToClustersUpdateMap() (map[string]any, error)
   144  }
   145  
   146  // ToClusterUpdateMap assembles a request body based on the contents of
   147  // UpdateOpts.
   148  func (opts UpdateOpts) ToClustersUpdateMap() (map[string]any, error) {
   149  	return gophercloud.BuildRequestBody(opts, "")
   150  }
   151  
   152  // Update implements cluster updated request.
   153  func Update[T UpdateOptsBuilder](ctx context.Context, client *gophercloud.ServiceClient, id string, opts []T) (r UpdateResult) {
   154  	var o []map[string]any
   155  	for _, opt := range opts {
   156  		b, err := opt.ToClustersUpdateMap()
   157  		if err != nil {
   158  			r.Err = err
   159  			return r
   160  		}
   161  		o = append(o, b)
   162  	}
   163  	resp, err := client.Patch(ctx, updateURL(client, id), o, &r.Body, &gophercloud.RequestOpts{
   164  		OkCodes: []int{200, 202},
   165  	})
   166  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   167  	return
   168  }
   169  
   170  type UpgradeOpts struct {
   171  	ClusterTemplate string `json:"cluster_template" required:"true"`
   172  	MaxBatchSize    *int   `json:"max_batch_size,omitempty"`
   173  	NodeGroup       string `json:"nodegroup,omitempty"`
   174  }
   175  
   176  // UpgradeOptsBuilder allows extensions to add additional parameters to the
   177  // Upgrade request.
   178  type UpgradeOptsBuilder interface {
   179  	ToClustersUpgradeMap() (map[string]any, error)
   180  }
   181  
   182  // ToClustersUpgradeMap constructs a request body from UpgradeOpts.
   183  func (opts UpgradeOpts) ToClustersUpgradeMap() (map[string]any, error) {
   184  	if opts.MaxBatchSize == nil {
   185  		defaultMaxBatchSize := 1
   186  		opts.MaxBatchSize = &defaultMaxBatchSize
   187  	}
   188  	return gophercloud.BuildRequestBody(opts, "")
   189  }
   190  
   191  // Upgrade implements cluster upgrade request.
   192  func Upgrade(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpgradeOptsBuilder) (r UpgradeResult) {
   193  	b, err := opts.ToClustersUpgradeMap()
   194  	if err != nil {
   195  		r.Err = err
   196  		return
   197  	}
   198  
   199  	resp, err := client.Post(ctx, upgradeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   200  		OkCodes: []int{202},
   201  	})
   202  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   203  	return
   204  }
   205  
   206  // ResizeOptsBuilder allows extensions to add additional parameters to the
   207  // Resize request.
   208  type ResizeOptsBuilder interface {
   209  	ToClusterResizeMap() (map[string]any, error)
   210  }
   211  
   212  // ResizeOpts params
   213  type ResizeOpts struct {
   214  	NodeCount     *int     `json:"node_count" required:"true"`
   215  	NodesToRemove []string `json:"nodes_to_remove,omitempty"`
   216  	NodeGroup     string   `json:"nodegroup,omitempty"`
   217  }
   218  
   219  // ToClusterResizeMap constructs a request body from ResizeOpts.
   220  func (opts ResizeOpts) ToClusterResizeMap() (map[string]any, error) {
   221  	return gophercloud.BuildRequestBody(opts, "")
   222  }
   223  
   224  // Resize an existing cluster node count.
   225  func Resize(ctx context.Context, client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ResizeResult) {
   226  	b, err := opts.ToClusterResizeMap()
   227  	if err != nil {
   228  		r.Err = err
   229  		return
   230  	}
   231  	resp, err := client.Post(ctx, resizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   232  		OkCodes: []int{200, 202},
   233  	})
   234  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   235  	return
   236  }