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

     1  package containers
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // ContainerType represents the valid types of containers.
    11  type ContainerType string
    12  
    13  const (
    14  	GenericContainer     ContainerType = "generic"
    15  	RSAContainer         ContainerType = "rsa"
    16  	CertificateContainer ContainerType = "certificate"
    17  )
    18  
    19  // ListOptsBuilder allows extensions to add additional parameters to
    20  // the List request
    21  type ListOptsBuilder interface {
    22  	ToContainerListQuery() (string, error)
    23  }
    24  
    25  // ListOpts provides options to filter the List results.
    26  type ListOpts struct {
    27  	// Limit is the amount of containers to retrieve.
    28  	Limit int `q:"limit"`
    29  
    30  	// Name is the name of the container
    31  	Name string `q:"name"`
    32  
    33  	// Offset is the index within the list to retrieve.
    34  	Offset int `q:"offset"`
    35  }
    36  
    37  // ToContainerListQuery formats a ListOpts into a query string.
    38  func (opts ListOpts) ToContainerListQuery() (string, error) {
    39  	q, err := gophercloud.BuildQueryString(opts)
    40  	return q.String(), err
    41  }
    42  
    43  // List retrieves a list of containers.
    44  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    45  	url := listURL(client)
    46  	if opts != nil {
    47  		query, err := opts.ToContainerListQuery()
    48  		if err != nil {
    49  			return pagination.Pager{Err: err}
    50  		}
    51  		url += query
    52  	}
    53  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    54  		return ContainerPage{pagination.LinkedPageBase{PageResult: r}}
    55  	})
    56  }
    57  
    58  // Get retrieves details of a container.
    59  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    60  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
    61  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    62  	return
    63  }
    64  
    65  // CreateOptsBuilder allows extensions to add additional parameters to
    66  // the Create request.
    67  type CreateOptsBuilder interface {
    68  	ToContainerCreateMap() (map[string]any, error)
    69  }
    70  
    71  // CreateOpts provides options used to create a container.
    72  type CreateOpts struct {
    73  	// Type represents the type of container.
    74  	Type ContainerType `json:"type" required:"true"`
    75  
    76  	// Name is the name of the container.
    77  	Name string `json:"name"`
    78  
    79  	// SecretRefs is a list of secret refs for the container.
    80  	SecretRefs []SecretRef `json:"secret_refs,omitempty"`
    81  }
    82  
    83  // ToContainerCreateMap formats a CreateOpts into a create request.
    84  func (opts CreateOpts) ToContainerCreateMap() (map[string]any, error) {
    85  	return gophercloud.BuildRequestBody(opts, "")
    86  }
    87  
    88  // Create creates a new container.
    89  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    90  	b, err := opts.ToContainerCreateMap()
    91  	if err != nil {
    92  		r.Err = err
    93  		return
    94  	}
    95  	resp, err := client.Post(ctx, createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
    96  		OkCodes: []int{201},
    97  	})
    98  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    99  	return
   100  }
   101  
   102  // Delete deletes a container.
   103  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   104  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
   105  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   106  	return
   107  }
   108  
   109  // ListConsumersOptsBuilder allows extensions to add additional parameters to
   110  // the ListConsumers request
   111  type ListConsumersOptsBuilder interface {
   112  	ToContainerListConsumersQuery() (string, error)
   113  }
   114  
   115  // ListConsumersOpts provides options to filter the List results.
   116  type ListConsumersOpts struct {
   117  	// Limit is the amount of consumers to retrieve.
   118  	Limit int `q:"limit"`
   119  
   120  	// Offset is the index within the list to retrieve.
   121  	Offset int `q:"offset"`
   122  }
   123  
   124  // ToContainerListConsumersQuery formats a ListConsumersOpts into a query
   125  // string.
   126  func (opts ListOpts) ToContainerListConsumersQuery() (string, error) {
   127  	q, err := gophercloud.BuildQueryString(opts)
   128  	return q.String(), err
   129  }
   130  
   131  // ListConsumers retrieves a list of consumers from a container.
   132  func ListConsumers(client *gophercloud.ServiceClient, containerID string, opts ListConsumersOptsBuilder) pagination.Pager {
   133  	url := listConsumersURL(client, containerID)
   134  	if opts != nil {
   135  		query, err := opts.ToContainerListConsumersQuery()
   136  		if err != nil {
   137  			return pagination.Pager{Err: err}
   138  		}
   139  		url += query
   140  	}
   141  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   142  		return ConsumerPage{pagination.LinkedPageBase{PageResult: r}}
   143  	})
   144  }
   145  
   146  // CreateConsumerOptsBuilder allows extensions to add additional parameters to
   147  // the Create request.
   148  type CreateConsumerOptsBuilder interface {
   149  	ToContainerConsumerCreateMap() (map[string]any, error)
   150  }
   151  
   152  // CreateConsumerOpts provides options used to create a container.
   153  type CreateConsumerOpts struct {
   154  	// Name is the name of the consumer.
   155  	Name string `json:"name"`
   156  
   157  	// URL is the URL to the consumer resource.
   158  	URL string `json:"URL"`
   159  }
   160  
   161  // ToContainerConsumerCreateMap formats a CreateConsumerOpts into a create
   162  // request.
   163  func (opts CreateConsumerOpts) ToContainerConsumerCreateMap() (map[string]any, error) {
   164  	return gophercloud.BuildRequestBody(opts, "")
   165  }
   166  
   167  // CreateConsumer creates a new consumer.
   168  func CreateConsumer(ctx context.Context, client *gophercloud.ServiceClient, containerID string, opts CreateConsumerOptsBuilder) (r CreateConsumerResult) {
   169  	b, err := opts.ToContainerConsumerCreateMap()
   170  	if err != nil {
   171  		r.Err = err
   172  		return
   173  	}
   174  	resp, err := client.Post(ctx, createConsumerURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
   175  		OkCodes: []int{200},
   176  	})
   177  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   178  	return
   179  }
   180  
   181  // DeleteConsumerOptsBuilder allows extensions to add additional parameters to
   182  // the Delete request.
   183  type DeleteConsumerOptsBuilder interface {
   184  	ToContainerConsumerDeleteMap() (map[string]any, error)
   185  }
   186  
   187  // DeleteConsumerOpts represents options used for deleting a consumer.
   188  type DeleteConsumerOpts struct {
   189  	// Name is the name of the consumer.
   190  	Name string `json:"name"`
   191  
   192  	// URL is the URL to the consumer resource.
   193  	URL string `json:"URL"`
   194  }
   195  
   196  // ToContainerConsumerDeleteMap formats a DeleteConsumerOpts into a create
   197  // request.
   198  func (opts DeleteConsumerOpts) ToContainerConsumerDeleteMap() (map[string]any, error) {
   199  	return gophercloud.BuildRequestBody(opts, "")
   200  }
   201  
   202  // DeleteConsumer deletes a consumer.
   203  func DeleteConsumer(ctx context.Context, client *gophercloud.ServiceClient, containerID string, opts DeleteConsumerOptsBuilder) (r DeleteConsumerResult) {
   204  	url := deleteConsumerURL(client, containerID)
   205  
   206  	b, err := opts.ToContainerConsumerDeleteMap()
   207  	if err != nil {
   208  		r.Err = err
   209  		return
   210  	}
   211  
   212  	resp, err := client.Request(ctx, "DELETE", url, &gophercloud.RequestOpts{
   213  		JSONBody:     b,
   214  		JSONResponse: &r.Body,
   215  		OkCodes:      []int{200},
   216  	})
   217  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   218  	return
   219  }
   220  
   221  // SecretRefBuilder allows extensions to add additional parameters to the
   222  // Create request.
   223  type SecretRefBuilder interface {
   224  	ToContainerSecretRefMap() (map[string]any, error)
   225  }
   226  
   227  // ToContainerSecretRefMap formats a SecretRefBuilder into a create
   228  // request.
   229  func (opts SecretRef) ToContainerSecretRefMap() (map[string]any, error) {
   230  	return gophercloud.BuildRequestBody(opts, "")
   231  }
   232  
   233  // CreateSecret creates a new consumer.
   234  func CreateSecretRef(ctx context.Context, client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r CreateSecretRefResult) {
   235  	b, err := opts.ToContainerSecretRefMap()
   236  	if err != nil {
   237  		r.Err = err
   238  		return
   239  	}
   240  	resp, err := client.Post(ctx, createSecretRefURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
   241  		OkCodes: []int{201},
   242  	})
   243  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   244  	return
   245  }
   246  
   247  // DeleteSecret deletes a consumer.
   248  func DeleteSecretRef(ctx context.Context, client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r DeleteSecretRefResult) {
   249  	url := deleteSecretRefURL(client, containerID)
   250  
   251  	b, err := opts.ToContainerSecretRefMap()
   252  	if err != nil {
   253  		r.Err = err
   254  		return
   255  	}
   256  
   257  	resp, err := client.Request(ctx, "DELETE", url, &gophercloud.RequestOpts{
   258  		JSONBody: b,
   259  		OkCodes:  []int{204},
   260  	})
   261  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   262  	return
   263  }