Repository navigation
Expand file tree
/
Copy pathcas_data_operations.go
More file actions
107 lines (94 loc) · 3.63 KB
/
Copy pathcas_data_operations.go
File metadata and controls
107 lines (94 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package viya
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"go.opentelemetry.io/otel/codes"
)
// SAS Viya CAS Management API reference:
// https://developer.sas.com/rest-apis/casManagement
// UploadCSVToCASTable uploads CSV data into a CAS table.
//
// The CSV data should include a header row. The target table is created in
// caslibName on serverID using the supplied tableName.
func (c *Client) UploadCSVToCASTable(ctx context.Context, serverID string, caslibName string, tableName string, csv []byte) (resp CASTable, err error) {
if serverID == "" {
return resp, &ErrInvalidParameter{Parameter: "serverID", Reason: "must not be empty"}
}
if caslibName == "" {
return resp, &ErrInvalidParameter{Parameter: "caslibName", Reason: "must not be empty"}
}
if tableName == "" {
return resp, &ErrInvalidParameter{Parameter: "tableName", Reason: "must not be empty"}
}
return c.UploadCSVToCASTableFromReader(ctx, serverID, caslibName, tableName, bytes.NewReader(csv))
}
// UploadCSVToCASTableFromReader uploads CSV data from r into a CAS table.
//
// The CSV data should include a header row. The target table is created in
// caslibName on serverID using the supplied tableName.
func (c *Client) UploadCSVToCASTableFromReader(ctx context.Context, serverID string, caslibName string, tableName string, r io.Reader) (resp CASTable, err error) {
if serverID == "" {
return resp, &ErrInvalidParameter{Parameter: "serverID", Reason: "must not be empty"}
}
if caslibName == "" {
return resp, &ErrInvalidParameter{Parameter: "caslibName", Reason: "must not be empty"}
}
if tableName == "" {
return resp, &ErrInvalidParameter{Parameter: "tableName", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "UploadCSVToCASTableFromReader")
defer span.End()
httpResp, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptJSONError).
SetMultipartFormData(map[string]string{
"tableName": tableName,
"format": "csv",
"containsHeaderRow": "true",
}).
SetMultipartField("file", "data.csv", "text/csv", r).
SetResult(&resp).
Post(fmt.Sprintf("/casManagement/servers/%s/caslibs/%s/tables", serverID, url.PathEscape(caslibName)))
if err != nil {
return resp, err
}
if !httpResp.IsStatusSuccess() {
span.SetStatus(codes.Error, httpResp.String())
return resp, fmt.Errorf("failed to upload CSV to CAS table, status code: %d", httpResp.StatusCode())
}
return resp, nil
}
// PromoteCASTable promotes a CAS table to global scope.
//
// A promoted table is visible outside the session that created it.
func (c *Client) PromoteCASTable(ctx context.Context, serverID string, caslibName string, tableName string) (resp CASTable, err error) {
if serverID == "" {
return resp, &ErrInvalidParameter{Parameter: "serverID", Reason: "must not be empty"}
}
if caslibName == "" {
return resp, &ErrInvalidParameter{Parameter: "caslibName", Reason: "must not be empty"}
}
if tableName == "" {
return resp, &ErrInvalidParameter{Parameter: "tableName", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "PromoteCASTable")
defer span.End()
httpResp, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptJSONError).
SetContentType("application/json").
SetBody(map[string]any{"scope": "global"}).
SetResult(&resp).
Post(fmt.Sprintf("/casManagement/servers/%s/caslibs/%s/tables/%s", serverID, url.PathEscape(caslibName), url.PathEscape(tableName)))
if err != nil {
return resp, err
}
if !httpResp.IsStatusSuccess() {
span.SetStatus(codes.Error, httpResp.String())
return resp, fmt.Errorf("failed to promote CAS table, status code: %d", httpResp.StatusCode())
}
return resp, nil
}