Skip to content

Commit 3ebe383

Browse files
authored
Merge pull request #60 from FakieHeelflip/feat/contExport-externalTables
2 parents 7cb8410 + 8984551 commit 3ebe383

File tree

6 files changed

+645
-0
lines changed

6 files changed

+645
-0
lines changed

adx/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func Provider() *schema.Provider {
6868
"adx_materialized_view_retention_policy": resourceADXMaterializedViewRetentionPolicy(),
6969
"adx_materialized_view_row_level_security_policy": resourceADXMaterializedViewRowLevelSecurityPolicy(),
7070
"adx_column_encoding_policy": resourceADXColumnEncodingPolicy(),
71+
"adx_table_continuous_export": resourceADXTableContinuousExport(),
72+
"adx_external_table": resourceADXExternalTable(),
7173
},
7274
}
7375

adx/resource_adx_external_table.go

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
package adx
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/favoretti/terraform-provider-adx/adx/validate"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
type ADXExternalTable struct {
15+
Name string
16+
ConnectionStrings string
17+
Partitions string
18+
PathFormat string
19+
Folder string
20+
Properties string
21+
}
22+
23+
func resourceADXExternalTable() *schema.Resource {
24+
return &schema.Resource{
25+
CreateContext: resourceADXExternalTableCreateUpdate,
26+
UpdateContext: resourceADXExternalTableCreateUpdate,
27+
ReadContext: resourceADXExternalTableRead,
28+
DeleteContext: resourceADXExternalTableDelete,
29+
Importer: &schema.ResourceImporter{
30+
StateContext: schema.ImportStatePassthroughContext,
31+
},
32+
33+
Schema: map[string]*schema.Schema{
34+
"cluster": getClusterConfigInputSchema(),
35+
"database_name": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
ValidateDiagFunc: validate.StringIsNotEmpty,
40+
},
41+
42+
"name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
ForceNew: true,
46+
ValidateDiagFunc: validate.StringIsNotEmpty,
47+
},
48+
49+
"data_format": {
50+
Type: schema.TypeString,
51+
Required: true,
52+
ValidateDiagFunc: validate.StringIsNotEmpty,
53+
},
54+
55+
"storage_connection_string": {
56+
Type: schema.TypeString,
57+
Required: true,
58+
Sensitive: true,
59+
ValidateDiagFunc: validate.StringIsNotEmpty,
60+
},
61+
62+
"schema": {
63+
Type: schema.TypeString,
64+
Required: true,
65+
ValidateDiagFunc: validate.StringIsNotEmpty,
66+
},
67+
68+
"kind": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Default: "storage",
72+
},
73+
74+
"partitions": {
75+
Type: schema.TypeString,
76+
Optional: true,
77+
Default: "jsonencode([])",
78+
},
79+
80+
"path_format": {
81+
Type: schema.TypeString,
82+
Optional: true,
83+
},
84+
85+
"folder": {
86+
Type: schema.TypeString,
87+
Optional: true,
88+
},
89+
90+
"doc_string": {
91+
Type: schema.TypeString,
92+
Optional: true,
93+
},
94+
95+
"compressed": {
96+
Type: schema.TypeBool,
97+
Optional: true,
98+
},
99+
100+
"include_headers": {
101+
Type: schema.TypeString,
102+
Optional: true,
103+
Default: "All",
104+
},
105+
106+
"name_prefix": {
107+
Type: schema.TypeString,
108+
Optional: true,
109+
},
110+
111+
"file_extension": {
112+
Type: schema.TypeString,
113+
Optional: true,
114+
},
115+
116+
"encoding": {
117+
Type: schema.TypeString,
118+
Optional: true,
119+
Default: "UTF8NoBOM",
120+
},
121+
122+
"sample_uris": {
123+
Type: schema.TypeBool,
124+
Optional: true,
125+
},
126+
127+
"files_preview": {
128+
Type: schema.TypeBool,
129+
Optional: true,
130+
},
131+
132+
"validate_not_empty": {
133+
Type: schema.TypeBool,
134+
Optional: true,
135+
},
136+
137+
"dry_run": {
138+
Type: schema.TypeBool,
139+
Optional: true,
140+
},
141+
},
142+
CustomizeDiff: clusterConfigCustomDiff,
143+
Timeouts: &schema.ResourceTimeout{
144+
Create: schema.DefaultTimeout(30 * time.Minute),
145+
},
146+
}
147+
}
148+
149+
func resourceADXExternalTableCreateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
150+
clusterConfig := getAndExpandClusterConfigWithDefaults(ctx, d, meta)
151+
name := d.Get("name").(string)
152+
databaseName := d.Get("database_name").(string)
153+
dataFormat := d.Get("data_format").(string)
154+
storageConnectionString := d.Get("storage_connection_string").(string)
155+
schema := d.Get("schema").(string)
156+
partitions := d.Get("partitions").(string)
157+
pathFormat := d.Get("path_format").(string)
158+
kind := d.Get("kind").(string)
159+
160+
var withParams []string
161+
162+
if folder, ok := d.GetOk("folder"); ok {
163+
withParams = append(withParams, fmt.Sprintf("folder=%s", folder.(string)))
164+
}
165+
if docString, ok := d.GetOk("doc_string"); ok {
166+
withParams = append(withParams, fmt.Sprintf("docString=%s", docString.(string)))
167+
}
168+
if compressed, ok := d.GetOk("compressed"); ok {
169+
withParams = append(withParams, fmt.Sprintf("compressed=%t", compressed.(bool)))
170+
}
171+
if includeHeaders, ok := d.GetOk("include_headers"); ok {
172+
withParams = append(withParams, fmt.Sprintf("includeHeaders=%s", includeHeaders.(string)))
173+
}
174+
if namePrefix, ok := d.GetOk("name_prefix"); ok {
175+
withParams = append(withParams, fmt.Sprintf("namePrefix=%s", namePrefix.(string)))
176+
}
177+
if fileExtension, ok := d.GetOk("file_extension"); ok {
178+
withParams = append(withParams, fmt.Sprintf("fileExtension='%s'", fileExtension.(string)))
179+
}
180+
if encoding, ok := d.GetOk("encoding"); ok {
181+
withParams = append(withParams, fmt.Sprintf("encoding='%s'", encoding.(string)))
182+
}
183+
if sampleUris, ok := d.GetOk("sample_uris"); ok {
184+
withParams = append(withParams, fmt.Sprintf("sampleUris=%t", sampleUris.(bool)))
185+
}
186+
if filesPreview, ok := d.GetOk("files_preview"); ok {
187+
withParams = append(withParams, fmt.Sprintf("filesPreview=%t", filesPreview.(bool)))
188+
}
189+
if validateNotEmpty, ok := d.GetOk("validate_not_empty"); ok {
190+
withParams = append(withParams, fmt.Sprintf("validateNotEmpty=%t", validateNotEmpty.(bool)))
191+
}
192+
if dryRun, ok := d.GetOk("dry_run"); ok {
193+
withParams = append(withParams, fmt.Sprintf("dryRun=%t", dryRun.(bool)))
194+
}
195+
196+
withClause := ""
197+
if len(withParams) > 0 {
198+
withClause = fmt.Sprintf("with(%s)", strings.Join(withParams, ", "))
199+
}
200+
201+
createStatement := ""
202+
if len(partitions) > 0 && len(pathFormat) > 0 {
203+
createStatement = fmt.Sprintf(".create-or-alter external table %s (%s) kind = %s partition by (%s) pathformat = (%s) dataformat = %s ('%s') %s", name, schema, kind, partitions, pathFormat, dataFormat, storageConnectionString, withClause)
204+
} else if len(partitions) > 0 && len(pathFormat) == 0 {
205+
createStatement = fmt.Sprintf(".create-or-alter external table %s (%s) kind = %s partition by (%s) dataformat = %s ('%s') %s", name, schema, kind, partitions, dataFormat, storageConnectionString, withClause)
206+
} else if len(partitions) == 0 && len(pathFormat) > 0 {
207+
createStatement = fmt.Sprintf(".create-or-alter external table %s (%s) kind = %s pathformat = (%s) dataformat = %s ('%s') %s", name, schema, kind, pathFormat, dataFormat, storageConnectionString, withClause)
208+
} else {
209+
createStatement = fmt.Sprintf(".create-or-alter external table %s (%s) kind = %s dataformat = %s ('%s') %s", name, schema, kind, dataFormat, storageConnectionString, withClause)
210+
}
211+
212+
_, err := queryADXMgmt(ctx, meta, clusterConfig, databaseName, createStatement)
213+
if err != nil {
214+
return diag.Errorf("error creating external table %s (Database %q): %+v", name, databaseName, err)
215+
}
216+
217+
client, err := getADXClient(meta, clusterConfig)
218+
if err != nil {
219+
return diag.Errorf("error creating adx client connection: %+v", err)
220+
}
221+
d.SetId(buildADXResourceId(client.Endpoint(), databaseName, "externaltable", name))
222+
223+
return resourceADXExternalTableRead(ctx, d, meta)
224+
}
225+
226+
func resourceADXExternalTableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
227+
clusterConfig := getAndExpandClusterConfigWithDefaults(ctx, d, meta)
228+
229+
id, err := parseADXExternalTableID(d.Id())
230+
if err != nil {
231+
return diag.FromErr(err)
232+
}
233+
234+
showCommand := fmt.Sprintf(".show external table %s ", id.Name)
235+
236+
resultSet, diags := readADXEntity[ADXExternalTable](ctx, meta, clusterConfig, id, showCommand, "external table")
237+
if diags.HasError() {
238+
return diags
239+
}
240+
241+
if len(resultSet) < 1 {
242+
d.SetId("")
243+
} else {
244+
245+
d.Set("name", id.Name)
246+
d.Set("database_name", id.DatabaseName)
247+
d.Set("storage_connection_string", resultSet[0].ConnectionStrings)
248+
d.Set("partitions", resultSet[0].Partitions)
249+
d.Set("path_format", resultSet[0].PathFormat)
250+
d.Set("folder", resultSet[0].Folder)
251+
d.Set("properties", resultSet[0].Properties)
252+
253+
}
254+
255+
return diags
256+
}
257+
258+
func resourceADXExternalTableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
259+
clusterConfig := getAndExpandClusterConfigWithDefaults(ctx, d, meta)
260+
261+
id, err := parseADXExternalTableID(d.Id())
262+
if err != nil {
263+
return diag.FromErr(err)
264+
}
265+
266+
return deleteADXEntity(ctx, d, meta, clusterConfig, id.DatabaseName, fmt.Sprintf(".drop external table %s", id.Name))
267+
}
268+
269+
func parseADXExternalTableID(input string) (*adxResourceId, error) {
270+
return parseADXResourceID(input, 4, 0, 1, 2, 3)
271+
}

0 commit comments

Comments
 (0)