-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathworkspace_preset.go
101 lines (92 loc) · 2.6 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
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
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mitchellh/mapstructure"
)
type WorkspacePreset struct {
Name string `mapstructure:"name"`
Parameters map[string]string `mapstructure:"parameters"`
Prebuild []WorkspacePrebuild `mapstructure:"prebuilds"`
}
type WorkspacePrebuild struct {
Instances int `mapstructure:"instances"`
}
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{}
Prebuilds []struct {
Instances interface{}
}
}{
Name: rd.Get("name"),
Parameters: rd.Get("parameters"),
Prebuilds: []struct {
Instances interface{}
}{
{
Instances: rd.Get("prebuilds.0.instances"),
},
},
}, &preset)
if err != nil {
return diag.Errorf("decode workspace preset: %s", err)
}
// MinItems doesn't work with maps, so we need to check the length
// of the map manually. All other validation is handled by the
// schema.
if len(preset.Parameters) == 0 {
return diag.Errorf("expected \"parameters\" to not be an empty map")
}
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,
ValidateFunc: validation.StringIsNotEmpty,
},
"parameters": {
Type: schema.TypeMap,
Description: "Parameters of the workspace preset.",
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"prebuilds": {
Type: schema.TypeSet,
Description: "Prebuilds of the workspace preset.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instances": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(0),
},
},
},
},
},
}
}