|
4 | 4 | "context" |
5 | 5 | "encoding/json" |
6 | 6 | "fmt" |
| 7 | + "sort" |
7 | 8 | "strconv" |
8 | 9 |
|
9 | 10 | "github.com/databrickslabs/terraform-provider-databricks/common" |
@@ -41,6 +42,20 @@ type WidgetParameter struct { |
41 | 42 | Values []string `json:"values,omitempty"` |
42 | 43 | } |
43 | 44 |
|
| 45 | +type sortWidgetParameter []WidgetParameter |
| 46 | + |
| 47 | +func (a sortWidgetParameter) Len() int { |
| 48 | + return len(a) |
| 49 | +} |
| 50 | + |
| 51 | +func (a sortWidgetParameter) Swap(i, j int) { |
| 52 | + a[i], a[j] = a[j], a[i] |
| 53 | +} |
| 54 | + |
| 55 | +func (a sortWidgetParameter) Less(i, j int) bool { |
| 56 | + return a[i].Name < a[j].Name |
| 57 | +} |
| 58 | + |
44 | 59 | func (w *WidgetEntity) toAPIObject(schema map[string]*schema.Schema, data *schema.ResourceData) (*api.Widget, error) { |
45 | 60 | var aw api.Widget |
46 | 61 |
|
@@ -127,6 +142,51 @@ func (w *WidgetEntity) fromAPIObject(aw *api.Widget, schema map[string]*schema.S |
127 | 142 | } |
128 | 143 | } |
129 | 144 |
|
| 145 | + w.Parameter = make([]WidgetParameter, 0, len(aw.Options.ParameterMapping)) |
| 146 | + for _, p := range aw.Options.ParameterMapping { |
| 147 | + wp := WidgetParameter{ |
| 148 | + Name: p.Name, |
| 149 | + Type: p.Type, |
| 150 | + MapTo: p.MapTo, |
| 151 | + Title: p.Title, |
| 152 | + } |
| 153 | + |
| 154 | + // Re-marshal value so we can try to unmarshal different types. |
| 155 | + // We don't know about the type it holds, because it depends |
| 156 | + // on the parameter's type, which we don't have access to. |
| 157 | + b, err := json.Marshal(p.Value) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + // Try unmarshalling `string`. |
| 163 | + { |
| 164 | + var v string |
| 165 | + err := json.Unmarshal(b, &v) |
| 166 | + if err == nil { |
| 167 | + wp.Value = v |
| 168 | + w.Parameter = append(w.Parameter, wp) |
| 169 | + continue |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Try unmarshalling `[]string`. |
| 174 | + { |
| 175 | + var vs []string |
| 176 | + err := json.Unmarshal(b, &vs) |
| 177 | + if err == nil { |
| 178 | + wp.Values = vs |
| 179 | + w.Parameter = append(w.Parameter, wp) |
| 180 | + continue |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return fmt.Errorf("Unable to derive type from message: %v", string(b)) |
| 185 | + } |
| 186 | + |
| 187 | + // Sort parameters by their name for deterministic order. |
| 188 | + sort.Sort(sortWidgetParameter(w.Parameter)) |
| 189 | + |
130 | 190 | // Pass to ResourceData. |
131 | 191 | if err := common.StructToData(*w, schema, data); err != nil { |
132 | 192 | return err |
|
0 commit comments