github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/nat/v2/gateways/requests.go (about)

     1  package gateways
     2  
     3  import "github.com/chnsz/golangsdk"
     4  
     5  // CreateOpts is the structure used to create a new gateway for NAT service.
     6  type CreateOpts struct {
     7  	// The gateway name.
     8  	Name string `json:"name" required:"true"`
     9  	// The ID of the VPC to which the gateway belongs.
    10  	VpcId string `json:"router_id" required:"true"`
    11  	// The network ID that VPC have.
    12  	InternalNetworkId string `json:"internal_network_id" required:"true"`
    13  	// The gateway specification.
    14  	Spec string `json:"spec" required:"true"`
    15  	// The gateway description.
    16  	Description string `json:"description,omitempty"`
    17  	// The private IP address of the public NAT gateway.
    18  	// The IP address is assigned by the VPC subnet.
    19  	NgportIpAddress string `json:"ngport_ip_address,omitempty"`
    20  	// The enterprise project ID to which the gateway belongs.
    21  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    22  }
    23  
    24  var requestOpts = golangsdk.RequestOpts{
    25  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    26  }
    27  
    28  // Create is a method used to create a gateway using given parameters.
    29  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Gateway, error) {
    30  	b, err := golangsdk.BuildRequestBody(opts, "nat_gateway")
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	var r createResp
    36  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    37  		MoreHeaders: requestOpts.MoreHeaders,
    38  	})
    39  	return &r.Gateway, err
    40  }
    41  
    42  // Get is a method used to obtain the gateway detail by its ID.
    43  func Get(c *golangsdk.ServiceClient, gatewayId string) (*Gateway, error) {
    44  	var r queryResp
    45  	_, err := c.Get(resourceURL(c, gatewayId), &r, &golangsdk.RequestOpts{
    46  		MoreHeaders: requestOpts.MoreHeaders,
    47  	})
    48  	return &r.Gateway, err
    49  }
    50  
    51  // ListOpts allows to filter list data using given parameters.
    52  type ListOpts struct {
    53  	// The project ID.
    54  	TenantId string `q:"tenant_id"`
    55  	// The gateway ID.
    56  	ID string `q:"id"`
    57  	// The enterprise project ID to which the gateway belongs.
    58  	EnterpriseProjectId string `q:"enterprise_project_id"`
    59  	// The gateway description.
    60  	Description string `q:"description"`
    61  	// The creation time.
    62  	CreatedAt string `q:"created_at"`
    63  	// The gateway name.
    64  	Name string `q:"name"`
    65  	// The status of the gateway name.
    66  	Status string `q:"status"`
    67  	// The gateway specification.
    68  	Spec string `q:"spec"`
    69  	// The frozen status.
    70  	AdminStateUp string `q:"admin_state_up"`
    71  	// The network ID that VPC have.
    72  	InternalNetworkId string `q:"internal_network_id"`
    73  	// The ID of the VPC to which the gateway belongs.
    74  	VpcId string `q:"router_id"`
    75  	// The ID of the VPC to which the gateway belongs.
    76  	Limit int `q:"limit"`
    77  }
    78  
    79  // List is a method to query all gateways using given parameters.
    80  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Gateway, error) {
    81  	url := rootURL(client)
    82  	query, err := golangsdk.BuildQueryString(opts)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	url += query.String()
    87  
    88  	var r listResp
    89  	_, err = client.Get(url, &r, &golangsdk.RequestOpts{
    90  		MoreHeaders: client.MoreHeaders,
    91  	})
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return r.Gateways, nil
    96  }
    97  
    98  // UpdateOpts is the structure used to modify an existing gateway.
    99  type UpdateOpts struct {
   100  	// The gateway name.
   101  	Name string `json:"name,omitempty"`
   102  	// The gateway description.
   103  	Description *string `json:"description,omitempty"`
   104  	// The gateway specification.
   105  	Spec string `json:"spec,omitempty"`
   106  }
   107  
   108  // Update is a method used to modify an existing gateway using given parameters.
   109  func Update(c *golangsdk.ServiceClient, gatewayId string, opts UpdateOpts) (*Gateway, error) {
   110  	b, err := golangsdk.BuildRequestBody(opts, "nat_gateway")
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	var r updateResp
   116  	_, err = c.Put(resourceURL(c, gatewayId), b, &r, &golangsdk.RequestOpts{
   117  		MoreHeaders: requestOpts.MoreHeaders,
   118  	})
   119  	return &r.Gateway, err
   120  }
   121  
   122  // Delete is a method to remove the specified gateway using its ID.
   123  func Delete(c *golangsdk.ServiceClient, gatewayId string) error {
   124  	_, err := c.Delete(resourceURL(c, gatewayId), &golangsdk.RequestOpts{
   125  		MoreHeaders: requestOpts.MoreHeaders,
   126  	})
   127  	return err
   128  }