-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathworkspace_preset.go
64 lines (55 loc) · 1.59 KB
/
workspace_preset.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
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/mapstructure"
)
type WorkspacePreset struct {
Name string `mapstructure:"name"`
Parameters map[string]string `mapstructure:"parameters"`
}
func workspacePresetDataSource() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Description: "Use this data source to predefine common configurations for workspaces.",
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
var preset WorkspacePreset
err := mapstructure.Decode(struct {
Name interface{}
Parameters interface{}
}{
Name: rd.Get("name"),
Parameters: rd.Get("parameters"),
}, &preset)
if err != nil {
return diag.Errorf("decode workspace preset: %s", err)
}
if preset.Name == "" {
return diag.Errorf("workspace preset name must be set")
}
if len(preset.Parameters) == 0 {
return diag.Errorf("workspace preset must define a value for at least one parameter")
}
rd.SetId(preset.Name)
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ID of the workspace preset.",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the workspace preset.",
Required: true,
},
"parameters": {
Type: schema.TypeMap,
Description: "Parameters of the workspace preset.",
Required: true,
},
},
}
}