github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/vpcep/v1/services/requests.go (about)

     1  package services
     2  
     3  import (
     4  	"fmt"
     5  
     6  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     9  )
    10  
    11  type PortMapping struct {
    12  	// Specifies the port for accessing the VPC endpoint.
    13  	ClientPort int `json:"client_port"`
    14  	// Specifies the port for accessing the VPC endpoint service.
    15  	ServerPort int `json:"server_port"`
    16  	// Specifies the protocol used in port mappings. The value can be TCP or UDP. The default value is TCP.
    17  	Protocol string `json:"protocol"`
    18  }
    19  
    20  type CreateOpts struct {
    21  	// Specifies the ID for identifying the backend resource of the VPC endpoint service.
    22  	// The ID is in the form of the UUID.
    23  	PortID string `json:"port_id" required:"true"`
    24  
    25  	// Specifies the ID of the cluster associated with the target VPCEP resource.
    26  	PoolID string `json:"pool_id,omitempty"`
    27  
    28  	// Specifies the ID of the virtual NIC to which the virtual IP address is bound.
    29  	VIPPortID string `json:"vip_port_id,omitempty"`
    30  
    31  	// Specifies the name of the VPC endpoint service.
    32  	// The value contains a maximum of 16 characters, including letters, digits, underscores (_), and hyphens (-).
    33  	//
    34  	//  If you do not specify this parameter, the VPC endpoint service name is in the format: `regionName.serviceId`.
    35  	//  If you specify this parameter, the VPC endpoint service name is in the format: `regionName.serviceName.serviceId`.
    36  	ServiceName string `json:"service_name,omitempty"`
    37  
    38  	// Specifies the ID of the VPC (router) to which the backend resource of the VPC endpoint service belongs.
    39  	RouterID string `json:"vpc_id" required:"true"`
    40  
    41  	// Specifies whether connection approval is required.
    42  	// The default value is `true`.
    43  	ApprovalEnabled *bool `json:"approval_enabled,omitempty"`
    44  
    45  	// Specifies the type of the VPC endpoint service.
    46  	// Only your private services can be configured into interface VPC endpoint services.
    47  	ServiceType ServiceType `json:"service_type,omitempty"`
    48  
    49  	// Specifies the backend resource type.
    50  	//  - `VM`: Resource is an ECS. Backend resources of this type serve as servers.
    51  	//  - `VIP`: Resource is a virtual IP address that functions as a physical server hosting virtual resources.
    52  	//  - `LB`: Resource is an enhanced load balancer.
    53  	ServerType ServerType `json:"server_type" required:"true"`
    54  
    55  	// Lists the port mappings opened to the VPC endpoint service.
    56  	Ports []PortMapping `json:"ports" required:"true"`
    57  
    58  	// Specifies whether the client IP address and port number or `marker_id` information is transmitted to the server.
    59  	//
    60  	// The values are as follows:
    61  	//    close: indicates that the TOA and Proxy Protocol methods are neither used.
    62  	//    toa_open: indicates that the TOA method is used.
    63  	//    proxy_open: indicates that the Proxy Protocol method is used.
    64  	//    open: indicates that the TOA and Proxy Protocol methods are both used.
    65  	// The default value is close.
    66  	TCPProxy string `json:"tcp_proxy,omitempty"`
    67  
    68  	// Lists the resource tags.
    69  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    70  }
    71  
    72  type CreateOptsBuilder interface {
    73  	ToServiceCreateMap() (map[string]interface{}, error)
    74  }
    75  
    76  func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) {
    77  	return golangsdk.BuildRequestBody(opts, "")
    78  }
    79  
    80  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    81  	b, err := opts.ToServiceCreateMap()
    82  	if err != nil {
    83  		r.Err = err
    84  		return
    85  	}
    86  	_, r.Err = client.Post(baseURL(client), b, &r.Body, &golangsdk.RequestOpts{
    87  		OkCodes: []int{200, 201},
    88  	})
    89  	return
    90  }
    91  
    92  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    93  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
    94  	return
    95  }
    96  
    97  type ListOptsBuilder interface {
    98  	ToServiceListQuery() (string, error)
    99  }
   100  
   101  type ListOpts struct {
   102  	// Specifies the name of the VPC endpoint service. The value is not case-sensitive and supports fuzzy match.
   103  	Name string `q:"endpoint_service_name"`
   104  	// Specifies the unique ID of the VPC endpoint service.
   105  	ID string `q:"id"`
   106  	// Specifies the status of the VPC endpoint service.
   107  	//
   108  	//    creating: indicates the VPC endpoint service is being created.
   109  	//    available: indicates the VPC endpoint service is connectable.
   110  	//    failed: indicates the creation of the VPC endpoint service failed.
   111  	//    deleting: indicates the VPC endpoint service is being deleted.
   112  	Status Status `q:"status"`
   113  
   114  	SortKey string `q:"sort_key"`
   115  	SortDir string `q:"sort_dir"`
   116  }
   117  
   118  func (opts ListOpts) ToServiceListQuery() (string, error) {
   119  	q, err := golangsdk.BuildQueryString(opts)
   120  	if err != nil {
   121  		return "", err
   122  	}
   123  	return q.String(), nil
   124  }
   125  
   126  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   127  	url := baseURL(client)
   128  	if opts != nil {
   129  		q, err := opts.ToServiceListQuery()
   130  		if err != nil {
   131  			return pagination.Pager{Err: err}
   132  		}
   133  		url += q
   134  	}
   135  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   136  		return ServicePage{pagination.OffsetPageBase{PageResult: r}}
   137  	})
   138  }
   139  
   140  func ListPublic(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   141  	url := publicURL(client)
   142  	if opts != nil {
   143  		q, err := opts.ToServiceListQuery()
   144  		if err != nil {
   145  			return pagination.Pager{Err: err}
   146  		}
   147  		url += q
   148  	}
   149  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   150  		return ServicePage{pagination.OffsetPageBase{PageResult: r}}
   151  	})
   152  }
   153  
   154  type UpdateOptsBuilder interface {
   155  	ToServiceUpdateMap() (map[string]interface{}, error)
   156  }
   157  
   158  type UpdateOpts struct {
   159  	ApprovalEnabled *bool         `json:"approval_enabled,omitempty"`
   160  	ServiceName     string        `json:"service_name,omitempty"`
   161  	Ports           []PortMapping `json:"ports,omitempty"`
   162  	PortID          string        `json:"port_id,omitempty"`
   163  	VIPPortID       string        `json:"vip_port_id,omitempty"`
   164  }
   165  
   166  func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
   167  	return golangsdk.BuildRequestBody(opts, "")
   168  }
   169  
   170  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   171  	b, err := opts.ToServiceUpdateMap()
   172  	if err != nil {
   173  		r.Err = err
   174  		return
   175  	}
   176  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   177  		OkCodes: []int{200, 201},
   178  	})
   179  	return
   180  }
   181  
   182  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   183  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   184  	return
   185  }
   186  
   187  func WaitForServiceStatus(client *golangsdk.ServiceClient, id string, status Status, timeout int) error {
   188  	return golangsdk.WaitFor(timeout, func() (bool, error) {
   189  		srv, err := Get(client, id).Extract()
   190  		if err != nil {
   191  			if _, ok := err.(golangsdk.ErrDefault404); ok && status == StatusDeleted {
   192  				return true, nil
   193  			}
   194  			return false, fmt.Errorf("error waiting for service to have status %s: %w", status, err)
   195  		}
   196  		if srv.Status == status {
   197  			return true, nil
   198  		}
   199  		return false, nil
   200  	})
   201  }