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

     1  package clustertemplates
     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  	APIServerPort       *int              `json:"apiserver_port,omitempty"`
    18  	COE                 string            `json:"coe" required:"true"`
    19  	DNSNameServer       string            `json:"dns_nameserver,omitempty"`
    20  	DockerStorageDriver string            `json:"docker_storage_driver,omitempty"`
    21  	DockerVolumeSize    *int              `json:"docker_volume_size,omitempty"`
    22  	ExternalNetworkID   string            `json:"external_network_id,omitempty"`
    23  	FixedNetwork        string            `json:"fixed_network,omitempty"`
    24  	FixedSubnet         string            `json:"fixed_subnet,omitempty"`
    25  	FlavorID            string            `json:"flavor_id,omitempty"`
    26  	FloatingIPEnabled   *bool             `json:"floating_ip_enabled,omitempty"`
    27  	HTTPProxy           string            `json:"http_proxy,omitempty"`
    28  	HTTPSProxy          string            `json:"https_proxy,omitempty"`
    29  	ImageID             string            `json:"image_id" required:"true"`
    30  	InsecureRegistry    string            `json:"insecure_registry,omitempty"`
    31  	KeyPairID           string            `json:"keypair_id,omitempty"`
    32  	Labels              map[string]string `json:"labels,omitempty"`
    33  	MasterFlavorID      string            `json:"master_flavor_id,omitempty"`
    34  	MasterLBEnabled     *bool             `json:"master_lb_enabled,omitempty"`
    35  	Name                string            `json:"name,omitempty"`
    36  	NetworkDriver       string            `json:"network_driver,omitempty"`
    37  	NoProxy             string            `json:"no_proxy,omitempty"`
    38  	Public              *bool             `json:"public,omitempty"`
    39  	RegistryEnabled     *bool             `json:"registry_enabled,omitempty"`
    40  	ServerType          string            `json:"server_type,omitempty"`
    41  	TLSDisabled         *bool             `json:"tls_disabled,omitempty"`
    42  	VolumeDriver        string            `json:"volume_driver,omitempty"`
    43  	Hidden              *bool             `json:"hidden,omitempty"`
    44  }
    45  
    46  // ToClusterCreateMap constructs a request body from CreateOpts.
    47  func (opts CreateOpts) ToClusterCreateMap() (map[string]any, error) {
    48  	return gophercloud.BuildRequestBody(opts, "")
    49  }
    50  
    51  // Create requests the creation of a new cluster.
    52  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    53  	b, err := opts.ToClusterCreateMap()
    54  	if err != nil {
    55  		r.Err = err
    56  		return
    57  	}
    58  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    59  		OkCodes: []int{201},
    60  	})
    61  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    62  	return
    63  }
    64  
    65  // Delete deletes the specified cluster ID.
    66  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    67  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
    68  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    69  	return
    70  }
    71  
    72  // ListOptsBuilder allows extensions to add additional parameters to the
    73  // List request.
    74  type ListOptsBuilder interface {
    75  	ToClusterTemplateListQuery() (string, error)
    76  }
    77  
    78  // ListOpts allows the sorting of paginated collections through
    79  // the API. SortKey allows you to sort by a particular cluster templates attribute.
    80  // SortDir sets the direction, and is either `asc' or `desc'.
    81  // Marker and Limit are used for pagination.
    82  type ListOpts struct {
    83  	Marker  string `q:"marker"`
    84  	Limit   int    `q:"limit"`
    85  	SortKey string `q:"sort_key"`
    86  	SortDir string `q:"sort_dir"`
    87  }
    88  
    89  // ToClusterTemplateListQuery formats a ListOpts into a query string.
    90  func (opts ListOpts) ToClusterTemplateListQuery() (string, error) {
    91  	q, err := gophercloud.BuildQueryString(opts)
    92  	return q.String(), err
    93  }
    94  
    95  // List returns a Pager which allows you to iterate over a collection of
    96  // cluster-templates. It accepts a ListOptsBuilder, which allows you to sort
    97  // the returned collection for greater efficiency.
    98  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    99  	url := listURL(client)
   100  	if opts != nil {
   101  		query, err := opts.ToClusterTemplateListQuery()
   102  		if err != nil {
   103  			return pagination.Pager{Err: err}
   104  		}
   105  		url += query
   106  	}
   107  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   108  		return ClusterTemplatePage{pagination.LinkedPageBase{PageResult: r}}
   109  	})
   110  }
   111  
   112  // Get retrieves a specific cluster-template based on its unique ID.
   113  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
   114  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
   115  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   116  	return
   117  }
   118  
   119  type UpdateOp string
   120  
   121  const (
   122  	AddOp     UpdateOp = "add"
   123  	RemoveOp  UpdateOp = "remove"
   124  	ReplaceOp UpdateOp = "replace"
   125  )
   126  
   127  type UpdateOpts struct {
   128  	Op    UpdateOp `json:"op" required:"true"`
   129  	Path  string   `json:"path" required:"true"`
   130  	Value any      `json:"value,omitempty"`
   131  }
   132  
   133  // UpdateOptsBuilder allows extensions to add additional parameters to the
   134  // Update request.
   135  type UpdateOptsBuilder interface {
   136  	ToClusterTemplateUpdateMap() (map[string]any, error)
   137  }
   138  
   139  // ToClusterUpdateMap assembles a request body based on the contents of
   140  // UpdateOpts.
   141  func (opts UpdateOpts) ToClusterTemplateUpdateMap() (map[string]any, error) {
   142  	b, err := gophercloud.BuildRequestBody(opts, "")
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return b, nil
   148  }
   149  
   150  // Update implements cluster updated request.
   151  func Update[T UpdateOptsBuilder](ctx context.Context, client *gophercloud.ServiceClient, id string, opts []T) (r UpdateResult) {
   152  	var o []map[string]any
   153  	for _, opt := range opts {
   154  		b, err := opt.ToClusterTemplateUpdateMap()
   155  		if err != nil {
   156  			r.Err = err
   157  			return r
   158  		}
   159  		o = append(o, b)
   160  	}
   161  	resp, err := client.Patch(ctx, updateURL(client, id), o, &r.Body, &gophercloud.RequestOpts{
   162  		OkCodes: []int{200, 202},
   163  	})
   164  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   165  	return
   166  }