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

     1  package tables
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  )
     8  
     9  const (
    10  	TableTypeOBS  = "OBS"
    11  	TableTypeDLI  = "DLI"
    12  	TableTypeVIEW = "VIEW"
    13  )
    14  
    15  type CreateTableOpts struct {
    16  	TableName string `json:"table_name" required:"true"`
    17  	// Data storage location. OBS tables,DLI tables and view are available.
    18  	DataLocation    string       `json:"data_location" required:"true"`
    19  	Description     string       `json:"description,omitempty"`
    20  	Columns         []ColumnOpts `json:"columns" required:"true"`
    21  	SelectStatement string       `json:"select_statement,omitempty"`
    22  	// Type of the data to be added to the OBS table. The options: Parquet, ORC, CSV, JSON, Carbon, and Avro.
    23  	DataType string `json:"data_type,omitempty"`
    24  	// Storage path of data in the new OBS table, which must be a path on OBS and must begin with obs. start with s3a
    25  	DataPath string `json:"data_path,omitempty"`
    26  	// Whether the table header is included in the OBS table data. Only data in CSV files has this attribute.
    27  	WithColumnHeader *bool `json:"with_column_header,omitempty"`
    28  	// User-defined data delimiter. Only data in CSV files has this attribute.
    29  	Delimiter string `json:"delimiter,omitempty"`
    30  	// User-defined reference character. Double quotation marks ("\") are used by default. Only data in CSV files
    31  	// has this attribute.
    32  	QuoteChar string `json:"quote_char,omitempty"`
    33  	// User-defined escape character. Backslashes (\\) are used by default. Only data in CSV files has this attribute.
    34  	EscapeChar string `json:"escape_char,omitempty"`
    35  	// User-defined date type. yyyy-MM-dd is used by default. Only data in CSV and JSON files has this attribute.
    36  	DateFormat string `json:"date_format,omitempty"`
    37  	// User-defined timestamp type. yyyy-MM-dd HH:mm:ss is used by default. Only data in CSV and JSON files has
    38  	// this attribute.
    39  	TimestampFormat string `json:"timestamp_format,omitempty"`
    40  }
    41  
    42  type ColumnOpts struct {
    43  	ColumnName        string `json:"column_name" required:"true"`
    44  	Type              string `json:"type" required:"true"`
    45  	Description       string `json:"description,omitempty"`
    46  	IsPartitionColumn *bool  `json:"is_partition_column,omitempty"`
    47  }
    48  
    49  type ListOpts struct {
    50  	Keyword          string `q:"keyword"`
    51  	WithDetail       *bool  `q:"with-detail"`
    52  	PageSize         *int   `q:"page-size"`
    53  	CurrentPage      *int   `q:"current-page"`
    54  	WithPriv         *bool  `q:"with-priv"`
    55  	TableType        string `q:"table-type"`
    56  	DatasourceType   string `q:"table-type"`
    57  	WithoutTablemeta *bool  `q:"table-type"`
    58  }
    59  
    60  type PartitionsOpts struct {
    61  	// default:100
    62  	Limit  int `q:"limit"`
    63  	Offset int `q:"offset"`
    64  }
    65  
    66  type UpdateOwnerOpts struct {
    67  	NewOwner string `json:"new_owner" required:"true"`
    68  }
    69  
    70  var RequestOpts = golangsdk.RequestOpts{
    71  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    72  }
    73  
    74  func Create(c *golangsdk.ServiceClient, databaseName string, opts CreateTableOpts) (*CommonResp, error) {
    75  	b, err := golangsdk.BuildRequestBody(opts, "")
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	var rst CommonResp
    81  	_, err = c.Post(createURL(c, databaseName), b, &rst, &golangsdk.RequestOpts{
    82  		MoreHeaders: RequestOpts.MoreHeaders,
    83  	})
    84  
    85  	return &rst, err
    86  }
    87  
    88  func Delete(c *golangsdk.ServiceClient, databaseName string, tableName string, asyncFlag bool) (*DeleteResp, error) {
    89  	url := deleteURL(c, databaseName, tableName)
    90  	url += fmt.Sprintf("%s%t", "?async=", asyncFlag)
    91  	var rst DeleteResp
    92  
    93  	_, err := c.DeleteWithResponse(url, &rst, &golangsdk.RequestOpts{
    94  		MoreHeaders: RequestOpts.MoreHeaders,
    95  	})
    96  
    97  	return &rst, err
    98  }
    99  
   100  func List(c *golangsdk.ServiceClient, databaseName string, opts ListOpts) (*ListResp, error) {
   101  	url := listURL(c, databaseName)
   102  	query, err := golangsdk.BuildQueryString(opts)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	url += query.String()
   107  
   108  	var rst ListResp
   109  	_, err = c.Get(url, &rst, &golangsdk.RequestOpts{
   110  		MoreHeaders: RequestOpts.MoreHeaders,
   111  	})
   112  
   113  	return &rst, err
   114  }
   115  
   116  func Get(c *golangsdk.ServiceClient, databaseName string, tableName string) (*Table, error) {
   117  	var rst Table
   118  
   119  	_, err := c.Get(getURL(c, databaseName, tableName), &rst, &golangsdk.RequestOpts{
   120  		MoreHeaders: RequestOpts.MoreHeaders,
   121  	})
   122  
   123  	return &rst, err
   124  }
   125  
   126  func Partitions(c *golangsdk.ServiceClient, databaseName string, tableName string, opts PartitionsOpts) (*PartitionsResp, error) {
   127  	url := partitionsURL(c, databaseName, tableName)
   128  	query, err := golangsdk.BuildQueryString(opts)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	url += query.String()
   133  
   134  	var rst PartitionsResp
   135  	_, err = c.Get(url, &rst, &golangsdk.RequestOpts{
   136  		MoreHeaders: RequestOpts.MoreHeaders,
   137  	})
   138  
   139  	return &rst, err
   140  }
   141  
   142  func UpdateOwner(c *golangsdk.ServiceClient, databaseName string, tableName string, opts UpdateOwnerOpts) (*CommonResp, error) {
   143  	b, err := golangsdk.BuildRequestBody(opts, "")
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	var rst CommonResp
   149  	_, err = c.Put(updateOwnerURL(c, databaseName, tableName), b, &rst, &golangsdk.RequestOpts{
   150  		MoreHeaders: RequestOpts.MoreHeaders,
   151  	})
   152  
   153  	return &rst, err
   154  }