/
resource_lookml_model.go
144 lines (133 loc) · 5.13 KB
/
resource_lookml_model.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package provider
import (
"context"
"fmt"
"github.com/devoteamgcloud/terraform-provider-looker/pkg/lookergo"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceLookMlModel() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLookMlModelCreate,
ReadContext: resourceLookMlModelRead,
UpdateContext: resourceLookMlModelUpdate,
DeleteContext: resourceLookMlModelDelete,
Schema: map[string]*schema.Schema{
"name": {
Description: "Name of the model. Also used as the unique identifier",
Type: schema.TypeString,
Required: true,
},
"project_name": {
Description: "Name of project containing the model",
Type: schema.TypeString,
Required: true,
},
"allowed_db_connection_names": {
Description: "Array of names of connections this model is allowed to use (looker_connection)",
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"unlimited_db_connections": {
Description: "Is this model allowed to use all current and future connections?",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"label": {
Description: "UI-friendly name for this model",
Type: schema.TypeString,
Required: false,
Computed: true,
},
},
Importer: &schema.ResourceImporter{
State: resourceLookMlModelImport,
},
}
}
func resourceLookMlModelImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
d.Set("name", d.Id())
return []*schema.ResourceData{d}, nil
}
func resourceLookMlModelCreate(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
c := m.(*Config).Api // .(*lookergo.Client)
lmlMdlName := d.Get("name").(string)
projectName := d.Get("project_name").(string)
dbConnNames := schemaSetToStringSlice(d.Get("allowed_db_connection_names").(*schema.Set))
unlimitedConn := d.Get("unlimited_db_connections").(bool)
//logDebug(ctx, "Create MlModel", "lmlMdlName", lmlMdlName, "projectName", projectName, "dbConnNames", dbConnNames, "unlimitedConnections", unlimitedConn)
var lookmlModelOptions = lookergo.LookMLModel{Name: lmlMdlName, ProjectName: projectName, AllowedDbConnectionNames: dbConnNames, UnlimitedDbConnections: unlimitedConn}
createdModel, _, err := c.LookMLModel.Create(ctx, &lookmlModelOptions)
if err != nil {
return diag.FromErr(err)
}
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: end", currFuncName()))
d.Set("name", createdModel.Name)
d.SetId(createdModel.Name)
return resourceLookMlModelRead(ctx, d, m)
}
func resourceLookMlModelRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
c := m.(*Config).Api // .(*lookergo.Client)
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: start", currFuncName()))
lmlMdlName := d.Get("name").(string)
newModel, response, err := c.LookMLModel.Get(ctx, lmlMdlName)
if err != nil {
return diag.FromErr(err)
}
if response.StatusCode == 404 {
d.SetId("") // Mark as deleted
return diags
}
if newModel == nil {
return diag.FromErr(new(lookergo.ArgError))
}
if err = d.Set("project_name", newModel.ProjectName); err != nil {
return diag.FromErr(err)
}
if err = d.Set("allowed_db_connection_names", newModel.AllowedDbConnectionNames); err != nil {
return diag.FromErr(err)
}
if err = d.Set("unlimited_db_connections", newModel.UnlimitedDbConnections); err != nil {
return diag.FromErr(err)
}
if err = d.Set("label", newModel.Label); err != nil {
return diag.FromErr(err)
}
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: end", currFuncName()))
return diags
}
func resourceLookMlModelUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
c := m.(*Config).Api // .(*lookergo.Client)
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: start", currFuncName()))
lmlMdlName := d.Get("name").(string)
projectName := d.Get("project_name").(string)
dbConnNames := schemaSetToStringSlice(d.Get("allowed_db_connection_names").(*schema.Set))
unlimitedConn := d.Get("unlimited_db_connections").(bool)
oldLookMl := lookergo.LookMLModel{Name: lmlMdlName, ProjectName: projectName, AllowedDbConnectionNames: dbConnNames, UnlimitedDbConnections: unlimitedConn}
lookerML, _, err := c.LookMLModel.Update(ctx, d.Get("name").(string), &oldLookMl)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("project_name", lookerML.ProjectName); err != nil {
return diag.FromErr(err)
}
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: end", currFuncName()))
return resourceLookMlModelRead(ctx, d, m)
}
func resourceLookMlModelDelete(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
c := m.(*Config).Api // .(*lookergo.Client)
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: start", currFuncName()))
_, err := c.LookMLModel.Delete(ctx, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}
// Finally mark as deleted
d.SetId("")
tflog.Trace(ctx, fmt.Sprintf("Fn: %v, Action: end", currFuncName()))
return diags
}