github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/vpcep/v1/services/requests.go (about)

     1  package services
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/common/tags"
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  // PostOptsBuilder allows extensions to add parameters to the
    10  // Post request.
    11  type PostOptsBuilder interface {
    12  	ToServicePostMap() (map[string]interface{}, error)
    13  }
    14  
    15  // CreateOpts contains the options for create a VPC endpoint service.
    16  // This object is passed to Create().
    17  type CreateOpts struct {
    18  	// Specifies the ID of the VPC to which the backend resource of the VPC endpoint service belongs.
    19  	VpcID string `json:"vpc_id" required:"true"`
    20  	// Specifies the ID for identifying the backend resource of the VPC endpoint service.
    21  	PortID string `json:"port_id" required:"true"`
    22  	// Specifies the resource type.
    23  	ServerType string `json:"server_type" required:"true"`
    24  	// Lists the port mappings opened to the VPC endpoint service.
    25  	Ports []PortOpts `json:"ports" required:"true"`
    26  
    27  	// Specifies the name of the VPC endpoint service.
    28  	// The value contains a maximum of 16 characters, including letters, digits, underscores (_), and hyphens (-).
    29  	ServiceName string `json:"service_name,omitempty"`
    30  	// Specifies the type of the VPC endpoint service, only interface is valid.
    31  	ServiceType string `json:"service_type,omitempty"`
    32  	// Specifies whether connection approval is required.
    33  	Approval *bool `json:"approval_enabled,omitempty"`
    34  	// Specifies the ID of the virtual NIC to which the virtual IP address is bound.
    35  	VipPortID string `json:"vip_port_id,omitempty"`
    36  	// Specifies whether the client IP address and port number or marker_id information is transmitted to the server.
    37  	TCPProxy string `json:"tcp_proxy,omitempty"`
    38  	// Specifies the resource tags in key/value format
    39  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    40  	// Specifies the description
    41  	Description string `json:"description,omitempty"`
    42  	// Specifies whether the VPC endpoint policy is enabled.
    43  	EnablePolicy *bool `json:"enable_policy,omitempty"`
    44  }
    45  
    46  // PortOpts contains the port mappings opened to the VPC endpoint service.
    47  type PortOpts struct {
    48  	// Specifies the protocol used in port mappings. The value can be TCP or UDP. The default value is TCP.
    49  	Protocol string `json:"protocol,omitempty"`
    50  	// Specifies the port for accessing the VPC endpoint.
    51  	ClientPort int `json:"client_port,omitempty"`
    52  	// Specifies the port for accessing the VPC endpoint service.
    53  	ServerPort int `json:"server_port,omitempty"`
    54  }
    55  
    56  // ToServicePostMap assembles a request body based on the contents of a CreateOpts.
    57  func (opts CreateOpts) ToServicePostMap() (map[string]interface{}, error) {
    58  	return golangsdk.BuildRequestBody(opts, "")
    59  }
    60  
    61  // Create accepts a CreateOpts struct and uses the values to create a new
    62  // VPC endpoint service.
    63  func Create(c *golangsdk.ServiceClient, opts PostOptsBuilder) (r CreateResult) {
    64  	b, err := opts.ToServicePostMap()
    65  	if err != nil {
    66  		r.Err = err
    67  		return
    68  	}
    69  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    70  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    71  	return
    72  }
    73  
    74  // Get retrieves a particular nodes based on its unique ID and cluster ID.
    75  func Get(c *golangsdk.ServiceClient, serviceID string) (r GetResult) {
    76  	_, r.Err = c.Get(resourceURL(c, serviceID), &r.Body, nil)
    77  	return
    78  }
    79  
    80  // UpdateOptsBuilder allows extensions to add parameters to the
    81  // Update request.
    82  type UpdateOptsBuilder interface {
    83  	ToServiceUpdateMap() (map[string]interface{}, error)
    84  }
    85  
    86  // UpdateOpts contains all the values needed to update a VPC endpoint service
    87  type UpdateOpts struct {
    88  	// Specifies the name of the VPC endpoint service.
    89  	ServiceName string `json:"service_name,omitempty"`
    90  	// Specifies whether connection approval is required.
    91  	Approval *bool `json:"approval_enabled,omitempty"`
    92  	// Specifies the ID for identifying the backend resource of the VPC endpoint service.
    93  	PortID string `json:"port_id,omitempty"`
    94  	// Lists the port mappings opened to the VPC endpoint service.
    95  	Ports []PortOpts `json:"ports,omitempty"`
    96  	// Specifies the ID of the virtual NIC to which the virtual IP address is bound.
    97  	VipPortID string `json:"vip_port_id,omitempty"`
    98  	// Specifies the description
    99  	Description *string `json:"description,omitempty"`
   100  }
   101  
   102  // ToServiceUpdateMap builds an update body based on UpdateOpts.
   103  func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
   104  	return golangsdk.BuildRequestBody(opts, "")
   105  }
   106  
   107  // Update allows a VPC endpoint service to be updated.
   108  func Update(c *golangsdk.ServiceClient, serviceID string, opts UpdateOptsBuilder) (r UpdateResult) {
   109  	b, err := opts.ToServiceUpdateMap()
   110  	if err != nil {
   111  		r.Err = err
   112  		return
   113  	}
   114  	_, r.Err = c.Put(resourceURL(c, serviceID), b, &r.Body, &golangsdk.RequestOpts{
   115  		OkCodes: []int{200},
   116  	})
   117  	return
   118  }
   119  
   120  // Delete will permanently delete a particular node based on its unique ID and cluster ID.
   121  func Delete(c *golangsdk.ServiceClient, serviceID string) (r DeleteResult) {
   122  	_, r.Err = c.Delete(resourceURL(c, serviceID), nil)
   123  	return
   124  }
   125  
   126  // ListOptsBuilder allows extensions to add parameters to the
   127  // List request.
   128  type ListOptsBuilder interface {
   129  	ToListQuery() (string, error)
   130  }
   131  
   132  // ListOpts allows the filtering of list data using given parameters.
   133  type ListOpts struct {
   134  	ServiceName string `q:"endpoint_service_name"`
   135  	ID          string `q:"id"`
   136  	// Status is not supported for ListPublic
   137  	Status string `q:"status"`
   138  	// Number of records to be queried.
   139  	// The valid value is range from 1 to 1000, defaults to 10.
   140  	Limit int `q:"limit"`
   141  	// Specifies the index position, which starts from the next data record specified by offset.
   142  	// The value must be a number and connot be a negative number, defaults to 0.
   143  	Offset int `q:"offset"`
   144  	// Specifies the sorting field of the VPC endpoint services, which can be:
   145  	// create_at: VPC endpoint services are sorted by creation time.
   146  	// update_at: VPC endpoint services are sorted by update time. The default field is create_at.
   147  	// Default: create_at
   148  	SortKey string `q:"sort_key"`
   149  	// Specifies the sorting method of VPC endpoint services, which can be:
   150  	// desc: VPC endpoint services are sorted in descending order.
   151  	// asc: VPC endpoint services are sorted in ascending order. The default method is desc.
   152  	// Default: desc
   153  	SortDir string `q:"sort_dir"`
   154  	// Specifies the VPC endpoint service that matches the edge attribute in the filtering result.
   155  	PublicBorderGroup string `q:"public_border_group"`
   156  	// Specifies the resource type, which can be:
   157  	// VM: indicates a cloud server.
   158  	// LB: indicates a shared load balancer.
   159  	ServerType string `q:"server_type"`
   160  }
   161  
   162  // ToListQuery formats a ListOpts into a query string.
   163  func (opts ListOpts) ToListQuery() (string, error) {
   164  	q, err := golangsdk.BuildQueryString(opts)
   165  	return q.String(), err
   166  }
   167  
   168  // List makes a request against the API to list VPC endpoint services.
   169  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) ([]Service, error) {
   170  	var r ListResult
   171  	url := rootURL(client)
   172  	if opts != nil {
   173  		query, err := opts.ToListQuery()
   174  		if err != nil {
   175  			return nil, err
   176  		}
   177  		url += query
   178  	}
   179  	_, r.Err = client.Get(url, &r.Body, nil)
   180  	if r.Err != nil {
   181  		return nil, r.Err
   182  	}
   183  
   184  	allNodes, err := r.ExtractServices()
   185  	if err != nil {
   186  		return nil, err
   187  	}
   188  
   189  	return allNodes, nil
   190  }
   191  
   192  // ListPublic makes a request against the API to list public VPC endpoint services.
   193  func ListPublic(client *golangsdk.ServiceClient, opts ListOptsBuilder) ([]PublicService, error) {
   194  	var r ListPublicResult
   195  	url := publicResourceURL(client)
   196  	if opts != nil {
   197  		query, err := opts.ToListQuery()
   198  		if err != nil {
   199  			return nil, err
   200  		}
   201  		url += query
   202  	}
   203  	_, r.Err = client.Get(url, &r.Body, nil)
   204  	if r.Err != nil {
   205  		return nil, r.Err
   206  	}
   207  
   208  	allNodes, err := r.ExtractServices()
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  
   213  	return allNodes, nil
   214  }
   215  
   216  // ListAllPublics is a method to query the supported PublicService list using given parameters.
   217  func ListAllPublics(client *golangsdk.ServiceClient, opts ListOpts) ([]PublicService, error) {
   218  	url := publicResourceURL(client)
   219  	query, err := golangsdk.BuildQueryString(opts)
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  	url += query.String()
   224  
   225  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   226  		p := PublicServicePage{pagination.OffsetPageBase{PageResult: r}}
   227  		return p
   228  	}).AllPages()
   229  	if err != nil {
   230  		return nil, err
   231  	}
   232  	return extractPublicService(pages)
   233  }
   234  
   235  // ListAllServices is a method to query the supported Service list using given parameters with **pagination**.
   236  func ListAllServices(client *golangsdk.ServiceClient, opts ListOpts) ([]Service, error) {
   237  	url := rootURL(client)
   238  	query, err := golangsdk.BuildQueryString(opts)
   239  	if err != nil {
   240  		return nil, err
   241  	}
   242  	url += query.String()
   243  
   244  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   245  		p := PublicServicePage{pagination.OffsetPageBase{PageResult: r}}
   246  		return p
   247  	}).AllPages()
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	return extractService(pages)
   252  }
   253  
   254  // ConnActionOpts used to receive or reject a VPC endpoint for a VPC endpoint service.
   255  type ConnActionOpts struct {
   256  	// Specifies whether to receive or reject a VPC endpoint for a VPC endpoint service.
   257  	Action string `json:"action" required:"true"`
   258  	// Lists the VPC endpoints.
   259  	Endpoints []string `json:"endpoints" required:"true"`
   260  }
   261  
   262  // ToServicePostMap assembles a request body based on the contents of a ConnActionOpts.
   263  func (opts ConnActionOpts) ToServicePostMap() (map[string]interface{}, error) {
   264  	return golangsdk.BuildRequestBody(opts, "")
   265  }
   266  
   267  // ConnAction accepts a ConnActionOpts struct and uses the values to receive or reject
   268  // a VPC endpoint for a VPC endpoint service.
   269  func ConnAction(c *golangsdk.ServiceClient, serviceID string, opts PostOptsBuilder) (r ConnectionResult) {
   270  	b, err := opts.ToServicePostMap()
   271  	if err != nil {
   272  		r.Err = err
   273  		return
   274  	}
   275  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   276  	_, r.Err = c.Post(connectionsActionURL(c, serviceID), b, &r.Body, reqOpt)
   277  	return
   278  }
   279  
   280  // ListConnOpts used to query connections of a VPC endpoint service.
   281  type ListConnOpts struct {
   282  	// Specifies the unique ID of the VPC endpoint
   283  	EndpointID string `q:"id"`
   284  	// Specifies the packet ID of the VPC endpoint
   285  	MarkerID string `q:"marker_id"`
   286  	// Specifies the connection status of the VPC endpoint.
   287  	// pendingAcceptance: The VPC endpoint connection is to be accepted.
   288  	// creating: The VPC endpoint connection is being created.
   289  	// accepted: The VPC endpoint connection has been accepted.
   290  	// failed: The VPC endpoint connection failed.
   291  	Status string `q:"status"`
   292  	// Specifies the sorting field of the VPC endpoints, which can be:
   293  	// create_at: VPC endpoints are sorted by creation time.
   294  	// update_at: VPC endpoints are sorted by update time.
   295  	// The default field is create_at.
   296  	SortKey string `q:"sort_key"`
   297  	// Specifies the sorting method of VPC endpoints, which can be:
   298  	// desc: VPC endpoints are sorted in descending order.
   299  	// asc: VPC endpoints are sorted in ascending order.
   300  	// The default method is desc.
   301  	SortDir string `q:"sort_dir"`
   302  	// Specifies the maximum number of connections displayed on each page.
   303  	// The value ranges from 0 to 1000 and is generally 10, 20, or 50.
   304  	// The default number is 10.
   305  	Limit int `q:"limit"`
   306  	// Specifies the offset. All VPC endpoint services after this offset will be queried.
   307  	// The offset must be an integer greater than 0 but less than the number of VPC endpoint services.
   308  	Offset int `q:"offset"`
   309  }
   310  
   311  // ToListQuery formats a ListConnOpts into a query string.
   312  func (opts ListConnOpts) ToListQuery() (string, error) {
   313  	q, err := golangsdk.BuildQueryString(opts)
   314  	return q.String(), err
   315  }
   316  
   317  // ListConnections makes a request against the API to list connections of a VPC endpoint service.
   318  func ListConnections(client *golangsdk.ServiceClient, serviceID string, opts ListOptsBuilder) ([]Connection, error) {
   319  	var r ConnectionResult
   320  	url := connectionsURL(client, serviceID)
   321  	if opts != nil {
   322  		query, err := opts.ToListQuery()
   323  		if err != nil {
   324  			return nil, err
   325  		}
   326  		url += query
   327  	}
   328  	_, r.Err = client.Get(url, &r.Body, nil)
   329  	if r.Err != nil {
   330  		return nil, r.Err
   331  	}
   332  
   333  	allConnections, err := r.ExtractConnections()
   334  	if err != nil {
   335  		return nil, err
   336  	}
   337  
   338  	return allConnections, nil
   339  }
   340  
   341  // ListAllConnections is a method to query the supported connections list using given parameters.
   342  func ListAllConnections(client *golangsdk.ServiceClient, serviceID string, opts ListOptsBuilder) ([]Connection, error) {
   343  	url := connectionsURL(client, serviceID)
   344  	if opts != nil {
   345  		query, err := golangsdk.BuildQueryString(opts)
   346  		if err != nil {
   347  			return nil, err
   348  		}
   349  		url += query.String()
   350  	}
   351  
   352  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   353  		p := ConnectionPage{pagination.OffsetPageBase{PageResult: r}}
   354  		return p
   355  	}).AllPages()
   356  	if err != nil {
   357  		return nil, err
   358  	}
   359  	return extractConnection(pages)
   360  }
   361  
   362  // PermActionOpts used to add to or delete whitelist records from a VPC endpoint service.
   363  type PermActionOpts struct {
   364  	// Specifies the operation to be performed: add or remove.
   365  	Action string `json:"action" required:"true"`
   366  	// Lists the whitelist records. The record is in the iam:domain::domain_id format or organizations:orgPath::org_path.
   367  	Permissions []string `json:"permissions" required:"true"`
   368  	// Specifies the type of the whitelist: domainId or orgPath.
   369  	PermissionType string `json:"permission_type,omitempty"`
   370  }
   371  
   372  // ToServicePostMap assembles a request body based on the contents of a PermActionOpts.
   373  func (opts PermActionOpts) ToServicePostMap() (map[string]interface{}, error) {
   374  	return golangsdk.BuildRequestBody(opts, "")
   375  }
   376  
   377  // PermAction accepts a PermActionOpts struct and uses the values toadd to or delete
   378  // whitelist records from a VPC endpoint service.
   379  func PermAction(c *golangsdk.ServiceClient, serviceID string, opts PostOptsBuilder) (r PermActionResult) {
   380  	b, err := opts.ToServicePostMap()
   381  	if err != nil {
   382  		r.Err = err
   383  		return
   384  	}
   385  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   386  	_, r.Err = c.Post(permissionsActionURL(c, serviceID), b, &r.Body, reqOpt)
   387  	return
   388  }
   389  
   390  // ListPermOpts used to query permissions of a VPC endpoint service.
   391  type ListPermOpts struct {
   392  	Permission string `q:"permission"`
   393  	// Specifies the number of returned whitelist records of the VPC endpoint service on each page.
   394  	// The value ranges from 0 to 500 and is generally 10, 20, or 50. The default number is 10.
   395  	Limit int `q:"limit"`
   396  	// Specifies the offset. All VPC endpoint services after this offset will be queried.
   397  	// The offset must be an integer greater than 0 but less than the number of VPC endpoint services.
   398  	Offset int `q:"offset"`
   399  	// Specifies the sorting field of whitelist records.
   400  	// The value is create_at, indicating the time when the whitelist record is added.
   401  	SortKey string `q:"sort_key"`
   402  	// Specifies the sorting method of whitelist records, which can be:
   403  	// desc: The whitelist records are sorted in descending order.
   404  	// asc: The whitelist records are sorted in ascending order.
   405  	//The default method is desc.
   406  	SortDir string `q:"sort_dir"`
   407  }
   408  
   409  // ToListQuery formats a ListPermOpts into a query string.
   410  func (opts ListPermOpts) ToListQuery() (string, error) {
   411  	q, err := golangsdk.BuildQueryString(opts)
   412  	return q.String(), err
   413  }
   414  
   415  // ListPermissions makes a request against the API to query the whitelist records of
   416  // a VPC endpoint service.
   417  func ListPermissions(client *golangsdk.ServiceClient, serviceID string) ([]Permission, error) {
   418  	var r ListPermResult
   419  	url := permissionsURL(client, serviceID)
   420  
   421  	_, r.Err = client.Get(url, &r.Body, nil)
   422  	if r.Err != nil {
   423  		return nil, r.Err
   424  	}
   425  
   426  	allPermissions, err := r.ExtractPermissions()
   427  	if err != nil {
   428  		return nil, err
   429  	}
   430  
   431  	return allPermissions, nil
   432  }
   433  
   434  // ListAllPermissions is a method to query the supported permissions list using given parameters.
   435  func ListAllPermissions(client *golangsdk.ServiceClient, serviceID string, opts ListOptsBuilder) ([]Permission, error) {
   436  	url := permissionsURL(client, serviceID)
   437  	if opts != nil {
   438  		query, err := golangsdk.BuildQueryString(opts)
   439  		if err != nil {
   440  			return nil, err
   441  		}
   442  		url += query.String()
   443  	}
   444  
   445  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   446  		p := PermissionPage{pagination.OffsetPageBase{PageResult: r}}
   447  		return p
   448  	}).AllPages()
   449  	if err != nil {
   450  		return nil, err
   451  	}
   452  	return extractPermission(pages)
   453  }